View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  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  package org.apache.commons.scxml.env.jsp;
18  
19  import java.io.IOException;
20  import java.io.NotSerializableException;
21  import java.io.ObjectInputStream;
22  import java.io.ObjectOutputStream;
23  
24  import javax.servlet.jsp.JspContext;
25  import javax.servlet.jsp.el.ELException;
26  import javax.servlet.jsp.el.VariableResolver;
27  
28  /***
29   * EL Context for root SCXML element. Wrapper around the host JSP context.
30   * Must treat variables in the host JSP environments as read-only.
31   *
32   */
33  public final class RootContext extends ELContext {
34  
35      /*** Host JSP's VariableResolver. */
36      private VariableResolver variableResolver;
37      /*** Bark if JSP Context is null. */
38      private static final String ERR_HOST_JSP_CTX_NULL =
39          "Host JSP Context cannot be null";
40  
41      /***
42       * Constructor.
43       *
44       * @param ctx the host JspContext
45       */
46      public RootContext(final JspContext ctx) {
47          super();
48          if (ctx == null) {
49              getLog().error(ERR_HOST_JSP_CTX_NULL);
50              throw new IllegalArgumentException(ERR_HOST_JSP_CTX_NULL);
51          } else {
52            // only retain the VariableResolver
53            this.variableResolver = ctx.getVariableResolver();
54          }
55      }
56  
57      /***
58       * Get the value of the given variable in this Context.
59       *
60       * @param name The name of the variable
61       * @return The value (or null)
62       * @see org.apache.commons.scxml.Context#get(java.lang.String)
63       */
64      public Object get(final String name) {
65          Object value = super.get(name);
66          if (value == null) {
67              try {
68                  value = variableResolver.resolveVariable(name);
69              } catch (ELException ele) {
70                  getLog().error(ele.getMessage(), ele);
71              }
72          }
73          return value;
74      }
75  
76      /***
77       * Does the given variable exist in this Context.
78       *
79       * @param name The name of the variable
80       * @return boolean true if the variable exists
81       * @see org.apache.commons.scxml.Context#has(java.lang.String)
82       */
83      public boolean has(final String name) {
84          boolean exists = super.has(name);
85          Object value = null;
86          if (!exists) {
87              try {
88                  value = variableResolver.resolveVariable(name);
89              } catch (ELException ele) {
90                  getLog().error(ele.getMessage(), ele);
91              }
92              if (value != null) {
93                  exists = true;
94              }
95          }
96          return exists;
97      }
98  
99      /***
100      * Get the VariableResolver associated with this root context.
101      *
102      * @return Returns the variableResolver.
103      */
104     public VariableResolver getVariableResolver() {
105         return variableResolver;
106     }
107 
108     /***
109      * Set the VariableResolver associated with this root context.
110      *
111      * @param variableResolver The variableResolver to set.
112      */
113     public void setVariableResolver(final VariableResolver variableResolver) {
114         this.variableResolver = variableResolver;
115     }
116 
117     //--------------------------------------------------- Truth in advertising
118 
119     /***
120      * Instances of this class are not serializable.
121      *
122      * @param out The object output stream.
123      * @throws IOException Guaranteed to throw a NotSerializableException
124      */
125     private void writeObject(final ObjectOutputStream out)
126     throws IOException {
127         throw new NotSerializableException();
128     }
129 
130     /***
131      * Instances of this class are not serializable.
132      *
133      * @param in The object input stream.
134      * @throws IOException Guaranteed to throw a NotSerializableException
135      */
136     private void readObject(final ObjectInputStream in)
137     throws IOException {
138         throw new NotSerializableException();
139     }
140 
141 }
142