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 import java.util.Map ; 40 41 import junit.framework.*; 43 import junit.textui.TestRunner; 44 45 import javax.management.AttributeList ; 47 import javax.management.Attribute ; 48 import javax.management.ObjectName ; 49 import javax.management.MBeanException ; 50 import javax.management.MBeanServer ; 51 import javax.management.MBeanServerConnection ; 52 import javax.management.remote.JMXConnector ; 53 import javax.management.remote.JMXConnectorFactory ; 54 import javax.management.remote.JMXServiceURL ; 55 56 57 public class ResourcesMBeanTest extends TestCase { 58 59 private JMXConnector connector; 60 61 private final static String HOST = "localhost"; 62 private final static String PORT = "4848"; 63 private final static String ADMIN_USER = "admin"; 64 private final static String ADMIN_PASSWORD = "adminadmin"; 65 private final static String APPLICATION = "/export/stateless-simple.ear"; 66 67 public ResourcesMBeanTest(String name) throws Exception { 68 super(name); 69 } 70 71 74 public void testCreateResource() { 75 System.out.println("..testCreateResource"); 76 executeCreateResource("null","dummy", new java.util.HashMap ()); 77 executeCreateResource("","dummy1",new java.util.HashMap ()); 78 executeCreateResource(null,"dummy2",new java.util.HashMap ()); 79 } 80 81 84 public void testDeleteResource() { 85 System.out.println("..testDeleteResource"); 86 executeDeleteResource("null","dummy"); 87 executeDeleteResource(null,"dummy1"); 88 executeDeleteResource("","dummy2"); 89 } 90 91 public void executeCreateResource(String target, String resourceName, Map options) { 92 try { 93 final MBeanServerConnection server = getMBeanServer(); 94 String methodToInvoke = "createJdbcConnectionPool"; 95 Attribute attr1 = new Attribute ("datasource_classname","com.pointbase.xa.xaDataSource"); 96 Attribute attr2 = new Attribute ("name",resourceName); 97 AttributeList list = new AttributeList (); 98 list.add(attr1); 99 list.add(attr2); 100 ObjectName objectName = 101 new ObjectName ( 102 "com.sun.appserv:type=resources,category=config"); 103 printMBeanTestInfo(objectName); 104 ObjectName result = (ObjectName )server.invoke(objectName, 105 methodToInvoke, new Object []{list,new Properties(),target}, 106 new String []{"javax.management.AttributeList", "java.util.Properties","java.lang.String"}); 107 } catch(Throwable e) { 108 fail(e.getMessage()); 109 } 110 } 111 112 public void executeDeleteResource(String target, String resourceName) { 113 try { 114 final MBeanServerConnection server = getMBeanServer(); 115 String methodToInvoke = "deleteJdbcConnectionPool"; 116 ObjectName objectName = 117 new ObjectName ( 118 "com.sun.appserv:type=resources,category=config"); 119 printMBeanTestInfo(objectName); 120 ObjectName result = (ObjectName )server.invoke(objectName, 121 methodToInvoke, new Object []{resourceName,target}, 122 new String []{"java.lang.String","java.lang.String"}); 123 } catch(Throwable e) { 124 System.out.println("Something failed in deleting the resource!"); 125 e.printStackTrace(); 126 } 127 } 128 129 132 public java.util.Properties getDeploymentProperties() { 133 java.util.Properties props = new java.util.Properties (); 134 props.setProperty(DeploymentProperties.ARCHIVE_NAME, APPLICATION); 135 props.setProperty(DeploymentProperties.VERIFY, "false"); 136 props.setProperty(DeploymentProperties.PRECOMPILE_JSP, "false"); 137 props.setProperty(DeploymentProperties.ENABLE, "true"); 138 props.setProperty(DeploymentProperties.FORCE, "true"); 139 return props; 140 } 141 142 145 private void printMBeanTestInfo(ObjectName objectName) { 146 System.out.println("Testing mbean invocation on <" + objectName + ">"); 147 } 148 149 152 private ObjectName getSampleObjectName() { 153 ObjectName name = null; 154 try { 155 name = 156 new ObjectName ("com.sun.appserv:type=jdbc-connection-pool," + 157 "name=__TimerPool,category=config"); 158 } catch(Exception e) { 159 e.printStackTrace(); 160 } 161 return name; 162 } 163 164 167 private MBeanServerConnection getMBeanServer() { 168 MBeanServerConnection conn = null; 169 try { 170 conn = connector.getMBeanServerConnection(); 171 } catch(Exception e) { 172 System.out.println("Could not retrieve connection! " 173 + e.getMessage()); 174 e.printStackTrace(); 175 } 176 return conn; 177 } 178 179 183 private void initConnector() { 184 try { 185 connector = JMXConnectorFactory.connect( 186 new JMXServiceURL ("service:jmx:s1ashttp://" + HOST + 187 ":" + PORT), initConnectorEnvironment()); 188 } catch (Exception ex) { 189 System.out.println("Remote Connect Failed!!!"); 190 System.out.println(ex.getMessage()); 191 throw new RuntimeException (ex); 192 } 193 } 194 195 198 private java.util.Map initConnectorEnvironment() { 199 final java.util.Map env = new java.util.HashMap (); 200 final String PKGS = "com.sun.enterprise.admin.jmx.remote.protocol"; 201 env.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, PKGS); 202 env.put(DefaultConfiguration.ADMIN_USER_ENV_PROPERTY_NAME, ADMIN_USER); 203 env.put(DefaultConfiguration.ADMIN_PASSWORD_ENV_PROPERTY_NAME, ADMIN_PASSWORD); 204 env.put(DefaultConfiguration.HTTP_AUTH_PROPERTY_NAME, 205 DefaultConfiguration.DEFAULT_HTTP_AUTH_SCHEME); 206 return (env); 207 } 208 209 protected void setUp() { 210 initConnector(); 211 } 212 213 protected void tearDown() { 214 } 215 216 public static junit.framework.Test suite() { 217 TestSuite suite = new TestSuite(ResourcesMBeanTest.class); 218 return suite; 219 } 220 221 public static void main(String args[]) throws Exception { 222 final TestRunner runner= new TestRunner(); 223 final TestResult result = runner.doRun(ResourcesMBeanTest.suite(), false); 224 System.exit(result.errorCount() + result.failureCount()); 225 } 226 } | Popular Tags |