KickJava   Java API By Example, From Geeks To Geeks.

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


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.remote.JMXConnector JavaDoc;
30
31 import org.apache.tools.ant.BuildException;
32 import org.objectweb.petals.tools.ant.managers.JBIAntTaskAbstract;
33 import org.objectweb.petals.tools.ant.util.JBIJMXConnectorUtil;
34
35 /**
36  * Task used to list the service assemblies
37  *
38  * @author ddesjardins - eBMWebsourcing
39  */

40 public class ListServiceAssembliesTask extends JBIAntTaskAbstract {
41
42     /**
43      * XML namespace
44      */

45     private static final String JavaDoc XMLNS = "http://java.sun.com/xml/ns/jbi/component-info-list";
46
47     /**
48      * List the service assemblies that has service units deployed to this
49      * component. Without this parameter, all the service assemblies that are
50      * deployed in the JBI Environment will be displayed.
51      */

52     private String JavaDoc componentName;
53
54     /**
55      * List the Bindings that have Service Units deployed to them as part of the
56      * Service Assembly deployed in the JBI Environment. Without this parameter,
57      * no Service Assembly dependency is verified.
58      */

59     private String JavaDoc serviceAssemblyName;
60
61     /**
62      * Binding component state Valid state are : shutdown, stated, stopped
63      */

64     private String JavaDoc state;
65
66     /**
67      * If supplied, set the given Ant property name value to the XML version of
68      * this listing task.
69      */

70     private String JavaDoc xmlOutput;
71
72     public ListServiceAssembliesTask() {
73         super();
74     }
75
76     /**
77      * Execute the ant task
78      *
79      * @throws IOException
80      */

81     public void execute() throws BuildException {
82         try {
83             if (state != null
84                     && (!state.equalsIgnoreCase("shutdown")
85                             || !state.equalsIgnoreCase("started") || !state
86                             .equalsIgnoreCase("stopped"))) {
87                 throw new BuildException(
88                         "Valid states are : 'shutdown', 'started', or 'stopped'");
89             }
90             JMXConnector JavaDoc connector = JBIJMXConnectorUtil.getConnection(host,
91                     port, username, password);
92             MBeanServerConnection JavaDoc connection = connector
93                     .getMBeanServerConnection();
94             // Output
95
StringBuffer JavaDoc output = new StringBuffer JavaDoc();
96             output
97                     .append("-------------------------------------------------------\n");
98             output.append("------------- Service Assemblies-- ----------\n");
99             output
100                     .append("-------------------------------------------------------\n");
101             StringBuffer JavaDoc outputXML = new StringBuffer JavaDoc();
102             outputXML.append("<?xml version='1.0' encoding='utf-8'?>\n");
103             outputXML.append("<component-info-list ");
104             outputXML.append("xmlns='" + ListServiceAssembliesTask.XMLNS
105                     + "' version='1.0'>\n");
106             // Get the engines component names
107
Object JavaDoc result = connection.getAttribute(JBIJMXConnectorUtil
108                     .getDeploymentServiceMBeanName(connection),
109                     "DeployedServiceAssemblies");
110             Set JavaDoc<String JavaDoc> saForCompo = new TreeSet JavaDoc<String JavaDoc>();
111             if (componentName != null) {
112                 // Get getComponentsForDeployedServiceAssembly
113
Object JavaDoc[] objects = new Object JavaDoc[1];
114                 objects[0] = componentName;
115                 String JavaDoc[] sig = new String JavaDoc[1];
116                 sig[0] = "java.lang.String";
117                 Object JavaDoc resultSA = connection.invoke(JBIJMXConnectorUtil
118                         .getDeploymentServiceMBeanName(connection),
119                         "getDeployedServiceAssembliesForComponent", objects,
120                         sig);
121                 if (resultSA instanceof String JavaDoc[]) {
122                     saForCompo.addAll(Arrays.asList((String JavaDoc[]) result));
123                 }
124             }
125             String JavaDoc saName = serviceAssemblyName;
126             String JavaDoc saState = state;
127             if (result instanceof String JavaDoc[]) {
128                 String JavaDoc[] assemblies = (String JavaDoc[]) result;
129                 for (String JavaDoc objectName : assemblies) {
130                     Object JavaDoc[] objects = new Object JavaDoc[1];
131                     objects[0] = objectName;
132                     String JavaDoc[] sig = new String JavaDoc[1];
133                     sig[0] = "java.lang.String";
134                     String JavaDoc objState = (String JavaDoc) connection.invoke(
135                             JBIJMXConnectorUtil
136                                     .getDeploymentServiceMBeanName(connection),
137                             "getState", objects, sig);
138                     String JavaDoc name = objectName;
139                     // TODO extract the description
140
String JavaDoc description = "";
141                     // Set the variable to have a positive test
142
if (state == null) {
143                         saState = objState;
144                     }
145                     if (serviceAssemblyName == null) {
146                         saName = name;
147                     }
148                     if (componentName == null) {
149                         saForCompo.add(name);
150                     }
151                     if (name.equals(saName) && objState.equals(saState)
152                             && saForCompo.contains(name)) {
153                         // Get the component for the service assembly
154
objects = new Object JavaDoc[1];
155                         objects[0] = objectName;
156                         sig = new String JavaDoc[1];
157                         sig[0] = "java.lang.String";
158                         Set JavaDoc<String JavaDoc> composForSA = new TreeSet JavaDoc<String JavaDoc>(
159                                 Arrays
160                                         .asList((String JavaDoc[]) connection
161                                                 .invoke(
162                                                         JBIJMXConnectorUtil
163                                                                 .getDeploymentServiceMBeanName(connection),
164                                                         "getComponentsForDeployedServiceAssembly",
165                                                         objects, sig)));
166                         for (String JavaDoc compoName : composForSA) {
167                             objects = new Object JavaDoc[1];
168                             objects[0] = compoName;
169                             sig = new String JavaDoc[1];
170                             sig[0] = "java.lang.String";
171                             // Set<String> serviceUnits = new TreeSet<String>(
172
// Arrays
173
// .asList((String[]) connection
174
// .invoke(
175
// JBIJMXConnectorUtil
176
// .getDeploymentServiceMBeanName(connection),
177
// "getDeployedServiceUnitList",
178
// objects, sig)));
179
// // TODO get the service unit to the output
180
}
181                         // Add the component to the output
182
output.append("Name : " + name + "\n");
183                         output.append("State : " + objState + "\n");
184                         output.append("Description : " + description + "\n\n");
185                         outputXML
186                                 .append("\t<service-assembly-info type='service-assembly' name='"
187                                         + name
188                                         + "' state='"
189                                         + objState
190                                         + "'>\n");
191                         outputXML.append("\t\t<description>");
192                         outputXML.append(description);
193                         outputXML.append("</description>\n");
194                         outputXML.append("\t</service-assembly-info>\n");
195                     }
196                 }
197                 outputXML.append("</service-assembly-info-list>");
198                 if (xmlOutput == null) {
199                     // Display the list on the console
200
try {
201                         log(output.toString());
202                     } catch (NullPointerException JavaDoc e) {
203                         // Exception thrown when execute outisde of the ant
204
// context
205
}
206                 } else {
207                     try {
208                         // Fill the property xmlOutput
209
getProject().setNewProperty(xmlOutput,
210                                 outputXML.toString());
211                     } catch (Exception JavaDoc e) {
212                         // Exception thrown if not executed into a Ant context
213
}
214                 }
215             }
216             connector.close();
217         } catch (Exception JavaDoc e) {
218             if (Boolean.parseBoolean(failOnError)) {
219                 throw new BuildException(e.getMessage(), e.getCause());
220             }
221         }
222     }
223
224     public void setComponentName(String JavaDoc componentName) {
225         this.componentName = componentName;
226     }
227
228     public void setServiceAssemblyName(String JavaDoc serviceAssemblyName) {
229         this.serviceAssemblyName = serviceAssemblyName;
230     }
231
232     public void setState(String JavaDoc state) {
233         this.state = state;
234     }
235
236     public void setXmlOutput(String JavaDoc xmlOutput) {
237         this.xmlOutput = xmlOutput;
238     }
239 }
240
Popular Tags