KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > util > url > config > URLConfigRegistry


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.util.url.config;
8
9
10 import java.util.HashMap JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import javax.servlet.ServletRequest JavaDoc;
14
15 import com.inversoft.config.ConfigRegistry;
16
17
18 /**
19  * <p>
20  * This class is the configuration registry for the URL
21  * configuration objects
22  * </p>
23  *
24  * @author Brian Pontarelli
25  * @since 2.0
26  * @version 2.0
27  */

28 public class URLConfigRegistry implements ConfigRegistry {
29
30     /**
31      * The key that the registry is stored under in the request to ensure that
32      * for a single request to the server, the configuration remains the same
33      */

34     public static final String JavaDoc KEY = URLConfigRegistry.class.getName();
35
36     /**
37      * ThreadLocals will NOT work because containers use Thread pooling so the
38      * execute Thread probably has already been used and the ThreadLocal will
39      * already have been setup
40      */

41     private static volatile URLConfigRegistry instance = new URLConfigRegistry();
42
43
44     private Map JavaDoc categories;
45
46
47     /**
48      * Constructor for URLConfigRegistry.
49      */

50     protected URLConfigRegistry() {
51         categories = new HashMap JavaDoc();
52     }
53
54
55     /**
56      * <p>
57      * Singleton accessor method except that the HttpServletRequest is used in
58      * order to maintain the context of a single request. Since a single request
59      * could be in progress while the configuration is changing, this prevents
60      * that request from seeing the changes because a reference to the
61      * configuration is stored in the request and then retrieved from there.
62      * </p>
63      *
64      * <p>
65      * Passing null to this method returns the current configuration. Subsequent
66      * calls with null are not guarenteed to return the same configuration
67      * instance.
68      * </p>
69      *
70      * @param request (Optional) The ServletRequest used to maintained request
71      * consistency
72      * @return The singleton instance
73      */

74     public static URLConfigRegistry getInstance(ServletRequest JavaDoc request) {
75         assert (instance != null) : "instance == null";
76
77         URLConfigRegistry localInstance = null;
78         if (request == null) {
79             localInstance = instance;
80         } else {
81             localInstance = (URLConfigRegistry) request.getAttribute(KEY);
82         }
83         
84         if (localInstance == null && request != null) {
85             request.setAttribute(KEY, instance);
86             localInstance = instance;
87         }
88
89         return localInstance;
90     }
91     
92     /**
93      * Sets a new singleton instance
94      *
95      * @param newInstance The singleton instance of the URLConfigRegistry
96      */

97     protected static void setInstance(URLConfigRegistry newInstance) {
98         instance = newInstance;
99     }
100
101
102     /**
103      * Adds a new CategoryConfig to this registry
104      *
105      * @param cc The new CategoryConfig
106      */

107     protected void register(CategoryConfig cc) {
108         categories.put(cc.getName(), cc);
109     }
110     
111     /**
112      * Retrieves the CategoryConfig stored under the given name
113      *
114      * @param name The name the config is stored under
115      * @return The CategoryConfig or null if it does not exist
116      */

117     public CategoryConfig lookupCategory(String JavaDoc name) {
118         return (CategoryConfig) categories.get(name);
119     }
120 }
Popular Tags