001    /*
002     * Copyright (C) 2012 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    package org.crsh.lang.impl.java;
020    
021    import javax.tools.Diagnostic;
022    import javax.tools.DiagnosticCollector;
023    import javax.tools.JavaCompiler;
024    import javax.tools.JavaFileObject;
025    import javax.tools.ToolProvider;
026    import java.io.IOException;
027    import java.net.URISyntaxException;
028    import java.nio.charset.Charset;
029    import java.util.ArrayList;
030    import java.util.Collections;
031    import java.util.List;
032    
033    /** @author Julien Viet */
034    class Compiler {
035    
036      /** . */
037      private final ClassLoader classLoader;
038    
039      Compiler() {
040        this.classLoader = Thread.currentThread().getContextClassLoader();
041      }
042    
043      Compiler(ClassLoader classLoader) {
044        this.classLoader = classLoader;
045      }
046    
047      List<JavaClassFileObject> compile(String className, String source) throws IOException, CompilationFailureException {
048    
049        // Get compiler
050        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
051    
052        // Diagnostics
053        DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
054    
055        // The compiler
056        JavaFileManagerImpl fileManager = new JavaFileManagerImpl(
057            compiler.getStandardFileManager(diagnostics, null, Charset.defaultCharset()),
058            new ClasspathResolver(classLoader));
059    
060        //
061        List<JavaFileObject> sources;
062        try {
063          sources = Collections.<JavaFileObject>singletonList(new JavaSourceFileObject(className, source));
064        }
065        catch (URISyntaxException e) {
066          throw new IOException(e);
067        }
068    
069        // Compile
070        JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, null, null, sources);
071    
072        //
073        Boolean ok = task.call();
074        if (!ok) {
075          ArrayList<Diagnostic<? extends JavaFileObject>> errors = new ArrayList<Diagnostic<? extends JavaFileObject>>();
076          for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
077            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
078              errors.add(diagnostic);
079            }
080          }
081          throw new CompilationFailureException(errors);
082        } else {
083          return new ArrayList<JavaClassFileObject>(fileManager.getClasses());
084        }
085      }
086    
087    }