001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2004 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: WsGenWrapper.java 7454 2005-10-03 08:35:29Z sauthieg $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ws.wsgen.wrapper;
025:
026: import java.lang.reflect.InvocationTargetException;
027: import java.lang.reflect.Method;
028:
029: import org.objectweb.jonas.common.JProp;
030: import org.objectweb.jonas.server.LoaderManager;
031:
032: /**
033: * WSGen wrapper Used to launch WsGen in DeployFile of JSR 88
034: * @author Florent Benoit
035: */
036: public class WsGenWrapper {
037:
038: /**
039: * WsGen fully qualified classname
040: */
041: private static final String WSGEN_CLASSNAME = "org.objectweb.jonas_ws.wsgen.WsGen";
042:
043: /**
044: * Number of arguments of execute method
045: */
046: private static final int ARGS_NUMBER = 3;
047:
048: /**
049: * Method execute of WsGen
050: */
051: private static Method executeMethod = null;
052:
053: /**
054: * WsGen has changed the archive or not ?
055: */
056: private static Method isInputModifiedMethod = null;
057:
058: /**
059: * WsGen class
060: */
061: private Object wsgen = null;
062:
063: /**
064: * Empty constructor.
065: */
066: public WsGenWrapper() {
067: };
068:
069: /**
070: * Wrapper around WsGen.execute(String[]) method
071: * @param fileName name of the file
072: * @return name of the modified/built file
073: * @throws Exception If WsGen fails or if Reflection errors occurs
074: */
075: public String callWsGenExecute(String fileName) throws Exception {
076: return callWsGenExecute(fileName, Boolean.FALSE);
077: }
078:
079: /**
080: * Wrapper around WsGen.execute(String[]) method
081: * @param fileName name of the file
082: * @param unpacked true if the result must be packed in a directory (default = false)
083: * @return name of the modified/built file
084: * @throws Exception If WsGen fails or if Reflection errors occurs
085: */
086: public String callWsGenExecute(String fileName, Boolean unpacked)
087: throws Exception {
088: LoaderManager lm = LoaderManager.getInstance();
089: ClassLoader old = null;
090:
091: try {
092: ClassLoader tools = lm.getToolsLoader();
093: old = Thread.currentThread().getContextClassLoader();
094: Thread.currentThread().setContextClassLoader(tools);
095:
096: if (executeMethod == null) {
097: Class clazz = tools.loadClass(WSGEN_CLASSNAME);
098: executeMethod = clazz.getDeclaredMethod("execute",
099: new Class[] { String[].class });
100: }
101:
102: if (wsgen == null) {
103: Class clazz = tools.loadClass(WSGEN_CLASSNAME);
104: wsgen = clazz.newInstance();
105: }
106:
107: String[] args = null;
108: if (unpacked.booleanValue()) {
109: args = new String[ARGS_NUMBER + 1];
110: } else {
111: args = new String[ARGS_NUMBER];
112: }
113: args[0] = "-d";
114: args[1] = JProp.getWorkDir();
115: if (unpacked.booleanValue()) {
116: args[2] = "-unpacked";
117: args[2 + 1] = fileName;
118: } else {
119: args[2] = fileName;
120: }
121:
122: return (String) executeMethod.invoke(wsgen,
123: new Object[] { args });
124: } catch (InvocationTargetException e) {
125: if (e.getTargetException() instanceof Error) {
126: throw (Error) e.getTargetException();
127: } else {
128: throw new Exception(
129: "Exception when executing WsGen.execute(String[])",
130: e.getTargetException());
131: }
132: } catch (Exception e) {
133: throw new Exception(
134: "Problems when invoking method WsGen.execute(String[])",
135: e);
136: } finally {
137: if (old != null) {
138: Thread.currentThread().setContextClassLoader(old);
139: }
140: }
141: }
142:
143: /**
144: * Wrapper around WsGen.isModified() method
145: * @return true/false
146: * @throws Exception If WsGen fails or if Reflection errors occurs
147: */
148: public boolean callWsGenIsInputModifed() throws Exception {
149: LoaderManager lm = LoaderManager.getInstance();
150: ClassLoader old = null;
151:
152: try {
153: ClassLoader tools = lm.getToolsLoader();
154: old = Thread.currentThread().getContextClassLoader();
155: Thread.currentThread().setContextClassLoader(tools);
156:
157: if (isInputModifiedMethod == null) {
158: Class clazz = tools.loadClass(WSGEN_CLASSNAME);
159: isInputModifiedMethod = clazz.getDeclaredMethod(
160: "isInputModified", new Class[] {});
161: }
162:
163: if (wsgen == null) {
164: Class clazz = tools.loadClass(WSGEN_CLASSNAME);
165: wsgen = clazz.newInstance();
166: }
167:
168: return ((Boolean) isInputModifiedMethod.invoke(wsgen,
169: (Object[]) null)).booleanValue();
170: } catch (InvocationTargetException e) {
171: if (e.getTargetException() instanceof Error) {
172: throw (Error) e.getTargetException();
173: } else {
174: throw new Exception(
175: "Exception when executing WsGen.isInputModified()",
176: e.getTargetException());
177: }
178: } catch (Exception e) {
179: throw new Exception(
180: "Problems when invoking method WsGen.isInputModified()",
181: e);
182: } finally {
183: if (old != null) {
184: Thread.currentThread().setContextClassLoader(old);
185: }
186: }
187: }
188: }
|