KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > repository > config > RepositoryConfigRegistry


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.repository.config;
8
9
10 import java.util.Collection JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Map JavaDoc;
13
14 import javax.servlet.ServletRequest JavaDoc;
15
16 import com.inversoft.config.ConfigRegistry;
17
18
19 /**
20  * <p>
21  * This class is the config registry for the repository system
22  * of the Inversoft Portal framework.
23  * </p>
24  *
25  * @author Brian Pontarelli
26  * @since 2.0
27  * @version 2.0
28  */

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

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

42     private static volatile RepositoryConfigRegistry instance =
43         new RepositoryConfigRegistry();
44
45
46     /**
47      * The map that stores the configuration (unsynchronized)
48      */

49     private Map JavaDoc configurations = new HashMap JavaDoc();
50
51
52     /**
53      * Constructs a new <code>RepositoryConfigRegistry</code>.
54      */

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

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

101     protected static RepositoryConfigRegistry setInstance(
102             RepositoryConfigRegistry instance) {
103         RepositoryConfigRegistry old = RepositoryConfigRegistry.instance;
104         RepositoryConfigRegistry.instance = instance;
105         return old;
106     }
107
108     /**
109      * Stores the given Config object in the registry under the given id. This
110      * method accesses an unsynchronized Map and should not be used by application
111      * developers. This is public for Inversoft internal tests only.
112      *
113      * @param id The id to store the config under
114      * @param config The Config to store
115      */

116     protected void register(String JavaDoc id, Config config) {
117         assert (id != null) : "id == null";
118         assert (config != null) : "config == null";
119
120         configurations.put(id, config);
121     }
122
123     /**
124      * Retrieves the Config object stored under the given id
125      *
126      * @param id The id the Config was registered under
127      * @return The Config object or null if one was never registered under the
128      * given id
129      */

130     public Config lookup(String JavaDoc id) {
131         return (Config) configurations.get(id);
132     }
133
134     /**
135      * Returns a list of all the configuration objects stored in the regsitry
136      *
137      * @return A List of all the configuration objects in the registry
138      */

139     public Collection JavaDoc getConfigurations() {
140         return configurations.values();
141     }
142 }
143
Popular Tags