1 17 package org.apache.servicemix.jbi.management.task; 18 19 import java.io.IOException ; 20 import java.net.MalformedURLException ; 21 import java.util.HashMap ; 22 import java.util.Map ; 23 24 import javax.jbi.management.DeploymentServiceMBean; 25 import javax.management.MBeanServerInvocationHandler ; 26 import javax.management.ObjectName ; 27 import javax.management.remote.JMXConnector ; 28 import javax.management.remote.JMXConnectorFactory ; 29 import javax.management.remote.JMXServiceURL ; 30 31 import org.apache.servicemix.jbi.container.JBIContainer; 32 import org.apache.servicemix.jbi.framework.AdminCommandsServiceMBean; 33 import org.apache.servicemix.jbi.framework.DeploymentService; 34 import org.apache.servicemix.jbi.management.ManagementContext; 35 import org.apache.tools.ant.BuildException; 36 import org.apache.tools.ant.Project; 37 import org.apache.tools.ant.Task; 38 39 44 public abstract class JbiTask extends Task { 45 46 private String serverProtocol = "rmi"; 47 private String host = "localhost"; 48 private String containerName = JBIContainer.DEFAULT_NAME; 49 private String jmxDomainName = ManagementContext.DEFAULT_DOMAIN; 50 private int port = ManagementContext.DEFAULT_CONNECTOR_PORT; 51 private String jndiPath = ManagementContext.DEFAULT_CONNECTOR_PATH; 52 private String username; 53 private String password; 54 private boolean failOnError = true; 55 private JMXConnector jmxConnector; 56 57 58 62 public JMXServiceURL getServiceURL() throws MalformedURLException { 63 JMXServiceURL url = null; 64 url = new JMXServiceURL ("service:jmx:rmi:///jndi/rmi://" 65 + host + ":" + port + jndiPath); 66 return url; 67 } 68 69 75 public JMXConnector getJMXConnector (JMXServiceURL url) throws IOException { 76 String [] credentials = new String [] { getUsername(), getPassword() }; 77 Map environment = new HashMap (); 78 environment.put(JMXConnector.CREDENTIALS, credentials); 79 return JMXConnectorFactory.connect(url, environment); 80 } 81 82 86 public void connect() throws IOException { 87 this.jmxConnector = getJMXConnector(getServiceURL()); 88 } 89 90 91 95 public void close(){ 96 if (this.jmxConnector != null){ 97 try { 98 jmxConnector.close(); 99 } 100 catch (IOException e) { 101 log("Caught an error closing the jmxConnector" + e.getMessage(), Project.MSG_WARN); 102 } 103 } 104 } 105 106 107 112 protected ObjectName getObjectName (Class systemClass){ 113 return ManagementContext.getSystemObjectName(jmxDomainName, containerName, systemClass); 114 } 115 116 117 122 public AdminCommandsServiceMBean getAdminCommandsService() throws IOException { 123 ObjectName objectName = getObjectName(AdminCommandsServiceMBean.class); 124 125 return (AdminCommandsServiceMBean) MBeanServerInvocationHandler.newProxyInstance(jmxConnector.getMBeanServerConnection(), objectName, 126 AdminCommandsServiceMBean.class, true); 127 } 128 129 134 public DeploymentServiceMBean getDeploymentService() throws IOException { 135 ObjectName objectName = getObjectName(DeploymentService.class); 136 137 return (DeploymentServiceMBean) MBeanServerInvocationHandler.newProxyInstance(jmxConnector.getMBeanServerConnection(), objectName, 138 DeploymentServiceMBean.class, true); 139 } 140 141 142 145 public String getContainerName() { 146 return containerName; 147 } 148 151 public void setContainerName(String containerName) { 152 this.containerName = containerName; 153 } 154 157 public String getJmxDomainName() { 158 return jmxDomainName; 159 } 160 163 public void setJmxDomainName(String jmxDomainName) { 164 this.jmxDomainName = jmxDomainName; 165 } 166 169 public String getJndiPath() { 170 return jndiPath; 171 } 172 175 public void setJndiPath(String jndiPath) { 176 this.jndiPath = jndiPath; 177 } 178 181 public String getHost() { 182 return host; 183 } 184 187 public void setHost(String host) { 188 this.host = host; 189 } 190 193 public int getPort() { 194 return port; 195 } 196 199 public void setPort(int port) { 200 this.port = port; 201 } 202 203 206 public String getServerProtocol() { 207 return serverProtocol; 208 } 209 212 public void setServerProtocol(String serverProtocol) { 213 this.serverProtocol = serverProtocol; 214 } 215 216 219 public String getPassword() { 220 return password; 221 } 222 223 226 public void setPassword(String password) { 227 this.password = password; 228 } 229 230 233 public String getUsername() { 234 return username; 235 } 236 237 240 public void setUsername(String username) { 241 this.username = username; 242 } 243 244 247 public boolean isFailOnError() { 248 return failOnError; 249 } 250 251 254 public void setFailOnError(boolean failOnError) { 255 this.failOnError = failOnError; 256 } 257 258 263 public void execute() throws BuildException { 264 AdminCommandsServiceMBean acs; 265 try { 266 log("Retrieving remote admin interface", Project.MSG_DEBUG); 267 connect(); 268 acs = getAdminCommandsService(); 269 } catch (Throwable e) { 270 log("Error accessing ServiceMix administration: " + e.getMessage(), Project.MSG_WARN); 271 if (isFailOnError()) { 272 throw new BuildException("Error accessing ServiceMix administration", e); 273 } else { 274 return; 275 } 276 } 277 try { 278 log("Executing command", Project.MSG_DEBUG); 279 doExecute(acs); 280 } catch (Throwable e) { 281 log("Error executing command: " + e.getMessage(), Project.MSG_WARN); 282 if (isFailOnError()) { 283 throw new BuildException("Error accessing ServiceMix administration", e); 284 } else { 285 return; 286 } 287 } 288 } 289 290 protected abstract void doExecute(AdminCommandsServiceMBean acs) throws Exception ; 291 292 } | Popular Tags |