KickJava   Java API By Example, From Geeks To Geeks.

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


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: ApplicationsConfigMBeanTest.java,v 1.3 2005/12/25 03:43:11 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
40 //junit imports
41
import junit.framework.*;
42 import junit.textui.TestRunner;
43
44 //JMX imports
45
import javax.management.AttributeList JavaDoc;
46 import javax.management.Attribute JavaDoc;
47 import javax.management.ObjectName JavaDoc;
48 import javax.management.MBeanException JavaDoc;
49 import javax.management.MBeanServer JavaDoc;
50 import javax.management.MBeanServerConnection JavaDoc;
51 import javax.management.remote.JMXConnector JavaDoc;
52 import javax.management.remote.JMXConnectorFactory JavaDoc;
53 import javax.management.remote.JMXServiceURL JavaDoc;
54
55
56 public class ApplicationsConfigMBeanTest extends TestCase {
57     
58     private JMXConnector JavaDoc connector;
59     
60     private final static String JavaDoc HOST = "hoyas.red.iplanet.com";
61     private final static String JavaDoc PORT = "1234";
62     private final static String JavaDoc ADMIN_USER = "admin";
63     private final static String JavaDoc ADMIN_PASSWORD = "adminadmin";
64     private final static String JavaDoc APPLICATION = "/export/stateless-simple.ear";
65     
66     public ApplicationsConfigMBeanTest(String JavaDoc name) throws Exception JavaDoc {
67         super(name);
68     }
69    
70     /**
71      * This test is used as a driver to test the deployment of an
72      * application to multiple targets against a running instance of
73      * the application server. Uncomment the lines within this method
74      * and set the appropriate class member variables to administer the test.
75      *
76      * @author Rob Ruyak
77      */

78     public void testTargetAwareDeployment() {
79         final java.io.File JavaDoc appArchive =
80             new java.io.File JavaDoc(APPLICATION);
81         printAllDeployedComponents();
82         //deployApplication(appArchive,getTargets());
83
printAllDeployedComponents();
84         //checkDeploymentPassed("stateless-simple", getTargets());
85
//undeployApplication(appArchive, getTargets());
86
}
87    
88     /**
89      *
90      */

91     public void checkDeploymentPassed(String JavaDoc appName, String JavaDoc[] targets) {
92         final MBeanServerConnection JavaDoc server = getMBeanServer();
93         try{
94             ObjectName JavaDoc clusters =
95                 new ObjectName JavaDoc("com.sun.appserv:type=clusters,category=config");
96             for(int i = 0; i < targets.length; i++) {
97                 ObjectName JavaDoc name = (ObjectName JavaDoc)server.invoke(clusters,
98                     "getClusterByName", new Object JavaDoc[]{targets[i]},
99                     new String JavaDoc[]{"java.lang.String"});
100                 if(name != null) {
101                     System.out.println("What is the objectname: " + name);
102                     String JavaDoc clusterName =
103                         (String JavaDoc)server.getAttribute(name, "name");
104                     ObjectName JavaDoc cluster = new ObjectName JavaDoc(
105                         "com.sun.appserv:type=cluster,name=" +
106                         clusterName + ",category=config");
107                     ObjectName JavaDoc[] deployedObjs = (ObjectName JavaDoc[])server.invoke(
108                         cluster,"getApplicationRef",
109                         new Object JavaDoc[]{}, new String JavaDoc[]{});
110                     for(int z = 0; z < deployedObjs.length; z++) {
111                         String JavaDoc objName =
112                            (String JavaDoc)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             }//end first for loop
122
}catch(Exception JavaDoc e){
123             e.printStackTrace();
124         }
125     }
126     
127     /**
128      *
129      */

130     public void deployApplication(java.io.File JavaDoc appArchive, String JavaDoc [] targets) {
131         final MBeanServerConnection JavaDoc server = getMBeanServer();
132         java.util.Properties JavaDoc myProps = getDeploymentProperties();
133         try {
134              String JavaDoc methodToInvoke = "deploy";
135              ObjectName JavaDoc objectName =
136                     new ObjectName JavaDoc(
137                         "com.sun.appserv:type=applications,category=config");
138              printMBeanTestInfo(objectName);
139              Object JavaDoc deployResult = server.invoke(objectName,
140                 methodToInvoke, new Object JavaDoc[]{myProps, targets},
141                 new String JavaDoc[]{"java.util.Properties", "[Ljava.lang.String;"});
142         } catch(Throwable JavaDoc e) {
143             System.out.println("Something Failed!");
144             e.printStackTrace();
145         }
146     }
147     
148     /**
149      *
150      */

151     public void undeployApplication(java.io.File JavaDoc appArchive, String JavaDoc [] targets) {
152         final MBeanServerConnection JavaDoc server = getMBeanServer();
153         java.util.Properties JavaDoc myProps = getDeploymentProperties();
154         try {
155              String JavaDoc methodToInvoke = "undeploy";
156              ObjectName JavaDoc objectName =
157                     new ObjectName JavaDoc(
158                         "com.sun.appserv:type=applications,category=config");
159              Object JavaDoc undeployResult = server.invoke(objectName,
160                 methodToInvoke, new Object JavaDoc[]{myProps, targets},
161                 new String JavaDoc[]{"java.util.Properties", "[Ljava.lang.String;"});
162         } catch(Throwable JavaDoc e) {
163             System.out.println("Something failed when undeploying!");
164             e.printStackTrace();
165         }
166     }
167     
168     /**
169      *
170      */

171     public void printAllDeployedComponents() {
172        final MBeanServerConnection JavaDoc server = getMBeanServer();
173        try {
174              String JavaDoc methodToInvoke = "getAllDeployedComponents";
175              ObjectName JavaDoc objectName =
176                     new ObjectName JavaDoc(
177                         "com.sun.appserv:type=applications,category=config");
178              ObjectName JavaDoc[] result = (ObjectName JavaDoc[])server.invoke(objectName,
179                 methodToInvoke, new Object JavaDoc[]{},
180                 new String JavaDoc[]{});
181              for(int i = 0; i < result.length; i++) {
182                 System.out.println("Deployed: " + result[i]);
183              }
184         } catch(Throwable JavaDoc e) {
185             System.out.println("Something failed when getting all deployed components!");
186             e.printStackTrace();
187         }
188     }
189     
190     /**
191      *
192      */

193     public String JavaDoc[] getTargets() {
194        final MBeanServerConnection JavaDoc server = getMBeanServer();
195        String JavaDoc[] targets = null;
196        System.out.println("Testing getTargets!");
197        try {
198              String JavaDoc methodToInvoke = "getTargets";
199              ObjectName JavaDoc objectName =
200                     new ObjectName JavaDoc(
201                         "com.sun.appserv:type=applications,category=config");
202              targets = (String JavaDoc[])server.invoke(objectName,
203                 methodToInvoke, new Object JavaDoc[]{},
204                 new String JavaDoc[]{});
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 JavaDoc e) {
211             System.out.println("Something failed when getting all targets!");
212             e.printStackTrace();
213         }
214        return targets;
215     }
216     
217     /**
218      *
219      */

220     public void getServers() {
221        final MBeanServerConnection JavaDoc server = getMBeanServer();
222        try {
223              String JavaDoc methodToInvoke = "getServer";
224              ObjectName JavaDoc objectName =
225                     new ObjectName JavaDoc(
226                         "com.sun.appserv:type=servers,category=config");
227              ObjectName JavaDoc[] result = (ObjectName JavaDoc[])server.invoke(objectName,
228                 methodToInvoke, new Object JavaDoc[]{},
229                 new String JavaDoc[]{});
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 JavaDoc e) {
235             System.out.println("Something failed when getting all targets!");
236             e.printStackTrace();
237         }
238     }
239     
240     /**
241      *
242      */

243     public void getClusters() {
244        final MBeanServerConnection JavaDoc server = getMBeanServer();
245        try {
246              String JavaDoc methodToInvoke = "getCluster";
247              ObjectName JavaDoc objectName =
248                     new ObjectName JavaDoc(
249                         "com.sun.appserv:type=clusters,category=config");
250              ObjectName JavaDoc[] result = (ObjectName JavaDoc[])server.invoke(objectName,
251                 methodToInvoke, new Object JavaDoc[]{},
252                 new String JavaDoc[]{});
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 JavaDoc e) {
258             System.out.println("Something failed when getting all targets!");
259             e.printStackTrace();
260         }
261     }
262     
263     /**
264      *
265      */

266     public java.util.Properties JavaDoc getDeploymentProperties() {
267         java.util.Properties JavaDoc props = new java.util.Properties JavaDoc();
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     /**
277      *
278      */

279     private void printMBeanTestInfo(ObjectName JavaDoc objectName) {
280         System.out.println("Testing mbean invocation on <" + objectName + ">");
281     }
282     
283     /**
284      *
285      */

286     private ObjectName JavaDoc getSampleObjectName() {
287         ObjectName JavaDoc name = null;
288         try {
289             name =
290                 new ObjectName JavaDoc("com.sun.appserv:type=jdbc-connection-pool," +
291                     "name=__TimerPool,category=config");
292         } catch(Exception JavaDoc e) {
293             e.printStackTrace();
294         }
295         return name;
296     }
297     
298     /**
299      *
300      */

301     private MBeanServerConnection JavaDoc getMBeanServer() {
302         MBeanServerConnection JavaDoc conn = null;
303         try {
304             conn = connector.getMBeanServerConnection();
305         } catch(Exception JavaDoc e) {
306             System.out.println("Could not retrieve connection! "
307                 + e.getMessage());
308             e.printStackTrace();
309         }
310         return conn;
311     }
312     
313     /**
314      *
315      *
316      */

317     private void initConnector() {
318         try {
319             connector = JMXConnectorFactory.connect(
320                 new JMXServiceURL JavaDoc("service:jmx:s1ashttp://" + HOST +
321                     ":" + PORT), initConnectorEnvironment());
322         } catch (Exception JavaDoc ex) {
323             System.out.println("Remote Connect Failed!!!");
324             System.out.println(ex.getMessage());
325             throw new RuntimeException JavaDoc(ex);
326         }
327     }
328     
329     /**
330      *
331      */

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