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 org
.lttng
.ust
.agent
.jul
.LTTngJUL
;
22 import java
.io
.IOException
;
23 import java
.io
.InputStream
;
24 import java
.io
.BufferedReader
;
25 import java
.io
.FileReader
;
26 import java
.util
.concurrent
.Semaphore
;
27 import java
.util
.concurrent
.TimeUnit
;
28 import java
.util
.Enumeration
;
29 import java
.lang
.reflect
.InvocationTargetException
;
31 import java
.util
.logging
.Logger
;
32 import java
.util
.logging
.SimpleFormatter
;
34 public class LTTngAgent
{
40 private Domain(int value
) {
49 private static LogFramework julUser
;
50 private static LogFramework julRoot
;
51 private static LogFramework log4jUser
;
52 private static LogFramework log4jRoot
;
54 /* Sessiond clients */
55 private static LTTngTCPSessiondClient julUserClient
;
56 private static LTTngTCPSessiondClient julRootClient
;
57 private static LTTngTCPSessiondClient log4jUserClient
;
58 private static LTTngTCPSessiondClient log4jRootClient
;
60 private static Thread sessiondThreadJULUser
;
61 private static Thread sessiondThreadJULRoot
;
62 private static Thread sessiondThreadLog4jUser
;
63 private static Thread sessiondThreadLog4jRoot
;
65 private boolean useJUL
= false;
66 private boolean useLog4j
= false;
68 /* Singleton agent object */
69 private static LTTngAgent curAgent
= null;
71 /* Indicate if this object has been initialized. */
72 private static boolean initialized
= false;
74 private static Semaphore registerSem
;
75 private final static int semTimeout
= 3; /* Seconds */
78 * Constructor is private. This is a singleton and a reference should be
79 * acquired using getLTTngAgent().
81 private LTTngAgent() throws IOException
{
82 initAgentJULClasses();
84 /* Since Log4j is a 3rd party JAR, we need to check if we can load any of its classes */
85 Boolean log4jLoaded
= loadLog4jClasses();
87 initAgentLog4jClasses();
90 this.registerSem
= new Semaphore(0, true);
93 private Boolean
loadLog4jClasses() {
97 ClassLoader loader
= ClassLoader
.getSystemClassLoader();
98 logging
= loader
.loadClass("org.apache.log4j.spi.LoggingEvent");
99 } catch (ClassNotFoundException e
) {
100 /* Log4j classes not found, no need to create the relevant objects */
105 * Detect capabilities of the log4j library. We only
106 * support log4j >= 1.2.15. The getTimeStamp() method
107 * was introduced in log4j 1.2.15, so verify that it
110 * We can't rely on the getPackage().getImplementationVersion()
111 * call that would retrieves information from the manifest file
112 * found in the JAR since the manifest file shipped
113 * from upstream is known to be broken in several
114 * versions of the library.
117 * https://issues.apache.org/bugzilla/show_bug.cgi?id=44370
121 logging
.getDeclaredMethod("getTimeStamp");
122 } catch (NoSuchMethodException e
) {
124 } catch (NullPointerException e
) {
125 /* Should never happen */
127 } catch (SecurityException e
) {
134 private void initAgentJULClasses() {
136 ClassLoader loader
= ClassLoader
.getSystemClassLoader();
137 Class
<?
> lttngJUL
= loader
.loadClass("org.lttng.ust.agent.jul.LTTngJUL");
138 this.julUser
= (LogFramework
)lttngJUL
.getDeclaredConstructor(new Class
[] {Boolean
.class}).newInstance(false);
139 this.julRoot
= (LogFramework
)lttngJUL
.getDeclaredConstructor(new Class
[] {Boolean
.class}).newInstance(true);
141 } catch (ClassNotFoundException e
) {
142 /* LTTng JUL classes not found, no need to create the relevant objects */
144 } catch (InstantiationException e
) {
146 } catch (NoSuchMethodException e
) {
148 } catch (IllegalAccessException e
) {
150 } catch (InvocationTargetException e
) {
155 private void initAgentLog4jClasses() {
157 ClassLoader loader
= ClassLoader
.getSystemClassLoader();
158 Class
<?
> lttngLog4j
= loader
.loadClass("org.lttng.ust.agent.log4j.LTTngLog4j");
159 this.log4jUser
= (LogFramework
)lttngLog4j
.getDeclaredConstructor(new Class
[] {Boolean
.class}).newInstance(false);
160 this.log4jRoot
= (LogFramework
)lttngLog4j
.getDeclaredConstructor(new Class
[] {Boolean
.class}).newInstance(true);
161 this.useLog4j
= true;
162 } catch (ClassNotFoundException e
) {
163 /* LTTng Log4j classes not found, no need to create the relevant objects */
164 this.useLog4j
= false;
165 } catch (InstantiationException e
) {
166 this.useLog4j
= false;
167 } catch (NoSuchMethodException e
) {
168 this.useLog4j
= false;
169 } catch (IllegalAccessException e
) {
170 this.useLog4j
= false;
171 } catch (InvocationTargetException e
) {
172 this.useLog4j
= false;
177 * Public getter to acquire a reference to this singleton object.
179 public static synchronized LTTngAgent
getLTTngAgent() throws IOException
{
180 if (curAgent
== null) {
181 curAgent
= new LTTngAgent();
188 private synchronized void init() throws SecurityException
, IOException
{
189 if (this.initialized
) {
193 Integer numJULThreads
= 0;
194 Integer numLog4jThreads
= 0;
197 numJULThreads
= initJULClientThreads();
201 numLog4jThreads
= initLog4jClientThreads();
204 Integer numThreads
= numJULThreads
+ numLog4jThreads
;
206 /* Wait for each registration to end. */
208 this.registerSem
.tryAcquire(numThreads
,
211 } catch (InterruptedException e
) {
215 this.initialized
= true;
218 private synchronized Integer
initJULClientThreads() {
219 Integer numThreads
= 2;
221 /* Handle user session daemon if any. */
222 this.julUserClient
= new LTTngTCPSessiondClient(Domain
.JUL
,
226 String userThreadName
= "LTTng UST agent JUL user thread";
227 this.sessiondThreadJULUser
= new Thread(julUserClient
, userThreadName
);
228 this.sessiondThreadJULUser
.setDaemon(true);
229 this.sessiondThreadJULUser
.start();
231 /* Handle root session daemon. */
232 this.julRootClient
= new LTTngTCPSessiondClient(Domain
.JUL
,
236 String rootThreadName
= "LTTng UST agent JUL root thread";
237 this.sessiondThreadJULRoot
= new Thread(julRootClient
, rootThreadName
);
238 this.sessiondThreadJULRoot
.setDaemon(true);
239 this.sessiondThreadJULRoot
.start();
244 private synchronized Integer
initLog4jClientThreads() {
245 Integer numThreads
= 2;
247 this.log4jUserClient
= new LTTngTCPSessiondClient(Domain
.LOG4J
,
251 String userThreadName
= "LTTng UST agent Log4j user thread";
252 this.sessiondThreadLog4jUser
= new Thread(log4jUserClient
, userThreadName
);
253 this.sessiondThreadLog4jUser
.setDaemon(true);
254 this.sessiondThreadLog4jUser
.start();
256 this.log4jRootClient
= new LTTngTCPSessiondClient(Domain
.LOG4J
,
260 String rootThreadName
= "LTTng UST agent Log4j root thread";
261 this.sessiondThreadLog4jRoot
= new Thread(log4jRootClient
,rootThreadName
);
262 this.sessiondThreadLog4jRoot
.setDaemon(true);
263 this.sessiondThreadLog4jRoot
.start();
269 public void dispose() throws IOException
{
271 this.julUserClient
.destroy();
272 this.julRootClient
.destroy();
273 this.julUser
.reset();
274 this.julRoot
.reset();
278 this.log4jUserClient
.destroy();
279 this.log4jRootClient
.destroy();
280 this.log4jUser
.reset();
281 this.log4jRoot
.reset();
286 this.sessiondThreadJULUser
.join();
287 this.sessiondThreadJULRoot
.join();
291 this.sessiondThreadLog4jUser
.join();
292 this.sessiondThreadLog4jRoot
.join();
295 } catch (InterruptedException e
) {