1 22 package org.objectweb.petals.tools.jbiplugin; 23 24 import javax.management.MBeanServerConnection ; 25 import javax.management.ObjectName ; 26 import javax.management.remote.JMXConnector ; 27 28 import org.objectweb.petals.tools.jbiplugin.util.JBIJMXConnectorUtil; 29 30 35 public abstract class JMXComponentAbstractMojo extends JMXTaskAbstractMojo { 36 37 40 protected static final String INSTALL_SHARED_LIBRARY = "installSharedLibrary"; 41 42 45 protected static final String LOAD_NEW_INSTALLED = "loadNewInstaller"; 46 47 50 protected static final String UNLOAD_INSTALLER = "unloadInstaller"; 51 52 55 protected static final String INSTALL = "install"; 56 57 60 protected static final String UNINSTALL = "uninstall"; 61 62 65 protected static final String SHUTDOWN = "shutDown"; 66 67 70 protected static final String START = "start"; 71 72 75 protected static final String STOP = "stop"; 76 77 83 protected String getState(String componentName) throws Exception { 84 JMXConnector connector = null; 85 String state = null; 86 try { 87 88 connector = JBIJMXConnectorUtil.getConnection(host, port, username, 89 password); 90 MBeanServerConnection connection = connector 91 .getMBeanServerConnection(); 92 ObjectName objectName = JBIJMXConnectorUtil.getComponentMBeanName( 93 connection, componentName); 94 if (objectName == null) { 95 objectName = JBIJMXConnectorUtil.getInstallerComponentMBeanName( 97 connection, componentName); 98 if (objectName == null) { 99 state = "Unknown"; 101 } 102 else { 103 state = "Loaded"; 104 } 105 } 106 else { 107 state = (String ) connection.getAttribute( 108 objectName, "CurrentState"); 109 } 110 111 } catch (Exception e) { 112 if (failOnError) { 113 throw e; 114 } 115 } finally { 116 connector.close(); 117 } 118 119 System.out.println("Component: " 120 + componentName + " is in state: " + state); 121 return state; 122 123 } 124 125 131 protected void performComponentAction(String action, String componentName) 132 throws Exception { 133 JMXConnector connector = null; 134 try { 135 connector = JBIJMXConnectorUtil.getConnection(host, port, username, 136 password); 137 MBeanServerConnection connection = connector 138 .getMBeanServerConnection(); 139 Object [] objects = new Object [0]; 140 String [] strings = new String [0]; 141 ObjectName objectName = JBIJMXConnectorUtil.getComponentMBeanName( 142 connection, componentName); 143 if (objectName == null) { 144 throw new Exception ("The component is not installed"); 145 } 146 connection.invoke(objectName, action, objects, strings); 147 System.out.println(action + " component name : " + componentName); 148 149 } catch (Exception e) { 150 if (failOnError) { 151 throw e; 152 } 153 } finally { 154 connector.close(); 155 } 156 } 157 158 164 protected void performInstallerComponentAction(String action, String componentName) 165 throws Exception { 166 JMXConnector connector = null; 167 try { 168 connector = JBIJMXConnectorUtil.getConnection(host, port, username, 169 password); 170 MBeanServerConnection connection = connector 171 .getMBeanServerConnection(); 172 Object [] objects = new Object [0]; 173 String [] strings = new String [0]; 174 ObjectName objectName = JBIJMXConnectorUtil.getInstallerComponentMBeanName( 175 connection, componentName); 176 if (objectName == null) { 177 throw new Exception ("The component has no installer"); 178 } 179 connection.invoke(objectName, action, objects, strings); 180 System.out.println(action + " component name : " + componentName); 181 182 } catch (Exception e) { 183 if (failOnError) { 184 throw e; 185 } 186 } finally { 187 connector.close(); 188 } 189 } 190 191 198 protected void performInstallationAction(String action, String componentName) throws Exception { 199 200 JMXConnector connector = null; 201 try { 202 connector = JBIJMXConnectorUtil.getConnection(host, port, username, 203 password); 204 MBeanServerConnection connection = connector 205 .getMBeanServerConnection(); 206 Object [] objectsLoad = new Object [1]; 207 objectsLoad[0] = componentName; 208 String [] stringsLoad = new String [1]; 209 stringsLoad[0] = "java.lang.String"; 210 connection.invoke(JBIJMXConnectorUtil 211 .getInstallationServiceMBeanName(connection), 212 action, objectsLoad, 213 stringsLoad); 214 System.out.println(action + " component name : " + componentName); 215 216 } catch (Exception e) { 217 if (failOnError) { 218 throw e; 219 } 220 } finally { 221 connector.close(); 222 } 223 } 224 225 229 protected void performUnload(String componentName) throws Exception { 230 JMXConnector connector = null; 231 try { 232 connector = JBIJMXConnectorUtil.getConnection(host, port, username, 233 password); 234 MBeanServerConnection connection = connector 235 .getMBeanServerConnection(); 236 Object [] objects = new Object [2]; 237 objects[0] = componentName; 238 objects[1] = true; 239 String [] strings = new String [2]; 240 strings[0] = "java.lang.String"; 241 strings[1] = "boolean"; 242 connection.invoke(JBIJMXConnectorUtil 244 .getInstallationServiceMBeanName(connection), 245 JMXComponentAbstractMojo.UNLOAD_INSTALLER, objects, strings); 246 System.out.println("Unload component name : " + componentName); 247 } catch (Exception e) { 248 if (failOnError) { 249 throw e; 250 } 251 } finally { 252 connector.close(); 253 } 254 } 255 } 256 | Popular Tags |