1 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 ; 19 import java.util.Iterator ; 20 import java.util.Map ; 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 ; 28 29 import org.apache.commons.lang.BooleanUtils; 30 import org.apache.commons.lang.StringUtils; 31 import org.apache.log4j.Logger; 32 33 34 38 public final class Server { 39 40 public static final String CONFIG_PAGE = "server"; 42 45 protected static Logger log = Logger.getLogger(Server.class); 46 47 private static Map cachedContent = new Hashtable (); 48 49 private static Map cachedURImapping = new Hashtable (); 50 51 private static Map cachedCacheableURIMapping = new Hashtable (); 52 53 56 private Server() { 57 } 59 60 63 public static void init() throws ConfigurationException { 64 load(); 65 registerEventListener(); 66 } 67 68 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"); Content startPage = ContentRepository.getHierarchyManager(ContentRepository.CONFIG).getContent(CONFIG_PAGE); 79 cacheServerConfiguration(startPage); 80 cacheSecureURIList(startPage); 81 log.info("Config : Server config loaded"); } 83 catch (RepositoryException re) { 84 log.error("Config : Failed to load Server config: " + re.getMessage(), re); throw new ConfigurationException("Config : Failed to load Server config: " + re.getMessage(), re); } 87 } 88 89 93 public static void reload() throws ConfigurationException { 94 log.info("Config : re-loading Server config"); Server.load(); 96 } 97 98 101 private static void cacheServerConfiguration(Content page) { 102 103 boolean isAdmin = page.getNodeData("admin").getBoolean(); Server.cachedContent.put("admin", BooleanUtils.toBooleanObject(isAdmin)); 106 String ext = page.getNodeData("defaultExtension").getString(); Server.cachedContent.put("defaultExtension", ext); 109 String basicRealm = page.getNodeData("basicRealm").getString(); Server.cachedContent.put("basicRealm", basicRealm); 112 try { 113 String mailServer = page.getNodeData("defaultMailServer").getString(); Server.cachedContent.put("defaultMailServer", mailServer); } 116 catch (Exception e) { 117 log.error(e.getMessage()); 118 Server.cachedContent.put("defaultMailServer", StringUtils.EMPTY); } 120 121 Server.cachedContent.put("404URI", page.getNodeData("ResourceNotAvailableURIMapping").getString()); 123 boolean visibleToObinary = page.getNodeData("visibleToObinary").getBoolean(); Server.cachedContent.put("visibleToObinary", BooleanUtils.toBooleanObject(visibleToObinary)); 126 } 127 128 131 private static void cacheSecureURIList(Content page) { 132 try { 133 addToSecureList(page.getContent("secureURIList")); } 135 catch (RepositoryException re) { 136 log.error(re.getMessage(), re); 137 } 138 139 } 140 141 145 private static void registerEventListener() { 146 147 log.info("Registering event listener for server"); 149 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 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, false, null, null, false); 169 } 170 catch (RepositoryException e) { 171 log.error("Unable to add event listeners for server", e); } 173 174 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 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); } 198 catch (RepositoryException e) { 199 log.error("Unable to add event listeners for server", e); } 201 } 202 203 private static void addToSecureList(Content node) { 204 205 if (node == null) { 206 return; 207 } 208 Iterator childIterator = node.getChildren().iterator(); 209 while (childIterator.hasNext()) { 210 Content sub = (Content) childIterator.next(); 211 String uri = sub.getNodeData("URI").getString(); SecureURI.add(uri); 213 } 214 } 215 216 219 public static String get404URI() { 220 String uri = (String ) Server.cachedContent.get("404URI"); if (StringUtils.isEmpty(uri)) { 222 return "/"; } 224 if (log.isDebugEnabled()) { 225 log.debug("404URI is \"" + uri + "\""); } 227 return uri; 228 } 229 230 233 public static String getDefaultExtension() { 234 String defaultExtension = (String ) Server.cachedContent.get("defaultExtension"); if (defaultExtension == null) { 236 return StringUtils.EMPTY; 237 } 238 return defaultExtension; 239 } 240 241 244 public static String getDefaultMailServer() { 245 return (String ) Server.cachedContent.get("defaultMailServer"); } 247 248 251 public static String getBasicRealm() { 252 return (String ) Server.cachedContent.get("basicRealm"); } 254 255 258 public static boolean isAdmin() { 259 return ((Boolean ) Server.cachedContent.get("admin")).booleanValue(); } 261 262 265 public static boolean isVisibleToObinary() { 266 return ((Boolean ) Server.cachedContent.get("visibleToObinary")).booleanValue(); } 268 269 274 public static boolean isCacheable() { 275 return Cache.isCacheable(); 276 } 277 278 284 public static boolean isCacheable(HttpServletRequest request) { 285 return Cache.isCacheable(request); 286 } 287 } 288 | Popular Tags |