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: ClientGenStubWrapper.java 5928 2004-12-13 16:27:33Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_lib.genclientstub.wrapper;
025:
026: import java.lang.reflect.InvocationTargetException;
027: import java.lang.reflect.Method;
028:
029: import org.objectweb.jonas.server.LoaderManager;
030:
031: /**
032: * ClientGenStub wrapper Used to launch ClientGenStub in DeployFile of JSR 88
033: * @author Florent Benoit
034: */
035: public class ClientGenStubWrapper {
036:
037: /**
038: * ClientGenStub fully qualified classname
039: */
040: private static final String GENSTUB_CLASSNAME = "org.objectweb.jonas_lib.genclientstub.ClientStubGen";
041:
042: /**
043: * Number of arguments of execute method
044: */
045: private static final int ARGS_NUMBER = 3;
046:
047: /**
048: * Method execute of ClientGenStub
049: */
050: private static Method executeMethod = null;
051:
052: /**
053: * The archive was changed or not ?
054: */
055: private static Method isInputModifiedMethod = null;
056:
057: /**
058: * ClientGenStub class
059: */
060: private Object clientGenStub = null;
061:
062: /**
063: * Empty constructor.
064: */
065: public ClientGenStubWrapper() {
066: };
067:
068: /**
069: * Wrapper around ClientstubGen.execute(String[]) method
070: * @param fileName name of the file
071: * @return name of the modified/built file
072: * @throws Exception If ClientstubGen fails or if Reflection errors occurs
073: */
074: public String callClientGenStubExecute(String fileName)
075: throws Exception {
076: LoaderManager lm = LoaderManager.getInstance();
077: ClassLoader old = null;
078:
079: try {
080: ClassLoader tools = lm.getToolsLoader();
081: old = Thread.currentThread().getContextClassLoader();
082: Thread.currentThread().setContextClassLoader(tools);
083:
084: if (executeMethod == null) {
085: Class clazz = tools.loadClass(GENSTUB_CLASSNAME);
086: executeMethod = clazz.getDeclaredMethod("execute",
087: new Class[] { String[].class });
088: }
089:
090: if (clientGenStub == null) {
091: Class clazz = tools.loadClass(GENSTUB_CLASSNAME);
092: clientGenStub = clazz.newInstance();
093: }
094:
095: String[] args = new String[ARGS_NUMBER];
096: args[0] = "-d";
097: args[1] = System.getProperty("java.io.tmpdir");
098: args[2] = fileName;
099:
100: return (String) executeMethod.invoke(clientGenStub,
101: new Object[] { args });
102: } catch (InvocationTargetException e) {
103: if (e.getTargetException() instanceof Error) {
104: throw (Error) e.getTargetException();
105: } else {
106: throw new Exception(
107: "Exception when executing ClientstubGen.execute(String[])",
108: e.getTargetException());
109: }
110: } catch (Exception e) {
111: throw new Exception(
112: "Problems when invoking method ClientstubGen.execute(String[])",
113: e);
114: } finally {
115: if (old != null) {
116: Thread.currentThread().setContextClassLoader(old);
117: }
118: }
119: }
120:
121: /**
122: * Wrapper around ClientGenStubGen.isModified() method
123: * @return true/false
124: * @throws Exception If ClientStub fails or if Reflection errors occurs
125: */
126: public boolean callClientGenStubIsInputModifed() throws Exception {
127: LoaderManager lm = LoaderManager.getInstance();
128: ClassLoader old = null;
129:
130: try {
131: ClassLoader tools = lm.getToolsLoader();
132: old = Thread.currentThread().getContextClassLoader();
133: Thread.currentThread().setContextClassLoader(tools);
134:
135: if (isInputModifiedMethod == null) {
136: Class clazz = tools.loadClass(GENSTUB_CLASSNAME);
137: isInputModifiedMethod = clazz.getDeclaredMethod(
138: "isInputModified", new Class[] {});
139: }
140:
141: if (clientGenStub == null) {
142: Class clazz = tools.loadClass(GENSTUB_CLASSNAME);
143: clientGenStub = clazz.newInstance();
144: }
145:
146: return ((Boolean) isInputModifiedMethod.invoke(
147: clientGenStub, (Object[]) null)).booleanValue();
148: } catch (InvocationTargetException e) {
149: if (e.getTargetException() instanceof Error) {
150: throw (Error) e.getTargetException();
151: } else {
152: throw new Exception(
153: "Exception when executing clientGenStub.isInputModified()",
154: e.getTargetException());
155: }
156: } catch (Exception e) {
157: throw new Exception(
158: "Problems when invoking method clientGenStub.isInputModified()",
159: e);
160: } finally {
161: if (old != null) {
162: Thread.currentThread().setContextClassLoader(old);
163: }
164: }
165: }
166: }
|