From ed6712719c4e911ab4492d0b5f9f0610a02ae551 Mon Sep 17 00:00:00 2001 From: hns Date: Wed, 16 Nov 2005 11:44:22 +0000 Subject: [PATCH] Application.java: * Check logdir app property in updateProperties() rather than in getLogger(). * Only set helma.logdir system property if it isn't already set to prevent conflicts between applications. * Insert new repositories at the beginning of the list in addRepository(). * Some cleanup. ApplicationBean.java: * Use "helma.[appname].app" as default log name for messages logged through app.log(), app.debug(). * Introduce app.getLogger() and app.getLogger(name) that return a Jakarta commons Log instance. The zero argument method uses "helma.[appname].app" as category name. * Added and fixed some JavaDocs. --- src/helma/framework/core/Application.java | 48 ++++---- src/helma/framework/core/ApplicationBean.java | 103 ++++++++++-------- 2 files changed, 83 insertions(+), 68 deletions(-) diff --git a/src/helma/framework/core/Application.java b/src/helma/framework/core/Application.java index ad984197..94db7327 100644 --- a/src/helma/framework/core/Application.java +++ b/src/helma/framework/core/Application.java @@ -108,6 +108,8 @@ public final class Application implements IPathElement, Runnable { // Map of requesttrans -> active requestevaluators Hashtable activeRequests; + String logDir; + // Two logs for each application Log eventLog; Log accessLog; @@ -251,8 +253,10 @@ public final class Application implements IPathElement, Runnable { props = new ResourceProperties(this, "app.properties", sysProps); // get log names - accessLogName = props.getProperty("accessLog", "helma."+name+".access"); - eventLogName = props.getProperty("eventLog", "helma."+name+".event"); + accessLogName = props.getProperty("accessLog", + new StringBuffer("helma.").append(name).append(".access").toString()); + eventLogName = props.getProperty("eventLog", + new StringBuffer("helma.").append(name).append(".event").toString()); // create app-level db sources dbProps = new ResourceProperties(this, "db.properties", sysDbProps, false); @@ -303,17 +307,16 @@ public final class Application implements IPathElement, Runnable { // create and init type mananger typemgr = new TypeManager(this, ignoreDirs); + // set the context classloader. Note that this must be done before + // using the logging framework so that a new LogFactory gets created + // for this app. + Thread.currentThread().setContextClassLoader(typemgr.getClassLoader()); try { typemgr.createPrototypes(); } catch (Exception x) { logError("Error creating prototypes", x); } - // set the context classloader. Note that this must be done before - // using the logging framework so that a new LogFactory gets created - // for this app. - Thread.currentThread().setContextClassLoader(typemgr.getClassLoader()); - if (Server.getServer() != null) { Vector extensions = Server.getServer().getExtensions(); @@ -1150,23 +1153,26 @@ public final class Application implements IPathElement, Runnable { } /** - * Returns the prototype name that Hrefs in this application should start with. + * Returns the prototype name that Hrefs in this application should + * start with. */ public String getHrefRootPrototype() { return hrefRootPrototype; } /** - * Tell other classes whether they should output logging information for this application. + * Tell other classes whether they should output logging information for + * this application. */ public boolean debug() { return debug; } /** + * Get the current RequestEvaluator, or null if the calling thread + * is not evaluating a request. * - * - * @return ... + * @return the RequestEvaluator belonging to the current thread */ public RequestEvaluator getCurrentRequestEvaluator() { Thread thread = Thread.currentThread(); @@ -1406,16 +1412,9 @@ public final class Application implements IPathElement, Runnable { * Get a logger object to log events for this application. */ public Log getLogger(String logname) { - String logdir = props.getProperty("logdir", "log"); - - if ("console".equals(logdir) || "console".equals(logname)) { + if ("console".equals(logDir) || "console".equals(logname)) { return Logging.getConsoleLog(); } else { - - // set up helma.logdir system property in case we're using it - File dir = new File(logdir); - System.setProperty("helma.logdir", dir.getAbsolutePath()); - return LogFactory.getLog(logname); } } @@ -1698,7 +1697,7 @@ public final class Application implements IPathElement, Runnable { * Add a repository to this app's repository list. This is used for * ZipRepositories contained in top-level file repositories, for instance. * - * @param rep + * @param rep the repository to add * @return if the repository was not yet contained */ public boolean addRepository(Repository rep) { @@ -1715,7 +1714,7 @@ public final class Application implements IPathElement, Runnable { } } // no parent or parent not in app's repositories. - repositories.add(rep); + repositories.add(0, rep); return true; } return false; @@ -1833,6 +1832,13 @@ public final class Application implements IPathElement, Runnable { } } + logDir = props.getProperty("logdir", "log"); + if (System.getProperty("helma.logdir") == null) { + // set up helma.logdir system property in case we're using it + File dir = new File(logDir); + System.setProperty("helma.logdir", dir.getAbsolutePath()); + } + // set prop read timestamp lastPropertyRead = props.lastModified(); } diff --git a/src/helma/framework/core/ApplicationBean.java b/src/helma/framework/core/ApplicationBean.java index 09e5e5bd..fb962439 100644 --- a/src/helma/framework/core/ApplicationBean.java +++ b/src/helma/framework/core/ApplicationBean.java @@ -25,12 +25,15 @@ import java.io.File; import java.io.Serializable; import java.util.*; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + /** * */ public class ApplicationBean implements Serializable { Application app; - + Log appLog; WrappedMap properties = null; /** @@ -43,59 +46,77 @@ public class ApplicationBean implements Serializable { } /** - * + * Clear the application cache. */ public void clearCache() { app.clearCache(); } /** + * Get the app logger. This is a commons-logging Log with the + * category helma.[appname].app. * + * @return the app logger. + */ + public synchronized Log getLogger() { + if (appLog == null) { + appLog = app.getLogger(new StringBuffer("helma.") + .append(app.getName()).append(".app").toString()); + } + return appLog; + } + + /** + * Get the app logger. This is a commons-logging Log with the + * category logname. * - * @param msg ... + * @return a logger for the given log name. + */ + public Log getLogger(String logname) { + return LogFactory.getLog(logname); + } + + /** + * Log a INFO message to the app log. + * + * @param msg the log message */ public void log(Object msg) { - String str = (msg == null) ? "null" : msg.toString(); - - app.logEvent(str); + getLogger().info(msg); } /** + * Log a INFO message to the app log. * - * - * @param logname ... - * @param msg ... + * @param logname the name (category) of the log + * @param msg the log message */ public void log(String logname, Object msg) { - String str = (msg == null) ? "null" : msg.toString(); - - app.getLogger(logname).info(str); + getLogger(logname).info(msg); } /** + * Log a DEBUG message to the app log if debug is set to true in + * app.properties. * - * - * @param msg ... + * @param msg the log message */ public void debug(Object msg) { if (app.debug()) { - String str = (msg == null) ? "null" : msg.toString(); - - app.logEvent(str); + getLogger().debug(msg); } } /** + * Log a DEBUG message to the app log if debug is set to true in + * app.properties. * - * - * @param logname ... - * @param msg ... + * @param logname the name (category) of the log + * @param msg the log message */ public void debug(String logname, Object msg) { if (app.debug()) { - String str = (msg == null) ? "null" : msg.toString(); - - app.getLogger(logname).info(str); + getLogger(logname).debug(msg); } } @@ -162,7 +183,7 @@ public class ApplicationBean implements Serializable { Iterator it = sessions.values().iterator(); while (it.hasNext()) { - array[i++] = new SessionBean((Session) it.next()); + array[i++] = new SessionBean((Session) it.next()); } return array; @@ -296,7 +317,7 @@ public class ApplicationBean implements Serializable { /** * Returns an read-only map of the custom cron jobs registered with the app * - * @return + * @return a map of cron jobs */ public Map getCronJobs() { return new WrappedMap(app.customCronJobs, true); @@ -312,7 +333,7 @@ public class ApplicationBean implements Serializable { /** * Returns the app's data node used to share data between the app's evaluators * - * @return + * @return the app.data node */ public INode getData() { return app.getCacheNode(); @@ -321,7 +342,7 @@ public class ApplicationBean implements Serializable { /** * Returns the app's modules map used to register application modules * - * @return + * @return the module map */ public Map getModules() { return app.modules; @@ -331,61 +352,49 @@ public class ApplicationBean implements Serializable { * Returns the absolute path of the app dir. When using repositories this * equals the first file based repository. * - * @return + * @return the app dir */ public String getDir() { return app.getAppDir().getAbsolutePath(); } /** - * - * - * @return ... + * @return the app name */ public String getName() { return app.getName(); } /** - * - * - * @return ... + * @return the application start time */ public Date getUpSince() { return new Date(app.starttime); } /** - * - * - * @return ... + * @return the number of requests processed by this app */ public long getRequestCount() { return app.getRequestCount(); } /** - * - * - * @return ... + * @return the number of XML-RPC requests processed */ public long getXmlrpcCount() { return app.getXmlrpcCount(); } /** - * - * - * @return ... + * @return the number of errors encountered */ public long getErrorCount() { return app.getErrorCount(); } /** - * - * - * @return ... + * @return the wrapped helma.framework.core.Application object */ public Application get__app__() { return app; @@ -474,7 +483,7 @@ public class ApplicationBean implements Serializable { // add one to the number to compensate for the internal scheduler. app.setNumberOfEvaluators(n + 1); } - + /** * Return a skin for a given object. The skin is found by determining the prototype * to use for the object, then looking up the skin for the prototype.