OK. Here you go. EmbeddedJavaCompiler is an implementation of LanguageCompiler that doesn't depend on CLASSPATH but instead uses the thread context class loader to load dependent classes.

I've attached a working version (minimally tested) . I've also attached a new version of JavaLanguage.java that you'll need also. I modified it slightly to not rely on the AbstractJavaCompiler class but instead to use interfaces only. You'll also need to place javacApi.jar somewhere in your classpath when you compile cocoon, and you'll need to place javacApi.jar, javacImpl.jar, and jdtcore.jar in your classpath when you run cocoon. (You can get these here: ftp://ftp.primaryinterface.com/pub/javacAPI).

Right now, I can't commit these changes because:
1) I'm still waiting for Brian Behlendorf to change my password
2) We need to decide where to put the JavacAPI classes (other than "org.tempuri")


Regards,

Chris

Stefano Mazzocchi wrote:

Christopher Oliver wrote:

Actually I am in the process of trying to do this but got immediately stopped by the below when I first tried to run cocoon (built from today's cvs),

Anyone care to help?

Regards,

Chris

ERROR (2003-02-16) 13:12.15:159 [access] (/cocoon/documents/index.html) Thread-10/CocoonServlet: Problem with Cocoon servlet
org.apache.cocoon.ProcessingException: Failed to execute pipeline.: org.apache.cocoon.CascadingIOException: org.apache.xml.utils.WrappedRuntimeException: The output format must have a '{http://xml.apache.org/xslt}content-handler' property!: org.apache.xml.utils.WrappedRuntimeException: The output format must have a '{http://xml.apache.org/xslt}content-handler' property!

I'm experiencing similar errors as well but I'm currently refactoring things heavily on my disk so I can't really help you out until I'm done (should take a few days). Then I'm glad to help you on this javacapi stuff.


/*

 ============================================================================
                   The Apache Software License, Version 1.1
 ============================================================================

 Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.

 Redistribution and use in source and binary forms, with or without modifica-
 tion, are permitted provided that the following conditions are met:

 1. Redistributions of  source code must  retain the above copyright  notice,
    this list of conditions and the following disclaimer.

 2. Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.

 3. The end-user documentation included with the redistribution, if any, must
    include  the following  acknowledgment:  "This product includes  software
    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
    Alternately, this  acknowledgment may  appear in the software itself,  if
    and wherever such third-party acknowledgments normally appear.

 4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
    used to  endorse or promote  products derived from  this software without
    prior written permission. For written permission, please contact
    [EMAIL PROTECTED]

 5. Products  derived from this software may not  be called "Apache", nor may
    "Apache" appear  in their name,  without prior written permission  of the
    Apache Software Foundation.

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
 APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
 INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
 DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
 OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
 ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
 (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 This software  consists of voluntary contributions made  by many individuals
 on  behalf of the Apache Software  Foundation and was  originally created by
 Stefano Mazzocchi  <[EMAIL PROTECTED]>. For more  information on the Apache
 Software Foundation, please see <http://www.apache.org/>.

*/
package org.apache.cocoon.components.language.programming.java;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.excalibur.pool.Recyclable;
import org.apache.cocoon.components.language.programming.LanguageCompiler;
import org.apache.cocoon.components.language.programming.CompilerError;
import org.apache.cocoon.util.ClassUtils;
import org.apache.avalon.framework.CascadingRuntimeException;
import org.tempuri.javac.JavaCompiler;
import org.tempuri.javac.JavaCompilerErrorHandler;
import org.tempuri.javac.JavaClassReaderFactory;
import org.tempuri.javac.JavaClassReader;
import org.tempuri.javac.JavaClassWriterFactory;
import org.tempuri.javac.JavaClassWriter;
import org.tempuri.javac.JavaSourceReaderFactory;
import org.tempuri.javac.JavaSourceReader;
import org.tempuri.javac.JavaCompiler;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.io.IOException;
import java.io.Reader;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.BufferedOutputStream;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.File;

public class EmbeddedJavaCompiler implements LanguageCompiler, Recyclable {

    JavaCompiler compiler;
    List errors = new LinkedList();

    String sourceDir;
    String sourceFile; 
    String destDir;
    String encoding;

    public void recycle() {
        sourceFile = null;
        sourceDir = null;
        destDir = null;
        encoding = null;
        errors.clear();
    }

    static final String ECLIPSE_IMPL = 
        "org.tempuri.javacImpl.eclipse.JavaCompilerImpl";

    public EmbeddedJavaCompiler() {
        try {
            Class c = ClassUtils.loadClass(ECLIPSE_IMPL);
            compiler = (JavaCompiler)c.newInstance();
        } catch (Exception e) {
            throw new CascadingRuntimeException("Failed to load embedded Java 
Compiler", e);
        }
    }

    public void setFile(String file) {
        // This seems to be the absolute path to the file to be compiled
        this.sourceFile = file;
    }

    public void setSource(String source) {
        // This seems to simply be the directory the file to
        // be compiled resides in: not sure what this could be used for...
        //
        // Not used 
    }

    public void setDestination(String destDir) {
        // This seems to indicate the "sourcepath" of the file to be
        // compiled (as well as the output directory)
        this.destDir = destDir;
        this.sourceDir = destDir;
    }

    public void setEncoding(String encoding) {
        this.encoding = encoding; // TBD: support encoding!!!
    }

    public void setClasspath(String cp) {
        // Not used 
    }

    static class SourceReaderFactory implements JavaSourceReaderFactory {

        // This implementation doesn't support "-sourcepath" behavior
        // Instead it only allows a single input source file to be compiled
        // because that's all that LanguageCompiler supports. It would
        // be easy to support multiple source files, however

        String targetClassName;
        File sourceFile;

        SourceReaderFactory(String className, File sourceFile) {
            this.targetClassName = className;
            this.sourceFile = sourceFile;
        }

        public JavaSourceReader 
            getSourceReader(final String className) throws IOException {
            if (targetClassName.equals(className)) {
                return new JavaSourceReader() {
                        public String getClassName() {
                            return className;
                        }
                        public Reader getReader() throws IOException {
                            return new BufferedReader(new FileReader(sourceFile));
                        }
                    };
            }
            return null;
        }
    }

    class ClassReaderFactory 
        implements JavaClassReaderFactory {
        
        public JavaClassReader 
            getClassReader(final String className) 
            throws IOException {
            String resourceName = className.replace('.', '/') + ".class";
            ClassLoader cl = ClassUtils.getClassLoader();
            final InputStream strm = cl.getResourceAsStream(resourceName);
            if (strm != null) {
                return new JavaClassReader() {
                        public String getClassName() {
                            return className;
                        }
                        public InputStream getInputStream() {
                            return strm;
                        }
                    };
            }
            return null;
        }
    }

    static class ClassWriterImpl implements JavaClassWriter {

        String className;
        File file;

        ClassWriterImpl(String className, File file) {
            this.className = className;
            this.file = file;
        }

        public String getClassName() {
            return className;
        }

        public void writeClass(InputStream inputStream) throws IOException {
            if (file.exists()) {
                file.delete();
            }
            BufferedOutputStream bos = 
                new BufferedOutputStream(new FileOutputStream(file));
            byte[] buf = new byte[8192];
            int count;
            while ((count = inputStream.read(buf, 0, buf.length)) > 0) {
                bos.write(buf, 0, count);
            }
            bos.close();
        }
    }

    class ClassWriterFactory 
        implements JavaClassWriterFactory {
        
        public JavaClassWriter 
            getClassWriter(final String className) throws IOException {
            String path = className.replace('.', '/') + ".class";
            File file;
            if (destDir != null) {
                file = new File(new File(destDir), path);
            } else {
                file = new File(path);
            }
            file.mkdirs();
            return new ClassWriterImpl(className, file);
        }
    }

        
    class ErrorHandler implements JavaCompilerErrorHandler {
        public void handleError(String className,
                                int line,
                                int column,
                                Object errorMessage) {
            String fileName = 
                className.replace('.', File.separatorChar) + ".java";
            if (column < 0) column = 0;
            errors.add(new CompilerError(fileName,
                                         true,
                                         line,
                                         column,
                                         line,
                                         column,
                                         errorMessage.toString()));
        }
    }
    

    private String makeClassName(String fileName) throws IOException {
        File origFile = new File(fileName);
        String canonical = null;
        if (origFile.exists()) {
            canonical = origFile.getCanonicalPath().replace('\\', '/');
        }
        String str = fileName;
        str = str.replace('\\', '/');
        if (sourceDir != null) {
            String prefix = 
                new File(sourceDir).getCanonicalPath().replace('\\', '/');
            if (canonical != null) {
                if (canonical.startsWith(prefix)) {
                    String result = canonical.substring(prefix.length() + 1,
                                                        canonical.length() -5);
                    result = result.replace('/', '.');
                    return result;
                }
            } else {
                File t = new File(sourceDir, fileName);
                if (t.exists()) {
                    str = t.getCanonicalPath().replace('\\', '/');
                    String result = str.substring(prefix.length()+1,
                                                  str.length() - 5).replace('/', '.');
                    return result;
                }
            }
        }
        if (fileName.endsWith(".java")) {
            fileName = fileName.substring(0, fileName.length() - 5);
        }
        fileName = fileName.replace('\\', '.');
        return fileName.replace('/', '.');
    }

    public boolean compile() throws IOException {
        errors.clear();
        String className = makeClassName(sourceFile);
        compiler.setDebug(true);
        compiler.compile(new String[] {className},
                         new SourceReaderFactory(className, 
                                                 new File(sourceFile)),
                         new ClassReaderFactory(),
                         new ClassWriterFactory(),
                         new ErrorHandler());
        return errors.size() == 0;
    }

    public List getErrors() throws IOException {
        return errors;
    }
}
/*

 ============================================================================
                   The Apache Software License, Version 1.1
 ============================================================================

 Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.

 Redistribution and use in source and binary forms, with or without modifica-
 tion, are permitted provided that the following conditions are met:

 1. Redistributions of  source code must  retain the above copyright  notice,
    this list of conditions and the following disclaimer.

 2. Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.

 3. The end-user documentation included with the redistribution, if any, must
    include  the following  acknowledgment:  "This product includes  software
    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
    Alternately, this  acknowledgment may  appear in the software itself,  if
    and wherever such third-party acknowledgments normally appear.

 4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
    used to  endorse or promote  products derived from  this software without
    prior written permission. For written permission, please contact
    [EMAIL PROTECTED]

 5. Products  derived from this software may not  be called "Apache", nor may
    "Apache" appear  in their name,  without prior written permission  of the
    Apache Software Foundation.

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
 APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
 INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
 DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
 OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
 ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
 (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 This software  consists of voluntary contributions made  by many individuals
 on  behalf of the Apache Software  Foundation and was  originally created by
 Stefano Mazzocchi  <[EMAIL PROTECTED]>. For more  information on the Apache
 Software Foundation, please see <http://www.apache.org/>.

*/
package org.apache.cocoon.components.language.programming.java;

import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.avalon.framework.component.Composable;
import org.apache.avalon.framework.parameters.ParameterException;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.avalon.framework.logger.LogEnabled;
import org.apache.cocoon.components.language.programming.LanguageCompiler;

import org.apache.cocoon.components.classloader.ClassLoaderManager;
import org.apache.cocoon.components.language.LanguageException;
import org.apache.cocoon.components.language.markup.xsp.XSLTExtension;
import org.apache.cocoon.components.language.programming.CompiledProgrammingLanguage;
import org.apache.cocoon.components.language.programming.CompilerError;
import org.apache.cocoon.util.ClassUtils;
import org.apache.cocoon.util.JavaArchiveFilter;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.StringTokenizer;

/**
 * The Java programming language processor
 *
 * @author <a href="mailto:[EMAIL PROTECTED]";>Ricardo Rocha</a>
 * @version CVS $Id: JavaLanguage.java,v 1.14 2003/01/31 22:51:27 pier Exp $
 */
public class JavaLanguage extends CompiledProgrammingLanguage
        implements Initializable, ThreadSafe, Composable, Disposable {

    /** The class loader */
    private ClassLoaderManager classLoaderManager;

    /** The component manager */
    protected ComponentManager manager = null;

    /** Classpath */
    private String classpath;

    /**
     * Return the language's canonical source file extension.
     *
     * @return The source file extension
     */
    public String getSourceExtension() {
        return "java";
    }

    /**
     * Return the language's canonical object file extension.
     *
     * @return The object file extension
     */
    public String getObjectExtension() {
        return "class";
    }

    /**
     * Set the configuration parameters. This method instantiates the
     * sitemap-specified <code>ClassLoaderManager</code>
     *
     * @param params The configuration parameters
     * @exception ParameterException If the class loader manager cannot be instantiated
     */
    public void parameterize(Parameters params) throws ParameterException {
        super.parameterize(params);

        String classLoaderClass = params.getParameter("class-loader",
                "org.apache.cocoon.components.classloader.ClassLoaderManagerImpl");
        if (classLoaderClass != null) {
            try {
                this.classLoaderManager = (ClassLoaderManager) 
ClassUtils.newInstance(classLoaderClass);
            } catch (Exception e) {
                throw new ParameterException("Unable to load class loader: " + 
classLoaderClass, e);
            }
        }
    }

    /**
     * Set the global component manager. This methods initializes the class
     * loader manager if it was not (successfully) specified in the language
     * parameters
     *
     * @param manager The global component manager
     */
    public void compose(ComponentManager manager) {
        this.manager = manager;
        if (this.classLoaderManager == null) {
            try {
                getLogger().debug("Looking up " + ClassLoaderManager.ROLE);
                this.classLoaderManager =
                        (ClassLoaderManager) manager.lookup(ClassLoaderManager.ROLE);
            } catch (Exception e) {
                getLogger().error("Could not find component", e);
            }
        }
    }

    public void initialize() throws Exception {

        // Initialize the classpath
        String systemBootClasspath = System.getProperty("sun.boot.class.path");
        String systemClasspath = System.getProperty("java.class.path");
        String systemExtDirs = System.getProperty("java.ext.dirs");
        String systemExtClasspath = null;

        try {
            systemExtClasspath = expandDirs(systemExtDirs);
        } catch (Exception e) {
            getLogger().warn("Could not expand Directory:" + systemExtDirs, e);
        }

        this.classpath =
            ((super.classpath != null) ? File.pathSeparator + super.classpath : "") +
            ((systemBootClasspath != null) ? File.pathSeparator + systemBootClasspath 
: "") +
            ((systemClasspath != null) ? File.pathSeparator + systemClasspath : "") +
            ((systemExtClasspath != null) ? File.pathSeparator + systemExtClasspath : 
"");
    }

    /**
     * Actually load an object program from a class file.
     *
     * @param name The object program base file name
     * @param baseDirectory The directory containing the object program file
     * @return The loaded object program
     * @exception LanguageException If an error occurs during loading
     */
    protected Class loadProgram(String name, File baseDirectory)
            throws LanguageException {
        try {
            this.classLoaderManager.addDirectory(baseDirectory);
            return this.classLoaderManager.loadClass(name.replace(File.separatorChar, 
'.'));
        } catch (Exception e) {
            throw new LanguageException("Could not load class for program '" + name + 
"' due to a " + e.getClass().getName() + ": " + e.getMessage());
        }
    }

    /**
     * Compile a source file yielding a loadable class file.
     *
     * @param name The object program base file name
     * @param baseDirectory The directory containing the object program file
     * @param encoding The encoding expected in the source file or
     * <code>null</code> if it is the platform's default encoding
     * @exception LanguageException If an error occurs during compilation
     */
    protected void compile(String name, File baseDirectory, String encoding)
            throws LanguageException {

        try {
            LanguageCompiler compiler = (LanguageCompiler) 
this.compilerClass.newInstance();
            if (compiler instanceof LogEnabled) {
                ((LogEnabled)compiler).enableLogging(getLogger());
            }

            int pos = name.lastIndexOf(File.separatorChar);
            String filename = name.substring(pos + 1);
            String pathname =
                    baseDirectory.getCanonicalPath() + File.separator +
                    name.substring(0, pos).replace(File.separatorChar, '/');
            String filename_abs = pathname + File.separator + filename
                    + "." + this.getSourceExtension();

            compiler.setFile(filename_abs);
            compiler.setSource(pathname);
            compiler.setDestination(baseDirectory.getCanonicalPath());
            compiler.setClasspath(baseDirectory.getCanonicalPath() + this.classpath);

            if (encoding != null) {
                compiler.setEncoding(encoding);
            }

            getLogger().debug("Compiling " + filename_abs);
            if (!compiler.compile()) {
                StringBuffer message = new StringBuffer("Error compiling ");
                message.append(filename);
                message.append(":\n");

                List errors = compiler.getErrors();
                CompilerError[] compilerErrors = new CompilerError[errors.size()];
                errors.toArray(compilerErrors);

                throw new LanguageException(message.toString(), filename_abs, 
compilerErrors);
            }

        } catch (InstantiationException e) {
            getLogger().warn("Could not instantiate the compiler", e);
            throw new LanguageException("Could not instantiate the compiler: " + 
e.getMessage());
        } catch (IllegalAccessException e) {
            getLogger().warn("Could not access the compiler class", e);
            throw new LanguageException("Could not access the compiler class: " + 
e.getMessage());
        } catch (IOException e) {
            getLogger().warn("Error during compilation", e);
            throw new LanguageException("Error during compilation: " + e.getMessage());
        }
    }

    /**
     * Unload a previously loaded class. This method simply reinstantiates the
     * class loader to ensure that a new version of the same class will be
     * correctly loaded in a future loading operation
     *
     * @param program A previously loaded class
     * @exception LanguageException If an error occurs during unloading
     */
    public void doUnload(Object program) throws LanguageException {
        this.classLoaderManager.reinstantiate();
    }

    /**
     * Escape a <code>String</code> according to the Java string constant
     * encoding rules.
     *
     * @param constant The string to be escaped
     * @return The escaped string
     */
    public String quoteString(String constant) {
        return XSLTExtension.escapeString(constant);
    }

    /**
     * Expand a directory path or list of directory paths (File.pathSeparator
     * delimited) into a list of file paths of all the jar files in those
     * directories.
     *
     * @param dirPaths The string containing the directory path or list of
     *                 directory paths.
     * @return The file paths of the jar files in the directories. This is an
     *                empty string if no files were found, and is terminated by an
     *                additional pathSeparator in all other cases.
     */
    private String expandDirs(String dirPaths) {
        StringTokenizer st = new StringTokenizer(dirPaths, File.pathSeparator);
        StringBuffer buffer = new StringBuffer();
        while (st.hasMoreTokens()) {
            String d = st.nextToken();
            File dir = new File(d);
            if (!dir.isDirectory()) {
                // The absence of a listed directory may not be an error.
                if (getLogger().isWarnEnabled()) getLogger().warn("Attempted to 
retrieve directory listing of non-directory " + dir.toString());
            } else {
                File[] files = dir.listFiles(new JavaArchiveFilter());
                for (int i = 0; i < files.length; i++) {
                    buffer.append(files[i]).append(File.pathSeparator);
                }
            }
        }
        return buffer.toString();
    }

    /**
     *  dispose
     */
    public void dispose() {
        manager.release(this.classLoaderManager);
    }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]

Reply via email to