KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > config > ConfigVariableResolver


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.config;
31
32 import com.caucho.el.AbstractVariableResolver;
33 import com.caucho.el.EL;
34 import com.caucho.loader.Environment;
35 import com.caucho.loader.EnvironmentClassLoader;
36
37 import javax.el.ELContext;
38 import java.util.HashMap JavaDoc;
39
40 /**
41  * Creates a variable resolver based on the classloader.
42  */

43 public class ConfigVariableResolver extends AbstractVariableResolver {
44   private HashMap JavaDoc<String JavaDoc,Object JavaDoc> _varMap = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
45   
46   private ClassLoader JavaDoc _originalLoader;
47   private ClassLoader JavaDoc _configureClassLoader;
48   /**
49    * Creates the resolver
50    */

51   public ConfigVariableResolver()
52   {
53     this(Thread.currentThread().getContextClassLoader());
54   }
55   
56   /**
57    * Creates the resolver
58    */

59   public ConfigVariableResolver(ClassLoader JavaDoc loader)
60   {
61     super(EL.getEnvironment(loader).getELResolver());
62
63     _originalLoader = loader;
64   }
65
66   /**
67    * Gets the configure environment class loader.
68    */

69   ClassLoader JavaDoc getConfigLoader()
70   {
71     return _configureClassLoader;
72   }
73
74   /**
75    * Sets the configure environment class loader.
76    */

77   void setConfigLoader(ClassLoader JavaDoc loader)
78   {
79     // server/13co
80
if (_configureClassLoader == null)
81       _configureClassLoader = loader;
82   }
83   
84   /**
85    * Returns the named variable value.
86    */

87   @Override JavaDoc
88   public Object JavaDoc getValue(ELContext context,
89              Object JavaDoc base,
90              Object JavaDoc property)
91   {
92     if (base != null)
93       return null;
94     else if (! (property instanceof String JavaDoc))
95       return null;
96
97     context.setPropertyResolved(true);
98
99     String JavaDoc var = (String JavaDoc) property;
100     
101     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
102     
103     for (;
104      loader != _originalLoader && loader != null;
105      loader = loader.getParent()) {
106       if (loader instanceof EnvironmentClassLoader) {
107     Object JavaDoc value = EL.getLevelVar(var, loader);
108
109     if (value == EL.NULL)
110       return null;
111     else if (value != null)
112       return value;
113       }
114     }
115
116     if (loader == _configureClassLoader) {
117       Object JavaDoc value = EL.getLevelVar(var, loader);
118
119       if (value == EL.NULL)
120     return null;
121       else if (value != null)
122     return value;
123     }
124     
125     Object JavaDoc value = _varMap.get(var);
126
127     if (value == EL.NULL)
128       return null;
129     else if (value != null)
130       return value;
131
132     value = Environment.getAttribute(var, _originalLoader);
133
134     if (value == EL.NULL)
135       return null;
136     else if (value != null)
137       return value;
138     else
139       return super.getValue(context, base, property);
140   }
141   
142   /**
143    * Sets the value for the named variable.
144    */

145   @Override JavaDoc
146   public void setValue(ELContext context,
147                Object JavaDoc base,
148                Object JavaDoc property,
149                Object JavaDoc value)
150   {
151     if (base != null || ! (property instanceof String JavaDoc))
152       return;
153
154     context.setPropertyResolved(true);
155
156     String JavaDoc name = (String JavaDoc) property;
157     
158     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
159
160     for (;
161      loader != _originalLoader && loader != null;
162      loader = loader.getParent()) {
163       if (loader instanceof EnvironmentClassLoader) {
164     EL.putVar(name, value, loader);
165
166     return;
167       }
168     }
169
170     if (loader == _configureClassLoader) {
171       EL.putVar(name, value, loader);
172
173       return;
174     }
175     
176     _varMap.put(name, value);
177   }
178
179   public String JavaDoc toString()
180   {
181     return "ConfigVariableResolver[]";
182   }
183 }
184
Popular Tags