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.ObjectName ; 30 import javax.management.remote.JMXConnector ; 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 41 public class ListBindingComponentsTask extends JBIAntTaskAbstract { 42 43 46 private static final String XMLNS = "http://java.sun.com/xml/ns/jbi/component-info-list"; 47 48 52 private String bindingComponentName; 53 54 59 private String serviceAssemblyName; 60 61 65 69 private String state; 70 71 75 private String xmlOutput; 76 77 public ListBindingComponentsTask() { 78 super(); 79 } 80 81 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 connector = JBIJMXConnectorUtil.getConnection(host, 96 port, username, password); 97 MBeanServerConnection connection = connector 98 .getMBeanServerConnection(); 99 StringBuffer output = new StringBuffer (); 101 output 102 .append("-------------------------------------------------------\n"); 103 output.append("------------- Binding Components ----------\n"); 104 output 105 .append("-------------------------------------------------------\n"); 106 StringBuffer outputXML = new StringBuffer (); 107 outputXML.append("<?xml version='1.0' encoding='utf-8'?>\n"); 108 outputXML.append("xmlns='" + ListBindingComponentsTask.XMLNS 109 + "' version='1.0'>\n"); 110 Object result = connection.getAttribute(JBIJMXConnectorUtil 113 .getAdminServiceMBeanName(connection), "BindingComponents"); 114 Set <String > composForSA = new TreeSet <String >(); 115 if (serviceAssemblyName != null) { 116 Object [] objects = new Object [1]; 118 objects[0] = serviceAssemblyName; 119 String [] sig = new String [1]; 120 sig[0] = "java.lang.String"; 121 composForSA.addAll(Arrays.asList((String []) connection 122 .invoke(JBIJMXConnectorUtil 123 .getDeploymentServiceMBeanName(connection), 124 "getComponentsForDeployedServiceAssembly", 125 objects, sig))); 126 } 127 String bindingName = bindingComponentName; 128 String bindingState = state; 129 if (result instanceof ObjectName []) { 130 ObjectName [] engines = (ObjectName []) result; 131 for (ObjectName objectName : engines) { 132 String objState = (String ) connection.getAttribute( 133 objectName, "CurrentState"); 134 String name = objectName.getKeyProperty("name"); 135 String description = ""; 137 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 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 try { 170 log(output.toString()); 171 } catch (NullPointerException e) { 172 } 175 } else { 176 try { 178 getProject().setNewProperty(xmlOutput, 179 outputXML.toString()); 180 } catch (Exception e) { 181 } 183 } 184 } 185 connector.close(); 186 } catch (Exception e) { 187 if (Boolean.parseBoolean(failOnError)) { 188 throw new BuildException(e.getMessage(), e.getCause()); 189 } 190 } 191 } 192 193 public void setBindingComponentName(String bindingComponentName) { 194 this.bindingComponentName = bindingComponentName; 195 } 196 197 public void setServiceAssemblyName(String serviceAssemblyName) { 198 this.serviceAssemblyName = serviceAssemblyName; 199 } 200 201 205 public void setState(String state) { 206 this.state = state; 207 } 208 209 public void setXmlOutput(String xmlOutput) { 210 this.xmlOutput = xmlOutput; 211 } 212 } 213 | Popular Tags |