2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 package org
.lttng
.ust
.agent
;
20 import java
.io
.IOException
;
21 import java
.lang
.reflect
.InvocationTargetException
;
22 import java
.util
.concurrent
.Semaphore
;
23 import java
.util
.concurrent
.TimeUnit
;
25 public class LTTngAgent
{
31 private Domain(int value
) {
40 private static LogFramework julUser
;
41 private static LogFramework julRoot
;
42 private static LogFramework log4jUser
;
43 private static LogFramework log4jRoot
;
45 /* Sessiond clients */
46 private static LTTngTCPSessiondClient julUserClient
;
47 private static LTTngTCPSessiondClient julRootClient
;
48 private static LTTngTCPSessiondClient log4jUserClient
;
49 private static LTTngTCPSessiondClient log4jRootClient
;
51 private static Thread sessiondThreadJULUser
;
52 private static Thread sessiondThreadJULRoot
;
53 private static Thread sessiondThreadLog4jUser
;
54 private static Thread sessiondThreadLog4jRoot
;
56 private boolean useJUL
= false;
57 private boolean useLog4j
= false;
59 /* Singleton agent object */
60 private static LTTngAgent curAgent
= null;
62 /* Indicate if this object has been initialized. */
63 private static boolean initialized
= false;
65 private static Semaphore registerSem
;
66 private final static int semTimeout
= 3; /* Seconds */
69 * Constructor is private. This is a singleton and a reference should be
70 * acquired using getLTTngAgent().
72 private LTTngAgent() {
73 initAgentJULClasses();
75 /* Since Log4j is a 3rd party JAR, we need to check if we can load any of its classes */
76 Boolean log4jLoaded
= loadLog4jClasses();
78 initAgentLog4jClasses();
81 registerSem
= new Semaphore(0, true);
84 private static Boolean
loadLog4jClasses() {
88 logging
= loadClass("org.apache.log4j.spi.LoggingEvent");
89 } catch (ClassNotFoundException e
) {
90 /* Log4j classes not found, no need to create the relevant objects */
95 * Detect capabilities of the log4j library. We only
96 * support log4j >= 1.2.15. The getTimeStamp() method
97 * was introduced in log4j 1.2.15, so verify that it
100 * We can't rely on the getPackage().getImplementationVersion()
101 * call that would retrieves information from the manifest file
102 * found in the JAR since the manifest file shipped
103 * from upstream is known to be broken in several
104 * versions of the library.
107 * https://issues.apache.org/bugzilla/show_bug.cgi?id=44370
111 logging
.getDeclaredMethod("getTimeStamp");
112 } catch (NoSuchMethodException e
) {
113 System
.err
.println("Warning: The loaded log4j library is too old. Log4j tracing with LTTng will be disabled.");
115 } catch (NullPointerException e
) {
116 /* Should never happen */
118 } catch (SecurityException e
) {
125 private static Class
<?
> loadClass(String className
) throws ClassNotFoundException
{
127 Class
<?
> loadedClass
;
130 /* Try to load class using the current thread's context class loader */
131 loader
= Thread
.currentThread().getContextClassLoader();
132 loadedClass
= loader
.loadClass(className
);
133 } catch (ClassNotFoundException e
) {
134 /* Loading failed, try using the system class loader */
135 loader
= ClassLoader
.getSystemClassLoader();
136 loadedClass
= loader
.loadClass(className
);
142 private void initAgentJULClasses() {
144 Class
<?
> lttngJUL
= loadClass("org.lttng.ust.agent.jul.LTTngJUL");
145 julUser
= (LogFramework
)lttngJUL
.getDeclaredConstructor(new Class
[] {Boolean
.class}).newInstance(false);
146 julRoot
= (LogFramework
)lttngJUL
.getDeclaredConstructor(new Class
[] {Boolean
.class}).newInstance(true);
148 } catch (ClassNotFoundException e
) {
149 /* LTTng JUL classes not found, no need to create the relevant objects */
151 } catch (InstantiationException e
) {
153 } catch (NoSuchMethodException e
) {
155 } catch (IllegalAccessException e
) {
157 } catch (InvocationTargetException e
) {
162 private void initAgentLog4jClasses() {
164 Class
<?
> lttngLog4j
= loadClass("org.lttng.ust.agent.log4j.LTTngLog4j");
165 log4jUser
= (LogFramework
)lttngLog4j
.getDeclaredConstructor(new Class
[] {Boolean
.class}).newInstance(false);
166 log4jRoot
= (LogFramework
)lttngLog4j
.getDeclaredConstructor(new Class
[] {Boolean
.class}).newInstance(true);
167 this.useLog4j
= true;
168 } catch (ClassNotFoundException e
) {
169 /* LTTng Log4j classes not found, no need to create the relevant objects */
170 this.useLog4j
= false;
171 } catch (InstantiationException e
) {
172 this.useLog4j
= false;
173 } catch (NoSuchMethodException e
) {
174 this.useLog4j
= false;
175 } catch (IllegalAccessException e
) {
176 this.useLog4j
= false;
177 } catch (InvocationTargetException e
) {
178 this.useLog4j
= false;
183 * Public getter to acquire a reference to this singleton object.
185 public static synchronized LTTngAgent
getLTTngAgent() throws IOException
{
186 if (curAgent
== null) {
187 curAgent
= new LTTngAgent();
194 private synchronized void init() throws SecurityException
{
199 Integer numJULThreads
= 0;
200 Integer numLog4jThreads
= 0;
203 numJULThreads
= initJULClientThreads();
207 numLog4jThreads
= initLog4jClientThreads();
210 Integer numThreads
= numJULThreads
+ numLog4jThreads
;
212 /* Wait for each registration to end. */
214 registerSem
.tryAcquire(numThreads
,
217 } catch (InterruptedException e
) {
224 private synchronized static Integer
initJULClientThreads() {
225 Integer numThreads
= 2;
227 /* Handle user session daemon if any. */
228 julUserClient
= new LTTngTCPSessiondClient(Domain
.JUL
,
232 String userThreadName
= "LTTng UST agent JUL user thread";
233 sessiondThreadJULUser
= new Thread(julUserClient
, userThreadName
);
234 sessiondThreadJULUser
.setDaemon(true);
235 sessiondThreadJULUser
.start();
237 /* Handle root session daemon. */
238 julRootClient
= new LTTngTCPSessiondClient(Domain
.JUL
,
242 String rootThreadName
= "LTTng UST agent JUL root thread";
243 sessiondThreadJULRoot
= new Thread(julRootClient
, rootThreadName
);
244 sessiondThreadJULRoot
.setDaemon(true);
245 sessiondThreadJULRoot
.start();
250 private synchronized static Integer
initLog4jClientThreads() {
251 Integer numThreads
= 2;
253 log4jUserClient
= new LTTngTCPSessiondClient(Domain
.LOG4J
,
257 String userThreadName
= "LTTng UST agent Log4j user thread";
258 sessiondThreadLog4jUser
= new Thread(log4jUserClient
, userThreadName
);
259 sessiondThreadLog4jUser
.setDaemon(true);
260 sessiondThreadLog4jUser
.start();
262 log4jRootClient
= new LTTngTCPSessiondClient(Domain
.LOG4J
,
266 String rootThreadName
= "LTTng UST agent Log4j root thread";
267 sessiondThreadLog4jRoot
= new Thread(log4jRootClient
,rootThreadName
);
268 sessiondThreadLog4jRoot
.setDaemon(true);
269 sessiondThreadLog4jRoot
.start();
275 public void dispose() throws IOException
{
277 julUserClient
.destroy();
278 julRootClient
.destroy();
284 log4jUserClient
.destroy();
285 log4jRootClient
.destroy();
292 sessiondThreadJULUser
.join();
293 sessiondThreadJULRoot
.join();
297 sessiondThreadLog4jUser
.join();
298 sessiondThreadLog4jRoot
.join();
301 } catch (InterruptedException e
) {