Skip to content

Commit

Permalink
cleanup: Coding style fixes to the Java agent
Browse files Browse the repository at this point in the history
- Use camelCase for regular members and FULL_CAPS for static final
  ones (except for API classes).
- All members of an interface are public static final by default,
  no need to repeat the modifiers.
- Marked final some fields that could be.

Signed-off-by: Alexandre Montplaisir <[email protected]>
  • Loading branch information
Alexandre Montplaisir committed Jun 29, 2015
1 parent c518eed commit 0828455
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 84 deletions.
10 changes: 6 additions & 4 deletions liblttng-ust-java-agent/java/org/lttng/ust/agent/LTTngAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.concurrent.TimeUnit;

public class LTTngAgent {

/* Domains */
static enum Domain {
JUL(3), LOG4J(4);
Expand All @@ -37,6 +38,8 @@ public int value() {
}
}

private static final int SEM_TIMEOUT = 3; /* Seconds */

private static LogFramework julUser;
private static LogFramework julRoot;
private static LogFramework log4jUser;
Expand All @@ -63,7 +66,6 @@ public int value() {
private static boolean initialized = false;

private static Semaphore registerSem;
private final static int semTimeout = 3; /* Seconds */

/*
* Constructor is private. This is a singleton and a reference should be
Expand Down Expand Up @@ -142,8 +144,8 @@ private static Class<?> loadClass(String className) throws ClassNotFoundExceptio
private void initAgentJULClasses() {
try {
Class<?> lttngJUL = loadClass("org.lttng.ust.agent.jul.LTTngJUL");
julUser = (LogFramework)lttngJUL.getDeclaredConstructor(new Class[] {Boolean.class}).newInstance(false);
julRoot = (LogFramework)lttngJUL.getDeclaredConstructor(new Class[] {Boolean.class}).newInstance(true);
julUser = (LogFramework) lttngJUL.getDeclaredConstructor(new Class[] { Boolean.class }).newInstance(false);
julRoot = (LogFramework) lttngJUL.getDeclaredConstructor(new Class[] { Boolean.class }).newInstance(true);
this.useJUL = true;
} catch (ClassNotFoundException e) {
/* LTTng JUL classes not found, no need to create the relevant objects */
Expand Down Expand Up @@ -212,7 +214,7 @@ private synchronized void init() throws SecurityException {
/* Wait for each registration to end. */
try {
registerSem.tryAcquire(numThreads,
semTimeout,
SEM_TIMEOUT,
TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,21 @@
import java.util.List;

interface LTTngSessiondCmd2_6 {

/**
* Maximum name length for a logger name to be send to sessiond.
*/
final static int NAME_MAX = 255;
int NAME_MAX = 255;

/*
* Size of a primitive type int in byte. Because you know, Java can't
* provide that since it does not makes sense...
*
*
*/
final static int INT_SIZE = 4;
int INT_SIZE = 4;

public interface SessiondResponse {
interface SessiondResponse {
/**
* Gets a byte array of the command so that it may be streamed
*
Expand All @@ -45,7 +48,7 @@ public interface SessiondResponse {
public byte[] getBytes();
}

public interface SessiondCommand {
interface SessiondCommand {
/**
* Populate the class from a byte array
*
Expand All @@ -55,7 +58,7 @@ public interface SessiondCommand {
public void populate(byte[] data);
}

public enum lttng_agent_command {
enum lttng_agent_command {
/** List logger(s). */
CMD_LIST(1),
/** Enable logger by name. */
Expand Down Expand Up @@ -91,28 +94,31 @@ public int getCode() {
}
}

public class sessiond_hdr implements SessiondCommand {
class sessiond_hdr implements SessiondCommand {

/** ABI size of command header. */
public final static int SIZE = 16;
/** Payload size in bytes following this header. */
public long data_size;
public long dataSize;
/** Command type. */
public lttng_agent_command cmd;
/** Command version. */
public int cmd_version;
public int cmdVersion;

@Override
public void populate(byte[] data) {
ByteBuffer buf = ByteBuffer.wrap(data);
buf.order(ByteOrder.BIG_ENDIAN);

data_size = buf.getLong();
dataSize = buf.getLong();
cmd = lttng_agent_command.values()[buf.getInt() - 1];
cmd_version = buf.getInt();
cmdVersion = buf.getInt();
}
}

public class sessiond_enable_handler implements SessiondResponse, SessiondCommand {
private final static int SIZE = 4;
class sessiond_enable_handler implements SessiondResponse, SessiondCommand {

private static final int SIZE = 4;
public String name;
public int lttngLogLevel;
public int lttngLogLevelType;
Expand All @@ -122,13 +128,13 @@ public class sessiond_enable_handler implements SessiondResponse, SessiondComman

@Override
public void populate(byte[] data) {
int data_offset = INT_SIZE * 2;
int dataOffset = INT_SIZE * 2;

ByteBuffer buf = ByteBuffer.wrap(data);
buf.order(ByteOrder.LITTLE_ENDIAN);
lttngLogLevel = buf.getInt();
lttngLogLevelType = buf.getInt();
name = new String(data, data_offset, data.length - data_offset).trim();
name = new String(data, dataOffset, data.length - dataOffset).trim();
}

@Override
Expand All @@ -153,7 +159,8 @@ public void execute(LogFramework log) {
}
}

public class sessiond_disable_handler implements SessiondResponse, SessiondCommand {
class sessiond_disable_handler implements SessiondResponse, SessiondCommand {

private final static int SIZE = 4;
public String name;

Expand Down Expand Up @@ -190,29 +197,30 @@ public void execute(LogFramework log) {
}
}

public class sessiond_list_logger implements SessiondResponse {
class sessiond_list_logger implements SessiondResponse {

private final static int SIZE = 12;

private int data_size = 0;
private int nb_logger = 0;
private int dataSize = 0;
private int nbLogger = 0;

List<String> logger_list = new ArrayList<String>();
List<String> loggerList = new ArrayList<String>();

/** Return status code to the session daemon. */
public lttng_agent_ret_code code;

@Override
public byte[] getBytes() {
byte data[] = new byte[SIZE + data_size];
byte data[] = new byte[SIZE + dataSize];
ByteBuffer buf = ByteBuffer.wrap(data);
buf.order(ByteOrder.BIG_ENDIAN);

/* Returned code */
buf.putInt(code.getCode());
buf.putInt(data_size);
buf.putInt(nb_logger);
buf.putInt(dataSize);
buf.putInt(nbLogger);

for (String logger: logger_list) {
for (String logger: loggerList) {
buf.put(logger.getBytes());
/* NULL terminated byte after the logger name. */
buf.put((byte) 0x0);
Expand All @@ -226,9 +234,9 @@ public void execute(LogFramework log) {
Iterator<String> loggers = log.listLoggers();
while (loggers.hasNext()) {
loggerName = loggers.next();
this.logger_list.add(loggerName);
this.nb_logger++;
this.data_size += loggerName.length() + 1;
this.loggerList.add(loggerName);
this.nbLogger++;
this.dataSize += loggerName.length() + 1;
}

this.code = lttng_agent_ret_code.CODE_SUCCESS_CMD;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@

class LTTngTCPSessiondClient implements Runnable {

private static final String SESSION_HOST = "127.0.0.1";
private static final String ROOT_PORT_FILE = "/var/run/lttng/agent.port";
private static final String USER_PORT_FILE = "/.lttng/agent.port";

private static Integer protocolMajorVersion = 1;
private static Integer protocolMinorVersion = 0;

/* Command header from the session deamon. */
private LTTngSessiondCmd2_6.sessiond_hdr headerCmd =
new LTTngSessiondCmd2_6.sessiond_hdr();
Expand All @@ -46,17 +53,11 @@ class LTTngTCPSessiondClient implements Runnable {

private Semaphore registerSem;

private static final String sessiondHost = "127.0.0.1";
private static final String rootPortFile = "/var/run/lttng/agent.port";
private static final String userPortFile = "/.lttng/agent.port";

private static Integer protocolMajorVersion = 1;
private static Integer protocolMinorVersion = 0;

private LTTngAgent.Domain agentDomain;

/* Indicate if we've already release the semaphore. */
private boolean sem_posted = false;
/* Indicate if we've already released the semaphore. */
private boolean semPosted = false;

public LTTngTCPSessiondClient(LTTngAgent.Domain domain, LogFramework log, Semaphore sem) {
this.agentDomain = domain;
Expand All @@ -67,12 +68,11 @@ public LTTngTCPSessiondClient(LTTngAgent.Domain domain, LogFramework log, Semaph
/*
* Try to release the registerSem if it's not already done.
*/
private void tryReleaseSem()
{
private void tryReleaseSem() {
/* Release semaphore so we unblock the agent. */
if (!this.sem_posted) {
if (!this.semPosted) {
this.registerSem.release();
this.sem_posted = true;
this.semPosted = true;
}
}

Expand Down Expand Up @@ -139,11 +139,10 @@ public void destroy() {
* static buffer of the right size.
*/
private void recvHeader() throws Exception {
int read_len;
byte data[] = new byte[LTTngSessiondCmd2_6.sessiond_hdr.SIZE];

read_len = this.inFromSessiond.read(data, 0, data.length);
if (read_len != data.length) {
int readLen = this.inFromSessiond.read(data, 0, data.length);
if (readLen != data.length) {
throw new IOException();
}
this.headerCmd.populate(data);
Expand All @@ -157,7 +156,7 @@ private void recvHeader() throws Exception {
* is expected after the header.
*/
private byte[] recvPayload() throws Exception {
byte payload[] = new byte[(int) this.headerCmd.data_size];
byte payload[] = new byte[(int) this.headerCmd.dataSize];

/* Failsafe check so we don't waste our time reading 0 bytes. */
if (payload.length == 0) {
Expand All @@ -178,7 +177,7 @@ private void handleSessiondCmd() throws Exception {
/* Get header from session daemon. */
recvHeader();

if (headerCmd.data_size > 0) {
if (headerCmd.dataSize > 0) {
data = recvPayload();
}

Expand Down Expand Up @@ -279,24 +278,22 @@ private void connectToSessiond() throws Exception {
int port;

if (this.log.isRoot()) {
port = getPortFromFile(rootPortFile);
port = getPortFromFile(ROOT_PORT_FILE);
if (port == 0) {
/* No session daemon available. Stop and retry later. */
throw new IOException();
}
} else {
port = getPortFromFile(getHomePath() + userPortFile);
port = getPortFromFile(getHomePath() + USER_PORT_FILE);
if (port == 0) {
/* No session daemon available. Stop and retry later. */
throw new IOException();
}
}

this.sessiondSock = new Socket(sessiondHost, port);
this.inFromSessiond = new DataInputStream(
sessiondSock.getInputStream());
this.outToSessiond = new DataOutputStream(
sessiondSock.getOutputStream());
this.sessiondSock = new Socket(SESSION_HOST, port);
this.inFromSessiond = new DataInputStream(sessiondSock.getInputStream());
this.outToSessiond = new DataOutputStream(sessiondSock.getOutputStream());
}

private void registerToSessiond() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public abstract class LogFrameworkSkeleton implements LogFramework {

/* A map of event name and reference count */
private Map<String, Integer> enabledLoggers;
private final Map<String, Integer> enabledLoggers;

public LogFrameworkSkeleton() {
this.enabledLoggers = new HashMap<String, Integer>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
import java.util.logging.LogRecord;

class LTTngLogHandler extends Handler {
private Boolean is_root;

private final Boolean isRoot;

public LTTngLogHandler(Boolean isRoot) {
super();
this.is_root = isRoot;
this.isRoot = isRoot;
/* Initialize LTTng UST tracer. */
try {
System.loadLibrary("lttng-ust-jul-jni"); //$NON-NLS-1$
Expand All @@ -42,7 +43,7 @@ public LTTngLogHandler(Boolean isRoot) {
}

public Boolean isRoot() {
return this.is_root;
return this.isRoot;
}

@Override
Expand All @@ -54,11 +55,11 @@ public void flush() {}
@Override
public void publish(LogRecord record) {
/*
* Specific tracepoing designed for JUL events. The source class of the
* Specific tracepoint designed for JUL events. The source class of the
* caller is used for the event name, the raw message is taken, the
* loglevel of the record and the thread ID.
*/
if (this.is_root) {
if (this.isRoot) {
tracepointS(record.getMessage(),
record.getLoggerName(), record.getSourceClassName(),
record.getSourceMethodName(), record.getMillis(),
Expand All @@ -72,10 +73,20 @@ public void publish(LogRecord record) {
}

/* Use for a user session daemon. */
private native void tracepointU(String msg, String logger_name, String class_name,
String method_name, long millis, int log_level, int thread_id);
private native void tracepointU(String msg,
String logger_name,
String class_name,
String method_name,
long millis,
int log_level,
int thread_id);

/* Use for a root session daemon. */
private native void tracepointS(String msg, String logger_name, String class_name,
String method_name, long millis, int log_level, int thread_id);
private native void tracepointS(String msg,
String logger_name,
String class_name,
String method_name,
long millis,
int log_level,
int thread_id);
}
Loading

0 comments on commit 0828455

Please sign in to comment.