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.struts2.jasper.compiler;
18  
19  
20  import org.apache.struts2.jasper.JasperException;
21  
22  import java.util.Hashtable;
23  import java.util.Vector;
24  
25  /***
26   * Repository of {page, request, session, application}-scoped beans
27   *
28   * @author Mandar Raje
29   */
30  class BeanRepository {
31  
32      private Vector sessionBeans;
33      private Vector pageBeans;
34      private Vector appBeans;
35      private Vector requestBeans;
36      private Hashtable beanTypes;
37      private ClassLoader loader;
38      private ErrorDispatcher errDispatcher;
39  
40      /*
41       * Constructor.
42       */
43      public BeanRepository(ClassLoader loader, ErrorDispatcher err) {
44  
45          this.loader = loader;
46          this.errDispatcher = err;
47  
48          sessionBeans = new Vector(11);
49          pageBeans = new Vector(11);
50          appBeans = new Vector(11);
51          requestBeans = new Vector(11);
52          beanTypes = new Hashtable();
53      }
54  
55      public void addBean(Node.UseBean n, String s, String type, String scope)
56              throws JasperException {
57  
58          if (scope == null || scope.equals("page")) {
59              pageBeans.addElement(s);
60          } else if (scope.equals("request")) {
61              requestBeans.addElement(s);
62          } else if (scope.equals("session")) {
63              sessionBeans.addElement(s);
64          } else if (scope.equals("application")) {
65              appBeans.addElement(s);
66          } else {
67              errDispatcher.jspError(n, "jsp.error.usebean.badScope");
68          }
69  
70          putBeanType(s, type);
71      }
72  
73      public Class getBeanType(String bean) throws JasperException {
74          Class clazz = null;
75          try {
76              clazz = loader.loadClass((String) beanTypes.get(bean));
77          } catch (ClassNotFoundException ex) {
78              throw new JasperException(ex);
79          }
80          return clazz;
81      }
82  
83      public boolean checkVariable(String bean) {
84          // XXX Not sure if this is the correct way.
85          // After pageContext is finalised this will change.
86          return (checkPageBean(bean) || checkSessionBean(bean) ||
87                  checkRequestBean(bean) || checkApplicationBean(bean));
88      }
89  
90  
91      private void putBeanType(String bean, String type) {
92          beanTypes.put(bean, type);
93      }
94  
95      private boolean checkPageBean(String s) {
96          return pageBeans.contains(s);
97      }
98  
99      private boolean checkRequestBean(String s) {
100         return requestBeans.contains(s);
101     }
102 
103     private boolean checkSessionBean(String s) {
104         return sessionBeans.contains(s);
105     }
106 
107     private boolean checkApplicationBean(String s) {
108         return appBeans.contains(s);
109     }
110 
111 }
112 
113 
114 
115