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
.FileHandler
;;
33 import java
.util
.logging
.SimpleFormatter
;
35 public class LTTngAgent
{
41 private Domain(int value
) {
50 private static LogFramework julUser
;
51 private static LogFramework julRoot
;
52 private static LogFramework log4jUser
;
53 private static LogFramework log4jRoot
;
55 /* Sessiond clients */
56 private static LTTngTCPSessiondClient julUserClient
;
57 private static LTTngTCPSessiondClient julRootClient
;
58 private static LTTngTCPSessiondClient log4jUserClient
;
59 private static LTTngTCPSessiondClient log4jRootClient
;
61 private static Thread sessiondThreadJULUser
;
62 private static Thread sessiondThreadJULRoot
;
63 private static Thread sessiondThreadLog4jUser
;
64 private static Thread sessiondThreadLog4jRoot
;
66 private boolean useJUL
= false;
67 private boolean useLog4j
= false;
69 /* Singleton agent object */
70 private static LTTngAgent curAgent
= null;
72 /* Indicate if this object has been initialized. */
73 private static boolean initialized
= false;
75 private static Semaphore registerSem
;
76 private final static int semTimeout
= 3; /* Seconds */
79 * Constructor is private. This is a singleton and a reference should be
80 * acquired using getLTTngAgent().
82 private LTTngAgent() throws IOException
{
83 initAgentJULClasses();
85 /* Since Log4j is a 3rd party JAR, we need to check if we can load any of its classes */
86 Boolean log4jLoaded
= loadLog4jClasses();
88 initAgentLog4jClasses();
91 this.registerSem
= new Semaphore(0, true);
94 private Boolean
loadLog4jClasses() {
95 Boolean loaded
= false;
97 ClassLoader loader
= ClassLoader
.getSystemClassLoader();
98 loader
.loadClass("org.apache.log4j.Logger");
100 } catch (ClassNotFoundException e
) {
101 /* Log4j classes not found, no need to create the relevant objects */
108 private void initAgentJULClasses() {
110 ClassLoader loader
= ClassLoader
.getSystemClassLoader();
111 Class
<?
> lttngJUL
= loader
.loadClass("org.lttng.ust.agent.jul.LTTngJUL");
112 this.julUser
= (LogFramework
)lttngJUL
.getDeclaredConstructor(new Class
[] {Boolean
.class}).newInstance(false);
113 this.julRoot
= (LogFramework
)lttngJUL
.getDeclaredConstructor(new Class
[] {Boolean
.class}).newInstance(true);
115 } catch (ClassNotFoundException e
) {
116 /* LTTng JUL classes not found, no need to create the relevant objects */
118 } catch (InstantiationException e
) {
120 } catch (NoSuchMethodException e
) {
122 } catch (IllegalAccessException e
) {
124 } catch (InvocationTargetException e
) {
129 private void initAgentLog4jClasses() {
131 ClassLoader loader
= ClassLoader
.getSystemClassLoader();
132 Class
<?
> lttngLog4j
= loader
.loadClass("org.lttng.ust.agent.log4j.LTTngLog4j");
133 this.log4jUser
= (LogFramework
)lttngLog4j
.getDeclaredConstructor(new Class
[] {Boolean
.class}).newInstance(false);
134 this.log4jRoot
= (LogFramework
)lttngLog4j
.getDeclaredConstructor(new Class
[] {Boolean
.class}).newInstance(true);
135 this.useLog4j
= true;
136 } catch (ClassNotFoundException e
) {
137 /* LTTng Log4j classes not found, no need to create the relevant objects */
138 this.useLog4j
= false;
139 } catch (InstantiationException e
) {
140 this.useLog4j
= false;
141 } catch (NoSuchMethodException e
) {
142 this.useLog4j
= false;
143 } catch (IllegalAccessException e
) {
144 this.useLog4j
= false;
145 } catch (InvocationTargetException e
) {
146 this.useLog4j
= false;
151 * Public getter to acquire a reference to this singleton object.
153 public static synchronized LTTngAgent
getLTTngAgent() throws IOException
{
154 if (curAgent
== null) {
155 curAgent
= new LTTngAgent();
162 private synchronized void init() throws SecurityException
, IOException
{
163 if (this.initialized
) {
167 Integer numJULThreads
= 0;
168 Integer numLog4jThreads
= 0;
171 numJULThreads
= initJULClientThreads();
175 numLog4jThreads
= initLog4jClientThreads();
178 Integer numThreads
= numJULThreads
+ numLog4jThreads
;
180 /* Wait for each registration to end. */
182 this.registerSem
.tryAcquire(numThreads
,
185 } catch (InterruptedException e
) {
189 this.initialized
= true;
192 private synchronized Integer
initJULClientThreads() {
193 Integer numThreads
= 2;
195 /* Handle user session daemon if any. */
196 this.julUserClient
= new LTTngTCPSessiondClient(Domain
.JUL
,
200 String userThreadName
= "LTTng UST agent JUL user thread";
201 this.sessiondThreadJULUser
= new Thread(julUserClient
, userThreadName
);
202 this.sessiondThreadJULUser
.setDaemon(true);
203 this.sessiondThreadJULUser
.start();
205 /* Handle root session daemon. */
206 this.julRootClient
= new LTTngTCPSessiondClient(Domain
.JUL
,
210 String rootThreadName
= "LTTng UST agent JUL root thread";
211 this.sessiondThreadJULRoot
= new Thread(julRootClient
, rootThreadName
);
212 this.sessiondThreadJULRoot
.setDaemon(true);
213 this.sessiondThreadJULRoot
.start();
218 private synchronized Integer
initLog4jClientThreads() {
219 Integer numThreads
= 2;
221 this.log4jUserClient
= new LTTngTCPSessiondClient(Domain
.LOG4J
,
225 String userThreadName
= "LTTng UST agent Log4j user thread";
226 this.sessiondThreadLog4jUser
= new Thread(log4jUserClient
, userThreadName
);
227 this.sessiondThreadLog4jUser
.setDaemon(true);
228 this.sessiondThreadLog4jUser
.start();
230 this.log4jRootClient
= new LTTngTCPSessiondClient(Domain
.LOG4J
,
234 String rootThreadName
= "LTTng UST agent Log4j root thread";
235 this.sessiondThreadLog4jRoot
= new Thread(log4jRootClient
,rootThreadName
);
236 this.sessiondThreadLog4jRoot
.setDaemon(true);
237 this.sessiondThreadLog4jRoot
.start();
243 public void dispose() throws IOException
{
245 this.julUserClient
.destroy();
246 this.julRootClient
.destroy();
247 this.julUser
.reset();
248 this.julRoot
.reset();
252 this.log4jUserClient
.destroy();
253 this.log4jRootClient
.destroy();
254 this.log4jUser
.reset();
255 this.log4jRoot
.reset();
260 this.sessiondThreadJULUser
.join();
261 this.sessiondThreadJULRoot
.join();
265 this.sessiondThreadLog4jUser
.join();
266 this.sessiondThreadLog4jRoot
.join();
269 } catch (InterruptedException e
) {