KickJava   Java API By Example, From Geeks To Geeks.

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


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.jbi.management.LifeCycleMBean;
25 import javax.management.MBeanException JavaDoc;
26 import javax.management.MBeanServerConnection JavaDoc;
27 import javax.management.remote.JMXConnector JavaDoc;
28
29 import org.objectweb.petals.tools.jbiplugin.util.JBIJMXConnectorUtil;
30
31 /**
32  * This class gathers the action on a service assembly
33  *
34  * @author cdeneux - Capgemini Sud
35  */

36 public abstract class JMXServiceAssemblyAbstractMojo extends JMXTaskAbstractMojo {
37
38     /**
39      * Action : shutdown a service assembly
40      */

41     protected static final String JavaDoc SHUTDOWN = "shutDown";
42
43     /**
44      * Action : start a service assembly
45      */

46     protected static final String JavaDoc START = "start";
47
48     /**
49      * Action : stop a service assembly
50      */

51     protected static final String JavaDoc STOP = "stop";
52
53     /**
54      * Action : deploy a service assembly
55      */

56     protected static final String JavaDoc DEPLOY = "deploy";
57
58     /**
59      * Action : undeploy a service assembly
60      */

61     protected static final String JavaDoc UNDEPLOY = "undeploy";
62
63     /**
64      * Action : get state of a component
65      */

66     protected static final String JavaDoc GET_STATE = "getState";
67
68     /**
69      * Get state of a service assembly
70      *
71      * @param serviceAssemblyName
72      * service assembly
73      */

74     protected String JavaDoc getState(String JavaDoc serviceAssemblyName) throws Exception JavaDoc {
75         JMXConnector JavaDoc connector = null;
76         String JavaDoc state = null;
77         try {
78             connector = JBIJMXConnectorUtil.getConnection(host, port, username,
79                     password);
80             MBeanServerConnection JavaDoc connection = connector
81                     .getMBeanServerConnection();
82             Object JavaDoc[] objects = new Object JavaDoc[1];
83             objects[0] = serviceAssemblyName;
84             String JavaDoc[] strings = new String JavaDoc[1];
85             strings[0] = "java.lang.String";
86             Object JavaDoc result = connection.invoke(JBIJMXConnectorUtil
87                     .getDeploymentServiceMBeanName(connection),
88                     JMXServiceAssemblyAbstractMojo.GET_STATE, objects,
89                     strings);
90             if (result instanceof String JavaDoc) {
91                 state = (String JavaDoc)result;
92             } else if (result instanceof Exception JavaDoc) {
93                 state = LifeCycleMBean.UNKNOWN;
94             }
95             
96         } catch (MBeanException JavaDoc e) {
97             state = LifeCycleMBean.UNKNOWN;
98             
99         } catch (Exception JavaDoc e) {
100             if (failOnError) {
101                 throw e;
102             }
103         } finally {
104             connector.close();
105         }
106         
107         System.out.println("Service Assembly: "
108                 + serviceAssemblyName + " is in state: " + state);
109         return state;
110         
111     }
112     
113     /**
114      * Perform an action on a service assembly
115      * @param action
116      */

117     protected void performAction(String JavaDoc action, String JavaDoc serviceAssembly) throws Exception JavaDoc {
118         try {
119             if (verbose) {
120                 System.out.println("Start operation [" + action + "] on JBI Service Assembly archive: "
121                     + serviceAssembly);
122             }
123             
124             JMXConnector JavaDoc connector = JBIJMXConnectorUtil.getConnection(host,
125                     port, username, password);
126             MBeanServerConnection JavaDoc connection = connector
127                     .getMBeanServerConnection();
128             Object JavaDoc[] objects = new Object JavaDoc[1];
129             objects[0] = serviceAssembly;
130             String JavaDoc[] strings = new String JavaDoc[1];
131             strings[0] = "java.lang.String";
132             Object JavaDoc result = connection.invoke(JBIJMXConnectorUtil
133                     .getDeploymentServiceMBeanName(connection), action,
134                     objects, strings);
135             if (result instanceof String JavaDoc) {
136                 // Extract the service assembly name
137
String JavaDoc xml = (String JavaDoc) result;
138                 if (xml.indexOf("Successfully") > -1) {
139                     String JavaDoc saName = "";
140                     saName = xml.substring(xml.indexOf("<loc-param>") + 11, xml
141                             .indexOf("</loc-param>"));
142                     System.out.println("Operation succeeded on service assembly named : " + saName);
143                 } else {
144                     System.out.println("Problem during the " + action + " :\n"
145                                 + (String JavaDoc) result);
146                 }
147             }
148             connector.close();
149         } catch (Exception JavaDoc e) {
150             if (failOnError) {
151                 throw e;
152             }
153         }
154     }
155 }
156
Popular Tags