001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jasper.runtime;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import javax.el.ArrayELResolver;
024: import javax.el.BeanELResolver;
025: import javax.el.CompositeELResolver;
026: import javax.el.ELContextEvent;
027: import javax.el.ELContextListener;
028: import javax.el.ELResolver;
029: import javax.el.ExpressionFactory;
030: import javax.el.ListELResolver;
031: import javax.el.MapELResolver;
032: import javax.el.ResourceBundleELResolver;
033: import javax.servlet.ServletContext;
034: import javax.servlet.jsp.JspApplicationContext;
035: import javax.servlet.jsp.JspContext;
036: import javax.servlet.jsp.el.ImplicitObjectELResolver;
037: import javax.servlet.jsp.el.ScopedAttributeELResolver;
038:
039: import org.apache.el.ExpressionFactoryImpl;
040: import org.apache.jasper.el.ELContextImpl;
041:
042: /**
043: * Implementation of JspApplicationContext
044: *
045: * @author Jacob Hookom
046: */
047: public class JspApplicationContextImpl implements JspApplicationContext {
048:
049: private final static String KEY = JspApplicationContextImpl.class
050: .getName();
051:
052: private final static ExpressionFactory expressionFactory = new ExpressionFactoryImpl();
053:
054: private final List<ELContextListener> contextListeners = new ArrayList<ELContextListener>();
055:
056: private final List<ELResolver> resolvers = new ArrayList<ELResolver>();
057:
058: private boolean instantiated = false;
059:
060: private ELResolver resolver;
061:
062: public JspApplicationContextImpl() {
063:
064: }
065:
066: public void addELContextListener(ELContextListener listener) {
067: if (listener == null) {
068: throw new IllegalArgumentException(
069: "ELConextListener was null");
070: }
071: this .contextListeners.add(listener);
072: }
073:
074: public static JspApplicationContextImpl getInstance(
075: ServletContext context) {
076: if (context == null) {
077: throw new IllegalArgumentException(
078: "ServletContext was null");
079: }
080: JspApplicationContextImpl impl = (JspApplicationContextImpl) context
081: .getAttribute(KEY);
082: if (impl == null) {
083: impl = new JspApplicationContextImpl();
084: context.setAttribute(KEY, impl);
085: }
086: return impl;
087: }
088:
089: public ELContextImpl createELContext(JspContext context) {
090: if (context == null) {
091: throw new IllegalArgumentException("JspContext was null");
092: }
093:
094: // create ELContext for JspContext
095: ELResolver r = this .createELResolver();
096: ELContextImpl ctx = new ELContextImpl(r);
097: ctx.putContext(JspContext.class, context);
098:
099: // alert all ELContextListeners
100: ELContextEvent event = new ELContextEvent(ctx);
101: for (int i = 0; i < this .contextListeners.size(); i++) {
102: this .contextListeners.get(i).contextCreated(event);
103: }
104:
105: return ctx;
106: }
107:
108: private ELResolver createELResolver() {
109: this .instantiated = true;
110: if (this .resolver == null) {
111: CompositeELResolver r = new CompositeELResolver();
112: r.add(new ImplicitObjectELResolver());
113: for (Iterator itr = this .resolvers.iterator(); itr
114: .hasNext();) {
115: r.add((ELResolver) itr.next());
116: }
117: r.add(new MapELResolver());
118: r.add(new ResourceBundleELResolver());
119: r.add(new ListELResolver());
120: r.add(new ArrayELResolver());
121: r.add(new BeanELResolver());
122: r.add(new ScopedAttributeELResolver());
123: this .resolver = r;
124: }
125: return this .resolver;
126: }
127:
128: public void addELResolver(ELResolver resolver)
129: throws IllegalStateException {
130: if (resolver == null) {
131: throw new IllegalArgumentException("ELResolver was null");
132: }
133: if (this .instantiated) {
134: throw new IllegalStateException(
135: "cannot call addELResolver after the first request has been made");
136: }
137: this .resolvers.add(resolver);
138: }
139:
140: public ExpressionFactory getExpressionFactory() {
141: return expressionFactory;
142: }
143:
144: }
|