KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > management > task > JbiTask


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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.servicemix.jbi.management.task;
18
19 import java.io.IOException JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.jbi.management.DeploymentServiceMBean;
25 import javax.management.MBeanServerInvocationHandler JavaDoc;
26 import javax.management.ObjectName JavaDoc;
27 import javax.management.remote.JMXConnector JavaDoc;
28 import javax.management.remote.JMXConnectorFactory JavaDoc;
29 import javax.management.remote.JMXServiceURL JavaDoc;
30
31 import org.apache.servicemix.jbi.container.JBIContainer;
32 import org.apache.servicemix.jbi.framework.AdminCommandsServiceMBean;
33 import org.apache.servicemix.jbi.framework.DeploymentService;
34 import org.apache.servicemix.jbi.management.ManagementContext;
35 import org.apache.tools.ant.BuildException;
36 import org.apache.tools.ant.Project;
37 import org.apache.tools.ant.Task;
38
39 /**
40  * A bean for connecting to a remote JMX MBeanServer
41  *
42  * @version $Revision: 426415 $
43  */

44 public abstract class JbiTask extends Task {
45     
46     private String JavaDoc serverProtocol = "rmi";
47     private String JavaDoc host = "localhost";
48     private String JavaDoc containerName = JBIContainer.DEFAULT_NAME;
49     private String JavaDoc jmxDomainName = ManagementContext.DEFAULT_DOMAIN;
50     private int port = ManagementContext.DEFAULT_CONNECTOR_PORT;
51     private String JavaDoc jndiPath = ManagementContext.DEFAULT_CONNECTOR_PATH;
52     private String JavaDoc username;
53     private String JavaDoc password;
54     private boolean failOnError = true;
55     private JMXConnector JavaDoc jmxConnector;
56     
57     
58     /**
59      * Get the JMXServiceURL - built from the protocol used and host names
60      * @return the url
61      */

62     public JMXServiceURL JavaDoc getServiceURL() throws MalformedURLException JavaDoc {
63         JMXServiceURL JavaDoc url = null;
64         url = new JMXServiceURL JavaDoc("service:jmx:rmi:///jndi/rmi://"
65                 + host + ":" + port + jndiPath);
66         return url;
67     }
68     
69     /**
70      * Get a JMXConnector from a url
71      * @param url
72      * @return the JMXConnector
73      * @throws IOException
74      */

75     public JMXConnector JavaDoc getJMXConnector (JMXServiceURL JavaDoc url) throws IOException JavaDoc {
76         String JavaDoc[] credentials = new String JavaDoc[] { getUsername(), getPassword() };
77         Map JavaDoc environment = new HashMap JavaDoc();
78         environment.put(JMXConnector.CREDENTIALS, credentials);
79         return JMXConnectorFactory.connect(url, environment);
80     }
81     
82     /**
83      * initialize the connection
84      * @throws BuildException
85      */

86     public void connect() throws IOException JavaDoc {
87         this.jmxConnector = getJMXConnector(getServiceURL());
88     }
89     
90     
91     /**
92      * close any internal remote connections
93      *
94      */

95     public void close(){
96         if (this.jmxConnector != null){
97             try {
98                 jmxConnector.close();
99             }
100             catch (IOException JavaDoc e) {
101                 log("Caught an error closing the jmxConnector" + e.getMessage(), Project.MSG_WARN);
102             }
103         }
104     }
105     
106     
107     /**
108      * Get a servicemix internal system management instance, from it's class name
109      * @param systemClass
110      * @return the object name
111      */

112     protected ObjectName JavaDoc getObjectName (Class JavaDoc systemClass){
113         return ManagementContext.getSystemObjectName(jmxDomainName, containerName, systemClass);
114     }
115     
116         
117     /**
118      * Get the AdminCommandsService
119      * @return the main administration service MBean
120      * @throws IOException
121      */

122     public AdminCommandsServiceMBean getAdminCommandsService() throws IOException JavaDoc{
123         ObjectName JavaDoc objectName = getObjectName(AdminCommandsServiceMBean.class);
124         
125         return (AdminCommandsServiceMBean) MBeanServerInvocationHandler.newProxyInstance(jmxConnector.getMBeanServerConnection(), objectName,
126                 AdminCommandsServiceMBean.class, true);
127     }
128     
129     /**
130      * Get the DeploymentServiceMBean
131      * @return the deployment service mbean
132      * @throws IOException
133      */

134     public DeploymentServiceMBean getDeploymentService() throws IOException JavaDoc{
135         ObjectName JavaDoc objectName = getObjectName(DeploymentService.class);
136         
137         return (DeploymentServiceMBean) MBeanServerInvocationHandler.newProxyInstance(jmxConnector.getMBeanServerConnection(), objectName,
138                 DeploymentServiceMBean.class, true);
139     }
140     
141     
142     /**
143      * @return Returns the containerName.
144      */

145     public String JavaDoc getContainerName() {
146         return containerName;
147     }
148     /**
149      * @param containerName The containerName to set.
150      */

151     public void setContainerName(String JavaDoc containerName) {
152         this.containerName = containerName;
153     }
154     /**
155      * @return Returns the jmxDomainName.
156      */

157     public String JavaDoc getJmxDomainName() {
158         return jmxDomainName;
159     }
160     /**
161      * @param jmxDomainName The jmxDomainName to set.
162      */

163     public void setJmxDomainName(String JavaDoc jmxDomainName) {
164         this.jmxDomainName = jmxDomainName;
165     }
166     /**
167      * @return Returns the jndiPath.
168      */

169     public String JavaDoc getJndiPath() {
170         return jndiPath;
171     }
172     /**
173      * @param jndiPath The jndiPath to set.
174      */

175     public void setJndiPath(String JavaDoc jndiPath) {
176         this.jndiPath = jndiPath;
177     }
178     /**
179      * @return Returns the namingHost.
180      */

181     public String JavaDoc getHost() {
182         return host;
183     }
184     /**
185      * @param host The namingHost to set.
186      */

187     public void setHost(String JavaDoc host) {
188         this.host = host;
189     }
190     /**
191      * @return Returns the namingPort.
192      */

193     public int getPort() {
194         return port;
195     }
196     /**
197      * @param port The namingPort to set.
198      */

199     public void setPort(int port) {
200         this.port = port;
201     }
202     
203     /**
204      * @return Returns the serverProtocol.
205      */

206     public String JavaDoc getServerProtocol() {
207         return serverProtocol;
208     }
209     /**
210      * @param serverProtocol The serverProtocol to set.
211      */

212     public void setServerProtocol(String JavaDoc serverProtocol) {
213         this.serverProtocol = serverProtocol;
214     }
215
216     /**
217      * @return Returns the passwd.
218      */

219     public String JavaDoc getPassword() {
220         return password;
221     }
222
223     /**
224      * @param password The passwd to set.
225      */

226     public void setPassword(String JavaDoc password) {
227         this.password = password;
228     }
229
230     /**
231      * @return Returns the username.
232      */

233     public String JavaDoc getUsername() {
234         return username;
235     }
236
237     /**
238      * @param username The username to set.
239      */

240     public void setUsername(String JavaDoc username) {
241         this.username = username;
242     }
243     
244     /**
245      * @return Returns the failOnError.
246      */

247     public boolean isFailOnError() {
248         return failOnError;
249     }
250
251     /**
252      * @param failOnError The failOnError to set.
253      */

254     public void setFailOnError(boolean failOnError) {
255         this.failOnError = failOnError;
256     }
257     
258     /**
259      * execute the task
260      *
261      * @throws BuildException
262      */

263     public void execute() throws BuildException {
264         AdminCommandsServiceMBean acs;
265         try {
266             log("Retrieving remote admin interface", Project.MSG_DEBUG);
267             connect();
268             acs = getAdminCommandsService();
269         } catch (Throwable JavaDoc e) {
270             log("Error accessing ServiceMix administration: " + e.getMessage(), Project.MSG_WARN);
271             if (isFailOnError()) {
272                 throw new BuildException("Error accessing ServiceMix administration", e);
273             } else {
274                 return;
275             }
276         }
277         try {
278             log("Executing command", Project.MSG_DEBUG);
279             doExecute(acs);
280         } catch (Throwable JavaDoc e) {
281             log("Error executing command: " + e.getMessage(), Project.MSG_WARN);
282             if (isFailOnError()) {
283                 throw new BuildException("Error accessing ServiceMix administration", e);
284             } else {
285                 return;
286             }
287         }
288     }
289     
290     protected abstract void doExecute(AdminCommandsServiceMBean acs) throws Exception JavaDoc;
291
292 }
Popular Tags