KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
39
40 /**
41  * Creates a variable resolver based on the classloader.
42  */

43 public class EnvironmentLevelELResolver extends ELResolver {
44   private static final EnvironmentLocal<EnvironmentLevelELResolver> _local
45     = new EnvironmentLocal<EnvironmentLevelELResolver>();
46   
47   private final ClassLoader JavaDoc _loader;
48
49   private EnvironmentLevelELResolver(ClassLoader JavaDoc loader)
50   {
51     _loader = loader;
52   }
53   
54   /**
55    * Creates the resolver
56    */

57   public static EnvironmentLevelELResolver create()
58   {
59     return create(Thread.currentThread().getContextClassLoader());
60   }
61   
62   /**
63    * Creates the resolver
64    */

65   public static EnvironmentLevelELResolver create(ClassLoader JavaDoc loader)
66   {
67     EnvironmentLevelELResolver elResolver = _local.getLevel(loader);
68
69     if (elResolver == null) {
70       for (; loader != null; loader = loader.getParent()) {
71     if (loader instanceof EnvironmentClassLoader) {
72       elResolver = new EnvironmentLevelELResolver(loader);
73       _local.set(elResolver, loader);
74
75       return elResolver;
76     }
77       }
78
79       loader = ClassLoader.getSystemClassLoader();
80       elResolver = new EnvironmentLevelELResolver(loader);
81       _local.set(elResolver, loader);
82     }
83
84     return elResolver;
85   }
86
87   /**
88    * Returns true for read-only.
89    */

90   @Override JavaDoc
91   public boolean isReadOnly(ELContext context, Object JavaDoc base, Object JavaDoc property)
92   {
93     if (property != null || ! (base instanceof String JavaDoc))
94       return true;
95
96     context.setPropertyResolved(true);
97
98     return false;
99   }
100   
101   /**
102    * Returns the named variable value.
103    */

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

132   @Override JavaDoc
133   public Object JavaDoc getValue(ELContext env,
134              Object JavaDoc base,
135              Object JavaDoc property)
136   {
137     if (base != null)
138       return null;
139     else if (! (property instanceof String JavaDoc))
140       return null;
141
142     String JavaDoc var = (String JavaDoc) property;
143
144     Object JavaDoc value = EL.getLevelVar(var, _loader);
145
146     if (value == null)
147       return null;
148
149     env.setPropertyResolved(true);
150
151     if (value == EL.NULL)
152       return null;
153     else
154       return value;
155   }
156   
157   /**
158    * Sets the value for the named variable.
159    */

160   @Override JavaDoc
161   public void setValue(ELContext env,
162                Object JavaDoc base,
163                Object JavaDoc property,
164                Object JavaDoc value)
165   {
166     if (base != null || ! (property instanceof String JavaDoc))
167       return;
168
169     env.setPropertyResolved(true);
170
171     String JavaDoc name = (String JavaDoc) property;
172
173     EL.putVar(name, value, _loader);
174   }
175
176   public String JavaDoc toString()
177   {
178     return "EnvironmentLevelELResolver[" + _loader + "]";
179   }
180 }
181
Popular Tags