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: WebManagerWrapper.java 4799 2004-05-25 14:26:36Z sauthieg $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_web.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_web.deployment.api.WebContainerDeploymentDesc;
032: import org.objectweb.jonas_web.deployment.api.WebContainerDeploymentDescException;
033:
034: import org.objectweb.jonas.common.Log;
035: import org.objectweb.jonas.server.LoaderManager;
036:
037: import org.objectweb.util.monolog.api.BasicLevel;
038: import org.objectweb.util.monolog.api.Logger;
039:
040: /**
041: * Wrap the WebDeploymentDescManager to solve ClassLoader problems linked to
042: * Digester.
043: *
044: * @author Guillaume Sauthier
045: */
046: public class WebManagerWrapper {
047:
048: /**
049: * logger
050: */
051: private static Logger logger = Log.getLogger(Log.JONAS_EAR_PREFIX);
052:
053: /**
054: * WebDeploymentDescManager classname
055: */
056: private static final String WEBMANAGER_CLASSNAME = "org.objectweb.jonas_web.deployment.lib.WebDeploymentDescManager";
057:
058: /**
059: * Private Empty constructor for utility class
060: */
061: private WebManagerWrapper() {
062: }
063:
064: /**
065: * Wrap the WebDeploymentDescManager.getInstance().getDeploymentDesc() call.
066: *
067: * @param url WebApp URL
068: * @param moduleCL WebApp module ClassLoader
069: * @param earCL Application ClassLoader
070: *
071: * @return the WebContainerDeploymentDesc of the given WebApp
072: *
073: * @throws WebContainerDeploymentDescException When WebContainerDeploymentDesc cannot be instanciated
074: */
075: public static WebContainerDeploymentDesc getDeploymentDesc(URL url,
076: ClassLoader moduleCL, ClassLoader earCL)
077: throws WebContainerDeploymentDescException {
078: LoaderManager lm = LoaderManager.getInstance();
079: WebContainerDeploymentDesc webDD = null;
080:
081: try {
082: ClassLoader tools = lm.getToolsLoader();
083: Class manager = tools.loadClass(WEBMANAGER_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: webDD = (WebContainerDeploymentDesc) m.invoke(instance,
091: new Object[] { url, moduleCL, earCL });
092: } catch (InvocationTargetException ite) {
093: Throwable t = ite.getTargetException();
094: if (WebContainerDeploymentDescException.class.isInstance(t)) {
095: throw (WebContainerDeploymentDescException) ite
096: .getTargetException();
097: } else {
098: throw new WebContainerDeploymentDescException(
099: "WebDeploymentDescManager.getDeploymentDesc fails",
100: t);
101: }
102: } catch (Exception e) {
103: // TODO add i18n here
104: throw new WebContainerDeploymentDescException(
105: "Problems when using reflection on WebDeploymentDescManager",
106: e);
107: }
108:
109: return webDD;
110: }
111:
112: /**
113: * Wrap the WebDeploymentDescManager.getInstance().setAltDD() call.
114: *
115: * @param earClassLoader Application ClassLoader
116: * @param warUrls Array of WebApp URLs
117: * @param warsAltDDs Array of Alternatives WebApp Descriptor URL
118: */
119: public static void setAltDD(URLClassLoader earClassLoader,
120: URL[] warUrls, URL[] warsAltDDs) {
121: LoaderManager lm = LoaderManager.getInstance();
122:
123: try {
124: ClassLoader tools = lm.getToolsLoader();
125: Class manager = tools.loadClass(WEBMANAGER_CLASSNAME);
126: Method m = manager.getDeclaredMethod("getInstance",
127: new Class[] {});
128: Object instance = m.invoke(null, new Object[] {});
129: m = manager.getDeclaredMethod("setAltDD", new Class[] {
130: ClassLoader.class, URL[].class, URL[].class });
131: m.invoke(instance, new Object[] { earClassLoader, warUrls,
132: warsAltDDs });
133: } catch (Exception e) {
134: // Should never occurs
135: logger.log(BasicLevel.ERROR, e);
136: }
137: }
138:
139: /**
140: * Wrap the WebDeploymentDescManager.setParsingWithValidation() call.
141: *
142: * @param b true/false
143: */
144: public static void setParsingWithValidation(boolean b) {
145: LoaderManager lm = LoaderManager.getInstance();
146:
147: try {
148: ClassLoader tools = lm.getToolsLoader();
149: Class manager = tools.loadClass(WEBMANAGER_CLASSNAME);
150: Method m = manager.getDeclaredMethod(
151: "setParsingWithValidation",
152: new Class[] { boolean.class });
153: m.invoke(null, new Object[] { new Boolean(b) });
154: } catch (Exception e) {
155: // Should never occurs
156: logger.log(BasicLevel.ERROR, e);
157: }
158: }
159:
160: /**
161: * Wrap the WebDeploymentDescManager.getInstance().removeCache() call.
162: *
163: * @param classLoader WebApp ClassLoader
164: */
165: public static void removeCache(ClassLoader classLoader) {
166: LoaderManager lm = LoaderManager.getInstance();
167:
168: try {
169: ClassLoader tools = lm.getToolsLoader();
170: Class manager = tools.loadClass(WEBMANAGER_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: /**
184: * Wrap the WebDeploymentDescManager.getDeploymentDesc() call.
185: *
186: * @param warName war filename
187: * @param cl WebApp ClassLoader
188: *
189: * @return the WebContainerDeploymentDesc of the given WebApp
190: *
191: * @throws WebContainerDeploymentDescException When WebContainerDeploymentDesc cannot be instanciated
192: */
193: public static WebContainerDeploymentDesc getDeploymentDesc(
194: String warName, ClassLoader cl)
195: throws WebContainerDeploymentDescException {
196: LoaderManager lm = LoaderManager.getInstance();
197: WebContainerDeploymentDesc desc = null;
198:
199: try {
200: ClassLoader tools = lm.getToolsLoader();
201: Class manager = tools.loadClass(WEBMANAGER_CLASSNAME);
202: Method m = manager.getDeclaredMethod("getDeploymentDesc",
203: new Class[] { String.class, ClassLoader.class });
204: desc = (WebContainerDeploymentDesc) m.invoke(null,
205: new Object[] { warName, cl });
206: } catch (InvocationTargetException ite) {
207: Throwable t = ite.getTargetException();
208: if (WebContainerDeploymentDescException.class.isInstance(t)) {
209: throw (WebContainerDeploymentDescException) ite
210: .getTargetException();
211: } else {
212: throw new WebContainerDeploymentDescException(
213: "WebDeploymentDescManager.getDeploymentDesc fails",
214: t);
215: }
216: } catch (Exception e) {
217: // TODO add i18n here
218: throw new WebContainerDeploymentDescException(
219: "Problems when using reflection on WebDeploymentDescManager",
220: e);
221: }
222:
223: return desc;
224: }
225:
226: }
|