KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ejb > codegen > RMICompiler


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * JavaCompiler.java
26  *
27  * Created on June 22, 2002, 8:56 PM
28  *
29  * @author bnevins
30  * @version $Revision: 1.3 $
31  * <BR> <I>$Source: /cvs/glassfish/appserv-core/src/java/com/sun/ejb/codegen/RMICompiler.java,v $
32  *
33  */

34
35 package com.sun.ejb.codegen;
36
37 import java.util.*;
38 import java.io.*;
39 import java.util.logging.Level JavaDoc;
40 import com.sun.enterprise.util.OS;
41 import com.sun.enterprise.util.io.FileUtils;
42 import com.sun.enterprise.server.Constants;
43 import sun.rmi.rmic.Main;
44
45 class RMICompiler extends Compiler JavaDoc
46 {
47     private static final String JavaDoc RMIC_EXT_DIRS_OPTION = "-extdirs";
48
49     RMICompiler(List theOptions, List theFiles) throws JavaCompilerException
50     {
51         super(theOptions, theFiles);
52     }
53     
54     ///////////////////////////////////////////////////////////////////////////
55

56     void setClasspath(String JavaDoc cp)
57     {
58         classpath = cp;
59     }
60     
61     ///////////////////////////////////////////////////////////////////////////
62

63     protected void internal_compile() throws JavaCompilerException, ProcessExecutorException
64     {
65         try
66         {
67             if(nativeExternalCompile())
68                 return;
69         }
70         catch(Throwable JavaDoc t)
71         {
72                     logger.log(Level.WARNING,
73                                "ejb.rmic_compilation_exception", t);
74             // fall through to nativeCompile()
75
// I know what you're thinking -- why run the native compile
76
// if the the external already didn't work? For some reason
77
// the external will time out and we won't get an error message.
78
// The native call will always give an error message.
79
}
80         
81         nativeCompile();
82     }
83     
84     ///////////////////////////////////////////////////////////////////////////
85

86     private boolean nativeExternalCompile() throws ProcessExecutorException
87     {
88         if(classpath == null || javaExe == null)
89             return false;
90         
91                 
92         ArrayList cmd = new ArrayList();
93         cmd.add(javaExe.getPath());
94         cmd.add("-classpath");
95         cmd.add(classpath);
96                 if (OS.isDarwin()) {
97                     // add lib/endorsed so it finds the right rmic
98
cmd.add("-Djava.endorsed.dirs=" + System.getProperty("com.sun.aas.installRoot") +
99                         File.separatorChar + "lib" + File.separatorChar + "endorsed");
100                 }
101                 cmd.add("-D" + JAVA_EXT_DIRS_SYS_PROP + "="
102                         + System.getProperty(JAVA_EXT_DIRS_SYS_PROP));
103         cmd.add("sun.rmi.rmic.Main");
104         cmd.addAll(options);
105         addJavaFiles(cmd);
106         String JavaDoc[] cmds = new String JavaDoc[cmd.size()];
107         cmds = (String JavaDoc[])cmd.toArray(cmds);
108
109         runProcess(cmds, getRmicTimeout() * files.size());
110         logCompilerName("rmic in external JVM");
111         return true;
112     }
113     
114     ///////////////////////////////////////////////////////////////////////////
115

116     /** This can't work -- the jdk's rmic executable doesn't have the iAS
117      * hacks
118      *
119     private boolean rmicCompile() throws ProcessExecutorException
120     {
121         if(rmicExe == null || DONT_USE_RMIC_YET)
122             return false;
123         
124         ArrayList cmd = new ArrayList();
125         cmd.add(rmicExe.getPath());
126         cmd.addAll(options);
127         addJavaFiles(cmd);
128         String[] cmds = new String[cmd.size()];
129         cmds = (String[])cmd.toArray(cmds);
130         runProcess(cmds, getRmicTimeout() * files.size());
131         logCompilerName("stand-alone rmic");
132         return true;
133     }
134      */

135
136     ///////////////////////////////////////////////////////////////////////////
137

138     private void nativeCompile() throws JavaCompilerException
139     {
140         //options.add("-Xnocompile");
141
//options.add("-verbose");
142
options.add(RMIC_EXT_DIRS_OPTION);
143             options.add(System.getProperty(JAVA_EXT_DIRS_SYS_PROP));
144         options.addAll(files);
145         String JavaDoc[] cmds = new String JavaDoc[options.size()];
146         cmds = (String JavaDoc[])options.toArray(cmds);
147         ByteArrayOutputStream baos = new ByteArrayOutputStream();
148         
149         Main compiler = new Main(baos, "rmic");
150         boolean good = compiler.compile(cmds);
151         //good = true; // it ALWAYS returns an "error" if -Xnocompile is used!!
152

153         String JavaDoc output = baos.toString();
154         parseGeneratedFilenames(output);
155         
156         if(!good)
157         {
158             throw new JavaCompilerException("rmi_compiler.error",
159                 "RMI compiler returned an error: {0}", output);
160         }
161         
162         logCompilerName("native rmic (sun.rmi.rmic.Main)");
163     }
164     
165     ///////////////////////////////////////////////////////////////////////////
166

167     protected void internal_init()
168     {
169         rmicExe = null;
170         javaExe = null;
171         String JavaDoc rmicName, javaName;
172     
173         if(jdkDir == null)
174             return;
175         
176         if(OS.isWindows())
177         {
178             rmicName = "rmic.exe";
179             javaName = "java.exe";
180         }
181         else
182         {
183             rmicName = "rmic";
184             javaName = "java";
185         }
186         
187         // if rmic app can be located -- set it
188
rmicExe = new File(jdkDir + "/bin/" + rmicName);
189         
190         if(rmicExe.exists())
191             rmicExe = FileUtils.safeGetCanonicalFile(rmicExe);
192         else
193             rmicExe = null;
194
195         // if external JVM can be located -- set it
196
javaExe = new File(jdkDir + "/bin/" + javaName);
197         
198         if(javaExe.exists())
199             javaExe = FileUtils.safeGetCanonicalFile(javaExe);
200         else
201             javaExe = null;
202
203         logger.log(Level.FINE, "[RMICompiler] after internal_init: "
204                 + "javaExe: " + javaExe + "; rmicExe: " + rmicExe);
205     }
206
207     /**
208      * Returns the timeout, in milliseconds, for each java file.
209      * The compiler calling code will multiply this value by the
210      * number of java files. If the compiler takes longer than
211      * this amount of time the process will be killed.
212      * This is to avoid hangs.
213      *
214      * <p>For flexibility, a environmental variable is checked first. Failing that,
215      * it will use the hard-coded default value.
216      *
217      * <p>This method caches the value of timeout in "timeout" variable to prevent
218      * memory leak seen in the System.getProperty method in Compiler.java
219      *
220      * @return The timeout, in milliseconds, for each java file
221      */

222     private static int getRmicTimeout()
223     {
224             if(timeout < 0 )
225         {
226             timeout = getTimeout(Constants.RMIC_TIMEOUT_MS, Constants.DEFAULT_RMIC_TIMEOUT_MS, 5000, 300000);
227         }
228         return timeout;
229     }
230     
231     /**
232      * sun.rmi.rmic will return output like this:
233      * [generated d:\iAS7\domains\domain1\server1\generated\ejb\j2ee-apps\JDBCSimple\samples\jdbc\simple\ejb\_GreeterDBBean_EJBObjectImpl_Tie.java in 50 ms]
234      * [generated d:\iAS7\domains\domain1\server1\generated\ejb\j2ee-apps\JDBCSimple\org\omg\stub\com\sun\ejb\containers\_EJBObjectImpl_Tie.java in 20 ms]
235      * [loaded C:\iplanetx\jars\appserv-ext.jar(javax/ejb/CreateException.class) in 0 ms]
236      * A better solution is to modify rmic to simply return an array of filenames. But I can't get rmic
237      * changes to stick -- it's unclear which version is really being called. This will work for now
238      *
239      *@return a Set of full path filenames of the generated files.
240      */

241     private void parseGeneratedFilenames(String JavaDoc s)
242     {
243         generatedFilenames = new HashSet();
244         
245         StringTokenizer tk = new StringTokenizer(s);
246
247         while(tk.hasMoreTokens())
248         {
249             String JavaDoc token = tk.nextToken();
250             
251             if(token.equals("[generated") && tk.hasMoreTokens())
252             {
253                     String JavaDoc fName = tk.nextToken();
254                     generatedFilenames.add(fName);
255                     logger.log(Level.FINER, "[RMIC] Generated: " + fName);
256             }
257         }
258     }
259
260     Set getGeneratedFilenames()
261     {
262         return generatedFilenames;
263     }
264     
265     ///////////////////////////////////////////////////////////////////////////
266

267     private File rmicExe, javaExe;
268     private Set generatedFilenames = null;
269     private String JavaDoc classpath = null;
270     private static int timeout = -1;
271 }
272
Popular Tags