KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > ConfigFactory


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.config;
25
26 import java.util.Map JavaDoc;
27 import java.util.ArrayList JavaDoc;
28
29 import org.xml.sax.helpers.DefaultHandler JavaDoc;
30 import com.sun.enterprise.config.impl.ConfigContextImpl;
31 import com.sun.enterprise.config.pluggable.EnvironmentFactory;
32 import com.sun.enterprise.config.pluggable.ConfigEnvironment;
33
34 /**
35  * A factory to create ConfigContext objects and ConfigBeans.
36  *
37  * @deprecated use ConfigContextFactory
38  */

39 public class ConfigFactory {
40
41
42     public static ConfigContext createConfigContext(String JavaDoc fileUrl, DefaultHandler JavaDoc dh) throws ConfigException {
43         final ConfigEnvironment ce = getConfigEnvironment(fileUrl, false, false, true, true);
44         ce.setHandler(dh.getClass().getName());
45         return ConfigContextFactory.createConfigContext(ce);
46     }
47     
48         /**
49          * Creates a configuration context for the specified URL.
50          *
51          * @param fileUrl The URL to server.xml
52          *
53          * If a configuration context object for this URL was previously
54          * created then that object is returned.
55          */

56         public static ConfigContext createConfigContext(String JavaDoc fileUrl)
57             throws ConfigException {
58             return createConfigContext(fileUrl, false, false);
59         }
60         
61         /**
62          * Creates a configuration context for the specified URL and in
63          * the specified mode.
64          *
65          * @param fileUrl The URL to server.xml
66          * @param readOnly if true, then the ConfigContext returned cannot
67          * be used to make changes to the XML file
68          * @return If a configuration context object for this URL was
69          * previously created then that object is returned.
70          */

71         public static ConfigContext createConfigContext(String JavaDoc fileUrl,
72                 boolean readOnly) throws ConfigException {
73             return createConfigContext(fileUrl, readOnly, false);
74         }
75
76         /**
77          * Creates a configuration context for the specified URL and in
78          * the specified mode.
79          *
80          * @param fileUrl The URL to server.xml
81          * @param readOnly if true, then the ConfigContext returned cannot
82          * be used to make changes to the XML file
83          * @param autoCommit if true, then changes to the ConfigContext object
84          * are automatically committed to the file.
85          * @return If a configuration context object for this URL was
86          * previously created then that object is returned.
87          */

88         public static ConfigContext createConfigContext(String JavaDoc fileUrl,
89                 boolean readOnly, boolean autoCommit) throws ConfigException {
90             return createConfigContext(fileUrl, readOnly, autoCommit, true);
91         }
92
93         /**
94          * Returns a ConfigContext object that was either previously
95          * created (and stored in the cache) or created anew.
96          *
97          * If <code>cache</code> is <code>true</code> then the factory looks
98          * up its cache of previously created ConfigContext objects and if
99          * a matching one is found then that is returned. If the cache lookup
100          * failed, then a new ConfigContext is created and inserted into the
101          * cache.
102          *
103          * If <code>cache</code> is <code>false</code> then the cache is
104          * <i>not</i> used and a new ConfigContext object is returned. This
105          * object is <i>not</i> inserted into the cache.
106          *
107          * Note that if ConfigContext already exists, readOnly and autoCommit
108          * are not used.
109          * @param fileUrl
110          * @param readOnly
111          * @param autoCommit
112          * @param cache
113          * @throws ConfigException
114          * @return */

115         public static ConfigContext createConfigContext(String JavaDoc fileUrl,
116                 boolean readOnly, boolean autoCommit, boolean cache)
117                 throws ConfigException {
118            
119            return createConfigContext(fileUrl, readOnly, autoCommit,
120                    cache, true);
121         }
122         
123           /**
124          * Returns a ConfigContext object that was either previously
125          * created (and stored in the cache) or created anew.
126          *
127          * If <code>cache</code> is <code>true</code> then the factory looks
128          * up its cache of previously created ConfigContext objects and if
129          * a matching one is found then that is returned. If the cache lookup
130          * failed, then a new ConfigContext is created and inserted into the
131          * cache.
132          *
133          * If <code>cache</code> is <code>false</code> then the cache is
134          * <i>not</i> used and a new ConfigContext object is returned. This
135          * object is <i>not</i> inserted into the cache.
136          *
137          * If <code>resolvePath</code> is <code>true</code> then the
138          * absolute paths of certain attributes in domain.xml are resolved
139          * as in the runtime context.
140          *
141          * Note that if ConfigContext already exists, readOnly and autoCommit
142          * are not used.
143          * @param fileUrl
144          * @param readOnly
145          * @param autoCommit
146          * @param cache
147          * @param resolvePath
148          *
149          * @throws ConfigException
150          * @return
151          */

152         public static ConfigContext createConfigContext(String JavaDoc fileUrl,
153                 boolean readOnly, boolean autoCommit, boolean cache,
154                 boolean resolvePath)
155                 throws ConfigException {
156
157             return ConfigContextFactory.createConfigContext(
158                     getConfigEnvironment(fileUrl, readOnly, autoCommit,
159                         cache, resolvePath));
160         }
161
162         /**
163          * this method adds a validation handler for creating a context
164          * default is com.sun.enterprise.config.serverbeans.ServerValidationHandler
165          */

166         public static ConfigContext createConfigContext(String JavaDoc fileUrl,
167                 boolean readOnly, boolean autoCommit, boolean cache, Class JavaDoc rootClass)
168                 throws ConfigException {
169                     
170            ConfigEnvironment ce = getConfigEnvironment(fileUrl, readOnly,
171                autoCommit, cache, true);
172            ce.setRootClass(rootClass.getName());
173            return ConfigContextFactory.createConfigContext(ce);
174         }
175         
176         /**
177          * Creates a different class of beans based on rootClass
178          * By default, in the other createConfigContext APIs, rootClass
179          * is com.sun.enterprise.config.Server
180          *
181          * This API can be used to create serverbeans, clientbeans, or any other
182          * class of beans
183          */

184         public static synchronized ConfigContext createConfigContext(String JavaDoc fileUrl,
185                 boolean readOnly, boolean autoCommit, boolean cache, Class JavaDoc rootClass,
186                 DefaultHandler JavaDoc dh)
187                 throws ConfigException {
188                     
189             ConfigEnvironment ce = getConfigEnvironment(fileUrl, readOnly,
190                     autoCommit, cache, true);
191             ce.setRootClass(rootClass.getName());
192             ce.setHandler(dh.getClass().getName());
193             return ConfigContextFactory.createConfigContext(ce);
194         }
195                 
196         public static void removeConfigContext(ConfigContext ctx) {
197             ConfigContextFactory.removeConfigContext(ctx);
198         }
199         
200         public static synchronized void removeConfigContext(String JavaDoc fileUrl) {
201             ConfigContextFactory.removeConfigContext(fileUrl);
202         }
203         
204         /**
205          * Replaces a cached context with a new context
206          * So, next time the Url is accessed, it returns the new context
207          *
208          * Note the subtlity of this method: This method gets the url of
209          * the old context and replaces the value for that key with the new object
210          * Hence, even though there might be a configContext with the same url
211          * but a different object, it is still replaced. This behavior is desirable
212          * since we would like to have the newCtx set anyway.
213          *
214          * However, if the url was not present, then ConfigException is thrown
215          * with that message. Note that: this class is not i18n complaint.
216          *
217          * If the old context cannot be found in cache, then throw ConfigException
218          *
219          * This method is synchronized so to make it thread safe.
220          *
221          * @param oldCtx Old ConfigContext that was cached in this class. This
222          * cannot be null.
223          * @param newCtx New ConfigContext that will be replaced in the cache. This
224          * cannot be null.
225          * @throws ConfigException if url for old ctx is not found in cache
226          */

227         public static synchronized void replaceConfigContext(
228                                 ConfigContext oldCtx,
229                                 ConfigContext newCtx)
230                                 throws ConfigException {
231             ConfigContextFactory.replaceConfigContext(oldCtx, newCtx);
232         }
233         
234         /**
235          * xpath has to conform to the syntax below.
236          * <PRE>
237          * expression := /tagName | /tagName/tagExpression
238          * tagExpression := tagName| tagName[@name='value'] | tagName/tagExpression | tagName[@name='value']/tagExpression
239          * <PRE>
240          * @param ctx
241          * @param xpath
242          * @throws ConfigException
243          * @return */

244     /*public static ConfigBean getConfigBeanByXPath(ConfigContext ctx, String xpath)
245                 throws ConfigException {
246          return ConfigBeansFactory.getConfigBeanByXPath(ctx, xpath);
247     } */

248     
249     /**
250      * This method is used to activate the lastModified checking for a configContext
251      * If activated, configbeans will carry a lastmodified timestamp in every bean.
252      * This time is also carried onto the configChangeList and also to clones. When
253      * configContext.updateFromConfigChangeList is called, the timestamp is first checked
254      * to see if the bean has not changed since the clone and then the update is made.
255      * If a modification to the bean is detected, a staleWriteConfigException is thrown.
256      *
257      * @param ctx ConfigContext on which to enable/disable checking
258      * @param value boolean to enable/disable
259      * @return boolean previous value that was set (not the changed value)
260      */

261     public static boolean enableLastModifiedCheck(ConfigContext ctx, boolean value) {
262         return ConfigContextFactory.enableLastModifiedCheck(ctx, value);
263     }
264 /*
265     private static ThreadLocal _threadLocalConfigContext =
266                             new ThreadLocal();
267
268     public static ConfigContext getConfigContextFromThreadLocal() {
269         return (ConfigContext)_threadLocalConfigContext.get();
270     }
271
272     public static void setConfigContextInThreadLocal(ConfigContext ctx) {
273
274         try {
275             ((ConfigContextImpl)ctx).setXPathInAllBeans();
276         } catch (ConfigException ce) {
277             // ignore
278         }
279         _threadLocalConfigContext.set(ctx);
280     }*/

281     
282     private static ConfigEnvironment getConfigEnvironment(String JavaDoc fileUrl,
283         boolean readOnly, boolean autoCommit, boolean cache,
284         boolean resolvePath) {
285
286         ConfigEnvironment ce = EnvironmentFactory.getEnvironmentFactory().
287             getConfigEnvironment();
288         ce.setUrl(fileUrl);
289         ce.setReadOnly(readOnly);
290         ce.setCachingEnabled(cache);
291         ce.getConfigBeanInterceptor().setResolvingPaths(resolvePath);
292         return ce;
293     }
294 }
295
Popular Tags