1 17 package org.apache.jasper.runtime; 18 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 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 ; 34 import javax.servlet.jsp.JspApplicationContext ; 35 import javax.servlet.jsp.JspContext ; 36 import javax.servlet.jsp.el.ImplicitObjectELResolver ; 37 import javax.servlet.jsp.el.ScopedAttributeELResolver ; 38 39 import org.apache.el.ExpressionFactoryImpl; 40 import org.apache.jasper.el.ELContextImpl; 41 42 47 public class JspApplicationContextImpl implements JspApplicationContext { 48 49 private final static String KEY = JspApplicationContextImpl.class.getName(); 50 51 private final static ExpressionFactory expressionFactory = new ExpressionFactoryImpl(); 52 53 private final List <ELContextListener> contextListeners = new ArrayList <ELContextListener>(); 54 55 private final List <ELResolver> resolvers = new ArrayList <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 ("ELConextListener was null"); 68 } 69 this.contextListeners.add(listener); 70 } 71 72 public static JspApplicationContextImpl getInstance(ServletContext context) { 73 if (context == null) { 74 throw new IllegalArgumentException ("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 context) { 86 if (context == null) { 87 throw new IllegalArgumentException ("JspContext was null"); 88 } 89 90 ELResolver r = this.createELResolver(); 92 ELContextImpl ctx = new ELContextImpl(r); 93 ctx.putContext(JspContext .class, context); 94 95 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 ()); 109 for (Iterator 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 ()); 118 this.resolver = r; 119 } 120 return this.resolver; 121 } 122 123 public void addELResolver(ELResolver resolver) throws IllegalStateException { 124 if (resolver == null) { 125 throw new IllegalArgumentException ("ELResolver was null"); 126 } 127 if (this.instantiated) { 128 throw new IllegalStateException ( 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 |