KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jasper > runtime > JspApplicationContextImpl


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.jasper.runtime;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import javax.el.ArrayELResolver;
24 import javax.el.BeanELResolver;
25 import javax.el.CompositeELResolver;
26 import javax.el.ELContextEvent;
27 import javax.el.ELContextListener;
28 import javax.el.ELResolver;
29 import javax.el.ExpressionFactory;
30 import javax.el.ListELResolver;
31 import javax.el.MapELResolver;
32 import javax.el.ResourceBundleELResolver;
33 import javax.servlet.ServletContext JavaDoc;
34 import javax.servlet.jsp.JspApplicationContext JavaDoc;
35 import javax.servlet.jsp.JspContext JavaDoc;
36 import javax.servlet.jsp.el.ImplicitObjectELResolver JavaDoc;
37 import javax.servlet.jsp.el.ScopedAttributeELResolver JavaDoc;
38
39 import org.apache.el.ExpressionFactoryImpl;
40 import org.apache.jasper.el.ELContextImpl;
41
42 /**
43  * Implementation of JspApplicationContext
44  *
45  * @author Jacob Hookom
46  */

47 public class JspApplicationContextImpl implements JspApplicationContext JavaDoc {
48
49     private final static String JavaDoc KEY = JspApplicationContextImpl.class.getName();
50
51     private final static ExpressionFactory expressionFactory = new ExpressionFactoryImpl();
52
53     private final List JavaDoc<ELContextListener> contextListeners = new ArrayList JavaDoc<ELContextListener>();
54
55     private final List JavaDoc<ELResolver> resolvers = new ArrayList JavaDoc<ELResolver>();
56
57     private boolean instantiated = false;
58
59     private ELResolver resolver;
60
61     public JspApplicationContextImpl() {
62
63     }
64
65     public void addELContextListener(ELContextListener listener) {
66         if (listener == null) {
67             throw new IllegalArgumentException JavaDoc("ELConextListener was null");
68         }
69         this.contextListeners.add(listener);
70     }
71
72     public static JspApplicationContextImpl getInstance(ServletContext JavaDoc context) {
73         if (context == null) {
74             throw new IllegalArgumentException JavaDoc("ServletContext was null");
75         }
76         JspApplicationContextImpl impl = (JspApplicationContextImpl) context
77                 .getAttribute(KEY);
78         if (impl == null) {
79             impl = new JspApplicationContextImpl();
80             context.setAttribute(KEY, impl);
81         }
82         return impl;
83     }
84
85     public ELContextImpl createELContext(JspContext JavaDoc context) {
86         if (context == null) {
87             throw new IllegalArgumentException JavaDoc("JspContext was null");
88         }
89
90         // create ELContext for JspContext
91
ELResolver r = this.createELResolver();
92         ELContextImpl ctx = new ELContextImpl(r);
93         ctx.putContext(JspContext JavaDoc.class, context);
94
95         // alert all ELContextListeners
96
ELContextEvent event = new ELContextEvent(ctx);
97         for (int i = 0; i < this.contextListeners.size(); i++) {
98             this.contextListeners.get(i).contextCreated(event);
99         }
100
101         return ctx;
102     }
103
104     private ELResolver createELResolver() {
105         this.instantiated = true;
106         if (this.resolver == null) {
107             CompositeELResolver r = new CompositeELResolver();
108             r.add(new ImplicitObjectELResolver JavaDoc());
109             for (Iterator JavaDoc itr = this.resolvers.iterator(); itr.hasNext();) {
110                 r.add((ELResolver) itr.next());
111             }
112             r.add(new MapELResolver());
113             r.add(new ResourceBundleELResolver());
114             r.add(new ListELResolver());
115             r.add(new ArrayELResolver());
116             r.add(new BeanELResolver());
117             r.add(new ScopedAttributeELResolver JavaDoc());
118             this.resolver = r;
119         }
120         return this.resolver;
121     }
122
123     public void addELResolver(ELResolver resolver) throws IllegalStateException JavaDoc {
124         if (resolver == null) {
125             throw new IllegalArgumentException JavaDoc("ELResolver was null");
126         }
127         if (this.instantiated) {
128             throw new IllegalStateException JavaDoc(
129                     "cannot call addELResolver after the first request has been made");
130         }
131         this.resolvers.add(resolver);
132     }
133
134     public ExpressionFactory getExpressionFactory() {
135         return expressionFactory;
136     }
137
138 }
139
Popular Tags