Search in sources :

Example 1 with JavaCompilerException

use of com.ibm.xsp.extlib.javacompiler.JavaCompilerException in project org.openntf.nsfodp by OpenNTF.

the class ODPCompiler method compile.

/**
 * Runs the compilation process:
 *
 * <ol>
 * 	<li>Installs all bundles from the provided update sites</li>
 *	<li>Initializes plugin contributions from installed bundles</li>
 * 	<li>Compiles all XPage artifacts</li>
 * 	<li>Constructs the NSF from the on-disk project</li>
 * 	<li>Uninstalls any installed bundles</li>
 * </ol>
 *
 * @param cl the base {@link ClassLoader} to use during compilation
 * @return a {@link Path} representing the created database
 * @throws Exception if there is a problem compiling any component
 * @since 1.0.0
 */
public synchronized Path compile(ClassLoader cl) throws Exception {
    Collection<Bundle> bundles = installBundles();
    JavaSourceClassLoader classLoader = null;
    Set<Path> cleanup = new HashSet<>();
    try {
        boolean hasXPages = odp.hasXPagesElements();
        if (hasXPages) {
            initRegistry();
            Collection<String> dependencies = buildDependenciesCollection(cleanup);
            dependencies.addAll(ODPUtil.expandRequiredBundles(bundleContext, odp.getRequiredBundles()));
            // Add any Jars from the ODP
            for (Path jar : odp.getJars()) {
                // If the path is inside a JAR, extract it
                if ("jar".equals(jar.toUri().getScheme())) {
                    // $NON-NLS-1$
                    // $NON-NLS-1$
                    Path tempJar = Files.createTempFile(NSFODPUtil.getTempDirectory(), jar.getFileName().toString(), ".jar");
                    cleanup.add(tempJar);
                    Files.copy(jar, tempJar, StandardCopyOption.REPLACE_EXISTING);
                    // $NON-NLS-1$
                    dependencies.add("jar:" + tempJar.toUri());
                } else {
                    // $NON-NLS-1$
                    dependencies.add("jar:" + jar.toUri());
                }
            }
            String[] classPath = dependencies.toArray(new String[dependencies.size()]);
            List<String> options = Stream.concat(compilerOptions.stream(), // $NON-NLS-1$ //$NON-NLS-2$
            Stream.of("-source", compilerLevel, "-target", compilerLevel)).collect(Collectors.toList());
            classLoader = new JavaSourceClassLoader(cl, options, classPath);
            // Bar loading of different-version SSJS classes from ndext
            // $NON-NLS-1$
            classLoader.getJavaFileManager().setNonDelegatingPackages(Arrays.asList("com.ibm.jscript"));
            // Compile Java classes
            compileJavaSources(classLoader);
            compileCustomControls(classLoader);
            compileXPages(classLoader);
        }
        try (NotesAPI session = NotesAPI.get()) {
            Path file = createDatabase(session);
            try (NDatabase database = session.openDatabase("", file.toAbsolutePath().toString())) {
                // $NON-NLS-1$
                try (NDXLImporter importer = session.createDXLImporter()) {
                    importDbProperties(importer, database);
                    importEarlyBasicElements(importer, database);
                    importLotusScriptLibraries(importer, database);
                    importBasicElements(importer, database);
                    importFileResources(importer, database);
                    importDbScript(importer, database);
                    if (hasXPages) {
                        Set<String> compiledClassNames = new HashSet<>(classLoader.getCompiledClassNames());
                        importCustomControls(importer, database, classLoader, compiledClassNames);
                        importXPages(importer, database, classLoader, compiledClassNames);
                        importJavaElements(importer, database, classLoader, compiledClassNames);
                    }
                    // Append a timestamp if requested
                    if (this.isAppendTimestampToTitle()) {
                        // $NON-NLS-1$
                        database.setTitle(database.getTitle() + " - " + TIMESTAMP.get().format(new Date()));
                    }
                    // Set the template info if requested
                    String templateName = this.getTemplateName();
                    if (StringUtil.isNotEmpty(templateName)) {
                        // $NON-NLS-1$
                        int noteId = database.getSharedFieldNoteID("$TemplateBuild");
                        NNote doc;
                        if (noteId != 0) {
                            doc = database.getNoteByID(noteId);
                        } else {
                            // Import an empty one
                            try (InputStream is = ODPCompiler.class.getResourceAsStream("/dxl/TemplateBuild.xml")) {
                                // $NON-NLS-1$
                                // $NON-NLS-1$
                                String dxl = StreamUtil.readString(is, "UTF-8");
                                // $NON-NLS-1$
                                List<Integer> ids = importDxl(importer, dxl, database, "$TemplateBuild blank field");
                                doc = database.getNoteByID(ids.get(0));
                            }
                        }
                        String version = this.getTemplateVersion();
                        if (StringUtil.isNotEmpty(version)) {
                            // $NON-NLS-1$
                            doc.set("$TemplateBuild", version);
                        }
                        // $NON-NLS-1$
                        doc.set("$TemplateBuildName", templateName);
                        // $NON-NLS-1$
                        doc.set("$TemplateBuildDate", new Date());
                        doc.save();
                    }
                }
            }
            return file;
        }
    } catch (JavaCompilerException e) {
        StringWriter o = new StringWriter();
        PrintWriter errOut = new PrintWriter(o);
        e.printExtraInformation(errOut);
        throw new RuntimeException(MessageFormat.format(Messages.ODPCompiler_javaCompilationFailed, o), e);
    } finally {
        uninstallBundles(bundles);
        if (classLoader != null) {
            classLoader.close();
        }
        NSFODPUtil.deltree(cleanup);
    }
}
Also used : NDXLImporter(org.openntf.nsfodp.commons.odp.notesapi.NDXLImporter) NNote(org.openntf.nsfodp.commons.odp.notesapi.NNote) StringWriter(java.io.StringWriter) HashSet(java.util.HashSet) NotesAPI(org.openntf.nsfodp.commons.odp.notesapi.NotesAPI) PrintWriter(java.io.PrintWriter) Path(java.nio.file.Path) JavaSourceClassLoader(com.ibm.xsp.extlib.javacompiler.JavaSourceClassLoader) Bundle(org.osgi.framework.Bundle) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Date(java.util.Date) JavaCompilerException(com.ibm.xsp.extlib.javacompiler.JavaCompilerException) NDatabase(org.openntf.nsfodp.commons.odp.notesapi.NDatabase)

Aggregations

JavaCompilerException (com.ibm.xsp.extlib.javacompiler.JavaCompilerException)1 JavaSourceClassLoader (com.ibm.xsp.extlib.javacompiler.JavaSourceClassLoader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Path (java.nio.file.Path)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 NDXLImporter (org.openntf.nsfodp.commons.odp.notesapi.NDXLImporter)1 NDatabase (org.openntf.nsfodp.commons.odp.notesapi.NDatabase)1 NNote (org.openntf.nsfodp.commons.odp.notesapi.NNote)1 NotesAPI (org.openntf.nsfodp.commons.odp.notesapi.NotesAPI)1 Bundle (org.osgi.framework.Bundle)1