KickJava   Java API By Example, From Geeks To Geeks.

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


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.Hashtable JavaDoc;
28 import org.xml.sax.helpers.DefaultHandler JavaDoc;
29 import com.sun.enterprise.config.impl.ConfigContextImpl;
30 import com.sun.enterprise.config.pluggable.EnvironmentFactory;
31 import com.sun.enterprise.config.pluggable.ConfigEnvironment;
32
33 /**
34  * A factory to create ConfigContext
35  *
36  */

37 public class ConfigContextFactory {
38     
39     private static Hashtable JavaDoc _ctxCache = new Hashtable JavaDoc();
40     
41     
42     /**
43      * Returns a ConfigContext object that was either previously
44      * created (and stored in the cache) or created anew.
45      *
46      * If <code>cache</code> is <code>true</code> then the factory looks
47      * up its cache of previously created ConfigContext objects and if
48      * a matching one is found then that is returned. If the cache lookup
49      * failed, then a new ConfigContext is created and inserted into the
50      * cache.
51      *
52      * If <code>cache</code> is <code>false</code> then the cache is
53      * <i>not</i> used and a new ConfigContext object is returned. This
54      * object is <i>not</i> inserted into the cache.
55      *
56      * TBD
57      */

58     public ConfigContext createConfigContext(String JavaDoc url, String JavaDoc rootClass) {
59         ConfigEnvironment ce = getConfigEnvironment();
60         ce.setUrl(url);
61         ce.setRootClass(rootClass);
62         return createConfigContext(ce);
63     }
64     
65     /**
66      *
67      */

68     public static ConfigContext createConfigContext(ConfigEnvironment ce) {
69         
70         if(!ce.isCachingEnabled()) {
71             //log that a new one is created //FIXME
72
return newConfigContext(ce);
73         }
74         
75         ConfigContext context = getConfigContextFromCache(ce.getUrl());
76         
77         if(context == null) {
78             context = newConfigContext(ce);
79              addConfigContextToCache(ce.getUrl(), context);
80              //log that it was created. //FIXME
81
} else {
82             //log that it was found in cache //FIXME
83
}
84         return context;
85     }
86     
87     public static void removeConfigContext(ConfigContext ctx) {
88         String JavaDoc url = ctx.getUrl();
89         removeConfigContext(url);
90     }
91     
92     public static synchronized void removeConfigContext(String JavaDoc url) {
93         Object JavaDoc obj = _ctxCache.remove(url);
94         invalidateConfigContext(obj);
95     }
96     
97     /**
98      * Replaces a cached context with a new context
99      * So, next time the Url is accessed, it returns the new context
100      *
101      * Note the subtlity of this method: This method gets the url of
102      * the old context and replaces the value for that key with the new object
103      * Hence, even though there might be a configContext with the same url
104      * but a different object, it is still replaced. This behavior is desirable
105      * since we would like to have the newCtx set anyway.
106      *
107      * However, if the url was not present, then ConfigException is thrown
108      * with that message. Note that: this class is not i18n complaint.
109      *
110      * If the old context cannot be found in cache, then throw ConfigException
111      *
112      * This method is synchronized so to make it thread safe.
113      *
114      * @param oldCtx Old ConfigContext that was cached in this class. This
115      * cannot be null.
116      * @param newCtx New ConfigContext that will be replaced in the cache. This
117      * cannot be null.
118      * @throws ConfigException if url for old ctx is not found in cache
119      */

120     public static synchronized void replaceConfigContext(
121                                         ConfigContext oldCtx,
122                                         ConfigContext newCtx)
123                                         throws ConfigException {
124         
125         assert (oldCtx != null);
126         assert (newCtx != null);
127         
128         String JavaDoc url = oldCtx.getUrl();
129         
130         if(_ctxCache.containsKey(url)) {
131             _ctxCache.put(url, newCtx);
132         } else {
133             throw new ConfigException
134             ("Old ConfigContext is not found. Cannot replace with new one");
135             //FIxME
136
}
137     }
138     
139     /**
140      * This method is used to activate the lastModified checking for a configContext
141      * If activated, configbeans will carry a lastmodified timestamp in every bean.
142      * This time is also carried onto the configChangeList and also to clones. When
143      * configContext.updateFromConfigChangeList is called, the timestamp is first checked
144      * to see if the bean has not changed since the clone and then the update is made.
145      * If a modification to the bean is detected, a staleWriteConfigException is thrown.
146      *
147      * @param ctx ConfigContext on which to enable/disable checking
148      * @param value boolean to enable/disable
149      * @return boolean previous value that was set (not the changed value)
150      *
151      * FIXME move this to configcontext
152      */

153     public static boolean enableLastModifiedCheck(ConfigContext ctx, boolean value) {
154         return ((ConfigContextImpl)ctx).enableLastModifiedCheck(value);
155     }
156     
157     private static ConfigEnvironment getConfigEnvironment() {
158         ConfigEnvironment ce = null;
159         try {
160             ce = EnvironmentFactory.
161                     getEnvironmentFactory().
162                     getConfigEnvironment();
163         } catch(Exception JavaDoc e) {
164             throw new ConfigRuntimeException
165                     ("err_getting_config_env",
166                     "err_getting_config_env",
167                     e); //FIXME
168
}
169         return ce;
170     }
171     
172     public static ConfigContext getConfigContextFromCache(String JavaDoc url) {
173         return (ConfigContext) _ctxCache.get(url);
174     }
175     
176     private static ConfigContext newConfigContext(ConfigEnvironment ce) {
177         return new ConfigContextImpl(ce);
178     }
179     
180     private static void addConfigContextToCache(String JavaDoc url, ConfigContext ctx) {
181         _ctxCache.put(url, ctx);
182     }
183     
184     private static void invalidateConfigContext(Object JavaDoc obj) {
185         try {
186             if(obj != null)
187                 ((ConfigContextImpl)obj).cleanup(); //FIXME
188
} catch(Exception JavaDoc e) {
189             // ignore since it is just cleanup.
190
// multiple calls to remove should not fail.
191
}
192     }
193 }
194
Popular Tags