KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > mbeans > ResourcesMBeanTest


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * $Id: ResourcesMBeanTest.java,v 1.3 2005/12/25 03:43:13 tcfujii Exp $
26  */

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 //jdk imports
37
import java.util.Properties JavaDoc;
38 import java.io.File JavaDoc;
39 import java.util.Map JavaDoc;
40
41 //junit imports
42
import junit.framework.*;
43 import junit.textui.TestRunner;
44
45 //JMX imports
46
import javax.management.AttributeList JavaDoc;
47 import javax.management.Attribute JavaDoc;
48 import javax.management.ObjectName JavaDoc;
49 import javax.management.MBeanException JavaDoc;
50 import javax.management.MBeanServer JavaDoc;
51 import javax.management.MBeanServerConnection JavaDoc;
52 import javax.management.remote.JMXConnector JavaDoc;
53 import javax.management.remote.JMXConnectorFactory JavaDoc;
54 import javax.management.remote.JMXServiceURL JavaDoc;
55
56
57 public class ResourcesMBeanTest extends TestCase {
58     
59     private JMXConnector JavaDoc connector;
60     
61     private final static String JavaDoc HOST = "localhost";
62     private final static String JavaDoc PORT = "4848";
63     private final static String JavaDoc ADMIN_USER = "admin";
64     private final static String JavaDoc ADMIN_PASSWORD = "adminadmin";
65     private final static String JavaDoc APPLICATION = "/export/stateless-simple.ear";
66     
67     public ResourcesMBeanTest(String JavaDoc name) throws Exception JavaDoc {
68         super(name);
69     }
70     
71     /**
72      *
73      */

74     public void testCreateResource() {
75         System.out.println("..testCreateResource");
76         executeCreateResource("null","dummy", new java.util.HashMap JavaDoc());
77         executeCreateResource("","dummy1",new java.util.HashMap JavaDoc());
78         executeCreateResource(null,"dummy2",new java.util.HashMap JavaDoc());
79     }
80     
81     /**
82      *
83      */

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 JavaDoc target, String JavaDoc resourceName, Map JavaDoc options) {
92         try {
93              final MBeanServerConnection JavaDoc server = getMBeanServer();
94              String JavaDoc methodToInvoke = "createJdbcConnectionPool";
95              Attribute JavaDoc attr1 = new Attribute JavaDoc("datasource_classname","com.pointbase.xa.xaDataSource");
96              Attribute JavaDoc attr2 = new Attribute JavaDoc("name",resourceName);
97              AttributeList JavaDoc list = new AttributeList JavaDoc();
98              list.add(attr1);
99              list.add(attr2);
100              ObjectName JavaDoc objectName =
101                     new ObjectName JavaDoc(
102                         "com.sun.appserv:type=resources,category=config");
103              printMBeanTestInfo(objectName);
104              ObjectName JavaDoc result = (ObjectName JavaDoc)server.invoke(objectName,
105                 methodToInvoke, new Object JavaDoc[]{list,new Properties(),target},
106                 new String JavaDoc[]{"javax.management.AttributeList", "java.util.Properties","java.lang.String"});
107         } catch(Throwable JavaDoc e) {
108             fail(e.getMessage());
109         }
110     }
111     
112     public void executeDeleteResource(String JavaDoc target, String JavaDoc resourceName) {
113         try {
114              final MBeanServerConnection JavaDoc server = getMBeanServer();
115              String JavaDoc methodToInvoke = "deleteJdbcConnectionPool";
116              ObjectName JavaDoc objectName =
117                     new ObjectName JavaDoc(
118                         "com.sun.appserv:type=resources,category=config");
119              printMBeanTestInfo(objectName);
120              ObjectName JavaDoc result = (ObjectName JavaDoc)server.invoke(objectName,
121                 methodToInvoke, new Object JavaDoc[]{resourceName,target},
122                 new String JavaDoc[]{"java.lang.String","java.lang.String"});
123         } catch(Throwable JavaDoc e) {
124             System.out.println("Something failed in deleting the resource!");
125             e.printStackTrace();
126         }
127     }
128     
129     /**
130      *
131      */

132     public java.util.Properties JavaDoc getDeploymentProperties() {
133         java.util.Properties JavaDoc props = new java.util.Properties JavaDoc();
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     /**
143      *
144      */

145     private void printMBeanTestInfo(ObjectName JavaDoc objectName) {
146         System.out.println("Testing mbean invocation on <" + objectName + ">");
147     }
148     
149     /**
150      *
151      */

152     private ObjectName JavaDoc getSampleObjectName() {
153         ObjectName JavaDoc name = null;
154         try {
155             name =
156                 new ObjectName JavaDoc("com.sun.appserv:type=jdbc-connection-pool," +
157                     "name=__TimerPool,category=config");
158         } catch(Exception JavaDoc e) {
159             e.printStackTrace();
160         }
161         return name;
162     }
163     
164     /**
165      *
166      */

167     private MBeanServerConnection JavaDoc getMBeanServer() {
168         MBeanServerConnection JavaDoc conn = null;
169         try {
170             conn = connector.getMBeanServerConnection();
171         } catch(Exception JavaDoc e) {
172             System.out.println("Could not retrieve connection! "
173                 + e.getMessage());
174             e.printStackTrace();
175         }
176         return conn;
177     }
178     
179     /**
180      *
181      *
182      */

183     private void initConnector() {
184         try {
185             connector = JMXConnectorFactory.connect(
186                 new JMXServiceURL JavaDoc("service:jmx:s1ashttp://" + HOST +
187                     ":" + PORT), initConnectorEnvironment());
188         } catch (Exception JavaDoc ex) {
189             System.out.println("Remote Connect Failed!!!");
190             System.out.println(ex.getMessage());
191             throw new RuntimeException JavaDoc(ex);
192         }
193     }
194     
195     /**
196      *
197      */

198     private java.util.Map JavaDoc initConnectorEnvironment() {
199         final java.util.Map JavaDoc env = new java.util.HashMap JavaDoc();
200         final String JavaDoc 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 JavaDoc args[]) throws Exception JavaDoc {
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