KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > tools > ant > ListServiceEnginesTask


1 /**
2  * PETALS: PETALS Services Platform
3  * Copyright (C) 2005 EBM WebSourcing
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 any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): EBM WebSourcing
21  */

22 package org.objectweb.petals.tools.ant;
23
24 import java.util.Arrays JavaDoc;
25 import java.util.Set JavaDoc;
26 import java.util.TreeSet JavaDoc;
27
28 import javax.management.MBeanServerConnection JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import javax.management.remote.JMXConnector JavaDoc;
31
32 import org.apache.tools.ant.BuildException;
33 import org.objectweb.petals.tools.ant.managers.JBIAntTaskAbstract;
34 import org.objectweb.petals.tools.ant.util.JBIJMXConnectorUtil;
35
36 /**
37  * Task used to list the service engines
38  *
39  * @author ddesjardins - eBMWebsourcing
40  */

41 public class ListServiceEnginesTask extends JBIAntTaskAbstract {
42
43     /**
44      * XML namespace
45      */

46     private static final String JavaDoc XMLNS = "http://java.sun.com/xml/ns/jbi/component-info-list";
47
48     /**
49      * List the Engines that have Service Units deployed to them as part of the
50      * Service Assembly deployed in the JBI Environment. Without this parameter,
51      * no Service Assembly dependency is verified.
52      */

53     private String JavaDoc serviceAssemblyName;
54
55     /**
56      * If supplied, only the named service engine is reported. If the engine
57      * doesn’t exist, an empty component-list report is given.
58      */

59     private String JavaDoc serviceEngineName;
60
61     /**
62      * List the Engines that are dependent on the specified shared library
63      * Without this parameter, no shared library dependency is verified.
64      */

65     // private String sharedLibraryName;
66
/**
67      * Service engines state Valid state are : shutdown, stated, stopped
68      */

69     private String JavaDoc state;
70
71     /**
72      * If supplied, set the given Ant property name value to the XML version of
73      * this listing task.
74      */

75     private String JavaDoc xmlOutput;
76
77     public ListServiceEnginesTask() {
78         super();
79     }
80
81     /**
82      * Execute the ant task
83      *
84      * @throws IOException
85      */

86     public void execute() throws BuildException {
87         try {
88             if (state != null
89                     && (!state.equalsIgnoreCase("shutdown")
90                             || !state.equalsIgnoreCase("started") || !state
91                             .equalsIgnoreCase("stopped"))) {
92                 throw new BuildException(
93                         "Valid states are : 'shutdown', 'started', or 'stopped'");
94             }
95             JMXConnector JavaDoc connector = JBIJMXConnectorUtil.getConnection(host,
96                     port, username, password);
97             MBeanServerConnection JavaDoc connection = connector
98                     .getMBeanServerConnection();
99             // Output
100
StringBuffer JavaDoc output = new StringBuffer JavaDoc();
101             output
102                     .append("-------------------------------------------------------\n");
103             output.append("---------------- Service Engines --------------\n");
104             output
105                     .append("-------------------------------------------------------\n");
106             StringBuffer JavaDoc outputXML = new StringBuffer JavaDoc();
107             outputXML.append("<?xml version='1.0' encoding='utf-8'?>\n");
108             outputXML.append("<component-info-list ");
109             outputXML.append("xmlns='" + ListServiceEnginesTask.XMLNS
110                     + "' version='1.0'>\n");
111             // Get the engines component names
112
Object JavaDoc result = connection.getAttribute(JBIJMXConnectorUtil
113                     .getAdminServiceMBeanName(connection), "EngineComponents");
114             Set JavaDoc<String JavaDoc> composForSA = new TreeSet JavaDoc<String JavaDoc>();
115             if (serviceAssemblyName != null) {
116                 // Get getComponentsForDeployedServiceAssembly
117
Object JavaDoc[] objects = new Object JavaDoc[1];
118                 objects[0] = serviceAssemblyName;
119                 String JavaDoc[] sig = new String JavaDoc[1];
120                 sig[0] = "java.lang.String";
121                 Object JavaDoc resultSA = connection
122                         .invoke(JBIJMXConnectorUtil
123                                 .getDeploymentServiceMBeanName(connection),
124                                 "getComponentsForDeployedServiceAssembly",
125                                 objects, sig);
126                 if (resultSA instanceof String JavaDoc[]) {
127                     composForSA.addAll(Arrays.asList((String JavaDoc[]) resultSA));
128                 }
129             }
130             String JavaDoc engineName = serviceEngineName;
131             String JavaDoc engineState = state;
132             if (result instanceof ObjectName JavaDoc[]) {
133                 ObjectName JavaDoc[] engines = (ObjectName JavaDoc[]) result;
134                 for (ObjectName JavaDoc objectName : engines) {
135                     String JavaDoc objState = (String JavaDoc) connection.getAttribute(
136                             objectName, "CurrentState");
137                     String JavaDoc name = objectName.getKeyProperty("name");
138                     // TODO extract the description
139
String JavaDoc description = "";
140                     // Set the variable to have a positive test
141
if (state == null) {
142                         engineState = objState;
143                     }
144                     if (serviceEngineName == null) {
145                         engineName = name;
146                     }
147                     if (serviceAssemblyName == null) {
148                         composForSA.add(name);
149                     }
150                     if (name.equals(engineName) && objState.equals(engineState)
151                             && composForSA.contains(name)) {
152                         // Add the component to the output
153
output.append("Name : " + name + "\n");
154                         output.append("State : " + objState + "\n");
155                         output.append("Description : " + description + "\n\n");
156                         outputXML
157                                 .append("\t<component-info type='service-engine' name='"
158                                         + name
159                                         + "' state='"
160                                         + objState
161                                         + "'>\n");
162                         outputXML.append("\t\t<description>");
163                         outputXML.append(description);
164                         outputXML.append("</description>\n");
165                         outputXML.append("\t</component-info>\n");
166                     }
167                 }
168                 outputXML.append("</component-info-list>");
169                 if (xmlOutput == null) {
170                     // Display the list on the console
171
try {
172                         log(output.toString());
173                     } catch (NullPointerException JavaDoc e) {
174                         // Exception thrown when execute outisde of the ant
175
// context
176
}
177                 } else {
178                     try {
179                         // Fill the property xmlOutput
180
getProject().setNewProperty(xmlOutput,
181                                 outputXML.toString());
182                     } catch (Exception JavaDoc e) {
183                         // Exception thrown if executed outsite of a Ant context
184
}
185                 }
186             }
187             connector.close();
188         } catch (Exception JavaDoc e) {
189             if (Boolean.parseBoolean(failOnError)) {
190                 throw new BuildException(e.getMessage(), e.getCause());
191             }
192         }
193     }
194
195     public void setServiceAssemblyName(String JavaDoc serviceAssemblyName) {
196         this.serviceAssemblyName = serviceAssemblyName;
197     }
198
199     public void setServiceEngineName(String JavaDoc serviceEngineName) {
200         this.serviceEngineName = serviceEngineName;
201     }
202
203     // public void setSharedLibraryName(String sharedLibraryName) {
204
// this.sharedLibraryName = sharedLibraryName;
205
// }
206

207     public void setState(String JavaDoc state) {
208         this.state = state;
209     }
210
211     public void setXmlOutput(String JavaDoc xmlOutput) {
212         this.xmlOutput = xmlOutput;
213     }
214 }
215
Popular Tags