1 23 24 27 28 package com.sun.enterprise.admin.mbeans; 29 30 import com.sun.enterprise.admin.jmx.remote.DefaultConfiguration; 31 import com.sun.enterprise.admin.target.TargetType; 32 import com.sun.enterprise.config.ConfigContext; 33 import com.sun.enterprise.config.ConfigFactory; 34 import com.sun.enterprise.deployment.backend.DeploymentStatus; 35 import com.sun.enterprise.deployment.util.DeploymentProperties; 36 import java.util.Properties ; 38 import java.io.File ; 39 40 import junit.framework.*; 42 import junit.textui.TestRunner; 43 44 import javax.management.AttributeList ; 46 import javax.management.Attribute ; 47 import javax.management.ObjectName ; 48 import javax.management.MBeanException ; 49 import javax.management.MBeanServer ; 50 import javax.management.MBeanServerConnection ; 51 import javax.management.remote.JMXConnector ; 52 import javax.management.remote.JMXConnectorFactory ; 53 import javax.management.remote.JMXServiceURL ; 54 55 56 public class ApplicationsConfigMBeanTest extends TestCase { 57 58 private JMXConnector connector; 59 60 private final static String HOST = "hoyas.red.iplanet.com"; 61 private final static String PORT = "1234"; 62 private final static String ADMIN_USER = "admin"; 63 private final static String ADMIN_PASSWORD = "adminadmin"; 64 private final static String APPLICATION = "/export/stateless-simple.ear"; 65 66 public ApplicationsConfigMBeanTest(String name) throws Exception { 67 super(name); 68 } 69 70 78 public void testTargetAwareDeployment() { 79 final java.io.File appArchive = 80 new java.io.File (APPLICATION); 81 printAllDeployedComponents(); 82 printAllDeployedComponents(); 84 } 87 88 91 public void checkDeploymentPassed(String appName, String [] targets) { 92 final MBeanServerConnection server = getMBeanServer(); 93 try{ 94 ObjectName clusters = 95 new ObjectName ("com.sun.appserv:type=clusters,category=config"); 96 for(int i = 0; i < targets.length; i++) { 97 ObjectName name = (ObjectName )server.invoke(clusters, 98 "getClusterByName", new Object []{targets[i]}, 99 new String []{"java.lang.String"}); 100 if(name != null) { 101 System.out.println("What is the objectname: " + name); 102 String clusterName = 103 (String )server.getAttribute(name, "name"); 104 ObjectName cluster = new ObjectName ( 105 "com.sun.appserv:type=cluster,name=" + 106 clusterName + ",category=config"); 107 ObjectName [] deployedObjs = (ObjectName [])server.invoke( 108 cluster,"getApplicationRef", 109 new Object []{}, new String []{}); 110 for(int z = 0; z < deployedObjs.length; z++) { 111 String objName = 112 (String )server.getAttribute(deployedObjs[z], "name"); 113 if(appName.equals(objName)) { 114 System.out.println("Application deployed " + 115 "successfully on cluster: " + clusterName); 116 } 117 } 118 } else { 119 System.out.println("No clusters found from target list!"); 120 } 121 } }catch(Exception e){ 123 e.printStackTrace(); 124 } 125 } 126 127 130 public void deployApplication(java.io.File appArchive, String [] targets) { 131 final MBeanServerConnection server = getMBeanServer(); 132 java.util.Properties myProps = getDeploymentProperties(); 133 try { 134 String methodToInvoke = "deploy"; 135 ObjectName objectName = 136 new ObjectName ( 137 "com.sun.appserv:type=applications,category=config"); 138 printMBeanTestInfo(objectName); 139 Object deployResult = server.invoke(objectName, 140 methodToInvoke, new Object []{myProps, targets}, 141 new String []{"java.util.Properties", "[Ljava.lang.String;"}); 142 } catch(Throwable e) { 143 System.out.println("Something Failed!"); 144 e.printStackTrace(); 145 } 146 } 147 148 151 public void undeployApplication(java.io.File appArchive, String [] targets) { 152 final MBeanServerConnection server = getMBeanServer(); 153 java.util.Properties myProps = getDeploymentProperties(); 154 try { 155 String methodToInvoke = "undeploy"; 156 ObjectName objectName = 157 new ObjectName ( 158 "com.sun.appserv:type=applications,category=config"); 159 Object undeployResult = server.invoke(objectName, 160 methodToInvoke, new Object []{myProps, targets}, 161 new String []{"java.util.Properties", "[Ljava.lang.String;"}); 162 } catch(Throwable e) { 163 System.out.println("Something failed when undeploying!"); 164 e.printStackTrace(); 165 } 166 } 167 168 171 public void printAllDeployedComponents() { 172 final MBeanServerConnection server = getMBeanServer(); 173 try { 174 String methodToInvoke = "getAllDeployedComponents"; 175 ObjectName objectName = 176 new ObjectName ( 177 "com.sun.appserv:type=applications,category=config"); 178 ObjectName [] result = (ObjectName [])server.invoke(objectName, 179 methodToInvoke, new Object []{}, 180 new String []{}); 181 for(int i = 0; i < result.length; i++) { 182 System.out.println("Deployed: " + result[i]); 183 } 184 } catch(Throwable e) { 185 System.out.println("Something failed when getting all deployed components!"); 186 e.printStackTrace(); 187 } 188 } 189 190 193 public String [] getTargets() { 194 final MBeanServerConnection server = getMBeanServer(); 195 String [] targets = null; 196 System.out.println("Testing getTargets!"); 197 try { 198 String methodToInvoke = "getTargets"; 199 ObjectName objectName = 200 new ObjectName ( 201 "com.sun.appserv:type=applications,category=config"); 202 targets = (String [])server.invoke(objectName, 203 methodToInvoke, new Object []{}, 204 new String []{}); 205 System.out.println("\n"); 206 for(int i = 0; i < targets.length; i++) { 207 System.out.println("Target: " + targets[i]); 208 } 209 210 } catch(Throwable e) { 211 System.out.println("Something failed when getting all targets!"); 212 e.printStackTrace(); 213 } 214 return targets; 215 } 216 217 220 public void getServers() { 221 final MBeanServerConnection server = getMBeanServer(); 222 try { 223 String methodToInvoke = "getServer"; 224 ObjectName objectName = 225 new ObjectName ( 226 "com.sun.appserv:type=servers,category=config"); 227 ObjectName [] result = (ObjectName [])server.invoke(objectName, 228 methodToInvoke, new Object []{}, 229 new String []{}); 230 System.out.println("\n"); 231 for(int i = 0; i < result.length; i++) { 232 System.out.println("Server: " + result[i]); 233 } 234 } catch(Throwable e) { 235 System.out.println("Something failed when getting all targets!"); 236 e.printStackTrace(); 237 } 238 } 239 240 243 public void getClusters() { 244 final MBeanServerConnection server = getMBeanServer(); 245 try { 246 String methodToInvoke = "getCluster"; 247 ObjectName objectName = 248 new ObjectName ( 249 "com.sun.appserv:type=clusters,category=config"); 250 ObjectName [] result = (ObjectName [])server.invoke(objectName, 251 methodToInvoke, new Object []{}, 252 new String []{}); 253 System.out.println("\n"); 254 for(int i = 0; i < result.length; i++) { 255 System.out.println("Cluster: " + result[i]); 256 } 257 } catch(Throwable e) { 258 System.out.println("Something failed when getting all targets!"); 259 e.printStackTrace(); 260 } 261 } 262 263 266 public java.util.Properties getDeploymentProperties() { 267 java.util.Properties props = new java.util.Properties (); 268 props.setProperty(DeploymentProperties.ARCHIVE_NAME, APPLICATION); 269 props.setProperty(DeploymentProperties.VERIFY, "false"); 270 props.setProperty(DeploymentProperties.PRECOMPILE_JSP, "false"); 271 props.setProperty(DeploymentProperties.ENABLE, "true"); 272 props.setProperty(DeploymentProperties.FORCE, "true"); 273 return props; 274 } 275 276 279 private void printMBeanTestInfo(ObjectName objectName) { 280 System.out.println("Testing mbean invocation on <" + objectName + ">"); 281 } 282 283 286 private ObjectName getSampleObjectName() { 287 ObjectName name = null; 288 try { 289 name = 290 new ObjectName ("com.sun.appserv:type=jdbc-connection-pool," + 291 "name=__TimerPool,category=config"); 292 } catch(Exception e) { 293 e.printStackTrace(); 294 } 295 return name; 296 } 297 298 301 private MBeanServerConnection getMBeanServer() { 302 MBeanServerConnection conn = null; 303 try { 304 conn = connector.getMBeanServerConnection(); 305 } catch(Exception e) { 306 System.out.println("Could not retrieve connection! " 307 + e.getMessage()); 308 e.printStackTrace(); 309 } 310 return conn; 311 } 312 313 317 private void initConnector() { 318 try { 319 connector = JMXConnectorFactory.connect( 320 new JMXServiceURL ("service:jmx:s1ashttp://" + HOST + 321 ":" + PORT), initConnectorEnvironment()); 322 } catch (Exception ex) { 323 System.out.println("Remote Connect Failed!!!"); 324 System.out.println(ex.getMessage()); 325 throw new RuntimeException (ex); 326 } 327 } 328 329 332 private java.util.Map initConnectorEnvironment() { 333 final java.util.Map env = new java.util.HashMap (); 334 final String PKGS = "com.sun.enterprise.admin.jmx.remote.protocol"; 335 env.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, PKGS); 336 env.put(DefaultConfiguration.ADMIN_USER_ENV_PROPERTY_NAME, ADMIN_USER); 337 env.put(DefaultConfiguration.ADMIN_PASSWORD_ENV_PROPERTY_NAME, ADMIN_PASSWORD); 338 env.put(DefaultConfiguration.HTTP_AUTH_PROPERTY_NAME, 339 DefaultConfiguration.DEFAULT_HTTP_AUTH_SCHEME); 340 return (env); 341 } 342 343 protected void setUp() { 344 initConnector(); 345 } 346 347 protected void tearDown() { 348 } 349 350 public static junit.framework.Test suite() { 351 TestSuite suite = new TestSuite(ApplicationsConfigMBeanTest.class); 352 return suite; 353 } 354 355 public static void main(String args[]) throws Exception { 356 final TestRunner runner= new TestRunner(); 357 final TestResult result = runner.doRun(ApplicationsConfigMBeanTest.suite(), false); 358 System.exit(result.errorCount() + result.failureCount()); 359 } 360 } | Popular Tags |