Skip to content

Commit c98faba

Browse files
authored
Merge pull request #3 from intergral/config_auth
Config auth
2 parents c2e49e8 + aa28c52 commit c98faba

File tree

5 files changed

+142
-24
lines changed

5 files changed

+142
-24
lines changed

agent-api/src/main/java/com/intergral/deep/agent/api/auth/BasicAuthProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ public Map<String, String> provide() {
3939
}
4040
final String encodedString = Base64.getEncoder()
4141
.encodeToString(String.format("%s:%s", userName, password).getBytes());
42-
return Collections.singletonMap("authorization", encodedString);
42+
return Collections.singletonMap("authorization", "basic%20" + encodedString);
4343
}
4444
}

api/src/main/java/com/intergral/deep/api/IDeepLoader.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ public interface IDeepLoader {
2525
/**
2626
* Load the Deep agent into the provided process id.
2727
*
28-
* @param pid the current process id
29-
* @param config the config to use
28+
* @param pid the current process id
29+
* @param config the config to use
30+
* @param jarPath the full path to the jar to load (or {@code null} to auto discover the jar)
3031
* @throws Throwable if loader fails
3132
*/
32-
void load(final String pid, final String config) throws Throwable;
33+
void load(final String pid, final String config, final String jarPath) throws Throwable;
3334
}

deep/src/main/java/com/intergral/deep/Deep.java

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,38 @@
2424
import java.lang.reflect.Constructor;
2525
import java.lang.reflect.Method;
2626

27+
/**
28+
* This is the main entry point to deep when using the API. All calls to deep should precede with a call to this class.
29+
* <p>
30+
* To start deep first configure the instance.
31+
* <code>
32+
* Deep.config().start();
33+
* </code>
34+
* <p>
35+
* To use a deep API use
36+
* <code>
37+
* Deep.getInstance().<IDeep>api()
38+
* </code>
39+
*/
2740
public class Deep {
2841

2942
private static Deep DEEP_INSTANCE = null;
3043
private Object deepService = null;
3144
private Object reflection = null;
3245

3346

47+
/**
48+
* This is a shortcut for {@code Deep.config().start()}
49+
*/
3450
public static void start() {
3551
Deep.config().start();
3652
}
3753

54+
/**
55+
* This will create an instance of DEEP allowing access to the APIs from inside deep agent.
56+
*
57+
* @return {@link Deep}
58+
*/
3859
public static Deep getInstance() {
3960
if (DEEP_INSTANCE != null) {
4061
return DEEP_INSTANCE;
@@ -44,29 +65,49 @@ public static Deep getInstance() {
4465
return DEEP_INSTANCE;
4566
}
4667

68+
/**
69+
* This is the main point to start with deep. Call this to create a config builder and to customise the config of deep before starting
70+
* it.
71+
*
72+
* @return {@link DeepConfigBuilder}
73+
*/
4774
public static DeepConfigBuilder config() {
4875
return new DeepConfigBuilder();
4976
}
5077

51-
public void startWithConfig(final String config) {
52-
getInstance().startDeep(config);
78+
/**
79+
* This allows deep to be started with the parsed config
80+
*
81+
* @param config as a string
82+
*/
83+
void startWithConfig(final String config, final String jarPath) {
84+
getInstance().startDeep(config, jarPath);
85+
}
86+
87+
/**
88+
* This allows deep to be started with the parsed config builder
89+
*
90+
* @param builder the config to use
91+
*/
92+
public void start(final DeepConfigBuilder builder) {
93+
builder.start();
5394
}
5495

55-
private void startDeep(final String config) {
96+
private void startDeep(final String config, final String jarPath) {
5697
try {
57-
loadAgent(config);
98+
loadAgent(config, jarPath);
5899

59100
loadAPI();
60101
} catch (Throwable t) {
61102
t.printStackTrace();
62103
}
63104
}
64105

65-
private void loadAgent(final String config) throws Throwable {
106+
private void loadAgent(final String config, final String jarPath) throws Throwable {
66107
final IDeepLoader loader = getLoader();
67108
final String pid = getPid();
68109

69-
loader.load(pid, config);
110+
loader.load(pid, config, jarPath);
70111
}
71112

72113

@@ -123,6 +164,8 @@ private void loadAPI() {
123164

124165
/**
125166
* Get an instance of the API to allow calling NerdVision directly
167+
* <p>
168+
* This uses T as the type {@link IDeep} is not loaded so this class cannot use it.
126169
*
127170
* @param <T> this should be {@link IDeep}
128171
* @return the new instance or {@link IDeep}
@@ -138,12 +181,14 @@ public <T> T api() {
138181
}
139182

140183

141-
private void setProxyService(Object service) {
142-
final IDeepHook hook = (IDeepHook) service;
143-
this.deepService = hook.deepService();
144-
this.reflection = hook.reflectionService();
145-
}
146-
184+
/**
185+
* Get an instance of the Reflection api used in deep.
186+
* <p>
187+
* This uses T as the type {@link com.intergral.deep.agent.api.reflection.IReflection} is not loaded so this class cannot use it.
188+
*
189+
* @return the {@link com.intergral.deep.agent.api.reflection.IReflection} service
190+
* @param <T> this should be {@link com.intergral.deep.agent.api.reflection.IReflection}
191+
*/
147192
public <T> T reflection() {
148193
loadAPI();
149194
if (this.reflection == null) {
@@ -152,4 +197,10 @@ public <T> T reflection() {
152197
//noinspection unchecked
153198
return (T) reflection;
154199
}
200+
201+
private void setProxyService(Object service) {
202+
final IDeepHook hook = (IDeepHook) service;
203+
this.deepService = hook.deepService();
204+
this.reflection = hook.reflectionService();
205+
}
155206
}

deep/src/main/java/com/intergral/deep/DeepConfigBuilder.java

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,82 @@
2323
public class DeepConfigBuilder {
2424

2525
private final Map<String, Object> config = new HashMap<>();
26+
private String jarPath;
2627

28+
/**
29+
* Start Deep using this config
30+
*/
2731
public void start() {
28-
Deep.getInstance().startWithConfig(this.build());
32+
Deep.getInstance().startWithConfig(this.build(), this.jarPath);
33+
}
34+
35+
/**
36+
* This allows for overriding the path to the deep agent jar. This is useful when embedding deep into complex applications such as OSGI.
37+
*
38+
* @param path the full path to the agent jar file
39+
* @return {this}
40+
*/
41+
public DeepConfigBuilder setJarPath(final String path) {
42+
this.jarPath = path;
43+
return this;
44+
}
45+
46+
47+
/**
48+
* Set a config value for deep. For the possible keys see the docs on config.
49+
*
50+
* @param key the key for the config value
51+
* @param value the value as a string object
52+
* @return {this{}}
53+
*/
54+
public DeepConfigBuilder setValue(final String key, final String value) {
55+
this.config.put(key, value);
56+
return this;
57+
}
58+
59+
60+
/**
61+
* Set a config value for deep. For the possible keys see the docs on config.
62+
*
63+
* @param key the key for the config value
64+
* @param value the value as a boolean
65+
* @return {this{}}
66+
*/
67+
public DeepConfigBuilder setValue(final String key, final boolean value) {
68+
this.config.put(key, value);
69+
return this;
2970
}
3071

31-
public DeepConfigBuilder setValue(final String key, final Object value) {
72+
73+
/**
74+
* Set a config value for deep. For the possible keys see the docs on config.
75+
*
76+
* @param key the key for the config value
77+
* @param value the value as an int
78+
* @return {this{}}
79+
*/
80+
public DeepConfigBuilder setValue(final String key, final int value) {
3281
this.config.put(key, value);
3382
return this;
3483
}
3584

85+
86+
/**
87+
* Set a config value for deep. For the possible keys see the docs on config.
88+
*
89+
* @param key the key for the config value
90+
* @param value the value as a double
91+
* @return {this{}}
92+
*/
93+
public DeepConfigBuilder setValue(final String key, final double value) {
94+
this.config.put(key, value);
95+
return this;
96+
}
97+
98+
/**
99+
* Converts this object into a string that can be used by the attachment process
100+
* @return a string for this config
101+
*/
36102
private String build() {
37103
return this.configAsArgs(this.config);
38104
}

deep/src/main/java/com/intergral/deep/DeepLoader.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
public class DeepLoader implements IDeepLoader {
3030

3131
@Override
32-
public void load(final String pid, final String config) {
33-
final File agentJar = getAgentJar();
32+
public void load(final String pid, final String config, final String jarPath) {
33+
final File agentJar = getAgentJar(jarPath);
3434
final File tools = getToolsJar();
3535
if (agentJar == null) {
3636
throw new RuntimeException("Cannot find jar.");
@@ -60,8 +60,8 @@ private File getToolsJar() {
6060
*
6161
* @return the {@link File} object for the agent
6262
*/
63-
private File getAgentJar() {
64-
final InputStream resourceAsStream = getAgentJarStream();
63+
private File getAgentJar(final String jarPath) {
64+
final InputStream resourceAsStream = getAgentJarStream(jarPath);
6565
final String pathname = extractLibrary(resourceAsStream);
6666
if (pathname != null) {
6767
return new File(pathname);
@@ -75,9 +75,9 @@ private File getAgentJar() {
7575
*
7676
* @return the stream to use, or {@code null}
7777
*/
78-
private InputStream getAgentJarStream() {
78+
private InputStream getAgentJarStream(final String jarPath) {
7979
// this is pretty much just for testing, see Example
80-
final String property = System.getProperty("deep.jar.path");
80+
final String property = System.getProperty("deep.jar.path", jarPath);
8181
if (property != null) {
8282
try {
8383
return new FileInputStream(property);

0 commit comments

Comments
 (0)