My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* Copyright (c) 2006-2012 Rogério Liesenfeld
* This file is subject to the terms of the MIT license (see LICENSE.txt).
*/
package mockit.internal.startup;

import java.io.*;
import java.lang.management.*;
import java.util.*;

import com.sun.tools.attach.*;
import com.sun.tools.attach.spi.*;
import sun.tools.attach.*;

final class JDK6AgentLoader
{
private static final AttachProvider ATTACH_PROVIDER = new AttachProvider()
{
@Override
public String name() { return null; }

@Override
public String type() { return null; }

@Override
public VirtualMachine attachVirtualMachine(String id) { return null; }

@Override
public List<VirtualMachineDescriptor> listVirtualMachines() { return null; }
};

private final String jarFilePath;
private final String pid;

JDK6AgentLoader(String jarFilePath)
{
this.jarFilePath = jarFilePath;
pid = discoverProcessIdForRunningVM();
}

private String discoverProcessIdForRunningVM()
{
String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
int p = nameOfRunningVM.indexOf('@');

return nameOfRunningVM.substring(0, p);
}

boolean loadAgent()
{
VirtualMachine vm;

if (AttachProvider.providers().isEmpty()) {
vm = getVirtualMachineImplementationFromEmbeddedOnes();
}
else {
vm = attachToThisVM();
}

if (vm != null) {
loadAgentAndDetachFromThisVM(vm);
return true;
}

return false;
}

@SuppressWarnings("UseOfSunClasses")
private VirtualMachine getVirtualMachineImplementationFromEmbeddedOnes()
{
try {
if (File.separatorChar == '\\') {
return new WindowsVirtualMachine(ATTACH_PROVIDER, pid);
}
else if (System.getProperty("os.name").matches("Linux.*|LINUX.*")) {
return new LinuxVirtualMachine(ATTACH_PROVIDER, pid);
}
}
catch (AttachNotSupportedException e) {
throw new RuntimeException(e);
}
catch (IOException e) {
throw new RuntimeException(e);
}
catch (UnsatisfiedLinkError e) {
throw new IllegalStateException("Native library for Attach API not available in this JRE", e);
}

return null;
}

private VirtualMachine attachToThisVM()
{
try {
return VirtualMachine.attach(pid);
}
catch (AttachNotSupportedException e) {
throw new RuntimeException(e);
}
catch (IOException e) {
throw new RuntimeException(e);
}
}

private void loadAgentAndDetachFromThisVM(VirtualMachine vm)
{
try {
vm.loadAgent(jarFilePath, null);
vm.detach();
}
catch (AgentLoadException e) {
throw new RuntimeException(e);
}
catch (AgentInitializationException e) {
throw new RuntimeException(e);
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
}

Change log

r2022 by rliesenfeld on May 3, 2012   Diff
Avoided potential occurrences of
UnsatisfiedLinkError when running tests
without "-javaagent" on a JDK for Mac OS
X, or any other OS which is not Windows or
Linux (fixes  issue 224 ).
Go to: 

Older revisions

r1573 by rliesenfeld on Apr 30, 2011   Diff
Improved error message for missing
attach library.
r1504 by rliesenfeld on Feb 14, 2011   Diff
Changed file headers to use shorter
copyright/license notice.
r1153 by javamoc...@yahoo.com on Jun 14, 2010   Diff
Changes for  issue 71 .
All revisions of this file

File info

Size: 3144 bytes, 121 lines
Powered by Google Project Hosting