001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test;
023:
024: import org.jboss.logging.Logger;
025:
026: import javax.management.MBeanInfo;
027: import javax.management.MBeanServerConnection;
028: import javax.management.ObjectName;
029: import javax.naming.Context;
030: import javax.naming.InitialContext;
031: import javax.naming.NamingException;
032: import java.util.Hashtable;
033:
034: /**
035: * Helper Class that connects to the RMA Adaptor on any JBoss node
036: * to provide some services like start/stop JBoss services registered
037: * in the MBean server. Uses MBeanServerConnection.
038: *
039: * @author Anil.Saldhana@jboss.org
040: * @version $Revision: 57211 $
041: */
042:
043: public class JBossRMIAdaptorHelper {
044: protected MBeanServerConnection rmiserver = null;
045: protected Logger log = Logger
046: .getLogger(JBossRMIAdaptorHelper.class);
047:
048: /**
049: * Constructor
050: */
051: public JBossRMIAdaptorHelper() {
052: }
053:
054: /**
055: * Constructor that takes a JNDI url
056: *
057: * @param jndiurl JNDI Url (jnp://localhost:1099)
058: */
059: public JBossRMIAdaptorHelper(String jndiurl) {
060: this ();
061: try {
062: //Set Some JNDI Properties
063: Hashtable env = new Hashtable();
064: env.put(Context.PROVIDER_URL, jndiurl);
065: env.put(Context.INITIAL_CONTEXT_FACTORY,
066: "org.jnp.interfaces.NamingContextFactory");
067: env.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces");
068: getMBeanServer(new InitialContext(env));
069: } catch (Exception e) {
070: log.debug(e);
071: }
072: }
073:
074: /**
075: * Constructor that takes a JNDI url
076: *
077: * @param ctx InitialContext constructed
078: */
079: public JBossRMIAdaptorHelper(InitialContext ctx) {
080: this ();
081: getMBeanServer(ctx);
082: }
083:
084: /**
085: * Get the Metadata for the MBean
086: *
087: * @param oname ObjectName of the MBean
088: * @return MBeanInfo about the MBean
089: */
090: public MBeanInfo getMBeanInfo(ObjectName oname) {
091: /* Example:
092: //Get the MBeanInfo for the Tomcat MBean
093: ObjectName name = new ObjectName( "jboss.web:service=WebServer" );
094: */
095: MBeanInfo info = null;
096:
097: try {
098: info = rmiserver.getMBeanInfo(oname);
099: } catch (Exception e) {
100: log.debug(e);
101: }
102: return info;
103: }
104:
105: /**
106: * Invoke an Operation on the MBean
107: *
108: * @param oname ObjectName of the MBean
109: * @param methodname Name of the operation on the MBean
110: * @param pParams Arguments to the operation
111: * @param pSignature Signature for the operation.
112: * @return result from the MBean operation
113: * @throws Exception
114: */
115: public Object invokeOperation(ObjectName oname, String methodname,
116: Object[] pParams, String[] pSignature) throws Exception {
117: Object result = null;
118: try {
119: /* Example:
120: //Stop the Tomcat Instance
121: Object result = server.invoke(name, "stop",null,null);
122: */
123: result = rmiserver.invoke(oname, methodname, pParams,
124: pSignature);
125: } catch (Exception e) {
126: log.debug(e);
127: }
128:
129: return result;
130: }
131:
132: private void getMBeanServer(InitialContext ctx) {
133: if (ctx == null)
134: throw new IllegalArgumentException(
135: "Initial Context passed is null");
136: try {
137: rmiserver = (MBeanServerConnection) ctx
138: .lookup("jmx/invoker/RMIAdaptor");
139: } catch (NamingException e) {
140: log.debug(e);
141: }
142: if (rmiserver == null)
143: log.debug("RMIAdaptor is null");
144:
145: }
146:
147: }//end class
|