Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuration to be loaded from an external file #108

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public class HotswapAgent {
*/
private static boolean autoHotswap = false;

/**
* Path for an external properties file `hotswap-agent.properties`
*/
private static String propertiesFilePath;

public static void premain(String args, Instrumentation inst) {

LOGGER.info("Loading Hotswap agent {{}} - unlimited runtime class redefinition.", Version.version());
Expand All @@ -56,16 +61,26 @@ public static void parseArgs(String args) {

String option = val[0];
String optionValue = val[1];

if ("disablePlugin".equals(option)) {
disabledPlugins.add(optionValue.toLowerCase());
} else if ("autoHotswap".equals(option)) {
autoHotswap = Boolean.valueOf(optionValue);
} else if ("propertiesFilePath".equals(option)) {
propertiesFilePath = optionValue;
} else {
LOGGER.warning("Invalid javaagent option '{}'. Argument '{}' is ignored.", option, arg);
}
}
}

/**
* @return the path for the hotswap-agent.properties external file
*/
public static String getExternalPropertiesFile() {
return propertiesFilePath;
}


/**
* Checks if the plugin is disabled (by name).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package org.hotswap.agent.config;

import org.hotswap.agent.HotswapAgent;
import org.hotswap.agent.annotation.Plugin;
import org.hotswap.agent.logging.AgentLogger;
import org.hotswap.agent.util.classloader.HotswapAgentClassLoaderExt;
import org.hotswap.agent.util.classloader.URLClassLoaderHelper;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
Expand All @@ -12,12 +18,6 @@
import java.util.Properties;
import java.util.StringTokenizer;

import org.hotswap.agent.HotswapAgent;
import org.hotswap.agent.annotation.Plugin;
import org.hotswap.agent.logging.AgentLogger;
import org.hotswap.agent.util.classloader.HotswapAgentClassLoaderExt;
import org.hotswap.agent.util.classloader.URLClassLoaderHelper;

/**
* Plugin configuration.
* <p/>
Expand Down Expand Up @@ -46,68 +46,94 @@ public class PluginConfiguration {


public PluginConfiguration(ClassLoader classLoader) {
this(null, classLoader);
}

public PluginConfiguration(PluginConfiguration parent, ClassLoader classLoader) {
this.parent = parent;
this.classLoader = classLoader;
configurationURL = classLoader == null ? ClassLoader.getSystemResource(PLUGIN_CONFIGURATION) : classLoader.getResource(PLUGIN_CONFIGURATION);

loadConfigurationFile();
init();
}

private void loadConfigurationFile() {

try {
if (configurationURL != null) {
containsPropertyFileDirectly = true;
String externalPropertiesFile = HotswapAgent.getExternalPropertiesFile();

if (externalPropertiesFile != null) {
configurationURL = resourceNameToURL(externalPropertiesFile);
properties.load(configurationURL.openStream());
init();
return;
}

} catch (Exception e) {
LOGGER.error("Error while loading 'hotswap-agent.properties' from base URL " + configurationURL, e);
LOGGER.error("Error while loading external properties file " + configurationURL, e);
}
}

public PluginConfiguration(PluginConfiguration parent, ClassLoader classLoader) {
this.parent = parent;
this.classLoader = classLoader;
if (parent == null) {
configurationURL = classLoader == null
? ClassLoader.getSystemResource(PLUGIN_CONFIGURATION)
: classLoader.getResource(PLUGIN_CONFIGURATION);

// search for resources not known by parent classloader (defined in THIS classloader exclusively)
// this is necessary in case of parent classloader precedence
try {
Enumeration<URL> urls = classLoader == null ? ClassLoader.getSystemResources(PLUGIN_CONFIGURATION) : classLoader.getResources(PLUGIN_CONFIGURATION);
while (urls.hasMoreElements()) {
URL url = urls.nextElement();

boolean found = false;

if (parent != null) {
ClassLoader parentClassLoader = parent.getClassLoader();
Enumeration<URL> parentUrls = parentClassLoader == null ? ClassLoader.getSystemResources(PLUGIN_CONFIGURATION) : parentClassLoader.getResources(PLUGIN_CONFIGURATION);
while (parentUrls.hasMoreElements()) {
if (url.equals(parentUrls.nextElement()))
found = true;
}
}
try {
if (configurationURL != null) {
containsPropertyFileDirectly = true;
properties.load(configurationURL.openStream());

if (!found) {
configurationURL = url;
break;
}
} catch (Exception e) {
LOGGER.error("Error while loading 'hotswap-agent.properties' from base URL " + configurationURL, e);
}
} catch (IOException e) {
LOGGER.error("Error while loading 'hotswap-agent.properties' from URL " + configurationURL, e);
}

if (configurationURL == null && parent != null) {
configurationURL = parent.configurationURL;
LOGGER.debug("Classloader does not contain 'hotswap-agent.properties', using parent file '{}'", parent.configurationURL);
} else {
LOGGER.debug("Classloader contains 'hotswap-agent.properties' at location '{}'", configurationURL);
containsPropertyFileDirectly = true;
}
// search for resources not known by parent classloader (defined in THIS classloader exclusively)
// this is necessary in case of parent classloader precedence
try {
Enumeration<URL> urls = classLoader == null
? ClassLoader.getSystemResources(PLUGIN_CONFIGURATION)
: classLoader.getResources(PLUGIN_CONFIGURATION);

while (urls.hasMoreElements()) {
URL url = urls.nextElement();

boolean found = false;

if (parent != null) {
ClassLoader parentClassLoader = parent.getClassLoader();
Enumeration<URL> parentUrls = parentClassLoader == null
? ClassLoader.getSystemResources(PLUGIN_CONFIGURATION)
: parentClassLoader.getResources(PLUGIN_CONFIGURATION);

while (parentUrls.hasMoreElements()) {
if (url.equals(parentUrls.nextElement()))
found = true;
}
}

try {
if (configurationURL != null)
properties.load(configurationURL.openStream());
init();
} catch (Exception e) {
LOGGER.error("Error while loading 'hotswap-agent.properties' from URL " + configurationURL, e);
if (!found) {
configurationURL = url;
break;
}
}
} catch (IOException e) {
LOGGER.error("Error while loading 'hotswap-agent.properties' from URL " + configurationURL, e);
}

if (configurationURL == null) {
configurationURL = parent.configurationURL;
LOGGER.debug("Classloader does not contain 'hotswap-agent.properties', using parent file '{}'"
, parent.configurationURL);

} else {
LOGGER.debug("Classloader contains 'hotswap-agent.properties' at location '{}'", configurationURL);
containsPropertyFileDirectly = true;
}
}
}


/**
* Initialize the configuration.
*/
Expand Down