KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > tools > jbiplugin > JMXComponentAbstractMojo


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: PackageAssemblyMojo.java 16:56:43 ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.tools.jbiplugin;
23
24 import javax.management.MBeanServerConnection JavaDoc;
25 import javax.management.ObjectName JavaDoc;
26 import javax.management.remote.JMXConnector JavaDoc;
27
28 import org.objectweb.petals.tools.jbiplugin.util.JBIJMXConnectorUtil;
29
30 /**
31  * This class gathers the action on a service assembly
32  *
33  * @author cdeneux - Capgemini Sud
34  */

35 public abstract class JMXComponentAbstractMojo extends JMXTaskAbstractMojo {
36
37     /**
38      * Action : installSharedLibrary
39      */

40     protected static final String JavaDoc INSTALL_SHARED_LIBRARY = "installSharedLibrary";
41
42     /**
43      * Action : loadNewInstaller
44      */

45     protected static final String JavaDoc LOAD_NEW_INSTALLED = "loadNewInstaller";
46
47     /**
48      * Action : loadNewInstaller
49      */

50     protected static final String JavaDoc UNLOAD_INSTALLER = "unloadInstaller";
51
52     /**
53      * Action : install
54      */

55     protected static final String JavaDoc INSTALL = "install";
56
57     /**
58      * Action : uninstall
59      */

60     protected static final String JavaDoc UNINSTALL = "uninstall";
61
62     /**
63      * Action : shutdown a component
64      */

65     protected static final String JavaDoc SHUTDOWN = "shutDown";
66
67     /**
68      * Action : start a component
69      */

70     protected static final String JavaDoc START = "start";
71
72     /**
73      * Action : stop a component
74      */

75     protected static final String JavaDoc STOP = "stop";
76
77     /**
78      * Get state of a service assembly
79      *
80      * @param componentName
81      * component
82      */

83     protected String JavaDoc getState(String JavaDoc componentName) throws Exception JavaDoc {
84         JMXConnector JavaDoc connector = null;
85         String JavaDoc state = null;
86         try {
87             
88             connector = JBIJMXConnectorUtil.getConnection(host, port, username,
89                     password);
90             MBeanServerConnection JavaDoc connection = connector
91                     .getMBeanServerConnection();
92             ObjectName JavaDoc objectName = JBIJMXConnectorUtil.getComponentMBeanName(
93                     connection, componentName);
94             if (objectName == null) {
95                 // The component is not installed, we look for if an installer exists
96
objectName = JBIJMXConnectorUtil.getInstallerComponentMBeanName(
97                         connection, componentName);
98                 if (objectName == null) {
99                     // No installer of the component exists
100
state = "Unknown";
101                 }
102                 else {
103                     state = "Loaded";
104                 }
105             }
106             else {
107                 state = (String JavaDoc) connection.getAttribute(
108                         objectName, "CurrentState");
109             }
110             
111         } catch (Exception JavaDoc e) {
112             if (failOnError) {
113                 throw e;
114             }
115         } finally {
116             connector.close();
117         }
118         
119         System.out.println("Component: "
120                 + componentName + " is in state: " + state);
121         return state;
122         
123     }
124     
125     /**
126      * Perform an action on the component
127      *
128      * @param action
129      * action to perform
130      */

131     protected void performComponentAction(String JavaDoc action, String JavaDoc componentName)
132             throws Exception JavaDoc {
133         JMXConnector JavaDoc connector = null;
134         try {
135             connector = JBIJMXConnectorUtil.getConnection(host, port, username,
136                     password);
137             MBeanServerConnection JavaDoc connection = connector
138                     .getMBeanServerConnection();
139             Object JavaDoc[] objects = new Object JavaDoc[0];
140             String JavaDoc[] strings = new String JavaDoc[0];
141             ObjectName JavaDoc objectName = JBIJMXConnectorUtil.getComponentMBeanName(
142                     connection, componentName);
143             if (objectName == null) {
144                 throw new Exception JavaDoc("The component is not installed");
145             }
146             connection.invoke(objectName, action, objects, strings);
147             System.out.println(action + " component name : " + componentName);
148
149         } catch (Exception JavaDoc e) {
150             if (failOnError) {
151                 throw e;
152             }
153         } finally {
154             connector.close();
155         }
156     }
157
158     /**
159      * Perform an action on the component installer
160      *
161      * @param action
162      * action to perform (install, uninstall)
163      */

164     protected void performInstallerComponentAction(String JavaDoc action, String JavaDoc componentName)
165             throws Exception JavaDoc {
166         JMXConnector JavaDoc connector = null;
167         try {
168             connector = JBIJMXConnectorUtil.getConnection(host, port, username,
169                     password);
170             MBeanServerConnection JavaDoc connection = connector
171                     .getMBeanServerConnection();
172             Object JavaDoc[] objects = new Object JavaDoc[0];
173             String JavaDoc[] strings = new String JavaDoc[0];
174             ObjectName JavaDoc objectName = JBIJMXConnectorUtil.getInstallerComponentMBeanName(
175                     connection, componentName);
176             if (objectName == null) {
177                 throw new Exception JavaDoc("The component has no installer");
178             }
179             connection.invoke(objectName, action, objects, strings);
180             System.out.println(action + " component name : " + componentName);
181
182         } catch (Exception JavaDoc e) {
183             if (failOnError) {
184                 throw e;
185             }
186         } finally {
187             connector.close();
188         }
189     }
190
191     /**
192      * Perform an action on the Installation service
193      *
194      * @param action
195      * @return
196      * @throws Exception
197      */

198     protected void performInstallationAction(String JavaDoc action, String JavaDoc componentName) throws Exception JavaDoc {
199
200         JMXConnector JavaDoc connector = null;
201         try {
202             connector = JBIJMXConnectorUtil.getConnection(host, port, username,
203                     password);
204             MBeanServerConnection JavaDoc connection = connector
205                     .getMBeanServerConnection();
206             Object JavaDoc[] objectsLoad = new Object JavaDoc[1];
207             objectsLoad[0] = componentName;
208             String JavaDoc[] stringsLoad = new String JavaDoc[1];
209             stringsLoad[0] = "java.lang.String";
210             connection.invoke(JBIJMXConnectorUtil
211                     .getInstallationServiceMBeanName(connection),
212                     action, objectsLoad,
213                     stringsLoad);
214             System.out.println(action + " component name : " + componentName);
215         
216         } catch (Exception JavaDoc e) {
217             if (failOnError) {
218                 throw e;
219             }
220         } finally {
221             connector.close();
222         }
223     }
224
225     /**
226      * Perform an uninstall on the component
227      *
228      */

229     protected void performUnload(String JavaDoc componentName) throws Exception JavaDoc {
230         JMXConnector JavaDoc connector = null;
231         try {
232             connector = JBIJMXConnectorUtil.getConnection(host, port, username,
233                     password);
234             MBeanServerConnection JavaDoc connection = connector
235                     .getMBeanServerConnection();
236             Object JavaDoc[] objects = new Object JavaDoc[2];
237             objects[0] = componentName;
238             objects[1] = true;
239             String JavaDoc[] strings = new String JavaDoc[2];
240             strings[0] = "java.lang.String";
241             strings[1] = "boolean";
242             // Unload the installer
243
connection.invoke(JBIJMXConnectorUtil
244                     .getInstallationServiceMBeanName(connection),
245                     JMXComponentAbstractMojo.UNLOAD_INSTALLER, objects, strings);
246             System.out.println("Unload component name : " + componentName);
247         } catch (Exception JavaDoc e) {
248             if (failOnError) {
249                 throw e;
250             }
251         } finally {
252             connector.close();
253         }
254     }
255 }
256
Popular Tags