KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > plugin > packaging > JMXDistributor


1 /**
2  *
3  * Copyright 2005 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.plugin.packaging;
18
19 import java.io.IOException JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25 import javax.management.MBeanServerConnection 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.geronimo.kernel.config.NoSuchStoreException;
32
33 /**
34  * A Distributor that will distribute packages to a server using JMX remoting.
35  * Currently this is limited to a server on the same machine (because the
36  * location of the artifact is passed using a file: URL).
37  *
38  * @version $Rev: 159776 $ $Date: 2005-04-02 09:12:22 -0800 (Sat, 02 Apr 2005) $
39  */

40 public class JMXDistributor extends AbstractDistributor {
41     public void execute() throws Exception JavaDoc {
42         if (!getArtifact().canRead()) {
43             throw new IOException JavaDoc("Unable to read artifact " + getArtifact());
44         }
45
46         Map JavaDoc environment = new HashMap JavaDoc();
47         environment.put(JMXConnector.CREDENTIALS, new String JavaDoc[]{getUser(), getPassword()});
48
49         JMXServiceURL JavaDoc serviceURL = new JMXServiceURL JavaDoc(getUrl());
50         JMXConnector JavaDoc jmxConnector = JMXConnectorFactory.connect(serviceURL, environment);
51         try {
52             MBeanServerConnection JavaDoc mbServer = jmxConnector.getMBeanServerConnection();
53
54             ObjectName JavaDoc configStore = locateConfigStore(mbServer, storeName);
55             mbServer.invoke(configStore, "install", new Object JavaDoc[]{getArtifact().toURL()}, new String JavaDoc[]{URL JavaDoc.class.getName()});
56             System.out.println("Distributed " + getArtifact() + " to " + getUrl());
57         } finally {
58             jmxConnector.close();
59         }
60     }
61
62     private ObjectName JavaDoc locateConfigStore(MBeanServerConnection JavaDoc mbServer, ObjectName JavaDoc storeName) throws Exception JavaDoc {
63         Set JavaDoc set = mbServer.queryNames(storeName, null);
64         Iterator JavaDoc i = set.iterator();
65         if (!i.hasNext()) {
66             throw new NoSuchStoreException("No ConfigurationStore found matching " + storeName);
67         }
68         ObjectName JavaDoc configStore = (ObjectName JavaDoc) i.next();
69         if (i.hasNext()) {
70             throw new NoSuchStoreException("Multiple ConfigurationStores found matching " + storeName);
71         }
72         return configStore;
73     }
74 }
75
Popular Tags