001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: EjbManagerWrapper.java 4799 2004-05-25 14:26:36Z sauthieg $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ejb.deployment.lib.wrapper;
025:
026: import java.lang.reflect.InvocationTargetException;
027: import java.lang.reflect.Method;
028: import java.net.URL;
029: import java.net.URLClassLoader;
030:
031: import org.objectweb.jonas_ejb.deployment.api.DeploymentDesc;
032:
033: import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
034:
035: import org.objectweb.jonas.common.Log;
036: import org.objectweb.jonas.server.LoaderManager;
037:
038: import org.objectweb.util.monolog.api.BasicLevel;
039: import org.objectweb.util.monolog.api.Logger;
040:
041: /**
042: * Wrap the EjbDeploymentDescManager to solve ClassLoader problems linked to Digester.
043: *
044: * @author Guillaume Sauthier
045: */
046: public class EjbManagerWrapper {
047:
048: /**
049: * EjbDeploymentDescManager fully qualified classname
050: */
051: private static final String EJB_MANAGER_CLASSNAME = "org.objectweb.jonas_ejb.deployment.lib.EjbDeploymentDescManager";
052:
053: /**
054: * logger
055: */
056: private static Logger logger = Log.getLogger(Log.JONAS_EAR_PREFIX);
057:
058: /**
059: * Private empty constructor for utility class
060: */
061: private EjbManagerWrapper() {
062: }
063:
064: /**
065: * Wrap EjbDeploymentDescManager.getInstance().getDeploymentDesc()
066: *
067: * @param url EjbJar URL
068: * @param moduleCL EjbJar ClassLoader
069: * @param earCL Application ClassLoader
070: *
071: * @return the EjbJar DeploymentDesc of the given EjbJar
072: *
073: * @throws DeploymentDescException When DeploymentDesc cannot be instanciated
074: */
075: public static DeploymentDesc getDeploymentDesc(URL url,
076: ClassLoader moduleCL, ClassLoader earCL)
077: throws DeploymentDescException {
078: LoaderManager lm = LoaderManager.getInstance();
079: DeploymentDesc dd = null;
080:
081: try {
082: ClassLoader tools = lm.getToolsLoader();
083: Class manager = tools.loadClass(EJB_MANAGER_CLASSNAME);
084: Method m = manager.getDeclaredMethod("getInstance",
085: new Class[] {});
086: Object instance = m.invoke(null, new Object[] {});
087: m = manager.getDeclaredMethod("getDeploymentDesc",
088: new Class[] { URL.class, ClassLoader.class,
089: ClassLoader.class });
090: dd = (DeploymentDesc) m.invoke(instance, new Object[] {
091: url, moduleCL, earCL });
092: } catch (InvocationTargetException ite) {
093: Throwable t = ite.getTargetException();
094: if (DeploymentDescException.class.isInstance(t)) {
095: throw (DeploymentDescException) ite
096: .getTargetException();
097: } else {
098: throw new DeploymentDescException(
099: "EjbDeploymentDescManager.getDeploymentDesc fails",
100: t);
101: }
102: } catch (Exception e) {
103: // TODO add i18n here
104: throw new DeploymentDescException(
105: "Problems when using reflection on EjbDeploymentDescManager",
106: e);
107: }
108:
109: return dd;
110: }
111:
112: /**
113: * Wrap EjbDeploymentDescManager.getInstance().setAvailableEjbJarsAndAltDDs()
114: *
115: * @param earClassLoader Application ClassLoader
116: * @param jarUrls Array of EjbJar URLs
117: * @param ejbsAltDDs Array of alternatives EjbJar Descriptor URLs
118: */
119: public static void setAvailableEjbJarsAndAltDDs(
120: URLClassLoader earClassLoader, URL[] jarUrls,
121: URL[] ejbsAltDDs) {
122: LoaderManager lm = LoaderManager.getInstance();
123: try {
124: ClassLoader tools = lm.getToolsLoader();
125: Class manager = tools.loadClass(EJB_MANAGER_CLASSNAME);
126: Method m = manager.getDeclaredMethod("getInstance",
127: new Class[] {});
128: Object instance = m.invoke(null, new Object[] {});
129: m = manager.getDeclaredMethod(
130: "setAvailableEjbJarsAndAltDDs",
131: new Class[] { ClassLoader.class, URL[].class,
132: URL[].class });
133: m.invoke(instance, new Object[] { earClassLoader, jarUrls,
134: ejbsAltDDs });
135: } catch (Exception e) {
136: // Should never occurs
137: logger.log(BasicLevel.ERROR, e);
138: }
139: }
140:
141: /**
142: * Wrap EjbDeploymentDescManager.setParsingWithValidation()
143: *
144: * @param b true/false
145: */
146: public static void setParsingWithValidation(boolean b) {
147: LoaderManager lm = LoaderManager.getInstance();
148: try {
149: ClassLoader tools = lm.getToolsLoader();
150: Class manager = tools.loadClass(EJB_MANAGER_CLASSNAME);
151: Method m = manager.getDeclaredMethod(
152: "setParsingWithValidation",
153: new Class[] { boolean.class });
154: m.invoke(null, new Object[] { new Boolean(b) });
155: } catch (Exception e) {
156: // Should never occurs
157: logger.log(BasicLevel.ERROR, e);
158: }
159: }
160:
161: /**
162: * Wrap EjbDeploymentDescManager.getInstance().removeCache()
163: *
164: * @param classLoader EjbJar Classloader
165: */
166: public static void removeCache(ClassLoader classLoader) {
167: LoaderManager lm = LoaderManager.getInstance();
168: try {
169: ClassLoader tools = lm.getToolsLoader();
170: Class manager = tools.loadClass(EJB_MANAGER_CLASSNAME);
171: Method m = manager.getDeclaredMethod("getInstance",
172: new Class[] {});
173: Object instance = m.invoke(null, new Object[] {});
174: m = manager.getDeclaredMethod("removeCache",
175: new Class[] { ClassLoader.class });
176: m.invoke(instance, new Object[] { classLoader });
177: } catch (Exception e) {
178: // Should never occurs
179: logger.log(BasicLevel.ERROR, e);
180: }
181: }
182:
183: }
|