KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ws > wsgen > wrapper > WsGenWrapper


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2004 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: WsGenWrapper.java,v 1.3 2004/10/08 07:53:18 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_ws.wsgen.wrapper;
27
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30
31 import org.objectweb.jonas.server.LoaderManager;
32
33 /**
34  * WSGen wrapper Used to launch WsGen in DeployFile of JSR 88
35  * @author Florent Benoit
36  */

37 public class WsGenWrapper {
38
39     /**
40      * WsGen fully qualified classname
41      */

42     private static final String JavaDoc WSGEN_CLASSNAME = "org.objectweb.jonas_ws.wsgen.WsGen";
43
44     /**
45      * Number of arguments of execute method
46      */

47     private static final int ARGS_NUMBER = 3;
48
49     /**
50      * Method execute of WsGen
51      */

52     private static Method JavaDoc executeMethod = null;
53
54     /**
55      * WsGen has changed the archive or not ?
56      */

57     private static Method JavaDoc isInputModifiedMethod = null;
58
59     /**
60      * WsGen class
61      */

62     private Object JavaDoc wsgen = null;
63
64     /**
65      * Empty constructor.
66      */

67     public WsGenWrapper() {
68     };
69
70     /**
71      * Wrapper around WsGen.execute(String[]) method
72      * @param fileName name of the file
73      * @return name of the modified/built file
74      * @throws Exception If WsGen fails or if Reflection errors occurs
75      */

76     public String JavaDoc callWsGenExecute(String JavaDoc fileName) throws Exception JavaDoc {
77         LoaderManager lm = LoaderManager.getInstance();
78         ClassLoader JavaDoc old = null;
79
80         try {
81             ClassLoader JavaDoc tools = lm.getToolsLoader();
82             old = Thread.currentThread().getContextClassLoader();
83             Thread.currentThread().setContextClassLoader(tools);
84
85             if (executeMethod == null) {
86                 Class JavaDoc clazz = tools.loadClass(WSGEN_CLASSNAME);
87                 executeMethod = clazz.getDeclaredMethod("execute", new Class JavaDoc[] {String JavaDoc[].class});
88             }
89
90             if (wsgen == null) {
91                 Class JavaDoc clazz = tools.loadClass(WSGEN_CLASSNAME);
92                 wsgen = clazz.newInstance();
93             }
94
95             String JavaDoc[] args = new String JavaDoc[ARGS_NUMBER];
96             args[0] = "-d";
97             args[1] = System.getProperty("java.io.tmpdir");
98             args[2] = fileName;
99
100             return (String JavaDoc) executeMethod.invoke(wsgen, new Object JavaDoc[] {args});
101         } catch (InvocationTargetException JavaDoc e) {
102             if (e.getTargetException() instanceof Error JavaDoc) {
103                 throw (Error JavaDoc) e.getTargetException();
104             } else {
105                 throw new Exception JavaDoc("Exception when executing WsGen.execute(String[])", e.getTargetException());
106             }
107         } catch (Exception JavaDoc e) {
108             throw new Exception JavaDoc("Problems when invoking method WsGen.execute(String[])", e);
109         } finally {
110             if (old != null) {
111                 Thread.currentThread().setContextClassLoader(old);
112             }
113         }
114     }
115
116     /**
117      * Wrapper around WsGen.isModified() method
118      * @return true/false
119      * @throws Exception If WsGen fails or if Reflection errors occurs
120      */

121     public boolean callWsGenIsInputModifed() throws Exception JavaDoc {
122         LoaderManager lm = LoaderManager.getInstance();
123         ClassLoader JavaDoc old = null;
124
125         try {
126             ClassLoader JavaDoc tools = lm.getToolsLoader();
127             old = Thread.currentThread().getContextClassLoader();
128             Thread.currentThread().setContextClassLoader(tools);
129
130             if (isInputModifiedMethod == null) {
131                 Class JavaDoc clazz = tools.loadClass(WSGEN_CLASSNAME);
132                 isInputModifiedMethod = clazz.getDeclaredMethod("isInputModified", new Class JavaDoc[] {});
133             }
134
135             if (wsgen == null) {
136                 Class JavaDoc clazz = tools.loadClass(WSGEN_CLASSNAME);
137                 wsgen = clazz.newInstance();
138             }
139
140             return ((Boolean JavaDoc) isInputModifiedMethod.invoke(wsgen, (Object JavaDoc[]) null)).booleanValue();
141         } catch (InvocationTargetException JavaDoc e) {
142             if (e.getTargetException() instanceof Error JavaDoc) {
143                 throw (Error JavaDoc) e.getTargetException();
144             } else {
145                 throw new Exception JavaDoc("Exception when executing WsGen.isInputModified()", e.getTargetException());
146             }
147         } catch (Exception JavaDoc e) {
148             throw new Exception JavaDoc("Problems when invoking method WsGen.isInputModified()", e);
149         } finally {
150             if (old != null) {
151                 Thread.currentThread().setContextClassLoader(old);
152             }
153         }
154     }
155 }
156
157
Popular Tags