KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_web > deployment > lib > wrapper > WebManagerWrapper


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: WebManagerWrapper.java,v 1.1 2004/05/25 14:26:35 sauthieg Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas_web.deployment.lib.wrapper;
26
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.net.URLClassLoader JavaDoc;
31
32 import org.objectweb.jonas_web.deployment.api.WebContainerDeploymentDesc;
33 import org.objectweb.jonas_web.deployment.api.WebContainerDeploymentDescException;
34
35 import org.objectweb.jonas.common.Log;
36 import org.objectweb.jonas.server.LoaderManager;
37
38 import org.objectweb.util.monolog.api.BasicLevel;
39 import org.objectweb.util.monolog.api.Logger;
40
41 /**
42  * Wrap the WebDeploymentDescManager to solve ClassLoader problems linked to
43  * Digester.
44  *
45  * @author Guillaume Sauthier
46  */

47 public class WebManagerWrapper {
48
49     /**
50      * logger
51      */

52     private static Logger logger = Log.getLogger(Log.JONAS_EAR_PREFIX);
53
54     /**
55      * WebDeploymentDescManager classname
56      */

57     private static final String JavaDoc WEBMANAGER_CLASSNAME = "org.objectweb.jonas_web.deployment.lib.WebDeploymentDescManager";
58
59     /**
60      * Private Empty constructor for utility class
61      */

62     private WebManagerWrapper() { }
63
64     /**
65      * Wrap the WebDeploymentDescManager.getInstance().getDeploymentDesc() call.
66      *
67      * @param url WebApp URL
68      * @param moduleCL WebApp module ClassLoader
69      * @param earCL Application ClassLoader
70      *
71      * @return the WebContainerDeploymentDesc of the given WebApp
72      *
73      * @throws WebContainerDeploymentDescException When WebContainerDeploymentDesc cannot be instanciated
74      */

75     public static WebContainerDeploymentDesc getDeploymentDesc(URL JavaDoc url, ClassLoader JavaDoc moduleCL, ClassLoader JavaDoc earCL)
76             throws WebContainerDeploymentDescException {
77         LoaderManager lm = LoaderManager.getInstance();
78         WebContainerDeploymentDesc webDD = null;
79
80         try {
81             ClassLoader JavaDoc tools = lm.getToolsLoader();
82             Class JavaDoc manager = tools.loadClass(WEBMANAGER_CLASSNAME);
83             Method JavaDoc m = manager.getDeclaredMethod("getInstance", new Class JavaDoc[] {});
84             Object JavaDoc instance = m.invoke(null, new Object JavaDoc[] {});
85             m = manager.getDeclaredMethod("getDeploymentDesc", new Class JavaDoc[] {URL JavaDoc.class, ClassLoader JavaDoc.class,
86                     ClassLoader JavaDoc.class});
87             webDD = (WebContainerDeploymentDesc) m.invoke(instance, new Object JavaDoc[] {url, moduleCL, earCL});
88         } catch (InvocationTargetException JavaDoc ite) {
89             Throwable JavaDoc t = ite.getTargetException();
90             if (WebContainerDeploymentDescException.class.isInstance(t)) {
91                 throw (WebContainerDeploymentDescException) ite.getTargetException();
92             } else {
93                 throw new WebContainerDeploymentDescException("WebDeploymentDescManager.getDeploymentDesc fails", t);
94             }
95         } catch (Exception JavaDoc e) {
96             // TODO add i18n here
97
throw new WebContainerDeploymentDescException("Problems when using reflection on WebDeploymentDescManager", e);
98         }
99
100         return webDD;
101     }
102
103     /**
104      * Wrap the WebDeploymentDescManager.getInstance().setAltDD() call.
105      *
106      * @param earClassLoader Application ClassLoader
107      * @param warUrls Array of WebApp URLs
108      * @param warsAltDDs Array of Alternatives WebApp Descriptor URL
109      */

110     public static void setAltDD(URLClassLoader JavaDoc earClassLoader, URL JavaDoc[] warUrls, URL JavaDoc[] warsAltDDs) {
111         LoaderManager lm = LoaderManager.getInstance();
112
113         try {
114             ClassLoader JavaDoc tools = lm.getToolsLoader();
115             Class JavaDoc manager = tools.loadClass(WEBMANAGER_CLASSNAME);
116             Method JavaDoc m = manager.getDeclaredMethod("getInstance", new Class JavaDoc[] {});
117             Object JavaDoc instance = m.invoke(null, new Object JavaDoc[] {});
118             m = manager.getDeclaredMethod("setAltDD", new Class JavaDoc[] {ClassLoader JavaDoc.class, URL JavaDoc[].class, URL JavaDoc[].class});
119             m.invoke(instance, new Object JavaDoc[] {earClassLoader, warUrls, warsAltDDs});
120         } catch (Exception JavaDoc e) {
121             // Should never occurs
122
logger.log(BasicLevel.ERROR, e);
123         }
124     }
125
126     /**
127      * Wrap the WebDeploymentDescManager.setParsingWithValidation() call.
128      *
129      * @param b true/false
130      */

131     public static void setParsingWithValidation(boolean b) {
132         LoaderManager lm = LoaderManager.getInstance();
133
134         try {
135             ClassLoader JavaDoc tools = lm.getToolsLoader();
136             Class JavaDoc manager = tools.loadClass(WEBMANAGER_CLASSNAME);
137             Method JavaDoc m = manager.getDeclaredMethod("setParsingWithValidation", new Class JavaDoc[] {boolean.class});
138             m.invoke(null, new Object JavaDoc[] {new Boolean JavaDoc(b)});
139         } catch (Exception JavaDoc e) {
140             // Should never occurs
141
logger.log(BasicLevel.ERROR, e);
142         }
143     }
144
145     /**
146      * Wrap the WebDeploymentDescManager.getInstance().removeCache() call.
147      *
148      * @param classLoader WebApp ClassLoader
149      */

150     public static void removeCache(ClassLoader JavaDoc classLoader) {
151         LoaderManager lm = LoaderManager.getInstance();
152
153         try {
154             ClassLoader JavaDoc tools = lm.getToolsLoader();
155             Class JavaDoc manager = tools.loadClass(WEBMANAGER_CLASSNAME);
156             Method JavaDoc m = manager.getDeclaredMethod("getInstance", new Class JavaDoc[] {});
157             Object JavaDoc instance = m.invoke(null, new Object JavaDoc[] {});
158             m = manager.getDeclaredMethod("removeCache", new Class JavaDoc[] {ClassLoader JavaDoc.class});
159             m.invoke(instance, new Object JavaDoc[] {classLoader});
160         } catch (Exception JavaDoc e) {
161             // Should never occurs
162
logger.log(BasicLevel.ERROR, e);
163         }
164     }
165
166     /**
167      * Wrap the WebDeploymentDescManager.getDeploymentDesc() call.
168      *
169      * @param warName war filename
170      * @param cl WebApp ClassLoader
171      *
172      * @return the WebContainerDeploymentDesc of the given WebApp
173      *
174      * @throws WebContainerDeploymentDescException When WebContainerDeploymentDesc cannot be instanciated
175      */

176     public static WebContainerDeploymentDesc getDeploymentDesc(String JavaDoc warName, ClassLoader JavaDoc cl)
177             throws WebContainerDeploymentDescException {
178         LoaderManager lm = LoaderManager.getInstance();
179         WebContainerDeploymentDesc desc = null;
180
181         try {
182             ClassLoader JavaDoc tools = lm.getToolsLoader();
183             Class JavaDoc manager = tools.loadClass(WEBMANAGER_CLASSNAME);
184             Method JavaDoc m = manager.getDeclaredMethod("getDeploymentDesc", new Class JavaDoc[] {String JavaDoc.class, ClassLoader JavaDoc.class});
185             desc = (WebContainerDeploymentDesc) m.invoke(null, new Object JavaDoc[] {warName, cl});
186         } catch (InvocationTargetException JavaDoc ite) {
187             Throwable JavaDoc t = ite.getTargetException();
188             if (WebContainerDeploymentDescException.class.isInstance(t)) {
189                 throw (WebContainerDeploymentDescException) ite.getTargetException();
190             } else {
191                 throw new WebContainerDeploymentDescException("WebDeploymentDescManager.getDeploymentDesc fails", t);
192             }
193         } catch (Exception JavaDoc e) {
194             // TODO add i18n here
195
throw new WebContainerDeploymentDescException("Problems when using reflection on WebDeploymentDescManager", e);
196         }
197
198         return desc;
199     }
200
201 }
Popular Tags