1 22 package org.objectweb.petals.tools.ant; 23 24 import java.util.Arrays ; 25 import java.util.Set ; 26 import java.util.TreeSet ; 27 28 import javax.management.MBeanServerConnection ; 29 import javax.management.remote.JMXConnector ; 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 40 public class ListServiceAssembliesTask extends JBIAntTaskAbstract { 41 42 45 private static final String XMLNS = "http://java.sun.com/xml/ns/jbi/component-info-list"; 46 47 52 private String componentName; 53 54 59 private String serviceAssemblyName; 60 61 64 private String state; 65 66 70 private String xmlOutput; 71 72 public ListServiceAssembliesTask() { 73 super(); 74 } 75 76 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 connector = JBIJMXConnectorUtil.getConnection(host, 91 port, username, password); 92 MBeanServerConnection connection = connector 93 .getMBeanServerConnection(); 94 StringBuffer output = new StringBuffer (); 96 output 97 .append("-------------------------------------------------------\n"); 98 output.append("------------- Service Assemblies-- ----------\n"); 99 output 100 .append("-------------------------------------------------------\n"); 101 StringBuffer outputXML = new StringBuffer (); 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 Object result = connection.getAttribute(JBIJMXConnectorUtil 108 .getDeploymentServiceMBeanName(connection), 109 "DeployedServiceAssemblies"); 110 Set <String > saForCompo = new TreeSet <String >(); 111 if (componentName != null) { 112 Object [] objects = new Object [1]; 114 objects[0] = componentName; 115 String [] sig = new String [1]; 116 sig[0] = "java.lang.String"; 117 Object resultSA = connection.invoke(JBIJMXConnectorUtil 118 .getDeploymentServiceMBeanName(connection), 119 "getDeployedServiceAssembliesForComponent", objects, 120 sig); 121 if (resultSA instanceof String []) { 122 saForCompo.addAll(Arrays.asList((String []) result)); 123 } 124 } 125 String saName = serviceAssemblyName; 126 String saState = state; 127 if (result instanceof String []) { 128 String [] assemblies = (String []) result; 129 for (String objectName : assemblies) { 130 Object [] objects = new Object [1]; 131 objects[0] = objectName; 132 String [] sig = new String [1]; 133 sig[0] = "java.lang.String"; 134 String objState = (String ) connection.invoke( 135 JBIJMXConnectorUtil 136 .getDeploymentServiceMBeanName(connection), 137 "getState", objects, sig); 138 String name = objectName; 139 String description = ""; 141 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 objects = new Object [1]; 155 objects[0] = objectName; 156 sig = new String [1]; 157 sig[0] = "java.lang.String"; 158 Set <String > composForSA = new TreeSet <String >( 159 Arrays 160 .asList((String []) connection 161 .invoke( 162 JBIJMXConnectorUtil 163 .getDeploymentServiceMBeanName(connection), 164 "getComponentsForDeployedServiceAssembly", 165 objects, sig))); 166 for (String compoName : composForSA) { 167 objects = new Object [1]; 168 objects[0] = compoName; 169 sig = new String [1]; 170 sig[0] = "java.lang.String"; 171 } 181 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 try { 201 log(output.toString()); 202 } catch (NullPointerException e) { 203 } 206 } else { 207 try { 208 getProject().setNewProperty(xmlOutput, 210 outputXML.toString()); 211 } catch (Exception e) { 212 } 214 } 215 } 216 connector.close(); 217 } catch (Exception e) { 218 if (Boolean.parseBoolean(failOnError)) { 219 throw new BuildException(e.getMessage(), e.getCause()); 220 } 221 } 222 } 223 224 public void setComponentName(String componentName) { 225 this.componentName = componentName; 226 } 227 228 public void setServiceAssemblyName(String serviceAssemblyName) { 229 this.serviceAssemblyName = serviceAssemblyName; 230 } 231 232 public void setState(String state) { 233 this.state = state; 234 } 235 236 public void setXmlOutput(String xmlOutput) { 237 this.xmlOutput = xmlOutput; 238 } 239 } 240 | Popular Tags |