User: starksm
Date: 01/12/07 20:35:42
Modified: src/main/org/jboss/system MBeanClassLoader.java
ServiceLibraries.java Shutdown.java
URLClassLoader.java
Added: src/main/org/jboss/system BootstrapLogger.java
Log:
Switch from System.out to log4j based logging that is loaded using the
thread context class loader via a reflection based wrapper to avoid explicit
dependencies on the log4j classes. This is required for a minimal bootstrap
footprint.
Revision Changes Path
1.4 +22 -12 jboss/src/main/org/jboss/system/MBeanClassLoader.java
Index: MBeanClassLoader.java
===================================================================
RCS file: /cvsroot/jboss/jboss/src/main/org/jboss/system/MBeanClassLoader.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- MBeanClassLoader.java 2001/11/24 20:54:34 1.3
+++ MBeanClassLoader.java 2001/12/08 04:35:42 1.4
@@ -15,7 +15,7 @@
* The pupose of MBeanCL is to load the classes on behalf of an MBean.
*
* @author <a href="[EMAIL PROTECTED]">Marc Fleury</a>
- * @version $Revision: 1.3 $
+ * @version $Revision: 1.4 $
*
* <p><b>20010830 marc fleury:</b>
* <ul>
@@ -28,7 +28,9 @@
{
/** All SCL are just in orbit around a basic ServiceLibraries */
private static ServiceLibraries libraries;
-
+ /** The bootstrap interface to the log4j system */
+ private static BootstrapLogger log =
BootstrapLogger.getLogger(MBeanClassLoader.class);
+
private ObjectName objectName;
/**
@@ -42,7 +44,8 @@
super();
this.objectName = objectName;
- if (libraries == null) {
+ if (libraries == null)
+ {
libraries = ServiceLibraries.getLibraries();
}
}
@@ -53,7 +56,8 @@
*
* @return MBean object name.
*/
- public ObjectName getObjectName() {
+ public ObjectName getObjectName()
+ {
return objectName;
}
@@ -64,8 +68,9 @@
public Class loadClass(String name, boolean resolve)
throws ClassNotFoundException
{
- if (name.endsWith("CHANGEME")) {
- System.out.println("MCL LOAD " + this.hashCode() +
+ if (name.endsWith("CHANGEME"))
+ {
+ log.debug("MCL LOAD " + this.hashCode() +
" in loadClass " + name);
}
@@ -79,19 +84,24 @@
return loadClass(name, true);
}
- public URL getResource(String name) {
+ public URL getResource(String name)
+ {
- if (name.endsWith("CHANGEME")) {
- System.out.println("MCL GETRESOURCE " + name +
+ if (name.endsWith("CHANGEME"))
+ {
+ log.debug("MCL GETRESOURCE " + name +
" in SCL " + this.hashCode());
}
return libraries.getResource(name, this);
}
- public InputStream getResourceAsStream(String name) {
- try {
+ public InputStream getResourceAsStream(String name)
+ {
+ try
+ {
URL url = getResource(name);
- if (url != null) {
+ if (url != null)
+ {
return url.openStream();
}
}
1.10 +77 -42 jboss/src/main/org/jboss/system/ServiceLibraries.java
Index: ServiceLibraries.java
===================================================================
RCS file: /cvsroot/jboss/jboss/src/main/org/jboss/system/ServiceLibraries.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- ServiceLibraries.java 2001/12/07 02:31:05 1.9
+++ ServiceLibraries.java 2001/12/08 04:35:42 1.10
@@ -27,7 +27,7 @@
* @see <related>
* @author <a href="mailto:[EMAIL PROTECTED]">Marc Fleury</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Ole Husgaard</a>
- * @version $Revision: 1.9 $ <p>
+ * @version $Revision: 1.10 $ <p>
*
* <b>20010830 marc fleury:</b>
* <ul>initial import
@@ -44,13 +44,11 @@
* </ul>
*
*/
-
public class ServiceLibraries
implements ServiceLibrariesMBean, MBeanRegistration
{
-
- // JBoss logger version move to log4j if needed
- //Log log = Log.createLog("VM-ClassLoader");
+ /** The bootstrap interface to the log4j system */
+ private static BootstrapLogger log =
BootstrapLogger.getLogger(ServiceLibraries.class);
// Static --------------------------------------------------------
private static ServiceLibraries libraries;
@@ -149,7 +147,8 @@
Set classLoaders2;
long clToResourceSetMapVersion2;
- synchronized (this) {
+ synchronized (this)
+ {
// Is it in the global map?
if (resources.containsKey(name))
return (URL)resources.get(name);
@@ -172,18 +171,24 @@
if (resource == null)
{
// If not start asking around to URL classloaders for it
- for (Iterator iter = classLoaders2.iterator(); iter.hasNext();) {
+ for (Iterator iter = classLoaders2.iterator(); iter.hasNext();)
+ {
URLClassLoader cl = (URLClassLoader)iter.next();
- if (!cl.equals(scl)) { // already tried this one
+ if (!cl.equals(scl))
+ { // already tried this one
resource = cl.getResourceLocally(name);
- if (resource != null) {
- synchronized (this) {
+ if (resource != null)
+ {
+ synchronized (this)
+ {
// Did the version change?
- if (clToResourceSetMapVersion2 != clToResourceSetMapVersion) {
+ if (clToResourceSetMapVersion2 != clToResourceSetMapVersion)
+ {
// Yes. Is the class loader we used still here?
- if (!classLoaders.contains(cl)) {
+ if (!classLoaders.contains(cl))
+ {
// No, it was removed from under us.
// Don't change the maps, simply return the resource.
return resource;
@@ -194,7 +199,8 @@
// When we cycle the cl we also need to remove the classes it
loaded
Set set = (Set)clToResourceSetMap.get(cl);
- if (set == null) {
+ if (set == null)
+ {
set = new HashSet();
clToResourceSetMap.put(cl, set);
}
@@ -221,16 +227,24 @@
{
// we allow for duplicate class loader definitions in the services.xml files
// we should however only keep the first classloader declared
-
- if (!classLoaders.contains(cl)) {
+ boolean trace = log.isTraceEnabled();
+ if (!classLoaders.contains(cl))
+ {
// We create a new copy of the classLoaders set.
classLoaders = new HashSet(classLoaders);
classLoaders.add(cl);
-
- System.out.println("Libraries adding URLClassLoader " + cl.hashCode() + "
key URL " + ((URLClassLoader)cl).getKeyURL().toString());
- } else
- System.out.println("Libraries skipping duplicate URLClassLoader for key
URL " + ((URLClassLoader)cl).getKeyURL().toString());
+ if( trace )
+ {
+ log.trace("Libraries adding URLClassLoader " + cl.hashCode() +
+ " key URL " + cl.getKeyURL().toString());
+ }
+ }
+ else if( trace )
+ {
+ log.trace("Libraries skipping duplicate URLClassLoader for key URL " +
+ cl.getKeyURL().toString());
+ }
}
/**
@@ -240,7 +254,9 @@
*/
public synchronized void removeClassLoader(URLClassLoader cl)
{
- System.out.println("removing classloader " + cl);
+ boolean trace = log.isTraceEnabled();
+ if( trace )
+ log.trace("removing classloader " + cl);
if (!classLoaders.contains(cl))
return; // nothing to remove
@@ -249,29 +265,35 @@
classLoaders = new HashSet(classLoaders);
classLoaders.remove(cl);
- if (clToClassSetMap.containsKey(cl)) {
+ if (clToClassSetMap.containsKey(cl))
+ {
// We have a new version of the map
++clToClassSetMapVersion;
Set clClasses = (Set)clToClassSetMap.remove(cl);
- for (Iterator iter = clClasses.iterator(); iter.hasNext();) {
+ for (Iterator iter = clClasses.iterator(); iter.hasNext();)
+ {
Object o = iter.next();
Object o1 = classes.remove(o);
- System.out.println("removing class " + o + ", removed: " + o1);
+ if( trace )
+ log.trace("removing class " + o + ", removed: " + o1);
}
}
// Same procedure for resources
- if (clToResourceSetMap.containsKey(cl)) {
+ if (clToResourceSetMap.containsKey(cl))
+ {
++clToResourceSetMapVersion;
Set clResources = (Set)clToResourceSetMap.remove(cl);
- for (Iterator iter = clResources.iterator(); iter.hasNext();) {
+ for (Iterator iter = clResources.iterator(); iter.hasNext();)
+ {
Object o = iter.next();
Object o1 = resources.remove(o);
- System.out.println("removing resource " + o + ", removed: " + o1);
+ if( trace )
+ log.trace("removing resource " + o + ", removed: " + o1);
}
}
}
@@ -294,7 +316,8 @@
Set classLoaders2;
long clToClassSetMapVersion2;
- synchronized (this) {
+ synchronized (this)
+ {
// Try the local map already
foundClass = (Class)classes.get(name);
@@ -311,41 +334,53 @@
}
// If not start asking around to URL classloaders for it
-
// who will find it?
URLClassLoader cl = null;
- if (scl instanceof URLClassLoader) {
+ if (scl instanceof URLClassLoader)
+ {
// First ask the asking classloader chances are the dependent class is in
there
- try {
+ try
+ {
foundClass = ((URLClassLoader)scl).loadClassLocally(name, resolve);
//If we get here we know the scl is the right one
cl = (URLClassLoader)scl;
- } catch (ClassNotFoundException ignored) {
}
+ catch (ClassNotFoundException ignored)
+ {
+ }
}
Iterator allLoaders = classLoaders2.iterator();
- while (allLoaders.hasNext() && (foundClass == null)) {
+ while (allLoaders.hasNext() && (foundClass == null))
+ {
// next!
cl = (URLClassLoader)allLoaders.next();
- if (!scl.equals(cl)) {
- try {
+ if (!scl.equals(cl))
+ {
+ try
+ {
foundClass = cl.loadClassLocally(name, resolve);
- } catch (ClassNotFoundException ignored2) {
+ }
+ catch (ClassNotFoundException ignored2)
+ {
//try next loader
}
}
} //allLoaders
- if (foundClass != null) {
- synchronized (this) {
+ if (foundClass != null)
+ {
+ synchronized (this)
+ {
// Did the version change?
- if (clToClassSetMapVersion2 != clToClassSetMapVersion) {
+ if (clToClassSetMapVersion2 != clToClassSetMapVersion)
+ {
// Yes. Is the class loader we used still here?
- if (!classLoaders.contains(cl)) {
+ if (!classLoaders.contains(cl))
+ {
// No, it was removed from under us.
// Don't change the maps, simply return the class.
return foundClass;
@@ -356,7 +391,8 @@
// When we cycle the cl we also need to remove the classes it loaded
Set set = (Set)clToClassSetMap.get(cl);
- if (set == null) {
+ if (set == null)
+ {
set = new HashSet();
clToClassSetMap.put(cl, set);
}
@@ -392,7 +428,7 @@
clToResourceSetMap = new HashMap();
clToClassSetMap = new HashMap();
- System.out.println("[GPA] Microkernel ClassLoaders and Libraries
initialized");
+ log.info("[GPA] Microkernel ClassLoaders and Libraries initialized");
return name == null ? new ObjectName(OBJECT_NAME) : name;
}
@@ -433,4 +469,3 @@
// Inner classes -------------------------------------------------
}
-
1.10 +101 -85 jboss/src/main/org/jboss/system/Shutdown.java
Index: Shutdown.java
===================================================================
RCS file: /cvsroot/jboss/jboss/src/main/org/jboss/system/Shutdown.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- Shutdown.java 2001/11/24 20:54:34 1.9
+++ Shutdown.java 2001/12/08 04:35:42 1.10
@@ -18,94 +18,110 @@
* provides the ability to handle user shutdown requests.
* @author <a href="mailto:[EMAIL PROTECTED]">Rickard �berg</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Jason Dillon</a>
- * @version $Revision: 1.9 $
+ * @version $Revision: 1.10 $
*/
-public class Shutdown implements MBeanRegistration, ShutdownMBean {
- // Constants -----------------------------------------------------
- // Attributes ----------------------------------------------------
-
- /** Instance logger. */
- private final Category log = Category.getInstance(Shutdown.class);
-
- /** The MBean server we are attached to. */
- private MBeanServer server;
- // Public -------------------------------------------------------
-
- /** Shutdown the virtual machine and run shutdown hooks. */
- public void shutdown() {
- log.info("Shutting down");
- System.exit(0); // This will execute the shutdown hook
- }
-
- /** Forcibly terminates the currently running Java virtual machine. */
- public void halt() {
- System.err.println("Halting the system now!");
- Runtime.getRuntime().halt(0);
- }
- // MBeanRegistration implementation ------------------------------
-
- /**
- * Saves a reference to the MBean server for later use and installs a shutdown
hook.
- * @param server The MBean server which we are going to be registered.
- * @param name The object name we have been configured to use.
- * @return Our preferred object name.
- * @throws MalformedObjectNameException
- */
- public ObjectName preRegister(final MBeanServer server, final ObjectName name)
throws Exception {
- this.server = server;
- try {
- Runtime.getRuntime().addShutdownHook(
- new Thread("JBoss Shutdown Hook") {
- public void run() {
- log.info("Shutting down all services");
- System.out.println("Shutting down");
- // Make sure all services are down properly
- shutdownServices();
- log.info("Shutdown complete");
- System.out.println("Shutdown complete");
- }
- });
- log.info("Shutdown hook added");
- } catch (Throwable e) {
- log.error("Could not add shutdown hook", e);
- }
- return name == null ? new ObjectName(OBJECT_NAME) : name;
- }
-
- public void postRegister(Boolean registrationDone) {
- // empty
- }
-
- public void preDeregister() throws Exception {
- // empty
- }
-
- public void postDeregister() {
- // empty
- }
-
+public class Shutdown implements MBeanRegistration, ShutdownMBean
+{
+ // Constants -----------------------------------------------------
+ // Attributes ----------------------------------------------------
+
+ /** Instance logger. */
+ private final Category log = Category.getInstance(Shutdown.class);
+
+ /** The MBean server we are attached to. */
+ private MBeanServer server;
+ // Public -------------------------------------------------------
+
+ /** Shutdown the virtual machine and run shutdown hooks. */
+ public void shutdown()
+ {
+ log.info("Shutting down");
+ System.exit(0); // This will execute the shutdown hook
+ }
+
+ /** Forcibly terminates the currently running Java virtual machine. */
+ public void halt()
+ {
+ System.err.println("Halting the system now!");
+ Runtime.getRuntime().halt(0);
+ }
+ // MBeanRegistration implementation ------------------------------
+
/**
- * The <code>shutdownServices</code> method calls the one and only
- * ServiceController to shut down all the mbeans registered with it.
+ * Saves a reference to the MBean server for later use and installs a shutdown
hook.
+ * @param server The MBean server which we are going to be registered.
+ * @param name The object name we have been configured to use.
+ * @return Our preferred object name.
+ * @throws MalformedObjectNameException
+ */
+ public ObjectName preRegister(final MBeanServer server, final ObjectName name)
throws Exception
+ {
+ this.server = server;
+ try
+ {
+ Runtime.getRuntime().addShutdownHook(
+ new Thread("JBoss Shutdown Hook")
+ {
+ public void run()
+ {
+ log.info("Shutting down all services");
+ System.out.println("Shutting down");
+ // Make sure all services are down properly
+ shutdownServices();
+ log.info("Shutdown complete");
+ System.out.println("Shutdown complete");
+ }
+ }
+ );
+ log.info("Shutdown hook added");
+ }
+ catch (Throwable e)
+ {
+ log.error("Could not add shutdown hook", e);
+ }
+ return name == null ? new ObjectName(OBJECT_NAME) : name;
+ }
+
+ public void postRegister(Boolean registrationDone)
+ {
+ // empty
+ }
+
+ public void preDeregister() throws Exception
+ {
+ // empty
+ }
+
+ public void postDeregister()
+ {
+ // empty
+ }
+
+ /**
+ * The <code>shutdownServices</code> method calls the one and only
+ * ServiceController to shut down all the mbeans registered with it.
* We could make the ServiceController an mbean-ref...
*
*/
- protected void shutdownServices() {
- try {
- // set to true for detailed name printouts
- boolean verbose = false;
- // get the deployed objects from ServiceController
- server.invoke(new ObjectName(
- "JBOSS-SYSTEM:spine=ServiceController"),
- "shutdown",
- new Object[0],
- new String[0]);
- }
- catch (RuntimeMBeanException rmbe) {
- rmbe.getTargetException().printStackTrace();
- }
- catch (Exception e) {
- log.error("failed to destroy services", e);
- }
- }
+ protected void shutdownServices()
+ {
+ try
+ {
+ // set to true for detailed name printouts
+ boolean verbose = false;
+ // get the deployed objects from ServiceController
+ server.invoke(new ObjectName("JBOSS-SYSTEM:spine=ServiceController"),
+ "shutdown",
+ new Object[0],
+ new String[0]);
+ }
+ catch (RuntimeMBeanException rmbe)
+ {
+ rmbe.getTargetException().printStackTrace();
+ }
+ catch (Exception e)
+ {
+ log.error("failed to destroy services", e);
+ }
+ }
}
1.9 +13 -13 jboss/src/main/org/jboss/system/URLClassLoader.java
Index: URLClassLoader.java
===================================================================
RCS file: /cvsroot/jboss/jboss/src/main/org/jboss/system/URLClassLoader.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- URLClassLoader.java 2001/11/24 20:54:34 1.8
+++ URLClassLoader.java 2001/12/08 04:35:42 1.9
@@ -20,7 +20,7 @@
*
* @author <a href="[EMAIL PROTECTED]">Marc Fleury</a>
* @author <a href="[EMAIL PROTECTED]">Christoph G. Jung</a>
- * @version $Revision: 1.8 $
+ * @version $Revision: 1.9 $
*
* <p><b>20010830 marc fleury:</b>
* <ul>
@@ -47,8 +47,9 @@
/** All SCL are just in orbit around a basic ServiceLibraries */
private static ServiceLibraries libraries;
-
-
+ /** The bootstrap interface to the log4j system */
+ private static BootstrapLogger log =
BootstrapLogger.getLogger(URLClassLoader.class);
+
/**
* One url per SCL
*
@@ -67,14 +68,13 @@
libraries = ServiceLibraries.getLibraries();
}
-
// A URL enabled SCL must register itself with the libraries to
// be queried
libraries.addClassLoader(this);
}
catch(Exception e)
- {
- System.out.println("[GPA] WARNING: URL "+keyUrl+" could not be opened");
+ {
+ log.warn("URL "+keyUrl+" could not be opened");
}
}
@@ -95,9 +95,9 @@
{
if (name.endsWith("CHANGEME"))
{
- System.out.println("UCL LOAD "+this.hashCode()+" in loadClass "+name);
+ log.debug("UCL LOAD "+this.hashCode()+" in loadClass "+name);
}
-
+
return libraries.loadClass(name, resolve, this);
}
@@ -107,7 +107,7 @@
{
if (name.endsWith("CHANGEME"))
{
- System.out.println("UCL LOAD LOCALLY "+ this.hashCode() +
+ log.debug("UCL LOAD LOCALLY "+ this.hashCode() +
" in loadClass "+name);
}
@@ -118,11 +118,10 @@
{
if (name.endsWith("CHANGEME"))
{
-
- System.out.println("UCL GETRESOURCE "+name+ " in UCL " +
+ log.debug("UCL GETRESOURCE "+name+ " in UCL " +
this.hashCode());
}
-
+
URL resource = super.getResource(name);
if (resource == null)
@@ -132,7 +131,8 @@
if (resource == null)
{
- System.out.println("Did not find the UCL resource "+name);
+ if( log.isTraceEnabled() )
+ log.trace("Did not find the UCL resource "+name);
}
return resource;
}
1.1 jboss/src/main/org/jboss/system/BootstrapLogger.java
Index: BootstrapLogger.java
===================================================================
package org.jboss.system;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
/** A utility class that allows the use of log4j by classes that must be loaded
from the system classpath. It uses reflection to obtain access to the log4j
classes using the thread context class loader.
* @author [EMAIL PROTECTED]
* @version $Revision: 1.1 $
*/
public class BootstrapLogger
{
/** The log4j Category class name */
private static final String CATEGORY_CLASS = "org.apache.log4j.Category";
/** The log4j Priority class name */
private static final String PRIORITY_CLASS = "org.apache.log4j.Priority";
/** The custom JBoss TRACE Priority class name */
private static final String TRACE_PRIORITY_CLASS =
"org.jboss.logging.TracePriority";
/** Indicies into the log4jMethods for the Category methods */
private static final int GET_INSTANCE = 0;
private static final int IS_ENABLED_FOR_PRIORITY = 1;
private static final int LOG_PRIORITY_MSG = 2;
private static final int LOG_PRIORITY_MSG_EX = 3;
private static final int N_METHODS = 4;
/** An array of the org.apache.log4j.Category methods used by BootstrapLogger */
private static Method[] log4jMethods = null;
/** An array of the org.apache.log4j.Category methods used by BootstrapLogger */
private static String[] priorityNames = {"TRACE", "DEBUG", "INFO", "WARN",
"ERROR", "FATAL"
};
/** An array of Priority objects corresponding to the names TRACE..FATAL */
private static Object[] log4jPriorities = new Object[priorityNames.length];
private static final int TRACE = 0;
private static final int DEBUG = 1;
private static final int INFO = 2;
private static final int WARN = 3;
private static final int ERROR = 4;
private static final int FATAL = 5;
/** Should execptions during the load of log4j be dumped to System.err */
private static boolean logInitFailures = false;
/** The maximum # of initLog4j calls to attempt */
private static int maxInitAttempts = 100;
private static int initAttempts;
// Externalize behavior using properties
static
{
try
{
logInitFailures =
Boolean.getBoolean("org.jboss.system.BootstrapLogger.logInitFailures");
Integer i =
Integer.getInteger("org.jboss.system.BootstrapLogger.maxInitAttempts",
maxInitAttempts);
maxInitAttempts = i.intValue();
}
catch(Exception e)
{
e.printStackTrace();
}
}
/** The log4j Category object the BootstrapLogger delegates to. */
private Object category;
private String categoryType;
public static BootstrapLogger getLogger(Class categoryType)
{
return getLogger(categoryType.getName());
}
public static BootstrapLogger getLogger(String categoryType)
{
try
{
initLog4j();
}
catch(Exception ignore)
{
}
return new BootstrapLogger(categoryType);
}
// --- Begin log4j Category methods we expose
public void trace(Object msg)
{
log(TRACE, msg);
}
public void trace(Object msg, Throwable ex)
{
log(TRACE, msg, ex);
}
public void debug(Object msg)
{
log(DEBUG, msg);
}
public void debug(Object msg, Throwable ex)
{
log(DEBUG, msg, ex);
}
public void info(Object msg)
{
log(INFO, msg);
}
public void info(Object msg, Throwable ex)
{
log(INFO, msg, ex);
}
public void warn(Object msg)
{
log(WARN, msg);
}
public void warn(Object msg, Throwable ex)
{
log(WARN, msg, ex);
}
public void error(Object msg)
{
log(ERROR, msg);
}
public void error(Object msg, Throwable ex)
{
log(ERROR, msg, ex);
}
public void fatal(Object msg)
{
log(FATAL, msg);
}
public void fatal(Object msg, Throwable ex)
{
log(FATAL, msg, ex);
}
public boolean isTraceEnabled()
{
return isEnabledFor(TRACE);
}
public boolean isDebugEnabled()
{
return isEnabledFor(DEBUG);
}
public boolean isInfoEnabled()
{
return isEnabledFor(INFO);
}
// --- Begin log4j Category methods we expose
/** This method is called by all isXXXEnabled methods to invoke
the Category.isEnabledForPriority method using the priority instance.
If the log4j methods have not been loaded then the priority
*/
private boolean isEnabledFor(int priorityIdx)
{
boolean isEnabled = false;
try
{
if( category == null )
{
// Try to load the log4j classes
init();
// Return false if we still don't have a category
if( category == null )
return false;
}
Object priority = log4jPriorities[priorityIdx];
Object[] args = {priority};
Boolean bool = (Boolean)
log4jMethods[IS_ENABLED_FOR_PRIORITY].invoke(category, args);
isEnabled = bool.booleanValue();
}
catch(Exception e)
{
e.printStackTrace();
}
return isEnabled;
}
/** The log method is called by all explicit priority log methods
that do not accept a Throwable.
@param priorityIdx, the index into the log4jPriorities array to use as
the priority of the msg
@param msg, the log message object
*/
private void log(int priorityIdx, Object msg)
{
// If there is no category log4j is not yet available
if( category == null )
{
// Try to load the log4j classes
init();
if( category == null )
{
// Failed, dump the msg to System.out
String name = priorityNames[priorityIdx];
System.out.println(name+", "+msg);
return;
}
}
// Invoke the Category.log method
try
{
Object priority = log4jPriorities[priorityIdx];
Object[] args = {priority, msg};
log4jMethods[LOG_PRIORITY_MSG].invoke(category, args);
}
catch(Exception e)
{
e.printStackTrace();
}
}
/** The log method is called by all explicit priority log methods
that do accept a Throwable.
@param priorityIdx, the index into the log4jPriorities array to use as
the priority of the msg
@param msg, the log message object
@param ex, the exception associated with the msg
*/
private void log(int priorityIdx, Object msg, Throwable ex)
{
if( category == null )
{
// Try to load the log4j classes
init();
if( category == null )
{
// Failed, dump the msg to System.out & print stack trace
String name = priorityNames[priorityIdx];
System.out.println(name+", "+msg);
ex.printStackTrace();
return;
}
}
try
{
Object priority = log4jPriorities[priorityIdx];
Object[] args = {priority, msg, ex};
log4jMethods[LOG_PRIORITY_MSG_EX].invoke(category, args);
}
catch(Exception e)
{
e.printStackTrace();
}
}
// End log4j methods
private BootstrapLogger(String categoryType)
{
this.categoryType = categoryType;
init();
}
/** This method is called to set the logger category and retry loading
the log4j classes if they have not been loaded as yet.
*/
private void init()
{
try
{
if( log4jMethods == null )
initLog4j();
if( log4jMethods != null )
{
Object[] args = {categoryType};
category = log4jMethods[GET_INSTANCE].invoke(null, args);
}
}
catch(Exception e)
{
if( logInitFailures == true )
e.printStackTrace();
}
}
/** Load the log4j classes using the thread context class loader and
build an array of the methods and priorities we use in this class
using reflection.
*/
private static synchronized void initLog4j() throws ClassNotFoundException,
NoSuchMethodException, IllegalAccessException, IllegalArgumentException,
InvocationTargetException
{
if( log4jMethods != null || initAttempts > 100 )
return;
initAttempts ++;
ClassLoader loader = Thread.currentThread().getContextClassLoader();
// Load the custom trace Priority class and log4j Priority
Class trace = loader.loadClass(TRACE_PRIORITY_CLASS);
Class priorityClass = loader.loadClass(PRIORITY_CLASS);
// Fill in the standard log4j Priority instances
Class[] toPriorityTypes = {String.class};
Method toPriority = priorityClass.getDeclaredMethod("toPriority",
toPriorityTypes);
for(int n = 1; n < priorityNames.length; n ++)
{
Object[] args = {priorityNames[n]};
log4jPriorities[n] = toPriority.invoke(null, args);
}
toPriority = trace.getDeclaredMethod("toPriority", toPriorityTypes);
toPriorityTypes = new Class[] {String.class, priorityClass};
log4jPriorities[0] = toPriority.invoke(null, new Object[]{"TRACE",
log4jPriorities[1]});
// Build an array of the log4j Category methods we use
Method[] tmp = new Method[N_METHODS];
Class categoryClass = loader.loadClass(CATEGORY_CLASS);
Class[] paramTypes = {String.class};
tmp[GET_INSTANCE] = categoryClass.getDeclaredMethod("getInstance", paramTypes);
paramTypes = new Class[] {priorityClass};
tmp[IS_ENABLED_FOR_PRIORITY] = categoryClass.getDeclaredMethod("isEnabledFor",
paramTypes);
paramTypes = new Class[] {priorityClass, Object.class};
tmp[LOG_PRIORITY_MSG] = categoryClass.getDeclaredMethod("log", paramTypes);
paramTypes = new Class[] {priorityClass, Object.class, Throwable.class};
tmp[LOG_PRIORITY_MSG_EX] = categoryClass.getDeclaredMethod("log", paramTypes);
// The log4jMethods is only assigned if all methods were found
log4jMethods = tmp;
}
}
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development