Skip to content

Commit

Permalink
add possibility to define ExternalListener
Browse files Browse the repository at this point in the history
  • Loading branch information
mlinhard authored and kohsuke committed Nov 2, 2013
1 parent e0bede6 commit 18d0a05
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/main/java/org/kohsuke/file_leak_detector/AgentMain.java
Expand Up @@ -70,6 +70,9 @@ public static void premain(String agentArguments, Instrumentation instrumentatio
} else
if(t.startsWith("error=")) {
Listener.ERROR = new PrintWriter(new FileOutputStream(t.substring(6)));
} else
if(t.startsWith("listener=")) {
Listener.EXTERNAL = (ExternalListener) AgentMain.class.getClassLoader().loadClass(t.substring(9)).newInstance();
} else {
System.err.println("Unknown option: "+t);
usageAndQuit();
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/org/kohsuke/file_leak_detector/ExternalListener.java
@@ -0,0 +1,28 @@
package org.kohsuke.file_leak_detector;

import java.io.File;

/**
*
* Allows external listeners for the open file and open socket events.
*
* @author Michal Linhard (michal@linhard.sk)
*/
public interface ExternalListener {
/**
*
* Called when a new file is opened.
*
* @param obj
* @param file
*/
void open(Object obj, File file);

/**
*
* Called when a new socket is opened.
*
* @param obj
*/
void openSocket(Object obj);
}
25 changes: 21 additions & 4 deletions src/main/java/org/kohsuke/file_leak_detector/Listener.java
Expand Up @@ -69,7 +69,7 @@ private FileRecord(File file) {
}

public void dump(String prefix, PrintWriter pw) {
pw.println(prefix + file + " by thread:" + threadName + " on " + new Date(time));
pw.println(prefix + file + " by thread:" + threadName + " on " + format(time));
super.dump(prefix,pw);
}
}
Expand All @@ -96,7 +96,7 @@ public void dump(String prefix, PrintWriter ps) {
String peer = this.peer;
if (peer==null) peer=getRemoteAddress(socket);

ps.println(prefix+"socket to "+peer+" by thread:"+threadName+" on "+new Date(time));
ps.println(prefix+"socket to "+peer+" by thread:"+threadName+" on "+format(time));
super.dump(prefix,ps);
}
}
Expand All @@ -123,7 +123,7 @@ public void dump(String prefix, PrintWriter ps) {
String address = this.address;
if (address==null) address=getLocalAddress(socket);

ps.println(prefix+"server socket at "+address+" by thread:"+threadName+" on "+new Date(time));
ps.println(prefix+"server socket at "+address+" by thread:"+threadName+" on "+format(time));
super.dump(prefix,ps);
}
}
Expand All @@ -139,7 +139,7 @@ private SocketChannelRecord(SocketChannel socket) {
}

public void dump(String prefix, PrintWriter ps) {
ps.println(prefix+"socket channel by thread:"+threadName+" on "+new Date(time));
ps.println(prefix+"socket channel by thread:"+threadName+" on "+format(time));
super.dump(prefix,ps);
}
}
Expand Down Expand Up @@ -175,6 +175,11 @@ public void dump(String prefix, PrintWriter ps) {
*/
/*package*/ static boolean AGENT_INSTALLED = false;

/**
* Implementation of {@link ExternalListener}.
*/
public static ExternalListener EXTERNAL = null;

/**
* Returns true if the leak detector agent is running.
*/
Expand All @@ -195,13 +200,17 @@ public static synchronized void makeStrong() {
* File being opened.
*/
public static synchronized void open(Object _this, File f) {
if (EXTERNAL != null)
EXTERNAL.open(_this, f);
put(_this, new FileRecord(f));
}

/**
* Called when a socket is opened.
*/
public static synchronized void openSocket(Object _this) {
if (EXTERNAL != null)
EXTERNAL.openSocket(_this);
// intercept when
if (_this instanceof SocketImpl) {
try {
Expand Down Expand Up @@ -286,6 +295,14 @@ public static synchronized void outOfDescriptors() {
}
}

private static String format(long time) {
try {
return new Date(time).toString();
} catch (Exception e) {
return Long.toString(time);
}
}

private static Field SOCKETIMPL_SOCKET,SOCKETIMPL_SERVER_SOCKET;

static {
Expand Down

0 comments on commit 18d0a05

Please sign in to comment.