KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > beans > config > Server


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.beans.config;
14
15 import info.magnolia.cms.core.Content;
16 import info.magnolia.cms.security.SecureURI;
17
18 import java.util.Hashtable JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.jcr.RepositoryException;
23 import javax.jcr.observation.Event;
24 import javax.jcr.observation.EventIterator;
25 import javax.jcr.observation.EventListener;
26 import javax.jcr.observation.ObservationManager;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28
29 import org.apache.commons.lang.BooleanUtils;
30 import org.apache.commons.lang.StringUtils;
31 import org.apache.log4j.Logger;
32
33
34 /**
35  * @author Sameer Charles
36  * @version 1.1
37  */

38 public final class Server {
39
40     public static final String JavaDoc CONFIG_PAGE = "server"; //$NON-NLS-1$
41

42     /**
43      * Logger.
44      */

45     protected static Logger log = Logger.getLogger(Server.class);
46
47     private static Map JavaDoc cachedContent = new Hashtable JavaDoc();
48
49     private static Map JavaDoc cachedURImapping = new Hashtable JavaDoc();
50
51     private static Map JavaDoc cachedCacheableURIMapping = new Hashtable JavaDoc();
52
53     /**
54      * Utility class, don't instantiate.
55      */

56     private Server() {
57         // unused
58
}
59
60     /**
61      * @throws ConfigurationException if basic config nodes are missing
62      */

63     public static void init() throws ConfigurationException {
64         load();
65         registerEventListener();
66     }
67
68     /**
69      * Load the server configuration.
70      * @throws ConfigurationException
71      */

72     public static void load() throws ConfigurationException {
73         Server.cachedContent.clear();
74         Server.cachedURImapping.clear();
75         Server.cachedCacheableURIMapping.clear();
76         try {
77             log.info("Config : loading Server"); //$NON-NLS-1$
78
Content startPage = ContentRepository.getHierarchyManager(ContentRepository.CONFIG).getContent(CONFIG_PAGE);
79             cacheServerConfiguration(startPage);
80             cacheSecureURIList(startPage);
81             log.info("Config : Server config loaded"); //$NON-NLS-1$
82
}
83         catch (RepositoryException re) {
84             log.error("Config : Failed to load Server config: " + re.getMessage(), re); //$NON-NLS-1$
85
throw new ConfigurationException("Config : Failed to load Server config: " + re.getMessage(), re); //$NON-NLS-1$
86
}
87     }
88
89     /**
90      * Reload the server configuration: simply calls load().
91      * @throws ConfigurationException
92      */

93     public static void reload() throws ConfigurationException {
94         log.info("Config : re-loading Server config"); //$NON-NLS-1$
95
Server.load();
96     }
97
98     /**
99      * Cache server content from the config repository.
100      */

101     private static void cacheServerConfiguration(Content page) {
102
103         boolean isAdmin = page.getNodeData("admin").getBoolean(); //$NON-NLS-1$
104
Server.cachedContent.put("admin", BooleanUtils.toBooleanObject(isAdmin)); //$NON-NLS-1$
105

106         String JavaDoc ext = page.getNodeData("defaultExtension").getString(); //$NON-NLS-1$
107
Server.cachedContent.put("defaultExtension", ext); //$NON-NLS-1$
108

109         String JavaDoc basicRealm = page.getNodeData("basicRealm").getString(); //$NON-NLS-1$
110
Server.cachedContent.put("basicRealm", basicRealm); //$NON-NLS-1$
111

112         try {
113             String JavaDoc mailServer = page.getNodeData("defaultMailServer").getString(); //$NON-NLS-1$
114
Server.cachedContent.put("defaultMailServer", mailServer); //$NON-NLS-1$
115
}
116         catch (Exception JavaDoc e) {
117             log.error(e.getMessage());
118             Server.cachedContent.put("defaultMailServer", StringUtils.EMPTY); //$NON-NLS-1$
119
}
120
121         Server.cachedContent.put("404URI", page.getNodeData("ResourceNotAvailableURIMapping").getString()); //$NON-NLS-1$ //$NON-NLS-2$
122

123         boolean visibleToObinary = page.getNodeData("visibleToObinary").getBoolean(); //$NON-NLS-1$
124
Server.cachedContent.put("visibleToObinary", BooleanUtils.toBooleanObject(visibleToObinary)); //$NON-NLS-1$
125

126     }
127
128     /**
129      * Cache server content from the config repository.
130      */

131     private static void cacheSecureURIList(Content page) {
132         try {
133             addToSecureList(page.getContent("secureURIList")); //$NON-NLS-1$
134
}
135         catch (RepositoryException re) {
136             log.error(re.getMessage(), re);
137         }
138
139     }
140
141     /**
142      * Register an event listener: reload server configuration when something changes.
143      * @todo split reloading of base server configuration and secure URI list
144      */

145     private static void registerEventListener() {
146
147         log.info("Registering event listener for server"); //$NON-NLS-1$
148

149         // server properties, only on the root server node
150
try {
151             ObservationManager observationManager = ContentRepository
152                 .getHierarchyManager(ContentRepository.CONFIG)
153                 .getWorkspace()
154                 .getObservationManager();
155
156             observationManager.addEventListener(new EventListener() {
157
158                 public void onEvent(EventIterator iterator) {
159                     // reload everything
160
try {
161                         reload();
162                     }
163                     catch (ConfigurationException e) {
164                         log.error(e.getMessage(), e);
165                     }
166                 }
167             }, Event.PROPERTY_ADDED | Event.PROPERTY_CHANGED | Event.PROPERTY_REMOVED, "/" + CONFIG_PAGE, //$NON-NLS-1$
168
false, null, null, false);
169         }
170         catch (RepositoryException e) {
171             log.error("Unable to add event listeners for server", e); //$NON-NLS-1$
172
}
173
174         // secure URI list
175
try {
176             ObservationManager observationManager = ContentRepository
177                 .getHierarchyManager(ContentRepository.CONFIG)
178                 .getWorkspace()
179                 .getObservationManager();
180
181             observationManager.addEventListener(new EventListener() {
182
183                 public void onEvent(EventIterator iterator) {
184                     // reload everything
185
try {
186                         reload();
187                     }
188                     catch (ConfigurationException e) {
189                         log.error(e.getMessage(), e);
190                     }
191                 }
192             }, Event.NODE_ADDED
193                 | Event.NODE_REMOVED
194                 | Event.PROPERTY_ADDED
195                 | Event.PROPERTY_CHANGED
196                 | Event.PROPERTY_REMOVED, "/" + CONFIG_PAGE + "/secureURIList", true, null, null, false); //$NON-NLS-1$ //$NON-NLS-2$
197
}
198         catch (RepositoryException e) {
199             log.error("Unable to add event listeners for server", e); //$NON-NLS-1$
200
}
201     }
202
203     private static void addToSecureList(Content node) {
204
205         if (node == null) {
206             return;
207         }
208         Iterator JavaDoc childIterator = node.getChildren().iterator();
209         while (childIterator.hasNext()) {
210             Content sub = (Content) childIterator.next();
211             String JavaDoc uri = sub.getNodeData("URI").getString(); //$NON-NLS-1$
212
SecureURI.add(uri);
213         }
214     }
215
216     /**
217      * @return resource not available URI mapping as specifies in serverInfo, else /
218      */

219     public static String JavaDoc get404URI() {
220         String JavaDoc uri = (String JavaDoc) Server.cachedContent.get("404URI"); //$NON-NLS-1$
221
if (StringUtils.isEmpty(uri)) {
222             return "/"; //$NON-NLS-1$
223
}
224         if (log.isDebugEnabled()) {
225             log.debug("404URI is \"" + uri + "\""); //$NON-NLS-1$ //$NON-NLS-2$
226
}
227         return uri;
228     }
229
230     /**
231      * @return default URL extension as configured
232      */

233     public static String JavaDoc getDefaultExtension() {
234         String JavaDoc defaultExtension = (String JavaDoc) Server.cachedContent.get("defaultExtension"); //$NON-NLS-1$
235
if (defaultExtension == null) {
236             return StringUtils.EMPTY;
237         }
238         return defaultExtension;
239     }
240
241     /**
242      * @return default mail server
243      */

244     public static String JavaDoc getDefaultMailServer() {
245         return (String JavaDoc) Server.cachedContent.get("defaultMailServer"); //$NON-NLS-1$
246
}
247
248     /**
249      * @return basic realm string
250      */

251     public static String JavaDoc getBasicRealm() {
252         return (String JavaDoc) Server.cachedContent.get("basicRealm"); //$NON-NLS-1$
253
}
254
255     /**
256      * @return true if the instance is configured as an admin server
257      */

258     public static boolean isAdmin() {
259         return ((Boolean JavaDoc) Server.cachedContent.get("admin")).booleanValue(); //$NON-NLS-1$
260
}
261
262     /**
263      *
264      */

265     public static boolean isVisibleToObinary() {
266         return ((Boolean JavaDoc) Server.cachedContent.get("visibleToObinary")).booleanValue(); //$NON-NLS-1$
267
}
268
269     /**
270      * @see Cache#isCacheable()
271      * @return true if the pages could be cached
272      * @deprecated
273      */

274     public static boolean isCacheable() {
275         return Cache.isCacheable();
276     }
277
278     /**
279      * @param request HttpServletRequest
280      * @return true if the requested URI can be added to cache
281      * @deprecated
282      * @see Cache#isCacheable(javax.servlet.http.HttpServletRequest)
283      */

284     public static boolean isCacheable(HttpServletRequest JavaDoc request) {
285         return Cache.isCacheable(request);
286     }
287 }
288
Popular Tags