1 17 package org.apache.geronimo.deployment.mavenplugin; 18 19 import java.net.URI ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 import javax.management.MBeanServerConnection ; 23 import javax.management.ObjectName ; 24 import javax.management.remote.JMXConnector ; 25 import javax.management.remote.JMXConnectorFactory ; 26 import javax.management.remote.JMXServiceURL ; 27 28 import org.apache.geronimo.deployment.plugin.factories.DeploymentFactoryImpl; 29 import org.apache.geronimo.kernel.jmx.KernelDelegate; 30 import org.apache.geronimo.kernel.management.State; 31 import org.apache.geronimo.kernel.config.Configuration; 32 import org.apache.geronimo.kernel.Kernel; 33 import org.apache.geronimo.kernel.GBeanNotFoundException; 34 import org.apache.geronimo.kernel.InternalKernelException; 35 36 public class WaitForStarted extends AbstractModuleCommand { 37 38 private int maxTries = 40; 39 private int retryIntervalMilliseconds = 1000; 40 41 private MBeanServerConnection mbServerConnection; 42 private Kernel kernel; 43 private String id; 44 45 public String getId() { 46 return id; 47 } 48 49 public void setId(String id) { 50 this.id = id; 51 } 52 53 public void setMaxTries(int maxTries) { 54 this.maxTries = maxTries; 55 } 56 57 public void setRetryIntervalMilliseconds(int retryIntervalMilliseconds) { 58 this.retryIntervalMilliseconds = retryIntervalMilliseconds; 59 } 60 61 public void execute() throws Exception { 62 String uri = getUri().substring(DeploymentFactoryImpl.URI_PREFIX.length()); 63 if (!uri.startsWith("jmx")) { 64 throw new Exception ("bad uri"); 65 } 66 67 Map environment = new HashMap (); 68 String [] credentials = new String []{getUsername(), getPassword()}; 69 environment.put(JMXConnector.CREDENTIALS, credentials); 70 71 JMXServiceURL address = new JMXServiceURL ("service:" + uri); 72 ClassLoader oldcl = Thread.currentThread().getContextClassLoader(); 73 try { 74 Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader()); 75 for (int tries = maxTries; true; tries--) { 76 try { 77 JMXConnector jmxConnector = JMXConnectorFactory.connect(address, environment); 78 mbServerConnection = jmxConnector.getMBeanServerConnection(); 79 kernel = new KernelDelegate(mbServerConnection); 80 break; 81 } catch (Exception e) { 82 if (tries == 0) { 83 throw new Exception ("Could not connect"); 84 } 85 Thread.sleep(retryIntervalMilliseconds); 86 } 87 } 88 } finally { 89 Thread.currentThread().setContextClassLoader(oldcl); 90 } 91 ObjectName configName = Configuration.getConfigurationObjectName(new URI (getId())); 92 for (int tries = maxTries; tries > 0; tries--) { 93 try { 94 int state = kernel.getGBeanState(configName); 95 if (state == State.RUNNING_INDEX) { 96 return; 97 } 98 } catch (InternalKernelException e) { 99 } catch (GBeanNotFoundException e) { 101 } 103 Thread.sleep(retryIntervalMilliseconds); 104 } 105 throw new Exception ("Configuration is not yet started"); 106 } 107 108 } 109 | Popular Tags |