KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > el > EnvironmentELResolver


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.el;
31
32 import com.caucho.loader.EnvironmentClassLoader;
33 import com.caucho.loader.EnvironmentLocal;
34
35 import javax.el.ELContext;
36 import javax.el.ELResolver;
37 import java.beans.FeatureDescriptor JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.Iterator JavaDoc;
40
41 /**
42  * Creates a variable resolver based on the classloader.
43  */

44 public class EnvironmentELResolver extends ELResolver {
45   private static final EnvironmentLocal<EnvironmentELResolver> _local
46     = new EnvironmentLocal<EnvironmentELResolver>();
47   
48   private ArrayList JavaDoc<ELResolver> _resolvers
49     = new ArrayList JavaDoc<ELResolver>();
50
51   private EnvironmentELResolver(ClassLoader JavaDoc loader)
52   {
53     for (; loader != null; loader = loader.getParent()) {
54       if (loader instanceof EnvironmentClassLoader) {
55     EnvironmentLevelELResolver resolver
56       = EnvironmentLevelELResolver.create(loader);
57
58     if (! _resolvers.contains(resolver))
59       _resolvers.add(resolver);
60       }
61     }
62     
63     EnvironmentLevelELResolver resolver
64       = EnvironmentLevelELResolver.create(ClassLoader.getSystemClassLoader());
65
66     if (! _resolvers.contains(resolver))
67       _resolvers.add(resolver);
68   }
69   
70   /**
71    * Creates the resolver
72    */

73   public static EnvironmentELResolver create()
74   {
75     return create(Thread.currentThread().getContextClassLoader());
76   }
77   
78   /**
79    * Creates the resolver
80    */

81   public static EnvironmentELResolver create(ClassLoader JavaDoc loader)
82   {
83     EnvironmentELResolver elResolver = _local.getLevel(loader);
84
85     if (elResolver == null) {
86       elResolver = new EnvironmentELResolver(loader);
87       
88       _local.set(elResolver, loader);
89     }
90
91     return elResolver;
92   }
93
94   /**
95    * Returns true for read-only.
96    */

97   @Override JavaDoc
98   public boolean isReadOnly(ELContext context, Object JavaDoc base, Object JavaDoc property)
99   {
100     if (property != null || ! (base instanceof String JavaDoc))
101       return true;
102
103     context.setPropertyResolved(true);
104
105     return false;
106   }
107   
108   /**
109    * Returns the named variable value.
110    */

111   @Override JavaDoc
112   public Class JavaDoc<?> getType(ELContext context,
113             Object JavaDoc base,
114             Object JavaDoc property)
115   {
116     Object JavaDoc value = getValue(context, base, property);
117
118     if (value != null)
119       return value.getClass();
120     else
121       return null;
122   }
123
124   public Class JavaDoc<?> getCommonPropertyType(ELContext context,
125                     Object JavaDoc base)
126   {
127     return null;
128   }
129
130   public Iterator JavaDoc<FeatureDescriptor JavaDoc>
131     getFeatureDescriptors(ELContext context, Object JavaDoc base)
132   {
133     return null;
134   }
135   
136   /**
137    * Returns the named variable value.
138    */

139   @Override JavaDoc
140   public Object JavaDoc getValue(ELContext context,
141              Object JavaDoc base,
142              Object JavaDoc property)
143   {
144     if (base != null)
145       return null;
146     else if (! (property instanceof String JavaDoc))
147       return null;
148
149     context.setPropertyResolved(false);
150     for (int i = 0; i < _resolvers.size(); i++) {
151       Object JavaDoc value = _resolvers.get(i).getValue(context, base, property);
152
153       if (context.isPropertyResolved())
154     return value;
155     }
156
157     return null;
158   }
159   
160   /**
161    * Sets the value for the named variable.
162    */

163   @Override JavaDoc
164   public void setValue(ELContext context,
165                Object JavaDoc base,
166                Object JavaDoc property,
167                Object JavaDoc value)
168   {
169     if (base != null || ! (property instanceof String JavaDoc))
170       return;
171
172     context.setPropertyResolved(false);
173     for (int i = 0; i < _resolvers.size(); i++) {
174       _resolvers.get(i).setValue(context, base, property, value);
175       
176       if (context.isPropertyResolved()) {
177     return;
178       }
179     }
180   }
181
182   public String JavaDoc toString()
183   {
184     return "EnvironmentELResolver[]";
185   }
186 }
187
Popular Tags