Fix: lttng java agent: dispose is non-static
[lttng-ust.git] / liblttng-ust-java-agent / java / lttng-ust-agent-common / org / lttng / ust / agent / LTTngAgent.java
1 /*
2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
3 *
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.
7 *
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
11 * for more details.
12 *
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
16 */
17
18 package org.lttng.ust.agent;
19
20 import java.lang.reflect.Constructor;
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
23 import java.util.logging.Handler;
24 import java.util.logging.Logger;
25
26 /**
27 * The central agent managing the JUL and Log4j handlers.
28 *
29 * @author David Goulet
30 * @deprecated Applications are now expected to manage their Logger and Handler
31 * objects.
32 */
33 @Deprecated
34 public class LTTngAgent {
35
36 private static LTTngAgent instance = null;
37
38 /**
39 * Public getter to acquire a reference to this singleton object.
40 *
41 * @return The agent instance
42 */
43 public static synchronized LTTngAgent getLTTngAgent() {
44 if (instance == null) {
45 instance = new LTTngAgent();
46 }
47 return instance;
48 }
49
50 /**
51 * Dispose the agent. Applications should call this once they are done
52 * logging. This dispose function is non-static for backwards
53 * compatibility purposes.
54 */
55 public synchronized void dispose() {
56 if (instance != null) {
57 instance.disposeInstance();
58 instance = null;
59 }
60 return;
61 }
62
63 private ILttngHandler julHandler = null;
64 private ILttngHandler log4jAppender = null;
65
66 /**
67 * Private constructor. This is a singleton and a reference should be
68 * acquired using {@link #getLTTngAgent()}.
69 */
70 private LTTngAgent() {
71 initJulHandler();
72 initLog4jAppender();
73 }
74
75 /**
76 * "Destructor" method.
77 */
78 private void disposeInstance() {
79 disposeJulHandler();
80 disposeLog4jAppender();
81 }
82
83 /**
84 * Create a LTTng-JUL handler, and attach it to the JUL root logger.
85 */
86 private void initJulHandler() {
87 try {
88 Class<?> julHandlerClass = Class.forName("org.lttng.ust.agent.jul.LttngLogHandler");
89 /*
90 * It is safer to use Constructor.newInstance() rather than
91 * Class.newInstance(), because it will catch the exceptions thrown
92 * by the constructor below (which happens if the Java library is
93 * present, but the matching JNI one is not).
94 */
95 Constructor<?> julHandlerCtor = julHandlerClass.getConstructor();
96 julHandler = (ILttngHandler) julHandlerCtor.newInstance();
97
98 /* Attach the handler to the root JUL logger */
99 Logger.getLogger("").addHandler((Handler) julHandler);
100
101 /*
102 * If any of the following exceptions happen, it means we could not
103 * find or initialize LTTng JUL classes. We will not setup LTTng JUL
104 * tracing in this case.
105 */
106 } catch (SecurityException e) {
107 } catch (IllegalAccessException e) {
108 } catch (IllegalArgumentException e) {
109 } catch (ClassNotFoundException e) {
110 } catch (NoSuchMethodException e) {
111 } catch (InstantiationException e) {
112 } catch (InvocationTargetException e) {
113 }
114 }
115
116 /**
117 * Create a LTTng-logj4 appender, and attach it to the log4j root logger.
118 */
119 private void initLog4jAppender() {
120 /*
121 * Since Log4j is a 3rd party library, we first need to check if we can
122 * load any of its classes.
123 */
124 if (!testLog4jClasses()) {
125 return;
126 }
127
128 try {
129 Class<?> log4jAppenderClass = Class.forName("org.lttng.ust.agent.log4j.LttngLogAppender");
130 Constructor<?> log4jAppendCtor = log4jAppenderClass.getConstructor();
131 log4jAppender = (ILttngHandler) log4jAppendCtor.newInstance();
132
133 /*
134 * If any of the following exceptions happen, it means we could not
135 * find or initialize LTTng log4j classes. We will not setup LTTng
136 * log4j tracing in this case.
137 */
138 } catch (SecurityException e) {
139 return;
140 } catch (ClassNotFoundException e) {
141 return;
142 } catch (NoSuchMethodException e) {
143 return;
144 } catch (IllegalArgumentException e) {
145 return;
146 } catch (InstantiationException e) {
147 return;
148 } catch (IllegalAccessException e) {
149 return;
150 } catch (InvocationTargetException e) {
151 return;
152 }
153
154 /*
155 * Attach the appender to the root Log4j logger. Slightly more tricky
156 * here, as log4j.Logger is not in the base Java library, and we do not
157 * want the "common" package to depend on log4j. So we have to obtain it
158 * through reflection too.
159 */
160 try {
161 Class<?> loggerClass = Class.forName("org.apache.log4j.Logger");
162 Class<?> appenderClass = Class.forName("org.apache.log4j.Appender");
163
164 Method getRootLoggerMethod = loggerClass.getMethod("getRootLogger", (Class<?>[]) null);
165 Method addAppenderMethod = loggerClass.getMethod("addAppender", appenderClass);
166
167 Object rootLogger = getRootLoggerMethod.invoke(null, (Object[]) null);
168 addAppenderMethod.invoke(rootLogger, log4jAppender);
169
170 /*
171 * We have checked for the log4j library version previously, none of
172 * the following exceptions should happen.
173 */
174 } catch (SecurityException e) {
175 throw new IllegalStateException(e);
176 } catch (ClassNotFoundException e) {
177 throw new IllegalStateException(e);
178 } catch (NoSuchMethodException e) {
179 throw new IllegalStateException(e);
180 } catch (IllegalArgumentException e) {
181 throw new IllegalStateException(e);
182 } catch (IllegalAccessException e) {
183 throw new IllegalStateException(e);
184 } catch (InvocationTargetException e) {
185 throw new IllegalStateException(e);
186 }
187 }
188
189 /**
190 * Check if log4j >= 1.2.15 library is present.
191 */
192 private static boolean testLog4jClasses() {
193 Class<?> loggingEventClass;
194
195 try {
196 loggingEventClass = Class.forName("org.apache.log4j.spi.LoggingEvent");
197 } catch (ClassNotFoundException e) {
198 /*
199 * Log4j classes not found, no need to create the relevant objects
200 */
201 return false;
202 }
203
204 /*
205 * Detect capabilities of the log4j library. We only support log4j >=
206 * 1.2.15. The getTimeStamp() method was introduced in log4j 1.2.15, so
207 * verify that it is available.
208 *
209 * We can't rely on the getPackage().getImplementationVersion() call
210 * that would retrieves information from the manifest file found in the
211 * JAR since the manifest file shipped from upstream is known to be
212 * broken in several versions of the library.
213 *
214 * More info: https://issues.apache.org/bugzilla/show_bug.cgi?id=44370
215 */
216 try {
217 loggingEventClass.getDeclaredMethod("getTimeStamp");
218 } catch (NoSuchMethodException e) {
219 System.err.println(
220 "Warning: The loaded log4j library is too old. Log4j tracing with LTTng will be disabled.");
221 return false;
222 } catch (SecurityException e) {
223 return false;
224 }
225
226 return true;
227 }
228
229 /**
230 * Detach the JUL handler from its logger and close it.
231 */
232 private void disposeJulHandler() {
233 if (julHandler == null) {
234 /* The JUL handler was not activated, we have nothing to do */
235 return;
236 }
237 Logger.getLogger("").removeHandler((Handler) julHandler);
238 julHandler.close();
239 julHandler = null;
240 }
241
242 /**
243 * Detach the log4j appender from its logger and close it.
244 */
245 private void disposeLog4jAppender() {
246 if (log4jAppender == null) {
247 /* The log4j appender was not active, we have nothing to do */
248 return;
249 }
250
251 /*
252 * Detach the appender from the log4j root logger. Again, we have to do
253 * this via reflection.
254 */
255 try {
256 Class<?> loggerClass = Class.forName("org.apache.log4j.Logger");
257 Class<?> appenderClass = Class.forName("org.apache.log4j.Appender");
258
259 Method getRootLoggerMethod = loggerClass.getMethod("getRootLogger", (Class<?>[]) null);
260 Method removeAppenderMethod = loggerClass.getMethod("removeAppender", appenderClass);
261
262 Object rootLogger = getRootLoggerMethod.invoke(null, (Object[]) null);
263 removeAppenderMethod.invoke(rootLogger, log4jAppender);
264
265 /*
266 * We were able to attach the appender previously, we should not
267 * have problems here either!
268 */
269 } catch (SecurityException e) {
270 throw new IllegalStateException(e);
271 } catch (ClassNotFoundException e) {
272 throw new IllegalStateException(e);
273 } catch (NoSuchMethodException e) {
274 throw new IllegalStateException(e);
275 } catch (IllegalArgumentException e) {
276 throw new IllegalStateException(e);
277 } catch (IllegalAccessException e) {
278 throw new IllegalStateException(e);
279 } catch (InvocationTargetException e) {
280 throw new IllegalStateException(e);
281 }
282
283 /* Close the appender */
284 log4jAppender.close();
285 log4jAppender = null;
286 }
287
288 }
This page took 0.036544 seconds and 5 git commands to generate.