Skip to content

Commit

Permalink
Some fine tuning and added to documentation #86 #82
Browse files Browse the repository at this point in the history
  • Loading branch information
rhuss committed Mar 13, 2013
1 parent 76f3ad3 commit 9e0b90a
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 58 deletions.
Expand Up @@ -39,7 +39,7 @@
* @author roland
* @since 28.12.12
*/
public class SpringJolokiaConfigHolder extends JolokiaServerConfig implements Ordered {
public class SpringJolokiaConfigHolder implements Ordered {

// configuration
private Map<String, String> config = new HashMap<String, String>();
Expand Down

This file was deleted.

Expand Up @@ -154,11 +154,10 @@ protected final void init(JolokiaServerConfig pConfig, boolean pLazy) throws IOE
addAuthenticatorIfNeeded(config.getUser(),config.getPassword(),context);
initializeExecutor();

if (port == 0) {
port = httpServer.getAddress().getPort();
}

url = String.format("%s://%s:%d%s",protocol,address.getCanonicalHostName(),port,contextPath);
InetSocketAddress realSocketAddress = httpServer.getAddress();
InetAddress realAddress = realSocketAddress.getAddress() != null ? realSocketAddress.getAddress() : address;
url = String.format("%s://%s:%d%s",
protocol,realAddress.getCanonicalHostName(),realSocketAddress.getPort(),contextPath);
}

private void addAuthenticatorIfNeeded(final String user, final String password, HttpContext pContext) {
Expand Down
42 changes: 30 additions & 12 deletions agent/jvm/src/main/java/org/jolokia/jvmagent/JvmAgentConfig.java
Expand Up @@ -43,12 +43,24 @@ public class JvmAgentConfig extends JolokiaServerConfig {
* for an agent parameter
*/
public JvmAgentConfig(String pArgs) {
Map<String,String> agentConfig = parseArgs(pArgs);
init(split(pArgs));
}

init(agentConfig);
/**
* Constructor with a preparsed configuration
*
* @param pConfig config map with key value pairs
*/
public JvmAgentConfig(Map<String,String> pConfig) {
init(pConfig);
}

@Override
/** {@inheritDoc} */
protected void init(Map<String, String> pConfig) {
super.init(prepareConfig(pConfig));
// Special mode used by the client in order to indicate whether to stop/start the server.
initMode(agentConfig);
initMode(pConfig);
}

/**
Expand All @@ -73,7 +85,20 @@ private void initMode(Map<String, String> agentConfig) {
// ======================================================================================
// Parse argument

private Map<String, String> parseArgs(String pAgentArgs) {
// Prepare configuration with filling up default values
private Map<String, String> prepareConfig(Map<String, String> pRet) {
Map<String,String> config = getDefaultConfig();
if (pRet.containsKey("config")) {
Map<String,String> userConfig = readConfig(pRet.get("config"));
config.putAll(userConfig);
}
config.putAll(pRet);
prepareDetectorOptions(config);
return config;
}

// Split arguments into a map
private Map<String, String> split(String pAgentArgs) {
Map<String,String> ret = new HashMap<String, String>();
if (pAgentArgs != null && pAgentArgs.length() > 0) {
for (String arg : EscapeUtil.splitAsArray(pAgentArgs, EscapeUtil.CSV_ESCAPE, ",")) {
Expand All @@ -85,14 +110,7 @@ private Map<String, String> parseArgs(String pAgentArgs) {
}
}
}
Map<String,String> config = getDefaultConfig();
if (ret.containsKey("config")) {
Map<String,String> userConfig = readConfig(ret.get("config"));
config.putAll(userConfig);
}
config.putAll(ret);
prepareDetectorOptions(config);
return config;
return ret;
}

// Add detector specific options if given on the command line
Expand Down
Expand Up @@ -19,6 +19,8 @@
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import org.jolokia.Version;
import org.jolokia.test.util.EnvTestUtil;
Expand Down Expand Up @@ -49,8 +51,10 @@ public void http() throws IOException, InterruptedException {

@Test(expectedExceptions = IOException.class,expectedExceptionsMessageRegExp = ".*401.*")
public void httpWithAuthenticationRejected() throws IOException {
roundtrip("user=roland,password=s!cr!t",true);

Map config = new HashMap();
config.put("user","roland");
config.put("password","s!cr!t");
roundtrip(config,true);
}

@Test
Expand Down Expand Up @@ -90,16 +94,28 @@ private String getKeystorePath() {
throw new IllegalStateException(ksURL + " is not a file URL");
}

private void roundtrip(Map<String,String> pConfig, boolean pDoRequest) throws IOException {
checkServer(new JvmAgentConfig(pConfig),pDoRequest);
}

private void roundtrip(String pConfig, boolean pDoRequest) throws IOException {
JvmAgentConfig config = new JvmAgentConfig(prepareConfigString(pConfig));
checkServer(config, pDoRequest);
}

private String prepareConfigString(String pConfig) throws IOException {
String c = pConfig != null ? pConfig + "," : "";
boolean portSpecified = c.contains("port=");
c = c + "host=localhost,";
if (!portSpecified) {
int port = EnvTestUtil.getFreePort();
c = c + "port=" + port;
}
JvmAgentConfig config = new JvmAgentConfig(c);
JolokiaServer server = new JolokiaServer(config,false);
return c;
}

private void checkServer(JvmAgentConfig pConfig, boolean pDoRequest) throws IOException {
JolokiaServer server = new JolokiaServer(pConfig,false);
server.start();
//Thread.sleep(2000);
try {
Expand Down
9 changes: 9 additions & 0 deletions src/changes/changes.xml
Expand Up @@ -24,6 +24,15 @@
</properties>
<body>
<release version="1.1.1-SNAPSHOT" description="Release 1.1.1-SNAPSHOT">
<action dev="benson-basis" type="add" issue="86" date="2013-03-13">
If no host is given, the JVM agent will now bind to localhost. If a
host of "0.0.0.0" or "*" is given, then the agent will listen on
all interfaces.
</action>
<action dev="benson-basis" type="add" issue="82" date="2013-03-13">
If a port of 0 is given, then the JVM agent will select an arbitrary
free port and prints out the selected port.
</action>
<action dev="roland" type="add" issue="77" date="2013-03-07">
Added support for "ifModifiedSince" processing parameter.
</action>
Expand Down
8 changes: 5 additions & 3 deletions src/docbkx/agents/jvm.xml
Expand Up @@ -85,16 +85,18 @@ java -javaagent:agent.jar=port=7777,host=localhost]]></programlisting>
<tr>
<td><constant>host</constant></td>
<td>
Hostaddress to which the HTTP server should bind to.
Hostaddress to which the HTTP server should bind to. If "*" or "0.0.0.0" is
given, the servers binds to every network interface.
</td>
<td>
<constant>InetAddress.getLocalHost()</constant>
<constant>localhost</constant>
</td>
</tr>
<tr>
<td><constant>port</constant></td>
<td>
Port the HTTP server should listen to.
Port the HTTP server should listen to. If set to 0, then an arbitrary free port
will be selected.
</td>
<td>
<constant>8778</constant>
Expand Down

0 comments on commit 9e0b90a

Please sign in to comment.