cleanup: Coding style fixes to the Java agent
[lttng-ust.git] / liblttng-ust-java-agent / java / org / lttng / ust / agent / LTTngAgent.java
index 342ccfae15c0c0bb45c29291c2f236bc2e60d9fc..ca7bc777fa24b1042acf3d41bbd27fcc4e73a547 100644 (file)
 
 package org.lttng.ust.agent;
 
-import org.lttng.ust.agent.jul.LTTngJUL;
-
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.BufferedReader;
-import java.io.FileReader;
+import java.lang.reflect.InvocationTargetException;
 import java.util.concurrent.Semaphore;
 import java.util.concurrent.TimeUnit;
-import java.util.Enumeration;
-import java.lang.reflect.InvocationTargetException;
-
-import java.util.logging.Logger;
-import java.util.logging.FileHandler;;
-import java.util.logging.SimpleFormatter;
 
 public class LTTngAgent {
+
        /* Domains */
        static enum Domain {
                JUL(3), LOG4J(4);
@@ -47,6 +38,8 @@ public class LTTngAgent {
                }
        }
 
+       private static final int SEM_TIMEOUT = 3; /* Seconds */
+
        private static LogFramework julUser;
        private static LogFramework julRoot;
        private static LogFramework log4jUser;
@@ -73,13 +66,12 @@ public class LTTngAgent {
        private static boolean initialized = false;
 
        private static Semaphore registerSem;
-       private final static int semTimeout = 3; /* Seconds */
 
        /*
         * Constructor is private. This is a singleton and a reference should be
         * acquired using getLTTngAgent().
         */
-       private LTTngAgent() throws IOException {
+       private LTTngAgent() {
                initAgentJULClasses();
 
                /* Since Log4j is a 3rd party JAR, we need to check if we can load any of its classes */
@@ -88,29 +80,72 @@ public class LTTngAgent {
                        initAgentLog4jClasses();
                }
 
-               this.registerSem = new Semaphore(0, true);
+               registerSem = new Semaphore(0, true);
        }
 
-       private Boolean loadLog4jClasses() {
-               Boolean loaded = false;
+       private static Boolean loadLog4jClasses() {
+               Class<?> logging;
+
                try {
-                       ClassLoader loader = ClassLoader.getSystemClassLoader();
-                       loader.loadClass("org.apache.log4j.Logger");
-                       loaded = true;
+                       logging = loadClass("org.apache.log4j.spi.LoggingEvent");
                } catch (ClassNotFoundException e) {
                        /* Log4j classes not found, no need to create the relevant objects */
-                       loaded = false;
+                       return false;
+               }
+
+               /*
+                * Detect capabilities of the log4j library. We only
+                * support log4j >= 1.2.15.  The getTimeStamp() method
+                * was introduced in log4j 1.2.15, so verify that it
+                * is available.
+                *
+                * We can't rely on the getPackage().getImplementationVersion()
+                * call that would retrieves information from the manifest file
+                * found in the JAR since the manifest file shipped
+                * from upstream is known to be broken in several
+                * versions of the library.
+                *
+                * More info:
+                * https://issues.apache.org/bugzilla/show_bug.cgi?id=44370
+                */
+
+               try {
+                       logging.getDeclaredMethod("getTimeStamp");
+               } catch (NoSuchMethodException e) {
+                       System.err.println("Warning: The loaded log4j library is too old. Log4j tracing with LTTng will be disabled.");
+                       return false;
+               } catch (NullPointerException e) {
+                       /* Should never happen */
+                       return false;
+               } catch (SecurityException e) {
+                       return false;
+               }
+
+               return true;
+       }
+
+       private static Class<?> loadClass(String className) throws ClassNotFoundException {
+               ClassLoader loader;
+               Class<?> loadedClass;
+
+               try {
+                       /* Try to load class using the current thread's context class loader */
+                       loader = Thread.currentThread().getContextClassLoader();
+                       loadedClass = loader.loadClass(className);
+               } catch (ClassNotFoundException e) {
+                       /* Loading failed, try using the system class loader */
+                       loader = ClassLoader.getSystemClassLoader();
+                       loadedClass = loader.loadClass(className);
                }
 
-               return loaded;
+               return loadedClass;
        }
 
        private void initAgentJULClasses() {
                try {
-                       ClassLoader loader = ClassLoader.getSystemClassLoader();
-                       Class<?> lttngJUL = loader.loadClass("org.lttng.ust.agent.jul.LTTngJUL");
-                       this.julUser = (LogFramework)lttngJUL.getDeclaredConstructor(new Class[] {Boolean.class}).newInstance(false);
-                       this.julRoot = (LogFramework)lttngJUL.getDeclaredConstructor(new Class[] {Boolean.class}).newInstance(true);
+                       Class<?> lttngJUL = loadClass("org.lttng.ust.agent.jul.LTTngJUL");
+                       julUser = (LogFramework) lttngJUL.getDeclaredConstructor(new Class[] { Boolean.class }).newInstance(false);
+                       julRoot = (LogFramework) lttngJUL.getDeclaredConstructor(new Class[] { Boolean.class }).newInstance(true);
                        this.useJUL = true;
                } catch (ClassNotFoundException e) {
                        /* LTTng JUL classes not found, no need to create the relevant objects */
@@ -128,10 +163,9 @@ public class LTTngAgent {
 
        private void initAgentLog4jClasses() {
                try {
-                       ClassLoader loader = ClassLoader.getSystemClassLoader();
-                       Class<?> lttngLog4j = loader.loadClass("org.lttng.ust.agent.log4j.LTTngLog4j");
-                       this.log4jUser = (LogFramework)lttngLog4j.getDeclaredConstructor(new Class[] {Boolean.class}).newInstance(false);
-                       this.log4jRoot = (LogFramework)lttngLog4j.getDeclaredConstructor(new Class[] {Boolean.class}).newInstance(true);
+                       Class<?> lttngLog4j = loadClass("org.lttng.ust.agent.log4j.LTTngLog4j");
+                       log4jUser = (LogFramework)lttngLog4j.getDeclaredConstructor(new Class[] {Boolean.class}).newInstance(false);
+                       log4jRoot = (LogFramework)lttngLog4j.getDeclaredConstructor(new Class[] {Boolean.class}).newInstance(true);
                        this.useLog4j = true;
                } catch (ClassNotFoundException e) {
                        /* LTTng Log4j classes not found, no need to create the relevant objects */
@@ -159,8 +193,8 @@ public class LTTngAgent {
                return curAgent;
        }
 
-       private synchronized void init() throws SecurityException, IOException {
-               if (this.initialized) {
+       private synchronized void init() throws SecurityException {
+               if (initialized) {
                        return;
                }
 
@@ -179,62 +213,62 @@ public class LTTngAgent {
 
                /* Wait for each registration to end. */
                try {
-                       this.registerSem.tryAcquire(numThreads,
-                                                   semTimeout,
+                       registerSem.tryAcquire(numThreads,
+                                                   SEM_TIMEOUT,
                                                    TimeUnit.SECONDS);
                } catch (InterruptedException e) {
                        e.printStackTrace();
                }
 
-               this.initialized = true;
+               initialized = true;
        }
 
-       private synchronized Integer initJULClientThreads() {
+       private synchronized static Integer initJULClientThreads() {
                Integer numThreads = 2;
 
                /* Handle user session daemon if any. */
-               this.julUserClient = new LTTngTCPSessiondClient(Domain.JUL,
-                                                               this.julUser,
-                                                               this.registerSem);
+               julUserClient = new LTTngTCPSessiondClient(Domain.JUL,
+                                                               julUser,
+                                                               registerSem);
 
                String userThreadName = "LTTng UST agent JUL user thread";
-               this.sessiondThreadJULUser = new Thread(julUserClient, userThreadName);
-               this.sessiondThreadJULUser.setDaemon(true);
-               this.sessiondThreadJULUser.start();
+               sessiondThreadJULUser = new Thread(julUserClient, userThreadName);
+               sessiondThreadJULUser.setDaemon(true);
+               sessiondThreadJULUser.start();
 
                /* Handle root session daemon. */
-               this.julRootClient = new LTTngTCPSessiondClient(Domain.JUL,
-                                                               this.julRoot,
-                                                               this.registerSem);
+               julRootClient = new LTTngTCPSessiondClient(Domain.JUL,
+                                                               julRoot,
+                                                               registerSem);
 
                String rootThreadName = "LTTng UST agent JUL root thread";
-               this.sessiondThreadJULRoot = new Thread(julRootClient, rootThreadName);
-               this.sessiondThreadJULRoot.setDaemon(true);
-               this.sessiondThreadJULRoot.start();
+               sessiondThreadJULRoot = new Thread(julRootClient, rootThreadName);
+               sessiondThreadJULRoot.setDaemon(true);
+               sessiondThreadJULRoot.start();
 
                return numThreads;
        }
 
-       private synchronized Integer initLog4jClientThreads() {
+       private synchronized static Integer initLog4jClientThreads() {
                Integer numThreads = 2;
 
-               this.log4jUserClient = new LTTngTCPSessiondClient(Domain.LOG4J,
-                                                                 this.log4jUser,
-                                                                 this.registerSem);
+               log4jUserClient = new LTTngTCPSessiondClient(Domain.LOG4J,
+                                                                 log4jUser,
+                                                                 registerSem);
 
                String userThreadName = "LTTng UST agent Log4j user thread";
-               this.sessiondThreadLog4jUser = new Thread(log4jUserClient, userThreadName);
-               this.sessiondThreadLog4jUser.setDaemon(true);
-               this.sessiondThreadLog4jUser.start();
+               sessiondThreadLog4jUser = new Thread(log4jUserClient, userThreadName);
+               sessiondThreadLog4jUser.setDaemon(true);
+               sessiondThreadLog4jUser.start();
 
-               this.log4jRootClient = new LTTngTCPSessiondClient(Domain.LOG4J,
-                                                                 this.log4jRoot,
-                                                                 this.registerSem);
+               log4jRootClient = new LTTngTCPSessiondClient(Domain.LOG4J,
+                                                                 log4jRoot,
+                                                                 registerSem);
 
                String rootThreadName = "LTTng UST agent Log4j root thread";
-               this.sessiondThreadLog4jRoot = new Thread(log4jRootClient,rootThreadName);
-               this.sessiondThreadLog4jRoot.setDaemon(true);
-               this.sessiondThreadLog4jRoot.start();
+               sessiondThreadLog4jRoot = new Thread(log4jRootClient,rootThreadName);
+               sessiondThreadLog4jRoot.setDaemon(true);
+               sessiondThreadLog4jRoot.start();
 
                return numThreads;
        }
@@ -242,28 +276,28 @@ public class LTTngAgent {
 
        public void dispose() throws IOException {
                if (this.useJUL) {
-                       this.julUserClient.destroy();
-                       this.julRootClient.destroy();
-                       this.julUser.reset();
-                       this.julRoot.reset();
+                       julUserClient.destroy();
+                       julRootClient.destroy();
+                       julUser.reset();
+                       julRoot.reset();
                }
 
                if (this.useLog4j) {
-                       this.log4jUserClient.destroy();
-                       this.log4jRootClient.destroy();
-                       this.log4jUser.reset();
-                       this.log4jRoot.reset();
+                       log4jUserClient.destroy();
+                       log4jRootClient.destroy();
+                       log4jUser.reset();
+                       log4jRoot.reset();
                }
 
                try {
                        if (this.useJUL) {
-                               this.sessiondThreadJULUser.join();
-                               this.sessiondThreadJULRoot.join();
+                               sessiondThreadJULUser.join();
+                               sessiondThreadJULRoot.join();
                        }
 
                        if (this.useLog4j) {
-                               this.sessiondThreadLog4jUser.join();
-                               this.sessiondThreadLog4jRoot.join();
+                               sessiondThreadLog4jUser.join();
+                               sessiondThreadLog4jRoot.join();
                        }
 
                } catch (InterruptedException e) {
This page took 0.030294 seconds and 4 git commands to generate.