Class<?> connectionTypeDescriptorClass;
try {
connectionTypeDescriptorClass = Class.forName(connectionTypeDescriptorClassName);
} catch (ClassNotFoundException e) {
throw new InvalidPluginConfigurationException("Invalid connection type - class [" + connectionTypeDescriptorClassName
+ "] not found.");
}
if (!(ConnectionTypeDescriptor.class.isAssignableFrom(connectionTypeDescriptorClass))) {
throw new InvalidPluginConfigurationException("Invalid connection type - class [" + connectionTypeDescriptorClassName
+ "] does not implement the " + ConnectionTypeDescriptor.class.getName() + " interface.");
}
ConnectionTypeDescriptor connectionTypeDescriptor;
try {
connectionTypeDescriptor = (ConnectionTypeDescriptor) connectionTypeDescriptorClass.newInstance();
} catch (Exception e) {
throw new RuntimeException("Failed to instantiate connection type descriptor of type [" + connectionTypeDescriptorClassName + "].", e);
}
ConnectionSettings settings = new ConnectionSettings();
settings.initializeConnectionType(connectionTypeDescriptor);
// Set principal and credentials.
String principal = pluginConfig.getSimpleValue(JMXComponent.PRINCIPAL_CONFIG_PROP, null);
settings.setPrincipal(principal);
String credentials = pluginConfig.getSimpleValue(JMXComponent.CREDENTIALS_CONFIG_PROP, null);
settings.setCredentials(credentials);
if (connectionTypeDescriptor instanceof LocalVMTypeDescriptor) {
// NOTE (ips, 01/19/12): This is not very reliable for long-term management of a JVM, since it uses the
// command line from the time the JVM was originally discovered, which may have changed.
String commandLine = pluginConfig.getSimpleValue(JMXDiscoveryComponent.COMMAND_LINE_CONFIG_PROPERTY, null);
if (commandLine == null) {
throw new InvalidPluginConfigurationException("A command line is required for the "
+ connectionTypeDescriptorClassName + " connection type.");
}
Map<Integer, LocalVirtualMachine> vms = LocalVMFinder.getManageableVirtualMachines();
LocalVirtualMachine targetVm = null;
if (vms != null) {
for (LocalVirtualMachine vm : vms.values()) {
if (vm.getCommandLine().equals(commandLine)) {
targetVm = vm;
break;
}
}
}
if (targetVm == null) {
// This could just be because the JVM is not currently running.
throw new Exception("JVM with command line [" + commandLine + "] not found.");
}
String vmId = String.valueOf(targetVm.getVmid());
settings.setServerUrl(vmId);
} else if (connectionTypeDescriptor instanceof J2SE5ConnectionTypeDescriptor) {
// Connect via JMX Remoting, using the JVM Attach API to start up a JMX Remoting Agent if necessary.
String jmxConnectorAddress = getJmxConnectorAddress(pluginConfig, process);
settings.setServerUrl(jmxConnectorAddress);
} else {
// Handle internal connections (InternalVMTypeDescriptor) (i.e. the RHQ plugin container's own JVM), as
// well as miscellaneous types of remote connections - WebSphere, WebLogic, etc.
String connectorAddress = pluginConfig.getSimpleValue(JMXDiscoveryComponent.CONNECTOR_ADDRESS_CONFIG_PROPERTY,
null);
if (connectorAddress == null) {
throw new InvalidPluginConfigurationException("A connector address is required for the "
+ connectionTypeDescriptorClassName + " connection type.");
}
settings.setServerUrl(connectorAddress);
String installURI = pluginConfig.getSimpleValue(JMXDiscoveryComponent.INSTALL_URI, null);
settings.setLibraryURI(installURI);