Author: fmeschbe
Date: Thu Nov 12 10:15:27 2009
New Revision: 835321
URL: http://svn.apache.org/viewvc?rev=835321&view=rev
Log:
SLING-1884 Apply slightly modified patch provided by Stefan Guggisberg (thanks)
Modified:
sling/trunk/contrib/jcr/compiler/src/main/java/org/apache/sling/jcr/compiler/JcrJavaCompiler.java
sling/trunk/contrib/jcr/compiler/src/main/java/org/apache/sling/jcr/compiler/impl/JcrJavaCompilerImpl.java
Modified:
sling/trunk/contrib/jcr/compiler/src/main/java/org/apache/sling/jcr/compiler/JcrJavaCompiler.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/jcr/compiler/src/main/java/org/apache/sling/jcr/compiler/JcrJavaCompiler.java?rev=835321&r1=835320&r2=835321&view=diff
==============================================================================
---
sling/trunk/contrib/jcr/compiler/src/main/java/org/apache/sling/jcr/compiler/JcrJavaCompiler.java
(original)
+++
sling/trunk/contrib/jcr/compiler/src/main/java/org/apache/sling/jcr/compiler/JcrJavaCompiler.java
Thu Nov 12 10:15:27 2009
@@ -23,7 +23,12 @@
* repository and writes the generated class files to the repository.
*/
public interface JcrJavaCompiler {
-
+
boolean compile(String[] srcFiles, String outputDir, ErrorHandler
errorHandler,
- boolean generateDebug, String javaVersion) throws Exception;
+ boolean generateDebug, String javaVersion) throws
Exception;
+
+ boolean compile(String[] srcFiles, String outputDir,
+ ErrorHandler errorHandler, boolean generateDebug,
+ String javaVersion, ClassLoader
complementaryClassLoader)
+ throws Exception;
}
Modified:
sling/trunk/contrib/jcr/compiler/src/main/java/org/apache/sling/jcr/compiler/impl/JcrJavaCompilerImpl.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/jcr/compiler/src/main/java/org/apache/sling/jcr/compiler/impl/JcrJavaCompilerImpl.java?rev=835321&r1=835320&r2=835321&view=diff
==============================================================================
---
sling/trunk/contrib/jcr/compiler/src/main/java/org/apache/sling/jcr/compiler/impl/JcrJavaCompilerImpl.java
(original)
+++
sling/trunk/contrib/jcr/compiler/src/main/java/org/apache/sling/jcr/compiler/impl/JcrJavaCompilerImpl.java
Thu Nov 12 10:15:27 2009
@@ -23,6 +23,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
+import java.net.URL;
import javax.jcr.Item;
import javax.jcr.Node;
@@ -47,7 +48,7 @@
* @scr.component metatype="no"
* @scr.service interface="org.apache.sling.jcr.compiler.JcrJavaCompiler"
*/
-public class JcrJavaCompilerImpl implements JcrJavaCompiler,
CompilerEnvironment {
+public class JcrJavaCompilerImpl implements JcrJavaCompiler {
/** Logger instance */
private final Logger log =
LoggerFactory.getLogger(JcrJavaCompilerImpl.class);
@@ -68,12 +69,51 @@
private Session session;
//------------------------------------------------------< JcrJavaCompiler >
+
/* (non-Javadoc)
* @see
org.apache.sling.jcr.compiler.JcrJavaCompiler#compile(java.lang.String[],
java.lang.String, org.apache.sling.commons.compiler.ErrorHandler, boolean,
java.lang.String)
*/
public boolean compile(String[] srcFiles, String outputDir,
ErrorHandler errorHandler, boolean generateDebug, String
javaVersion)
throws Exception {
+ return compile(srcFiles, outputDir, errorHandler, generateDebug,
javaVersion, null);
+ }
+
+
+ /* (non-Javadoc)
+ * @see
org.apache.sling.jcr.compiler.JcrJavaCompiler#compile(java.lang.String[],
java.lang.String, org.apache.sling.commons.compiler.ErrorHandler, boolean,
java.lang.String, java.lang.ClassLoader complementaryClassLoader)
+ */
+ public boolean compile(String[] srcFiles, String outputDir,
+ ErrorHandler errorHandler, boolean generateDebug,
+ String javaVersion, final ClassLoader complementaryClassLoader)
+ throws Exception {
+
+ final ClassLoader currentClassLoader = javaClassLoader;
+ final CompilerEnvironment compilerEnvironment;
+ if (complementaryClassLoader == null) {
+ compilerEnvironment = new
JcrCompilerEnvironment(currentClassLoader);
+ } else {
+ ClassLoader loader = new ClassLoader(currentClassLoader) {
+ protected Class<?> findClass(String name)
+ throws ClassNotFoundException {
+ return complementaryClassLoader.loadClass(name);
+ }
+
+ protected URL findResource(String name) {
+ return complementaryClassLoader.getResource(name);
+ }
+ };
+ compilerEnvironment = new JcrCompilerEnvironment(loader);
+ }
+
+ return compileInternal(srcFiles, outputDir, compilerEnvironment,
+ errorHandler, generateDebug, javaVersion);
+ }
+
+ public boolean compileInternal(String[] srcFiles,
+ String outputDir, CompilerEnvironment compilerEnvironment,
+ ErrorHandler errorHandler, boolean generateDebug, String
javaVersion)
+ throws Exception {
if (javaVersion == null) {
javaVersion = System.getProperty("java.specification.version");
@@ -122,64 +162,88 @@
for (int i = 0; i < units.length; i++) {
units[i] = createCompileUnit(srcFiles[i]);
}
- compiler.compile(units, this, classWriter, handler,
+
+ compiler.compile(units, compilerEnvironment, classWriter, handler,
new Options(javaVersion, generateDebug));
return handler.getNumErrors() == 0;
}
//--------------------------------------------------< CompilerEnvironment >
- /* (non-Javadoc)
- * @see
org.apache.sling.commons.compiler.CompilerEnvironment#findClass(java.lang.String)
- */
- public byte[] findClass(String className) throws Exception {
- InputStream in =
this.javaClassLoader.getResourceAsStream(className.replace('.', '/') +
".class");
- if (in == null) {
- return null;
- }
- ByteArrayOutputStream out = new ByteArrayOutputStream(0x7fff);
- try {
- byte[] buffer = new byte[0x1000];
- int read = 0;
- while ((read = in.read(buffer)) > 0) {
- out.write(buffer, 0, read);
- }
- } finally {
- //out.close();
- in.close();
+ private static class JcrCompilerEnvironment implements CompilerEnvironment
{
+
+ private final ClassLoader classLoader;
+
+ JcrCompilerEnvironment(final ClassLoader classLoader) {
+ this.classLoader = classLoader;
}
- return out.toByteArray();
- }
+ /*
+ * (non-Javadoc)
+ * @see
+ * org.apache.sling.commons.compiler.CompilerEnvironment#findClass(java
+ * .lang.String)
+ */
+ public byte[] findClass(String className) throws Exception {
+ InputStream in =
this.classLoader.getResourceAsStream(className.replace(
+ '.', '/')
+ + ".class");
+ if (in == null) {
+ return null;
+ }
+ ByteArrayOutputStream out = new ByteArrayOutputStream(0x7fff);
- /* (non-Javadoc)
- * @see
org.apache.sling.commons.compiler.CompilerEnvironment#isPackage(java.lang.String)
- */
- public boolean isPackage(String packageName) {
- InputStream in =
this.javaClassLoader.getResourceAsStream(packageName.replace('.', '/') +
".class");
- if (in != null) {
try {
+ byte[] buffer = new byte[0x1000];
+ int read = 0;
+ while ((read = in.read(buffer)) > 0) {
+ out.write(buffer, 0, read);
+ }
+ } finally {
+ // out.close();
in.close();
- } catch (IOException ignore) {
}
- return false;
+
+ return out.toByteArray();
}
- // FIXME need a better way to determine whether a name resolves
- // to a package
- int pos = packageName.lastIndexOf('.');
- if (pos != -1) {
- if (Character.isUpperCase(packageName.substring(pos +
1).charAt(0))) {
+
+ /*
+ * (non-Javadoc)
+ * @see
+ * org.apache.sling.commons.compiler.CompilerEnvironment#isPackage(java
+ * .lang.String)
+ */
+ public boolean isPackage(String packageName) {
+ InputStream in =
this.classLoader.getResourceAsStream(packageName.replace(
+ '.', '/')
+ + ".class");
+ if (in != null) {
+ try {
+ in.close();
+ } catch (IOException ignore) {
+ }
return false;
}
+ // FIXME need a better way to determine whether a name resolves
+ // to a package
+ int pos = packageName.lastIndexOf('.');
+ if (pos != -1) {
+ if (Character.isUpperCase(packageName.substring(pos +
1).charAt(
+ 0))) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.sling.commons.compiler.CompilerEnvironment#cleanup()
+ */
+ public void cleanup() {
}
- return true;
- }
- /* (non-Javadoc)
- * @see org.apache.sling.commons.compiler.CompilerEnvironment#cleanup()
- */
- public void cleanup() {
}
//--------------------------------------------------------< misc. helpers >
@@ -268,7 +332,7 @@
/**
* Bind the class load provider.
- * @param repositoryClassLoaderProvider the new provider
+ * @param rclp the new provider
*/
protected void bindDynamicClassLoaderManager(DynamicClassLoaderManager
rclp) {
if ( this.javaClassLoader != null ) {
@@ -279,7 +343,7 @@
/**
* Unbind the class loader provider.
- * @param repositoryClassLoaderProvider the old provider
+ * @param rclp the old provider
*/
protected void unbindDynamicClassLoaderManager(DynamicClassLoaderManager
rclp) {
if ( this.dynamicClassLoaderManager == rclp ) {