1 10 package org.mmbase.module.core; 11 12 import java.util.*; 13 import java.io.*; 14 import javax.servlet.*; 15 import java.text.DateFormat ; 16 17 import org.mmbase.core.util.DaemonTask; 18 import org.mmbase.core.util.DaemonThread; 19 import org.mmbase.util.ResourceLoader; 20 import org.mmbase.util.logging.Logger; 21 import org.mmbase.util.logging.Logging; 22 23 32 public class MMBaseContext { 33 private static final Logger log = Logging.getLoggerInstance(MMBaseContext.class); 34 private static boolean initialized = false; 35 private static boolean htmlRootInitialized = false; 36 private static ServletContext sx; 37 private static String userDir; 38 private static String javaVersion; 39 40 private static String htmlRoot; 41 private static String htmlRootUrlPath = "/"; 42 private static boolean htmlRootUrlPathInitialized = false; 43 private static String outputFile; 44 private static ThreadGroup threadGroup; 45 46 56 public synchronized static void init(ServletContext servletContext) { 57 if (!initialized) { 58 javaVersion = System.getProperty("java.version"); 60 sx = servletContext; 62 userDir = sx.getInitParameter("user.dir"); 65 if (userDir == null) { 66 userDir = System.getProperty("user.dir"); 67 } 68 if (userDir != null && userDir.indexOf("$WEBROOT") == 0) { 70 userDir = servletContext.getRealPath(userDir.substring(8)); 71 } 72 String outputFile = sx.getInitParameter("mmbase.outputfile"); 74 if (outputFile == null) { 75 outputFile = System.getProperty("mmbase.outputfile"); 76 } 77 if (outputFile != null && outputFile.indexOf("$WEBROOT") == 0) { 79 outputFile = servletContext.getRealPath(outputFile.substring(8)); 80 } 81 initOutputfile(outputFile); 82 83 ResourceLoader.init(sx); 84 85 initLogging(); 87 initialized = true; 88 } 89 } 90 91 101 public synchronized static void init(String configPath, boolean initLogging) throws Exception { 102 if (!initialized) { 103 userDir = System.getProperty("user.dir"); 105 106 initOutputfile(System.getProperty("mmbase.outputfile")); 108 109 if (initLogging) { 111 initLogging(); 112 } 113 initialized = true; 114 } 115 } 116 117 126 public synchronized static void init() throws Exception { 127 init(System.getProperty("mmbase.config"), true); 128 } 129 130 134 public synchronized static ThreadGroup getThreadGroup() { 135 if (threadGroup == null) { 136 String groupName = org.mmbase.Version.get(); 137 log.service("Creating threadGroup: " + groupName); 138 threadGroup = new ThreadGroup (groupName); 139 } 140 return threadGroup; 141 } 142 143 149 public static Thread startThread(Runnable task, String name) { 150 DaemonThread kicker = new DaemonThread(task, name); 151 kicker.start(); 152 return kicker; 153 } 154 160 public static Thread startThread(DaemonTask task, String name) { 161 DaemonThread kicker = new DaemonThread(name); 162 kicker.setTask(task); 163 kicker.start(); 164 return kicker; 165 } 166 167 private static void initOutputfile(String o) { 168 outputFile = o; 169 if (outputFile != null) { 170 if (!new File(outputFile).isAbsolute()) { 171 outputFile = userDir + File.separator + outputFile; 172 } 173 try { 174 PrintStream stream = new PrintStream(new FileOutputStream(outputFile, true)); 175 System.setOut(stream); 176 System.setErr(stream); 177 } catch (IOException e) { 178 outputFile = null; 179 log.error("Failed to set mmbase.outputfile to '" + outputFile + "'."); 180 log.error(Logging.stackTrace(e)); 181 } 182 } 183 } 184 185 private static void initLogging() { 186 Logging.configure(ResourceLoader.getConfigurationRoot().getChildResourceLoader("log"), "log.xml"); 188 log.info("==========================="); 189 log.info("MMBase logging initialized."); 190 log.info("==========================="); 191 log.info("java.version : " + javaVersion); 192 log.info("user.dir : " + userDir); 193 String configPath = ResourceLoader.getConfigurationRoot().toString(); 194 log.info("configuration : " + configPath); 195 log.info("webroot : " + ResourceLoader.getWebRoot()); 196 String version = org.mmbase.Version.get(); 197 log.info("version : " + version); 198 Runtime rt = Runtime.getRuntime(); 199 log.info("total memory : " + rt.totalMemory() / (1024 * 1024) + " Mbyte"); 200 log.info("free memory : " + rt.freeMemory() / (1024 * 1024) + " Mbyte"); 201 log.info("system locale : " + Locale.getDefault()); 202 log.info("start time : " + DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(new Date(1000 * (long) MMBase.startTime))); 203 } 204 205 216 public synchronized static void initHtmlRoot() throws ServletException { 217 if (!initialized || sx == null) { 218 String message = "The init(ServletContext) method should be called first."; 219 log.error(message); 220 throw new RuntimeException (message); 221 } 222 if (!htmlRootInitialized) { 223 htmlRoot = sx.getInitParameter("mmbase.htmlroot"); 225 if (htmlRoot == null) { 226 htmlRoot = System.getProperty("mmbase.htmlroot"); 227 } 228 if (htmlRoot == null) { 229 htmlRoot = sx.getRealPath(""); 230 } 231 if (htmlRoot == null){ 232 log.error("Parameter mmbase.htmlroot not set."); 233 } else { 234 if (userDir != null && !new File(htmlRoot).isAbsolute()) { 235 htmlRoot = userDir + File.separator + htmlRoot; 236 } 237 if (!new File(htmlRoot).isDirectory()) { 238 userDir = null; 239 htmlRoot = null; 240 throw new ServletException("Parameter mmbase.htmlroot is not pointing to a directory."); 241 } else { 242 if (htmlRoot.endsWith(File.separator)) { 243 htmlRoot = htmlRoot.substring(0, htmlRoot.length() - 1); 244 } 245 } 246 } 247 htmlRootInitialized = true; 248 log.info("mmbase.htmlroot : " + htmlRoot); 249 log.info("context : " + getHtmlRootUrlPath()); 250 } 251 } 252 253 261 public synchronized static ServletContext getServletContext() { 262 if (!initialized) { 263 throw new RuntimeException ("The init method should be called first."); 264 } 265 return sx; 266 } 267 268 276 public synchronized static String getConfigPath() { 277 List files = ResourceLoader.getConfigurationRoot().getFiles(""); 278 if (files.size() == 0) { 279 return null; 280 } else { 281 return ((File) files.get(0)).getAbsolutePath(); 282 } 283 } 284 285 293 public synchronized static String getHtmlRoot() { 294 if (!htmlRootInitialized) { 295 String message = "The initHtmlRoot method should be called first."; 296 log.error(message); 297 throw new RuntimeException (); 298 } 299 return htmlRoot; 300 } 301 302 311 public synchronized static String getOutputFile() { 312 if (!initialized) { 313 String message = "The init method should be called first."; 314 log.error(message); 315 throw new RuntimeException (message); 316 } 317 return outputFile; 318 } 319 320 327 public synchronized static String getHtmlRootUrlPath() { 328 if (! htmlRootUrlPathInitialized) { 329 log.info("Finding root url"); 330 if (! initialized) { 331 String message = "The init method should be called first."; 332 log.error(message); 333 throw new RuntimeException (message); 334 } 335 if (sx == null) { htmlRootUrlPathInitialized = true; 337 return htmlRootUrlPath; 338 } 339 String initPath = sx.getInitParameter("mmbase.htmlrooturlpath"); 340 if (initPath != null) { 341 log.debug("Found mmbase.htmlrooturlpath explicitely configured"); 342 htmlRootUrlPath = initPath; 343 } else { 344 try { 346 log.debug("Autodetecting htmlrooturlpath "); 347 if (sx.equals(sx.getContext("/"))) { 350 htmlRootUrlPath = "/"; 351 } else { 352 String url = sx.getResource("/").toString(); 353 int length = url.length(); 356 int lastSlash = url.substring(0, length - 1).lastIndexOf('/'); 357 if (lastSlash > 0) { 358 htmlRootUrlPath = url.substring(lastSlash); 359 } else { 360 log.warn("Could not determine htmlRootUrlPath. Using default " + htmlRootUrlPath + "(contextUrl :" + url + ")"); 361 } 362 } 363 } catch (Exception e) { 364 log.error(e); 365 } 366 try { 367 if (!sx.equals(sx.getContext(htmlRootUrlPath))) { 368 log.warn("Probably did not succeed in determining htmlRootUrlPath ('" + htmlRootUrlPath + "'). Consider using the mmbase.htmlrooturlpath context-param in web.xml"); 369 } 370 } catch (Exception e2) { 371 log.error(e2); 372 } 373 } 374 htmlRootUrlPathInitialized = true; 375 } 376 return htmlRootUrlPath; 377 } 378 379 383 public static boolean isInitialized() { 384 return initialized; 385 } 386 } 387
| Popular Tags
|