001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.jasper.compiler;
017:
018: import java.util.Vector;
019: import java.util.Hashtable;
020:
021: import org.apache.jasper.JasperException;
022:
023: /**
024: * Repository of {page, request, session, application}-scoped beans
025: *
026: * @author Mandar Raje
027: */
028: class BeanRepository {
029:
030: private Vector sessionBeans;
031: private Vector pageBeans;
032: private Vector appBeans;
033: private Vector requestBeans;
034: private Hashtable beanTypes;
035: private ClassLoader loader;
036: private ErrorDispatcher errDispatcher;
037:
038: /*
039: * Constructor.
040: */
041: public BeanRepository(ClassLoader loader, ErrorDispatcher err) {
042:
043: this .loader = loader;
044: this .errDispatcher = err;
045:
046: sessionBeans = new Vector(11);
047: pageBeans = new Vector(11);
048: appBeans = new Vector(11);
049: requestBeans = new Vector(11);
050: beanTypes = new Hashtable();
051: }
052:
053: public void addBean(Node.UseBean n, String s, String type,
054: String scope) throws JasperException {
055:
056: if (scope == null || scope.equals("page")) {
057: pageBeans.addElement(s);
058: } else if (scope.equals("request")) {
059: requestBeans.addElement(s);
060: } else if (scope.equals("session")) {
061: sessionBeans.addElement(s);
062: } else if (scope.equals("application")) {
063: appBeans.addElement(s);
064: } else {
065: errDispatcher.jspError(n, "jsp.error.useBean.badScope");
066: }
067:
068: putBeanType(s, type);
069: }
070:
071: public Class getBeanType(String bean) throws JasperException {
072: Class clazz = null;
073: try {
074: clazz = loader.loadClass((String) beanTypes.get(bean));
075: } catch (ClassNotFoundException ex) {
076: throw new JasperException(ex);
077: }
078: return clazz;
079: }
080:
081: public boolean checkVariable(String bean) {
082: // XXX Not sure if this is the correct way.
083: // After pageContext is finalised this will change.
084: return (checkPageBean(bean) || checkSessionBean(bean)
085: || checkRequestBean(bean) || checkApplicationBean(bean));
086: }
087:
088: private void putBeanType(String bean, String type) {
089: beanTypes.put(bean, type);
090: }
091:
092: private boolean checkPageBean(String s) {
093: return pageBeans.contains(s);
094: }
095:
096: private boolean checkRequestBean(String s) {
097: return requestBeans.contains(s);
098: }
099:
100: private boolean checkSessionBean(String s) {
101: return sessionBeans.contains(s);
102: }
103:
104: private boolean checkApplicationBean(String s) {
105: return appBeans.contains(s);
106: }
107:
108: }
|