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: * @(#)JBossJBIBootstrap.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 org.jboss.mx.loading.UnifiedClassLoader3;
032:
033: import java.io.File;
034: import java.lang.reflect.Constructor;
035: import java.lang.reflect.Method;
036: import java.net.MalformedURLException;
037: import java.net.URI;
038: import java.net.URL;
039: import java.net.URLClassLoader;
040: import java.util.ArrayList;
041: import java.util.List;
042: import java.util.Properties;
043: import java.util.logging.Logger;
044:
045: /**
046: * This is the bootstrap class for the JBI runtime framework. It implements the
047: * JBoss App Server <CODE>Service</CODE> interface and is responsible
048: * for creating the class loader hierarchy required by the JBI runtime.
049: *
050: * @author Sun Microsystems Inc.
051: */
052: public class JBossJBIBootstrap implements JBossJBIBootstrapMBean {
053: /**
054: * JBI Framework for JBoss
055: */
056: private static final String JBI_FRAMEWORK_CLASS_NAME = "com.sun.jbi.framework.jboss.JBossASJBIFramework";
057:
058: /**
059: * System property for the JBoss Server Name
060: */
061: private static final String JBOSS_SERVER_NAME = "jboss.server.name";
062:
063: /**
064: * System property for the JBoss Server Home URL
065: */
066: private static final String JBOSS_HOME_URL = "jboss.server.home.url";
067:
068: /** JSR208 interfaces. */
069: private static final String JBI_JAR_NAME = "jbi.jar";
070:
071: /** JBI runtime interfaces exposed to components. */
072: private static final String JBI_EXT_JAR_NAME = "jbi-ext.jar";
073:
074: /**
075: * The Framework ClassLoader
076: */
077: private ClassLoader mFrameworkClassLoader;
078:
079: /**
080: * Instance of the JBI Framework.
081: */
082: private Object mJbiFramework;
083:
084: /**
085: * Environment Properties
086: */
087: private Properties mEnvironment;
088:
089: /**
090: * List of jars that should not be included in the runtime classloader.
091: */
092: private List<String> mBlacklistJars = new ArrayList<String>();
093:
094: /**
095: * Folder where JBoss is installed.
096: */
097: private String mInstallRoot;
098: private Logger mLog = Logger.getLogger(this .getClass().getPackage()
099: .getName());
100:
101: /**
102: * Called by the JBoss App server at the deployment time of the sar file. This will
103: * create the JBI Framework.
104: */
105: public void create() {
106: mLog.fine("Creating JBI Framework");
107: mBlacklistJars.add(JBI_JAR_NAME);
108: mBlacklistJars.add(JBI_EXT_JAR_NAME);
109:
110: try {
111: String serverName = System.getProperty(JBOSS_SERVER_NAME);
112:
113: String jbossurl = System.getProperty(JBOSS_HOME_URL);
114: URI jbossuri = new URI(jbossurl);
115: mInstallRoot = System.getProperty("jbi.install.root");
116:
117: String mInstanceRoot = jbossuri.getPath();
118: mEnvironment = new Properties();
119: mEnvironment.put("instance.name", serverName);
120: mEnvironment.put("instance.root", mInstanceRoot);
121: mEnvironment.put("install.root", mInstallRoot);
122: createJBIFramework();
123: loadJBIFramework();
124: } catch (Exception e) {
125: mLog.warning("Error creating JBI Framework: "
126: + e.getMessage());
127: }
128: }
129:
130: public void start() {
131: }
132:
133: public void stop() {
134: }
135:
136: public void destroy() {
137: destroyJBIFramework();
138: }
139:
140: /**
141: * Creates the JBI framework using the appropriate classloading structure.
142: * @throws Exception - Exception Creating Framework Class Loader
143: */
144: private void createJBIFramework() throws Exception {
145: Class fwClass;
146: Constructor fwCtor;
147:
148: try {
149: createExtensionClassLoader();
150: createFrameworkClassLoader();
151:
152: fwClass = mFrameworkClassLoader
153: .loadClass(JBI_FRAMEWORK_CLASS_NAME);
154: fwCtor = fwClass.getDeclaredConstructor(Properties.class);
155: mJbiFramework = fwCtor.newInstance(mEnvironment);
156: } catch (Exception ex) {
157: throw new Exception("Failed to create JBI framework: "
158: + ex.getMessage());
159: }
160: }
161:
162: /**
163: * Creates a separate runtime classloader to avoid namespace pollution
164: * between the component classloading hierarchy and the JBI implementation.
165: * At present, this method is greedy and includes any file in the lib
166: * directory in the runtime classpath.
167: */
168: private void createFrameworkClassLoader() {
169: ArrayList<URL> cpList = new ArrayList<URL>();
170: URL[] cpURLs = new URL[0];
171: File libDir = new File(mInstallRoot);
172: File libDir2 = new File(mInstallRoot, "lib");
173:
174: // Everything in the lib directory goes into the classpath
175: for (File lib : libDir.listFiles()) {
176: try {
177: if (mBlacklistJars.contains(lib.getName())) {
178: // skip blacklisted jars
179: continue;
180: }
181:
182: cpList.add(lib.toURL());
183: } catch (java.net.MalformedURLException urlEx) {
184: mLog.warning("Bad library URL: " + urlEx.getMessage());
185: }
186: }
187:
188: for (File lib2 : libDir2.listFiles()) {
189: try {
190: cpList.add(lib2.toURL());
191: } catch (MalformedURLException e) {
192: mLog.warning("Bad library URL: " + e.getMessage());
193: }
194: }
195:
196: cpURLs = cpList.toArray(cpURLs);
197: mFrameworkClassLoader = new URLClassLoader(cpURLs, getClass()
198: .getClassLoader());
199: }
200:
201: /** Creates a separate extension classloader for the component classloading
202: * chain. All jars added in the lib/ext directory are automatically added
203: * to this classloader's classpath.
204: */
205: private void createExtensionClassLoader() {
206: ClassLoader loader = Thread.currentThread()
207: .getContextClassLoader();
208: UnifiedClassLoader3 ucl = (UnifiedClassLoader3) loader;
209: File libDir = new File(mInstallRoot, "lib/ext");
210:
211: if (libDir.exists() || libDir.isDirectory()) {
212: try {
213: // Add the top-level ext directory
214: ucl.addURL(libDir.toURL());
215:
216: // Everything in the lib/ext directory goes into the classpath
217: for (File lib : libDir.listFiles()) {
218: ucl.addURL(lib.toURL());
219: }
220: } catch (java.net.MalformedURLException urlEx) {
221: mLog.warning("Bad library URL: " + urlEx.getMessage());
222: }
223: }
224: }
225:
226: /**
227: * Loads the JBI framework.
228: */
229: void loadJBIFramework() {
230: try {
231: invoke(mJbiFramework, "load");
232: } catch (Throwable t) {
233: mLog.severe(t.toString());
234: }
235: }
236:
237: /**
238: * Destroys the JBI Framework
239: */
240: private void destroyJBIFramework() {
241: try {
242: invoke(mJbiFramework, "unload");
243: } catch (Throwable t) {
244: mLog.severe(t.toString());
245: }
246: }
247:
248: /**
249: * This method is used to invoke a given method in a given object using
250: * reflection. This bootstrap class uses this method to invoke the load
251: * method on the framework.
252: * @param obj the object in which the method is to be invoked
253: * @param method the method name
254: * @param params the list of parameteres needed by the method to be invoked
255: * @return - The ojbect that was invoked.
256: * @throws Throwable - InvocationTargetException.
257: */
258: private Object invoke(Object obj, String method, Object... params)
259: throws Throwable {
260: Object result = null;
261:
262: try {
263: for (Method m : obj.getClass().getDeclaredMethods()) {
264: if (m.getName().equals(method)) {
265: result = m.invoke(obj, params);
266:
267: break;
268: }
269: }
270:
271: return result;
272: } catch (java.lang.reflect.InvocationTargetException itEx) {
273: throw itEx.getTargetException();
274: }
275: }
276: }
|