KickJava   Java API By Example, From Geeks To Geeks.

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


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 binding components
38  *
39  * @author ddesjardins - eBMWebsourcing
40  */

41 public class ListBindingComponentsTask 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      * If supplied, only the named binding component is reported. If the binding
50      * doesn’t exist, an empty component-list report is given.
51      */

52     private String JavaDoc bindingComponentName;
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      * List the Bindings that are dependent on the specified shared library.
63      * Without this parameter, no shared library dependency will be verified.
64      */

65     // private String sharedLibraryName;
66
/**
67      * Binding component 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 ListBindingComponentsTask() {
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("------------- Binding Components ----------\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("xmlns='" + ListBindingComponentsTask.XMLNS
109                     + "' version='1.0'>\n");
110             // Get the engines component
111
// names
112
Object JavaDoc result = connection.getAttribute(JBIJMXConnectorUtil
113                     .getAdminServiceMBeanName(connection), "BindingComponents");
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                 composForSA.addAll(Arrays.asList((String JavaDoc[]) connection
122                         .invoke(JBIJMXConnectorUtil
123                                 .getDeploymentServiceMBeanName(connection),
124                                 "getComponentsForDeployedServiceAssembly",
125                                 objects, sig)));
126             }
127             String JavaDoc bindingName = bindingComponentName;
128             String JavaDoc bindingState = state;
129             if (result instanceof ObjectName JavaDoc[]) {
130                 ObjectName JavaDoc[] engines = (ObjectName JavaDoc[]) result;
131                 for (ObjectName JavaDoc objectName : engines) {
132                     String JavaDoc objState = (String JavaDoc) connection.getAttribute(
133                             objectName, "CurrentState");
134                     String JavaDoc name = objectName.getKeyProperty("name");
135                     // TODO extract the description
136
String JavaDoc description = "";
137                     // Set the variable to have a positive test
138
if (state == null) {
139                         bindingState = objState;
140                     }
141                     if (bindingComponentName == null) {
142                         bindingName = name;
143                     }
144                     if (serviceAssemblyName == null) {
145                         composForSA.add(name);
146                     }
147                     if (name.equals(bindingName)
148                             && objState.equals(bindingState)
149                             && composForSA.contains(name)) {
150                         // Add the component to the output
151
output.append("Name : " + name + "\n");
152                         output.append("State : " + objState + "\n");
153                         output.append("Description : " + description + "\n\n");
154                         outputXML
155                                 .append("\t<component-info type='binding-component' name='"
156                                         + name
157                                         + "' state='"
158                                         + objState
159                                         + "'>\n");
160                         outputXML.append("\t\t<description>");
161                         outputXML.append(description);
162                         outputXML.append("</description>\n");
163                         outputXML.append("\t</component-info>\n");
164                     }
165                 }
166                 outputXML.append("</component-info-list>");
167                 if (xmlOutput == null) {
168                     // Display the list on the console
169
try {
170                         log(output.toString());
171                     } catch (NullPointerException JavaDoc e) {
172                         // Exception thrown when execute outisde of the ant
173
// context
174
}
175                 } else {
176                     // Fill the property xmlOutput
177
try {
178                         getProject().setNewProperty(xmlOutput,
179                                 outputXML.toString());
180                     } catch (Exception JavaDoc e) {
181                         // Exception if not used in a Ant context
182
}
183                 }
184             }
185             connector.close();
186         } catch (Exception JavaDoc e) {
187             if (Boolean.parseBoolean(failOnError)) {
188                 throw new BuildException(e.getMessage(), e.getCause());
189             }
190         }
191     }
192
193     public void setBindingComponentName(String JavaDoc bindingComponentName) {
194         this.bindingComponentName = bindingComponentName;
195     }
196
197     public void setServiceAssemblyName(String JavaDoc serviceAssemblyName) {
198         this.serviceAssemblyName = serviceAssemblyName;
199     }
200
201     // public void setSharedLibraryName(String sharedLibraryName) {
202
// this.sharedLibraryName = sharedLibraryName;
203
// }
204

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