KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > JBossRMIAdaptorHelper


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test;
23
24
25 import org.jboss.logging.Logger;
26
27 import javax.management.MBeanInfo JavaDoc;
28 import javax.management.MBeanServerConnection JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import javax.naming.Context JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.naming.NamingException JavaDoc;
33 import java.util.Hashtable JavaDoc;
34
35
36 /**
37  * Helper Class that connects to the RMA Adaptor on any JBoss node
38  * to provide some services like start/stop JBoss services registered
39  * in the MBean server. Uses MBeanServerConnection.
40  *
41  * @author Anil.Saldhana@jboss.org
42  * @version $Revision: 46409 $
43  */

44
45 public class JBossRMIAdaptorHelper
46 {
47     protected MBeanServerConnection JavaDoc rmiserver = null;
48     protected Logger log = Logger.getLogger(JBossRMIAdaptorHelper.class);
49
50     /**
51      * Constructor
52      */

53     public JBossRMIAdaptorHelper()
54     {
55     }
56
57     /**
58      * Constructor that takes a JNDI url
59      *
60      * @param jndiurl JNDI Url (jnp://localhost:1099)
61      */

62     public JBossRMIAdaptorHelper(String JavaDoc jndiurl)
63     {
64         this();
65         try
66         {
67             //Set Some JNDI Properties
68
Hashtable JavaDoc env = new Hashtable JavaDoc();
69             env.put(Context.PROVIDER_URL, jndiurl);
70             env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
71             env.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces");
72             getMBeanServer(new InitialContext JavaDoc(env));
73         } catch (Exception JavaDoc e)
74         {
75             log.debug(e);
76         }
77     }
78
79     /**
80      * Constructor that takes a JNDI url
81      *
82      * @param ctx InitialContext constructed
83      */

84     public JBossRMIAdaptorHelper(InitialContext JavaDoc ctx)
85     {
86         this();
87         getMBeanServer(ctx);
88     }
89
90
91     /**
92      * Get the Metadata for the MBean
93      *
94      * @param oname ObjectName of the MBean
95      * @return MBeanInfo about the MBean
96      */

97     public MBeanInfo JavaDoc getMBeanInfo(ObjectName JavaDoc oname)
98     {
99         /* Example:
100            //Get the MBeanInfo for the Tomcat MBean
101            ObjectName name = new ObjectName( "jboss.web:service=WebServer" );
102         */

103         MBeanInfo JavaDoc info = null;
104
105         try
106         {
107             info = rmiserver.getMBeanInfo(oname);
108         } catch (Exception JavaDoc e)
109         {
110             log.debug(e);
111         }
112         return info;
113     }
114
115     /**
116      * Invoke an Operation on the MBean
117      *
118      * @param oname ObjectName of the MBean
119      * @param methodname Name of the operation on the MBean
120      * @param pParams Arguments to the operation
121      * @param pSignature Signature for the operation.
122      * @return result from the MBean operation
123      * @throws Exception
124      */

125     public Object JavaDoc invokeOperation(ObjectName JavaDoc oname,
126                                   String JavaDoc methodname, Object JavaDoc[] pParams,
127                                   String JavaDoc[] pSignature)
128             throws Exception JavaDoc
129     {
130         Object JavaDoc result = null;
131         try
132         {
133             /* Example:
134             //Stop the Tomcat Instance
135             Object result = server.invoke(name, "stop",null,null);
136             */

137             result = rmiserver.invoke(oname, methodname, pParams, pSignature);
138         } catch (Exception JavaDoc e)
139         {
140             log.debug(e);
141         }
142
143         return result;
144     }
145
146     private void getMBeanServer(InitialContext JavaDoc ctx)
147     {
148         if (ctx == null)
149             throw new IllegalArgumentException JavaDoc("Initial Context passed is null");
150         try
151         {
152             rmiserver = (MBeanServerConnection JavaDoc) ctx.lookup("jmx/invoker/RMIAdaptor");
153         } catch (NamingException JavaDoc e)
154         {
155             log.debug(e);
156         }
157         if (rmiserver == null) log.debug("RMIAdaptor is null");
158
159     }
160
161
162 }//end class
Popular Tags