KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > mavenplugin > WaitForStarted


1 /**
2  *
3  * Copyright 2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.deployment.mavenplugin;
18
19 import java.net.URI JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22 import javax.management.MBeanServerConnection JavaDoc;
23 import javax.management.ObjectName JavaDoc;
24 import javax.management.remote.JMXConnector JavaDoc;
25 import javax.management.remote.JMXConnectorFactory JavaDoc;
26 import javax.management.remote.JMXServiceURL JavaDoc;
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 JavaDoc mbServerConnection;
42     private Kernel kernel;
43     private String JavaDoc id;
44
45     public String JavaDoc getId() {
46         return id;
47     }
48
49     public void setId(String JavaDoc 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 JavaDoc {
62         String JavaDoc uri = getUri().substring(DeploymentFactoryImpl.URI_PREFIX.length());
63         if (!uri.startsWith("jmx")) {
64             throw new Exception JavaDoc("bad uri");
65         }
66
67         Map JavaDoc environment = new HashMap JavaDoc();
68         String JavaDoc[] credentials = new String JavaDoc[]{getUsername(), getPassword()};
69         environment.put(JMXConnector.CREDENTIALS, credentials);
70
71         JMXServiceURL JavaDoc address = new JMXServiceURL JavaDoc("service:" + uri);
72         ClassLoader JavaDoc oldcl = Thread.currentThread().getContextClassLoader();
73         try {
74             Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
75             for (int tries = maxTries; true; tries--) {
76                 try {
77                     JMXConnector JavaDoc jmxConnector = JMXConnectorFactory.connect(address, environment);
78                     mbServerConnection = jmxConnector.getMBeanServerConnection();
79                     kernel = new KernelDelegate(mbServerConnection);
80                     break;
81                 } catch (Exception JavaDoc e) {
82                     if (tries == 0) {
83                         throw new Exception JavaDoc("Could not connect");
84                     }
85                     Thread.sleep(retryIntervalMilliseconds);
86                 }
87             }
88         } finally {
89             Thread.currentThread().setContextClassLoader(oldcl);
90         }
91         ObjectName JavaDoc configName = Configuration.getConfigurationObjectName(new URI JavaDoc(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                 //hasn't been loaded yet, keep trying
100
} catch (GBeanNotFoundException e) {
101                 //hasn't been loaded yet, keep trying
102
}
103             Thread.sleep(retryIntervalMilliseconds);
104         }
105         throw new Exception JavaDoc("Configuration is not yet started");
106     }
107
108 }
109
Popular Tags