KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > genclientstub > wrapper > ClientGenStubWrapper


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: ClientGenStubWrapper.java,v 1.2 2004/12/13 16:27:31 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_lib.genclientstub.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  * ClientGenStub wrapper Used to launch ClientGenStub in DeployFile of JSR 88
35  * @author Florent Benoit
36  */

37 public class ClientGenStubWrapper {
38
39     /**
40      * ClientGenStub fully qualified classname
41      */

42     private static final String JavaDoc GENSTUB_CLASSNAME = "org.objectweb.jonas_lib.genclientstub.ClientStubGen";
43
44     /**
45      * Number of arguments of execute method
46      */

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

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

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

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

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

76     public String JavaDoc callClientGenStubExecute(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(GENSTUB_CLASSNAME);
87                 executeMethod = clazz.getDeclaredMethod("execute", new Class JavaDoc[] {String JavaDoc[].class});
88             }
89
90             if (clientGenStub == null) {
91                 Class JavaDoc clazz = tools.loadClass(GENSTUB_CLASSNAME);
92                 clientGenStub = 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(clientGenStub, 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 ClientstubGen.execute(String[])", e.getTargetException());
106             }
107         } catch (Exception JavaDoc e) {
108             throw new Exception JavaDoc("Problems when invoking method ClientstubGen.execute(String[])", e);
109         } finally {
110             if (old != null) {
111                 Thread.currentThread().setContextClassLoader(old);
112             }
113         }
114     }
115
116     /**
117      * Wrapper around ClientGenStubGen.isModified() method
118      * @return true/false
119      * @throws Exception If ClientStub fails or if Reflection errors occurs
120      */

121     public boolean callClientGenStubIsInputModifed() 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(GENSTUB_CLASSNAME);
132                 isInputModifiedMethod = clazz.getDeclaredMethod("isInputModified", new Class JavaDoc[] {});
133             }
134
135             if (clientGenStub == null) {
136                 Class JavaDoc clazz = tools.loadClass(GENSTUB_CLASSNAME);
137                 clientGenStub = clazz.newInstance();
138             }
139
140             return ((Boolean JavaDoc) isInputModifiedMethod.invoke(clientGenStub, (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 clientGenStub.isInputModified()", e.getTargetException());
146             }
147         } catch (Exception JavaDoc e) {
148             throw new Exception JavaDoc("Problems when invoking method clientGenStub.isInputModified()", e);
149         } finally {
150             if (old != null) {
151                 Thread.currentThread().setContextClassLoader(old);
152             }
153         }
154     }
155 }
156
157
Popular Tags