11package act .db .ebean ;
22
3+ import act .Act ;
4+ import act .app .event .AppEventId ;
5+ import act .sys .Env ;
36import com .sun .tools .attach .AttachNotSupportedException ;
47import com .sun .tools .attach .VirtualMachine ;
58import com .sun .tools .attach .VirtualMachineDescriptor ;
69import com .sun .tools .attach .spi .AttachProvider ;
710import org .avaje .agentloader .AgentLoader ;
11+ import org .osgl .logging .LogManager ;
12+ import org .osgl .logging .Logger ;
13+ import org .osgl .util .S ;
814import sun .tools .attach .BsdVirtualMachine ;
915import sun .tools .attach .LinuxVirtualMachine ;
1016import sun .tools .attach .SolarisVirtualMachine ;
1117import sun .tools .attach .WindowsVirtualMachine ;
1218
13- import java .io .File ;
14- import java .io .IOException ;
15- import java .lang .management .ManagementFactory ;
19+ import java .io .*;
1620import java .net .URISyntaxException ;
1721import java .net .URL ;
1822import java .net .URLClassLoader ;
1923import java .util .ArrayList ;
2024import java .util .List ;
21- import java .util .logging .Logger ;
2225
2326public class EbeanAgentLoader extends AgentLoader {
2427
2528
26- private static final Logger log = Logger . getLogger ( AgentLoader .class .getName ());
29+ private static final Logger LOGGER = LogManager . get ( EbeanAgentLoader .class .getName ());
2730
2831 private static final List <String > loaded = new ArrayList <String >();
2932
30- private static final String discoverPid () {
31- String nameOfRunningVM = ManagementFactory .getRuntimeMXBean ().getName ();
32- int p = nameOfRunningVM .indexOf ('@' );
33- return nameOfRunningVM .substring (0 , p );
34- }
35-
3633 private static final AttachProvider ATTACH_PROVIDER = new AttachProvider () {
3734 @ Override
3835 public String name () {
@@ -66,11 +63,9 @@ public static void loadAgent(String jarFilePath) {
6663 * Load an agent providing the full file path with parameters.
6764 */
6865 public static void loadAgent (String jarFilePath , String params ) {
69-
70- log .info ("dynamically loading javaagent for " + jarFilePath );
7166 try {
7267
73- String pid = discoverPid ();
68+ String pid = Env . PID . get ();
7469
7570 VirtualMachine vm ;
7671 if (AttachProvider .providers ().isEmpty ()) {
@@ -79,7 +74,22 @@ public static void loadAgent(String jarFilePath, String params) {
7974 vm = VirtualMachine .attach (pid );
8075 }
8176
82- vm .loadAgent (jarFilePath , params );
77+ final PrintStream ps = System .out ;
78+ try {
79+ System .setOut (new PrintStream (new FileOutputStream (".ebean_agent.log" )));
80+ vm .loadAgent (jarFilePath , params );
81+ if (LOGGER .isTraceEnabled ()) {
82+ LOGGER .trace ("javaagent loaded: " + jarFilePath );
83+ }
84+ } finally {
85+ // ensure ebean2 EnhanceContext logout set to dump output
86+ Act .jobManager ().on (AppEventId .CLASS_LOADER_INITIALIZED , new Runnable () {
87+ @ Override
88+ public void run () {
89+ System .setOut (ps );
90+ }
91+ });
92+ }
8393 vm .detach ();
8494
8595 } catch (Exception e ) {
@@ -98,8 +108,10 @@ public static void loadAgentFromClasspath(String agentName) {
98108 * Load the agent from the classpath using its name and passing params.
99109 */
100110 public synchronized static boolean loadAgentFromClasspath (String agentName , String params ) {
101-
102111 if (loaded .contains (agentName )) {
112+ if (LOGGER .isDebugEnabled ()) {
113+ LOGGER .debug (S .concat ("agent already loaded: " , agentName ));
114+ }
103115 // the agent is already loaded
104116 return true ;
105117 }
@@ -118,6 +130,9 @@ public synchronized static boolean loadAgentFromClasspath(String agentName, Stri
118130 if (fullName .startsWith ("/" ) && isWindows ()) {
119131 fullName = fullName .substring (1 );
120132 }
133+ if (LOGGER .isTraceEnabled ()) {
134+ LOGGER .trace (S .concat ("loading agent: " , fullName ));
135+ }
121136 loadAgent (fullName , params );
122137 loaded .add (agentName );
123138 return true ;
@@ -126,6 +141,9 @@ public synchronized static boolean loadAgentFromClasspath(String agentName, Stri
126141 }
127142
128143 // Agent not found and not loaded
144+ if (LOGGER .isTraceEnabled ()) {
145+ LOGGER .trace ("agent not found" );
146+ }
129147 return false ;
130148
131149 } catch (URISyntaxException use ) {
0 commit comments