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
.jul
;
20 import java
.io
.IOException
;
21 import java
.io
.FileNotFoundException
;
22 import java
.io
.InputStream
;
23 import java
.io
.BufferedReader
;
24 import java
.io
.FileReader
;
25 import java
.util
.concurrent
.Semaphore
;
26 import java
.util
.logging
.FileHandler
;
27 import java
.util
.logging
.Handler
;
28 import java
.util
.logging
.Level
;
29 import java
.util
.logging
.Logger
;
30 import java
.util
.logging
.LogManager
;
31 import java
.util
.Enumeration
;
33 public class LTTngAgent
{
34 private static LTTngLogHandler lttngHandler
;
35 private static LogManager logManager
;
36 private static LTTngThread lttngThread
;
37 private static Thread sessiondTh
;
39 /* Singleton agent object. */
40 private static LTTngAgent curAgent
= null;
42 /* Indicate if this object has been initialized. */
43 private static boolean initialized
= false;
45 private static Semaphore registerSem
;
47 private static final String sessiondAddr
= "127.0.0.1";
48 private static final int sessiondPort
= 5345;
50 private static final String rootPortFile
= "/var/run/lttng/jul.port";
51 private static final String userPortFile
= "/.lttng/jul.port";
54 * Constructor is private. This is a singleton and a reference should be
55 * acquired using getLTTngAgent().
57 private LTTngAgent() throws IOException
{
58 this.logManager
= LogManager
.getLogManager();
59 this.lttngHandler
= new LTTngLogHandler(this.logManager
);
60 this.registerSem
= new Semaphore(0, true);
63 private void removeHandlers() throws SecurityException
, IOException
{
67 Enumeration list
= this.logManager
.getLoggerNames();
68 while (list
.hasMoreElements()) {
69 loggerName
= list
.nextElement().toString();
70 /* Somehow there is always an empty string at the end. */
71 if (loggerName
== "") {
75 logger
= this.logManager
.getLogger(loggerName
);
76 logger
.removeHandler(this.lttngHandler
);
80 private int getUID() throws IOException
{
82 byte b
[] = new byte[4];
83 String userName
= System
.getProperty("user.name");
84 String command
= "id -u " + userName
;
85 Process child
= Runtime
.getRuntime().exec(command
);
86 InputStream in
= child
.getInputStream();
89 uid
= Integer
.parseInt(new String(b
).trim(), 10);
95 private String
getHomePath() {
96 return System
.getProperty("user.home");
99 private int getPortFromFile() throws IOException
{
105 /* Check if root or not, it tells where to get the port file. */
109 path
= new String(getHomePath() + userPortFile
);
113 br
= new BufferedReader(new FileReader(path
));
114 String line
= br
.readLine();
115 port
= Integer
.parseInt(line
, 10);
116 if (port
< 0 || port
> 65535) {
120 } catch (FileNotFoundException e
) {
128 * Public getter to acquire a reference to this singleton object.
130 public static synchronized LTTngAgent
getLTTngAgent() throws IOException
{
131 if (curAgent
== null) {
132 curAgent
= new LTTngAgent();
140 * Initialize LTTngAgent. This will attach the log handler to all Logger
141 * returned by the logManager.
143 private synchronized void init() throws SecurityException
, IOException
{
144 if (this.initialized
) {
148 this.lttngThread
= new LTTngThread(this.sessiondAddr
,
149 getPortFromFile(), this.lttngHandler
, this.registerSem
);
150 this.sessiondTh
= new Thread(lttngThread
);
151 this.sessiondTh
.start();
153 this.initialized
= true;
155 /* Wait for the registration to end. */
157 this.registerSem
.acquire();
158 } catch (InterruptedException e
) {
163 public void dispose() throws IOException
{
164 this.lttngThread
.dispose();
166 /* Make sure there is no more LTTng handler attach to logger(s). */
167 this.removeHandlers();
170 this.sessiondTh
.join();
171 } catch (InterruptedException e
) {