View Javadoc

1   /*
2    *
3    *   Copyright 2005-2006 The Apache Software Foundation.
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   *
17   */
18  package org.apache.commons.scxml.env.jsp;
19  
20  import javax.servlet.jsp.JspContext;
21  import javax.servlet.jsp.el.ELException;
22  import javax.servlet.jsp.el.VariableResolver;
23  
24  /***
25   * EL Context for root SCXML element. Wrapper around the host JSP context.
26   * Must treat variables in the host JSP environments as read-only.
27   *
28   */
29  public final class RootContext extends ELContext {
30  
31      /*** Host JSP's VariableResolver. */
32      private VariableResolver variableResolver;
33      /*** Bark if JSP Context is null. */
34      private static final String ERR_HOST_JSP_CTX_NULL =
35          "Host JSP Context cannot be null";
36  
37      /***
38       * Constructor.
39       *
40       * @param ctx the host JspContext
41       */
42      public RootContext(final JspContext ctx) {
43          super();
44          if (ctx == null) {
45              getLog().error(ERR_HOST_JSP_CTX_NULL);
46              throw new IllegalArgumentException(ERR_HOST_JSP_CTX_NULL);
47          } else {
48            // only retain the VariableResolver
49            this.variableResolver = ctx.getVariableResolver();
50          }
51      }
52  
53      /***
54       * Get the value of the given variable in this Context.
55       *
56       * @param name The name of the variable
57       * @return The value (or null)
58       * @see org.apache.commons.scxml.Context#get(java.lang.String)
59       */
60      public Object get(final String name) {
61          Object value = super.get(name);
62          if (value == null) {
63              try {
64                  value = variableResolver.resolveVariable(name);
65              } catch (ELException ele) {
66                  getLog().error(ele.getMessage(), ele);
67              }
68          }
69          return value;
70      }
71  
72      /***
73       * Does the given variable exist in this Context.
74       *
75       * @param name The name of the variable
76       * @return boolean true if the variable exists
77       * @see org.apache.commons.scxml.Context#has(java.lang.String)
78       */
79      public boolean has(final String name) {
80          boolean exists = super.has(name);
81          Object value = null;
82          if (!exists) {
83              try {
84                  value = variableResolver.resolveVariable(name);
85              } catch (ELException ele) {
86                  getLog().error(ele.getMessage(), ele);
87              }
88              if (value != null) {
89                  exists = true;
90              }
91          }
92          return exists;
93      }
94  
95      /***
96       * Get the VariableResolver associated with this root context.
97       *
98       * @return Returns the variableResolver.
99       */
100     public VariableResolver getVariableResolver() {
101         return variableResolver;
102     }
103 
104     /***
105      * Set the VariableResolver associated with this root context.
106      *
107      * @param variableResolver The variableResolver to set.
108      */
109     public void setVariableResolver(final VariableResolver variableResolver) {
110         this.variableResolver = variableResolver;
111     }
112 
113 }
114