return urls;
}
@Override
public ResourceUpgradeReport upgrade(ResourceUpgradeContext inventoriedResource) {
JvmResourceKey oldKey = JvmResourceKey.valueOf(inventoriedResource.getResourceKey());
JvmResourceKey.Type oldKeyType = oldKey.getType();
if (oldKeyType == JvmResourceKey.Type.Legacy || oldKeyType == JvmResourceKey.Type.JmxRemotingPort) {
if (!inventoriedResource.getSystemInformation().isNative()) {
log.warn("Cannot attempt to upgrade Resource key [" + inventoriedResource.getResourceKey()
+ "] of JVM Resource, because this Agent is not running with native system info support (i.e. SIGAR).");
return null;
}
Configuration pluginConfig = inventoriedResource.getPluginConfiguration();
String connectorAddress = pluginConfig.getSimpleValue(CONNECTOR_ADDRESS_CONFIG_PROPERTY, null);
JMXServiceURL jmxServiceURL;
try {
jmxServiceURL = new JMXServiceURL(connectorAddress);
} catch (MalformedURLException e) {
throw new RuntimeException("Failed to parse connector address: " + connectorAddress, e);
}
JMXConnector jmxConnector = null;
Long pid;
try {
jmxConnector = connect(jmxServiceURL);
MBeanServerConnection mbeanServerConnection = jmxConnector.getMBeanServerConnection();
RuntimeMXBean runtimeMXBean = ManagementFactory.newPlatformMXBeanProxy(mbeanServerConnection,
ManagementFactory.RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);
pid = getJvmPid(runtimeMXBean);
if (pid == null) {
throw new RuntimeException("Failed to determine JVM pid by parsing JVM name.");
}
} catch (SecurityException e) {
// Authentication failed, which most likely means the username and password are not set correctly in
// the Resource's plugin config. This is not an error, so return null.
log.info("Unable to upgrade key of JVM Resource with key [" + inventoriedResource.getResourceKey()
+ "], since authenticating to its JMX service URL [" + jmxServiceURL + "] failed: "
+ e.getMessage());
return null;
} catch (IOException e) {
// The JVM's not currently running, which means we won't be able to figure out its main class name,
// which is needed to upgrade its key. This is not an error, so return null.
log.debug("Unable to upgrade key of JVM Resource with key [" + inventoriedResource.getResourceKey()
+ "], since connecting to its JMX service URL [" + jmxServiceURL + "] failed: " + e);
return null;
} finally {
close(jmxConnector);
}
List<ProcessInfo> processes = inventoriedResource.getSystemInformation().getProcesses(
"process|pid|match=" + pid);
if (processes.size() != 1) {
throw new IllegalStateException("Failed to find process with PID [" + pid + "].");
}
ProcessInfo process = processes.get(0);
String mainClassName = getJavaMainClassName(process);
String explicitKeyValue = getSystemPropertyValue(process, SYSPROP_RHQ_RESOURCE_KEY);
if (oldKeyType == JvmResourceKey.Type.Legacy || explicitKeyValue != null) {
// We need to upgrade the key.
JvmResourceKey newKey;
if (explicitKeyValue != null) {
newKey = JvmResourceKey.fromExplicitValue(mainClassName, explicitKeyValue);
} else {
newKey = JvmResourceKey.fromJmxRemotingPort(mainClassName, oldKey.getJmxRemotingPort());
}
ResourceUpgradeReport resourceUpgradeReport = new ResourceUpgradeReport();
resourceUpgradeReport.setNewResourceKey(newKey.toString());
return resourceUpgradeReport;
}
}
return null;