KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > mavenplugins > geronimo > ServerProxy


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

19
20 package org.apache.geronimo.mavenplugins.geronimo;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 import javax.management.remote.JMXServiceURL JavaDoc;
29 import javax.management.remote.JMXConnector JavaDoc;
30 import javax.management.remote.JMXConnectorFactory JavaDoc;
31 import javax.management.MBeanServerConnection JavaDoc;
32
33 //
34
// FIXME: It should be possible to query state with-out any Geronimo classes,
35
// just using JMX interfaces.
36
//
37

38 import org.apache.geronimo.gbean.AbstractName;
39 import org.apache.geronimo.gbean.AbstractNameQuery;
40 import org.apache.geronimo.kernel.Kernel;
41 import org.apache.geronimo.kernel.config.PersistentConfigurationList;
42
43 import org.apache.commons.logging.Log;
44 import org.apache.commons.logging.LogFactory;
45
46 /**
47  * Helper to communicate with a remote server via JMX.
48  *
49  * @version $Rev: 476061 $ $Date: 2006-11-17 01:36:50 -0500 (Fri, 17 Nov 2006) $
50  */

51 public class ServerProxy
52 {
53     private static final Log log = LogFactory.getLog(ServerProxy.class);
54
55     private JMXServiceURL JavaDoc url;
56
57     private Map JavaDoc environment;
58
59     private MBeanServerConnection JavaDoc mbeanConnection;
60
61     private Throwable JavaDoc lastError;
62
63     public ServerProxy(final JMXServiceURL JavaDoc url, final Map JavaDoc environment) throws Exception JavaDoc {
64         init(url, environment);
65     }
66
67     public ServerProxy(final String JavaDoc hostname, final int port, final String JavaDoc username, final String JavaDoc password) throws Exception JavaDoc {
68         //
69
// TODO: Check if this is the right way to build up the URI
70
//
71

72         this("service:jmx:rmi://" + hostname + "/jndi/rmi://" + hostname + ":" + port + "/JMXConnector", username, password);
73     }
74
75     public ServerProxy(final String JavaDoc url, final String JavaDoc username, final String JavaDoc password) throws Exception JavaDoc {
76         assert url != null;
77         assert username != null;
78         assert password != null;
79         
80         Map JavaDoc env = new HashMap JavaDoc();
81         env.put("jmx.remote.credentials", new String JavaDoc[] {username, password});
82
83         init(new JMXServiceURL JavaDoc(url), env);
84     }
85
86     private void init(final JMXServiceURL JavaDoc url, final Map JavaDoc environment) throws Exception JavaDoc {
87         assert url != null;
88         assert environment != null;
89         
90         this.url = url;
91         this.environment = new HashMap JavaDoc();
92         this.environment.put("jmx.remote.credentials", new String JavaDoc[] {"system", "manager"});
93
94         log.debug("Initialized with URL: " + url + ", environment: " + environment);
95     }
96
97     private MBeanServerConnection JavaDoc getConnection() throws IOException JavaDoc {
98         if (this.mbeanConnection == null) {
99             log.debug("Connecting to: " + url);
100             
101             JMXConnector JavaDoc connector = JMXConnectorFactory.connect(url, environment);
102             this.mbeanConnection = connector.getMBeanServerConnection();
103             
104             log.debug("Connected");
105         }
106
107         return mbeanConnection;
108     }
109
110     public boolean isFullyStarted() {
111         boolean fullyStarted = true;
112
113         try {
114             AbstractNameQuery query = new AbstractNameQuery(PersistentConfigurationList.class.getName());
115             Set JavaDoc result = listGBeans(query);
116             Iterator JavaDoc iter = result.iterator();
117             while (iter.hasNext()) {
118                 AbstractName name = (AbstractName)iter.next();
119                 boolean started = getBooleanAttribute(name, "kernelFullyStarted");
120                 if (!started) {
121                     fullyStarted = false;
122                     break;
123                 }
124             }
125         }
126         catch (IOException JavaDoc e) {
127             log.debug("Connection failure; ignoring", e);
128             fullyStarted = false;
129             lastError = e;
130         }
131         catch (Exception JavaDoc e) {
132             log.debug("Unable to determine if the server is fully started", e);
133             fullyStarted = false;
134             lastError = e;
135         }
136         
137         return fullyStarted;
138     }
139
140     public Throwable JavaDoc getLastError() {
141         return lastError;
142     }
143
144     public void shutdown() {
145         try {
146             invoke("shutdown");
147         }
148         catch (Exception JavaDoc e) {
149             log.warn("Unable to shutdown the server", e);
150             lastError = e;
151         }
152     }
153
154     //
155
// Kernel invocation helpers
156
//
157

158     private Object JavaDoc invoke(final String JavaDoc operation, final Object JavaDoc[] args, final String JavaDoc[] signature) throws Exception JavaDoc {
159         assert operation != null;
160         assert args != null;
161         assert signature != null;
162
163         return getConnection().invoke(Kernel.KERNEL, operation, args, signature);
164     }
165
166     private Object JavaDoc invoke(final String JavaDoc operation, final Object JavaDoc[] args) throws Exception JavaDoc {
167         assert args != null;
168
169         String JavaDoc[] signature = new String JavaDoc[args.length];
170         for (int i=0; i<args.length; i++) {
171             signature[i] = args[i].getClass().getName();
172         }
173
174         return invoke(operation, args, signature);
175     }
176
177     private Object JavaDoc invoke(final String JavaDoc operation) throws Exception JavaDoc {
178         return invoke(operation, new Object JavaDoc[0]);
179     }
180
181     private Set JavaDoc listGBeans(final AbstractNameQuery query) throws Exception JavaDoc {
182         return (Set JavaDoc)invoke("listGBeans", new Object JavaDoc[] { query });
183     }
184
185     private Object JavaDoc getAttribute(final AbstractName name, final String JavaDoc attribute) throws Exception JavaDoc {
186         assert name != null;
187         assert attribute != null;
188
189         return invoke("getAttribute", new Object JavaDoc[] { name, attribute });
190     }
191
192     private boolean getBooleanAttribute(final AbstractName name, final String JavaDoc attribute) throws Exception JavaDoc {
193         Object JavaDoc obj = getAttribute(name, attribute);
194         if (obj instanceof Boolean JavaDoc) {
195             return ((Boolean JavaDoc)obj).booleanValue();
196         }
197         else {
198             throw new RuntimeException JavaDoc("Attribute is not of type Boolean: " + attribute);
199         }
200     }
201 }
202
Popular Tags