[Jsptest-svn-commits] SF.net SVN: jsptest: [192] trunk
Status: Alpha
Brought to you by:
lkoskela
From: <lko...@us...> - 2008-03-09 18:55:20
|
Revision: 192 http://jsptest.svn.sourceforge.net/jsptest/?rev=192&view=rev Author: lkoskela Date: 2008-03-09 11:55:16 -0700 (Sun, 09 Mar 2008) Log Message: ----------- Applied a modified version of a patch by Mathias Broekelmann, which essentially adds a Sun JDK 'tools'.jar' based implementation of the JavaCompiler interface. Modified Paths: -------------- trunk/jsptest-generic/jsptest-common/pom.xml trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/JasperCompiler.java trunk/pom.xml Added Paths: ----------- trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/compiler/java/SunJavaC.java Modified: trunk/jsptest-generic/jsptest-common/pom.xml =================================================================== --- trunk/jsptest-generic/jsptest-common/pom.xml 2008-03-09 18:07:11 UTC (rev 191) +++ trunk/jsptest-generic/jsptest-common/pom.xml 2008-03-09 18:55:16 UTC (rev 192) @@ -11,4 +11,24 @@ <packaging>jar</packaging> <name>Common utilities</name> <description>Common utilities for the components of JspTest, including the JSP version-specific compiler implementations.</description> + <profiles> + <profile> + <id>default-tools.jar</id> + <activation> + <property> + <name>java.vendor</name> + <value>Sun Microsystems Inc.</value> + </property> + </activation> + <dependencies> + <dependency> + <groupId>com.sun</groupId> + <artifactId>tools</artifactId> + <version>1.4.2</version> + <scope>system</scope> + <systemPath>${java.home}/../lib/tools.jar</systemPath> + </dependency> + </dependencies> + </profile> + </profiles> </project> Added: trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/compiler/java/SunJavaC.java =================================================================== --- trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/compiler/java/SunJavaC.java (rev 0) +++ trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/compiler/java/SunJavaC.java 2008-03-09 18:55:16 UTC (rev 192) @@ -0,0 +1,70 @@ +/** + * + */ +package net.sf.jsptest.compiler.java; + +import java.lang.reflect.Method; + +/** + * This implementation uses the java compiler from sun which ships with since the jdk since java 1.3. + * The code is inspired by the javac ant task implementation. + * + * @author mathias.broekelmann + * + */ +public class SunJavaC implements JavaCompiler { + + private static final String SEPARATOR = System.getProperty("path.separator"); + + public boolean compile(String pathToJavaSource, String outputDirectory, String[] classpath) throws Exception { + String classpathString = join(classpath); + String[] args = buildArgs(pathToJavaSource, outputDirectory, classpathString); + return compile(args); + } + + private boolean compile(String[] args) { + try { + Class compilerClass = getCompileClass(); + Method compilerMethod = resolveCompilerMethod(compilerClass); + return ((Integer)compilerMethod.invoke(compilerClass.newInstance(), new Object[] { args })).intValue() == 0; + } catch (Throwable e) { + System.err.println(e.getMessage()); + return false; + } + } + + private String[] buildArgs(String pathToJavaSource, String outputDirectory, String classpathString) { + return new String[] { "-classpath", classpathString, "-d", outputDirectory, pathToJavaSource }; + } + + private String join(String[] classpath) { + StringBuffer s = new StringBuffer(); + for (int i = 0; i < classpath.length; i++) { + if (s.length() > 0) { + s.append(SEPARATOR); + } + s.append(classpath[i]); + } + return s.toString(); + } + + public boolean isAvailable() { + try { + Class c = getCompileClass(); + resolveCompilerMethod(c); + return true; + } catch (Throwable ex) { + return false; + } + } + + private Method resolveCompilerMethod(Class compilerClass) throws NoSuchMethodException { + return compilerClass.getMethod("compile", new Class[] { (new String[] {}).getClass() }); + } + + private Class getCompileClass() throws ClassNotFoundException { + Class c = Class.forName("com.sun.tools.javac.Main"); + return c; + } + +} Modified: trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/JasperCompiler.java =================================================================== --- trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/JasperCompiler.java 2008-03-09 18:07:11 UTC (rev 191) +++ trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/JasperCompiler.java 2008-03-09 18:55:16 UTC (rev 192) @@ -34,6 +34,7 @@ import net.sf.jsptest.compiler.JspCompilationInfo; import net.sf.jsptest.compiler.java.CommandLineJavac; import net.sf.jsptest.compiler.java.JavaCompiler; +import net.sf.jsptest.compiler.java.SunJavaC; import net.sf.jsptest.compiler.jsp20.mock.MockEmbeddedServletOptions; import net.sf.jsptest.compiler.jsp20.mock.MockServletConfig; import net.sf.jsptest.compiler.jsp20.mock.MockTagInfo; @@ -388,7 +389,7 @@ // this doesn't work because with Maven we need to set the classpath // explicitly as the "current" classpath does not include our // dependencies - // compilers.add(new SunJavac()); + compilers.add(new SunJavaC()); compilers.add(new CommandLineJavac()); for (Iterator i = compilers.iterator(); i.hasNext();) { JavaCompiler compiler = (JavaCompiler) i.next(); Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2008-03-09 18:07:11 UTC (rev 191) +++ trunk/pom.xml 2008-03-09 18:55:16 UTC (rev 192) @@ -54,6 +54,13 @@ <dependencyManagement> <dependencies> <dependency> + <groupId>sun.jdk</groupId> + <artifactId>tools</artifactId> + <version>1.4</version> + <scope>system</scope> + <systemPath>${java.home}/../lib/tools.jar</systemPath> + </dependency> + <dependency> <groupId>jtidy</groupId> <artifactId>jtidy</artifactId> <version>4aug2000r7-dev</version> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |