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 ListServiceEnginesTask extends JBIAntTaskAbstract { 42 43 46 private static final String XMLNS = "http://java.sun.com/xml/ns/jbi/component-info-list"; 47 48 53 private String serviceAssemblyName; 54 55 59 private String serviceEngineName; 60 61 65 69 private String state; 70 71 75 private String xmlOutput; 76 77 public ListServiceEnginesTask() { 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("---------------- Service Engines --------------\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("<component-info-list "); 109 outputXML.append("xmlns='" + ListServiceEnginesTask.XMLNS 110 + "' version='1.0'>\n"); 111 Object result = connection.getAttribute(JBIJMXConnectorUtil 113 .getAdminServiceMBeanName(connection), "EngineComponents"); 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 Object resultSA = connection 122 .invoke(JBIJMXConnectorUtil 123 .getDeploymentServiceMBeanName(connection), 124 "getComponentsForDeployedServiceAssembly", 125 objects, sig); 126 if (resultSA instanceof String []) { 127 composForSA.addAll(Arrays.asList((String []) resultSA)); 128 } 129 } 130 String engineName = serviceEngineName; 131 String engineState = state; 132 if (result instanceof ObjectName []) { 133 ObjectName [] engines = (ObjectName []) result; 134 for (ObjectName objectName : engines) { 135 String objState = (String ) connection.getAttribute( 136 objectName, "CurrentState"); 137 String name = objectName.getKeyProperty("name"); 138 String description = ""; 140 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 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 try { 172 log(output.toString()); 173 } catch (NullPointerException e) { 174 } 177 } else { 178 try { 179 getProject().setNewProperty(xmlOutput, 181 outputXML.toString()); 182 } catch (Exception e) { 183 } 185 } 186 } 187 connector.close(); 188 } catch (Exception e) { 189 if (Boolean.parseBoolean(failOnError)) { 190 throw new BuildException(e.getMessage(), e.getCause()); 191 } 192 } 193 } 194 195 public void setServiceAssemblyName(String serviceAssemblyName) { 196 this.serviceAssemblyName = serviceAssemblyName; 197 } 198 199 public void setServiceEngineName(String serviceEngineName) { 200 this.serviceEngineName = serviceEngineName; 201 } 202 203 207 public void setState(String state) { 208 this.state = state; 209 } 210 211 public void setXmlOutput(String xmlOutput) { 212 this.xmlOutput = xmlOutput; 213 } 214 } 215 | Popular Tags |