Author: rob.stryker(a)jboss.com
Date: 2007-09-14 16:10:01 -0400 (Fri, 14 Sep 2007)
New Revision: 3636
Added:
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/extensions/jmx/
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/extensions/jmx/JMXClassLoaderRepository.java
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/extensions/jmx/JMXUtil.java
Modified:
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/extensions/polling/JMXPoller.java
Log:
Moved JMX Classloader and credentialing to another class for re-use.
Added:
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/extensions/jmx/JMXClassLoaderRepository.java
===================================================================
---
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/extensions/jmx/JMXClassLoaderRepository.java
(rev 0)
+++
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/extensions/jmx/JMXClassLoaderRepository.java 2007-09-14
20:10:01 UTC (rev 3636)
@@ -0,0 +1,115 @@
+package org.jboss.ide.eclipse.as.core.extensions.jmx;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.ArrayList;
+import java.util.HashMap;
+
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.wst.server.core.IRuntime;
+import org.eclipse.wst.server.core.IServer;
+
+/**
+ * A repository for classloaders that relate to servers,
+ * specifically jmx. It basically loads teh jboss-clientall.jar.
+ *
+ *
+ * @author Rob Stryker
+ *
+ */
+public class JMXClassLoaderRepository {
+ protected static JMXClassLoaderRepository instance;
+ public static JMXClassLoaderRepository getDefault() {
+ if( instance == null ) {
+ instance = new JMXClassLoaderRepository();
+ }
+ return instance;
+ }
+
+ protected HashMap<String, ClassLoader> idToLoader;
+ protected HashMap<String, ArrayList<Object>> idToConcerned;
+ protected JMXClassLoaderRepository() {
+ idToLoader = new HashMap<String, ClassLoader>();
+ idToConcerned = new HashMap<String, ArrayList<Object>>();
+ }
+
+ /**
+ * Add a concerned citizen to the list of said citizens.
+ * These are people who may at any time ask to access
+ * the classloader.
+ *
+ * @param server
+ * @param concerned
+ */
+ public void addConcerned(IServer server, Object concerned) {
+ ArrayList<Object> list = idToConcerned.get(server.getId());
+ if( list == null ) {
+ list = new ArrayList<Object>();
+ idToConcerned.put(server.getId(), list);
+ }
+ if( !list.contains(concerned))
+ list.add(concerned);
+ }
+
+ /**
+ * Removes an element from the list of concerned citizens.
+ * If no one cares about this server any longer, remove
+ * its classloader cache and forget about it.
+ *
+ * @param server
+ * @param concerned
+ */
+ public void removeConcerned(IServer server, Object concerned) {
+ ArrayList<Object> list = idToConcerned.get(server.getId());
+ if( list != null ) {
+ list.remove(concerned);
+ if( list.size() == 0 ) {
+ idToConcerned.remove(server.getId());
+ idToLoader.remove(server.getId());
+ }
+ }
+ }
+
+ /**
+ * Get the classloader for some server.
+ * If there are 0 concerned citizens, null should be returned.
+ * @param server
+ * @return
+ */
+ public ClassLoader getClassLoader(IServer server) {
+ ClassLoader loader = idToLoader.get(server.getId());
+ if( loader == null && anyoneCares(server)) {
+ loadClassLoader(server);
+ }
+ return idToLoader.get(server.getId());
+ }
+
+ /**
+ * Actually create and store the classloader
+ * @param s
+ */
+ protected void loadClassLoader(IServer s) {
+ try {
+ IRuntime rt = s.getRuntime();
+ IPath loc = rt.getLocation();
+ URL url = loc.append("client").append("jbossall-client.jar")
+ .toFile().toURI().toURL();
+ URLClassLoader loader = new URLClassLoader(new URL[] { url, },
+ Thread.currentThread().getContextClassLoader());
+ idToLoader.put(s.getId(), loader);
+ } catch (MalformedURLException murle) {
+ }
+ }
+
+ /**
+ * Are there any concerned citizens for this server?
+ * @param server
+ * @return
+ */
+ protected boolean anyoneCares(IServer server) {
+ ArrayList<Object> list = idToConcerned.get(server.getId());
+ if( list != null && list.size() > 0 ) return true;
+ return false;
+ }
+}
Added:
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/extensions/jmx/JMXUtil.java
===================================================================
---
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/extensions/jmx/JMXUtil.java
(rev 0)
+++
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/extensions/jmx/JMXUtil.java 2007-09-14
20:10:01 UTC (rev 3636)
@@ -0,0 +1,104 @@
+package org.jboss.ide.eclipse.as.core.extensions.jmx;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.security.Principal;
+import java.util.Properties;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.debug.core.ILaunchConfiguration;
+import org.eclipse.wst.server.core.IServer;
+import org.jboss.ide.eclipse.as.core.util.ServerConverter;
+
+/**
+ * Utility class
+ * @author Rob Stryker
+ *
+ */
+public class JMXUtil {
+
+
+ /**
+ * In the current thread, set the credentials from some server
+ * @param server
+ */
+ public static void setCredentials(IServer server) throws CredentialException {
+ Exception temp = null;
+ try {
+ ILaunchConfiguration lc = server.getLaunchConfiguration(true,
+ new NullProgressMonitor());
+ // get user from the IServer, but override with launch configuration
+ String user = ServerConverter.getJBossServer(server).getUsername();
+
+ // get password from the IServer, but override with launch configuration
+ String pass = ServerConverter.getJBossServer(server).getPassword();
+
+ // get our methods
+ Class simplePrincipal = Thread.currentThread()
+ .getContextClassLoader().loadClass(
+ "org.jboss.security.SimplePrincipal");
+ Class securityAssoc = Thread.currentThread()
+ .getContextClassLoader().loadClass(
+ "org.jboss.security.SecurityAssociation");
+ securityAssoc.getMethods(); // force-init the methods since the
+ // class hasn't been initialized yet.
+
+ Constructor newSimplePrincipal = simplePrincipal
+ .getConstructor(new Class[] { String.class });
+ Object newPrincipalInstance = newSimplePrincipal
+ .newInstance(new Object[] { user });
+
+ // set the principal
+ Method setPrincipalMethod = securityAssoc.getMethod(
+ "setPrincipal", new Class[] { Principal.class });
+ setPrincipalMethod.invoke(null,
+ new Object[] { newPrincipalInstance });
+
+ // set the credential
+ Method setCredentialMethod = securityAssoc.getMethod(
+ "setCredential", new Class[] { Object.class });
+ setCredentialMethod.invoke(null, new Object[] { pass });
+ } catch (CoreException e) {
+ temp = e;
+ } catch (ClassNotFoundException e) {
+ temp = e;
+ } catch (SecurityException e) {
+ temp = e;
+ } catch (NoSuchMethodException e) {
+ temp = e;
+ } catch (IllegalArgumentException e) {
+ temp = e;
+ } catch (InstantiationException e) {
+ temp = e;
+ } catch (IllegalAccessException e) {
+ temp = e;
+ } catch (InvocationTargetException e) {
+ temp = e;
+ }
+ if( temp != null )
+ throw new CredentialException(temp);
+ }
+
+ public static class CredentialException extends Exception {
+ private static final long serialVersionUID = 1L;
+ protected Exception wrapped;
+ public CredentialException(Exception wrapped) {
+ this.wrapped = wrapped;
+ }
+ public Exception getWrapped() { return wrapped; }
+ }
+
+ public static Properties getDefaultProperties(IServer server) {
+ int port = ServerConverter.getJBossServer(server).getJNDIPort();
+ Properties props = new Properties();
+ props.put("java.naming.factory.initial",
+ "org.jnp.interfaces.NamingContextFactory");
+ props.put("java.naming.factory.url.pkgs",
+ "org.jboss.naming:org.jnp.interfaces");
+ props.put("java.naming.provider.url", "jnp://"
+ + server.getHost() + ":" + port);
+ return props;
+ }
+}
Modified:
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/extensions/polling/JMXPoller.java
===================================================================
---
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/extensions/polling/JMXPoller.java 2007-09-14
18:41:21 UTC (rev 3635)
+++
trunk/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/extensions/polling/JMXPoller.java 2007-09-14
20:10:01 UTC (rev 3636)
@@ -25,9 +25,6 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.net.URLClassLoader;
import java.security.Principal;
import java.util.Date;
import java.util.Properties;
@@ -38,18 +35,20 @@
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
+import javax.management.OperationsException;
import javax.management.ReflectionException;
import javax.naming.CommunicationException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.debug.core.ILaunchConfiguration;
-import org.eclipse.wst.server.core.IRuntime;
import org.eclipse.wst.server.core.IServer;
import org.jboss.ide.eclipse.as.core.extensions.events.EventLogModel.EventLogTreeItem;
+import org.jboss.ide.eclipse.as.core.extensions.jmx.JMXClassLoaderRepository;
+import org.jboss.ide.eclipse.as.core.extensions.jmx.JMXUtil;
+import org.jboss.ide.eclipse.as.core.extensions.jmx.JMXUtil.CredentialException;
import org.jboss.ide.eclipse.as.core.server.IServerStatePoller;
import org.jboss.ide.eclipse.as.core.server.internal.PollThread;
import org.jboss.ide.eclipse.as.core.util.ServerConverter;
@@ -85,22 +84,14 @@
private class PollerRunnable implements Runnable {
public void run() {
- ClassLoader currentLoader = Thread.currentThread()
- .getContextClassLoader();
- ClassLoader twiddleLoader = getClassLoader();
+ JMXClassLoaderRepository.getDefault().addConcerned(server, this);
+ ClassLoader currentLoader = Thread.currentThread().getContextClassLoader();
+ ClassLoader twiddleLoader =
JMXClassLoaderRepository.getDefault().getClassLoader(server);
if( pollingException != null ) {done = true; return;}
if (twiddleLoader != null) {
- int port = ServerConverter.getJBossServer(server).getJNDIPort();
Thread.currentThread().setContextClassLoader(twiddleLoader);
- Properties props = new Properties();
- props.put("java.naming.factory.initial",
- "org.jnp.interfaces.NamingContextFactory");
- props.put("java.naming.factory.url.pkgs",
- "org.jboss.naming:org.jnp.interfaces");
- props.put("java.naming.provider.url", "jnp://"
- + server.getHost() + ":" + port);
-
+ Properties props = JMXUtil.getDefaultProperties(server);
setCredentials();
if( pollingException != null ) {done = true; return;}
@@ -128,12 +119,8 @@
started = STATE_STOPPED;
} catch (NamingException nnfe) {
started = STATE_STOPPED;
- } catch (AttributeNotFoundException e) {
+ } catch( OperationsException e ) {
failingException = e;
- } catch (InstanceNotFoundException e) {
- failingException = e;
- } catch (MalformedObjectNameException e) {
- failingException = e;
} catch (MBeanException e) {
failingException = e;
} catch (ReflectionException e) {
@@ -143,6 +130,7 @@
} catch (IOException e) {
failingException = e;
}
+
if( failingException != null ) {
pollingException = new PollingException(failingException.getMessage());
done = true;
@@ -156,82 +144,16 @@
}
Thread.currentThread().setContextClassLoader(currentLoader);
+ JMXClassLoaderRepository.getDefault().removeConcerned(server, this);
}
protected void setCredentials() {
- Exception temp = null;
try {
- ILaunchConfiguration lc = server.getLaunchConfiguration(true,
- new NullProgressMonitor());
- // get user from the IServer, but override with launch configuration
- String user = ServerConverter.getJBossServer(server).getUsername();
-
- // get password from the IServer, but override with launch configuration
- String pass = ServerConverter.getJBossServer(server).getPassword();
-
- // get our methods
- Class simplePrincipal = Thread.currentThread()
- .getContextClassLoader().loadClass(
- "org.jboss.security.SimplePrincipal");
- Class securityAssoc = Thread.currentThread()
- .getContextClassLoader().loadClass(
- "org.jboss.security.SecurityAssociation");
- securityAssoc.getMethods(); // force-init the methods since the
- // class hasn't been initialized yet.
-
- Constructor newSimplePrincipal = simplePrincipal
- .getConstructor(new Class[] { String.class });
- Object newPrincipalInstance = newSimplePrincipal
- .newInstance(new Object[] { user });
-
- // set the principal
- Method setPrincipalMethod = securityAssoc.getMethod(
- "setPrincipal", new Class[] { Principal.class });
- setPrincipalMethod.invoke(null,
- new Object[] { newPrincipalInstance });
-
- // set the credential
- Method setCredentialMethod = securityAssoc.getMethod(
- "setCredential", new Class[] { Object.class });
- setCredentialMethod.invoke(null, new Object[] { pass });
- } catch (CoreException e) {
- temp = e;
- } catch (ClassNotFoundException e) {
- temp = e;
- } catch (SecurityException e) {
- temp = e;
- } catch (NoSuchMethodException e) {
- temp = e;
- } catch (IllegalArgumentException e) {
- temp = e;
- } catch (InstantiationException e) {
- temp = e;
- } catch (IllegalAccessException e) {
- temp = e;
- } catch (InvocationTargetException e) {
- temp = e;
+ JMXUtil.setCredentials(server);
+ } catch( CredentialException ce ) {
+ pollingException = new PollingException(ce.getWrapped().getMessage());
}
- if( temp != null ) {
- pollingException = new PollingException(temp.getMessage());
- }
}
-
- protected ClassLoader getClassLoader() {
- try {
- IRuntime rt = server.getRuntime();
- IPath loc = rt.getLocation();
- URL url = loc.append("client").append("jbossall-client.jar")
- .toFile().toURI().toURL();
- URL url2 = loc.append("bin").append("twiddle.jar").toFile()
- .toURI().toURL();
- URLClassLoader loader = new URLClassLoader(new URL[] { url,
- url2 }, Thread.currentThread().getContextClassLoader());
- return loader;
- } catch (MalformedURLException murle) {
- pollingException = new PollingException(murle.getMessage());
- }
- return null;
- }
}
private void launchJMXPoller() {