001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)JBossASJBIFramework.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.framework.jboss;
030:
031: import com.sun.jbi.framework.JBIFramework;
032:
033: import java.util.Properties;
034: import java.util.logging.Logger;
035:
036: import javax.management.MBeanServer;
037: import javax.management.ObjectName;
038: import javax.management.remote.JMXConnectorServer;
039: import javax.management.remote.JMXConnectorServerFactory;
040: import javax.management.remote.JMXServiceURL;
041:
042: import javax.naming.Context;
043: import javax.naming.InitialContext;
044: import javax.naming.NamingException;
045:
046: /**
047: * This is the JBI Framework class for JBoss.
048: *
049: * @author Sun Microsystems, Inc.
050: */
051: public class JBossASJBIFramework extends JBIFramework implements
052: JBossASJBIFrameworkMBean {
053: /**
054: * JBI Home property, which is the install root.
055: */
056: private static final String JBI_HOME = "com.sun.jbi.home";
057:
058: /**
059: * Jboss naming service.
060: */
061: private static final String JBOSS_NAMING_SERVICE = "jboss:service=Naming";
062:
063: /**
064: * key for the install root property *
065: */
066: public static final String INSTALL_ROOT = "install.root";
067:
068: /**
069: * key for the instance name property *
070: */
071: public static final String INSTANCE_NAME = "instance.name";
072:
073: /**
074: * key for the instance root property.
075: */
076: public static final String INSTANCE_ROOT = "instance.root";
077:
078: /**
079: * Configuration defaults.
080: */
081: private static final String DEFAULT_INSTALL_ROOT = System
082: .getProperty("user.dir");
083:
084: /**
085: * Default Jboss instance name.
086: */
087: private static final String DEFAULT_INSTANCE_NAME = "default";
088:
089: /**
090: * Naming prefix for Jboss
091: */
092: private static final String JBOSS_NAMING_PREFIX = "";
093:
094: /**
095: * Determines wheter or not the Framework has been loaded.
096: */
097: private boolean mLoaded;
098:
099: /**
100: * The Logger instance for the framework.
101: */
102: private Logger mLog = Logger.getLogger(this .getClass().getPackage()
103: .getName());
104:
105: /**
106: * Folder where JBoss is installed.
107: */
108: private String mInstallRoot;
109:
110: /**
111: * JMX connector server for accepting JMX connections.
112: */
113: private JMXConnectorServer mConnectorServer;
114:
115: /**
116: * platform context instance
117: */
118: private JBossPlatformContext mPlatformContext;
119:
120: public JBossASJBIFramework(Properties env) {
121: super ();
122: mInstallRoot = env.getProperty(INSTALL_ROOT,
123: DEFAULT_INSTALL_ROOT);
124:
125: mPlatformContext = new JBossPlatformContext(env.getProperty(
126: INSTANCE_NAME, DEFAULT_INSTANCE_NAME), mInstallRoot,
127: env.getProperty(INSTANCE_ROOT, DEFAULT_INSTALL_ROOT));
128: }
129:
130: /**
131: * This method is called by the Bootstrap to load the JBI Framework.
132: *
133: * @throws java.lang.Exception any exception.
134: */
135: public void load() throws java.lang.Exception {
136: try {
137: Properties props = new Properties();
138: props.put(JBI_HOME, mInstallRoot);
139:
140: MBeanServer mbs;
141: mbs = mPlatformContext.getMBeanServer();
142: startJMXConnectorServer(mbs);
143: super .init(mPlatformContext, props);
144: } catch (javax.jbi.JBIException je) {
145: throw je;
146: }
147:
148: try {
149: InitialContext ctx = new InitialContext();
150: Context subctx = null;
151:
152: try {
153: Object obj = ctx.lookup("jbi");
154:
155: // great ther is already one
156: subctx = (Context) obj;
157: } catch (NamingException ne) {
158: subctx = ctx.createSubcontext("jbi");
159: }
160:
161: mConnectorServer.start();
162: super .startup(ctx, JBOSS_NAMING_PREFIX);
163: super .prepare();
164: mLog.info("JBI Framework - Created");
165: super .ready(true);
166: mLog.info("JBI Framework - Started");
167: // JBI framework has been loaded
168: mLoaded = true;
169: } catch (javax.jbi.JBIException sje) {
170: throw sje;
171: }
172: }
173:
174: /**
175: * Starts the JMX connector server. This is a feature that is supported
176: * in J2SE 1.5 ( JMX-remoting). When run on 1.4 the jmx-remoting jar should
177: * be supplied. This is only a temporary measure as the Jboss AS 4.0.3
178: * does not have a JSR 160 implementation. Once the implementation is
179: * available that can be used instead of the jms-remoting RI.
180: *
181: * @param mbs Mbean server to be used.
182: */
183: private void startJMXConnectorServer(MBeanServer mbs) {
184: try {
185: ObjectName mbeanName = new ObjectName(JBOSS_NAMING_SERVICE);
186: Integer port = (Integer) mbs
187: .getAttribute(mbeanName, "Port");
188:
189: String jmxurl = "service:jmx:rmi:///jndi/jnp://localhost:"
190: + port.toString() + "/jbiconnector";
191: mLog.info("JMX Url: " + jmxurl);
192:
193: JMXServiceURL url = new JMXServiceURL(jmxurl);
194: mConnectorServer = JMXConnectorServerFactory
195: .newJMXConnectorServer(url, null, mbs);
196: mLog
197: .info("RMI Connector server started - JMXServiceURL to access "
198: + " the mbean server would be " + jmxurl);
199: } catch (Exception e) {
200: mLog.severe("Cannot create Connector " + e.getMessage());
201: }
202: }
203:
204: /**
205: * Queries the state of the JBI Framework.
206: *
207: * @return true if the JBI framework is loaded, false otherwise.
208: */
209: public boolean isLoaded() {
210: return mLoaded;
211: }
212:
213: /**
214: * Unloads the JBI framework. When this method retuns, all
215: * public interfaces, system services, and JMX connector (if configured)
216: * have been destroyed.
217: *
218: * @throws java.lang.Exception failed to unload JBI framework
219: */
220: public synchronized void unload() throws Exception {
221: if (!mLoaded) {
222: return;
223: }
224:
225: shutdown();
226: mLog.info("JBI Framework - Stopped");
227: mConnectorServer.stop();
228: mLog.info("JBI RMI Connector server stopped ");
229: terminate();
230:
231: mLoaded = false;
232: }
233: }
|