Example usage for org.apache.commons.jci.readers ResourceReader isAvailable

List of usage examples for org.apache.commons.jci.readers ResourceReader isAvailable

Introduction

In this page you can find the example usage for org.apache.commons.jci.readers ResourceReader isAvailable.

Prototype

boolean isAvailable(final String pResourceName);

Source Link

Usage

From source file:org.auroraide.server.jci.compilers.EclipseJavaCompiler.java

public org.apache.commons.jci.compilers.CompilationResult compile(final String[] pSourceFiles,
        final ResourceReader pReader, final ResourceStore pStore, final ClassLoader pClassLoader,
        final JavaCompilerSettings pSettings) {

    final Map<?, ?> settingsMap = ((EclipseJavaCompilerSettings) defaultSettings).getMap();

    final Collection<CompilationProblem> problems = new ArrayList<CompilationProblem>();

    final ICompilationUnit[] compilationUnits = new ICompilationUnit[pSourceFiles.length];
    for (int i = 0; i < compilationUnits.length; i++) {
        final String sourceFile = pSourceFiles[i];

        if (pReader.isAvailable(sourceFile)) {
            compilationUnits[i] = new CompilationUnit(pReader, sourceFile);
            log.debug("compiling " + sourceFile);
        } else {/* w  w w.  j a  v a2 s .com*/
            // log.error("source not found " + sourceFile);

            final CompilationProblem problem = new CompilationProblem() {

                public int getEndColumn() {
                    return 0;
                }

                public int getEndLine() {
                    return 0;
                }

                public String getFileName() {
                    return sourceFile;
                }

                public String getMessage() {
                    return "Source " + sourceFile + " could not be found";
                }

                public int getStartColumn() {
                    return 0;
                }

                public int getStartLine() {
                    return 0;
                }

                public boolean isError() {
                    return true;
                }

                public String toString() {
                    return getMessage();
                }
            };

            if (problemHandler != null) {
                problemHandler.handle(problem);
            }

            problems.add(problem);
        }
    }

    if (problems.size() > 0) {
        final CompilationProblem[] result = new CompilationProblem[problems.size()];
        problems.toArray(result);
        return new org.apache.commons.jci.compilers.CompilationResult(result);
    }

    final IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.proceedWithAllProblems();
    final IProblemFactory problemFactory = new DefaultProblemFactory(Locale.getDefault());
    final INameEnvironment nameEnvironment = new INameEnvironment() {

        public NameEnvironmentAnswer findType(final char[][] pCompoundTypeName) {
            final StringBuffer result = new StringBuffer();
            for (int i = 0; i < pCompoundTypeName.length; i++) {
                if (i != 0) {
                    result.append('.');
                }
                result.append(pCompoundTypeName[i]);
            }

            //log.debug("finding compoundTypeName=" + result.toString());

            return findType(result.toString());
        }

        public NameEnvironmentAnswer findType(final char[] pTypeName, final char[][] pPackageName) {
            final StringBuffer result = new StringBuffer();
            for (int i = 0; i < pPackageName.length; i++) {
                result.append(pPackageName[i]);
                result.append('.');
            }

            //                log.debug("finding typeName=" + new String(typeName) + " packageName=" + result.toString());

            result.append(pTypeName);
            return findType(result.toString());
        }

        private NameEnvironmentAnswer findType(final String pClazzName) {

            if (isPackage(pClazzName)) {
                return null;
            }

            log.debug("finding " + pClazzName);

            final String resourceName = ConversionUtils.convertClassToResourcePath(pClazzName);

            final byte[] clazzBytes = pStore.read(pClazzName);
            if (clazzBytes != null) {
                log.debug("loading from store " + pClazzName);

                final char[] fileName = pClazzName.toCharArray();
                try {
                    final ClassFileReader classFileReader = new ClassFileReader(clazzBytes, fileName, true);
                    return new NameEnvironmentAnswer(classFileReader, null);
                } catch (final ClassFormatException e) {
                    log.error("wrong class format", e);
                    return null;
                }
            }

            log.debug("not in store " + pClazzName);

            final InputStream is = pClassLoader.getResourceAsStream(resourceName);
            if (is == null) {
                log.debug("class " + pClazzName + " not found");
                return null;
            }

            final byte[] buffer = new byte[8192];
            final ByteArrayOutputStream baos = new ByteArrayOutputStream(buffer.length);
            int count;
            try {
                while ((count = is.read(buffer, 0, buffer.length)) > 0) {
                    baos.write(buffer, 0, count);
                }
                baos.flush();
                final char[] fileName = pClazzName.toCharArray();
                final ClassFileReader classFileReader = new ClassFileReader(baos.toByteArray(), fileName, true);
                return new NameEnvironmentAnswer(classFileReader, null);
            } catch (final IOException e) {
                log.error("could not read class", e);
                return null;
            } catch (final ClassFormatException e) {
                log.error("wrong class format", e);
                return null;
            } finally {
                try {
                    baos.close();
                } catch (final IOException oe) {
                    log.error("could not close output stream", oe);
                }
                try {
                    is.close();
                } catch (final IOException ie) {
                    log.error("could not close input stream", ie);
                }
            }
        }

        private boolean isPackage(final String pClazzName) {

            final InputStream is = pClassLoader
                    .getResourceAsStream(ConversionUtils.convertClassToResourcePath(pClazzName));
            if (is != null) {
                log.debug("found the class for " + pClazzName + "- no package");
                return false;
            }

            // FIXME: this should not be tied to the extension
            final String source = pClazzName.replace('.', '/') + ".java";
            if (pReader.isAvailable(source)) {
                log.debug("found the source " + source + " for " + pClazzName + " - no package ");
                return false;
            }

            return true;
        }

        public boolean isPackage(char[][] parentPackageName, char[] pPackageName) {
            final StringBuffer result = new StringBuffer();
            if (parentPackageName != null) {
                for (int i = 0; i < parentPackageName.length; i++) {
                    if (i != 0) {
                        result.append('.');
                    }
                    result.append(parentPackageName[i]);
                }
            }

            //                log.debug("isPackage parentPackageName=" + result.toString() + " packageName=" + new String(packageName));

            if (parentPackageName != null && parentPackageName.length > 0) {
                result.append('.');
            }
            result.append(pPackageName);
            return isPackage(result.toString());
        }

        public void cleanup() {
            log.debug("cleanup");
        }
    };

    final ICompilerRequestor compilerRequestor = new ICompilerRequestor() {
        public void acceptResult(final CompilationResult pResult) {
            if (pResult.hasProblems()) {
                final IProblem[] iproblems = pResult.getProblems();
                for (int i = 0; i < iproblems.length; i++) {
                    final IProblem iproblem = iproblems[i];
                    final CompilationProblem problem = new EclipseCompilationProblem(iproblem);
                    if (problemHandler != null) {
                        problemHandler.handle(problem);
                    }
                    problems.add(problem);
                }
            }
            if (!pResult.hasErrors()) {
                final ClassFile[] clazzFiles = pResult.getClassFiles();
                for (int i = 0; i < clazzFiles.length; i++) {
                    final ClassFile clazzFile = clazzFiles[i];
                    final char[][] compoundName = clazzFile.getCompoundName();
                    final StringBuffer clazzName = new StringBuffer();
                    for (int j = 0; j < compoundName.length; j++) {
                        if (j != 0) {
                            clazzName.append('.');
                        }
                        clazzName.append(compoundName[j]);
                    }
                    pStore.write(clazzName.toString().replace('.', '/') + ".class", clazzFile.getBytes());
                }
            }
        }
    };

    final Compiler compiler = new Compiler(nameEnvironment, policy, settingsMap, compilerRequestor,
            problemFactory, false);

    compiler.compile(compilationUnits);

    final CompilationProblem[] result = new CompilationProblem[problems.size()];
    problems.toArray(result);
    return new org.apache.commons.jci.compilers.CompilationResult(result);
}

From source file:org.auroraide.server.jci.compilers.Jsr199JavaCompiler.java

public CompilationResult compile(final String[] pResourcePaths, final ResourceReader pReader,
        final ResourceStore pStore, final ClassLoader classLoader, JavaCompilerSettings settings) {

    final Collection<JavaFileObject> units = new ArrayList<JavaFileObject>();
    for (int i = 0; i < pResourcePaths.length; i++) {
        final String sourcePath = pResourcePaths[i];
        log.debug("compiling " + sourcePath);
        final String source = pResourcePaths[i];
        if (pReader.isAvailable(source))
            units.add(new CompilationUnit(sourcePath, pReader));
    }/*  w ww  . j a  v  a2  s .c o  m*/

    final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

    final JavaFileManager fileManager = new JciJavaFileManager(units, pStore);
    final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
    // final JavaFileManager fileManager1 =
    // compiler.getStandardFileManager(null,null,null);

    System.getProperties();

    compiler.getTask(null, fileManager, diagnostics, options, null, units).call();

    try {
        fileManager.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    final List<Diagnostic<? extends JavaFileObject>> jsrProblems = diagnostics.getDiagnostics();
    final CompilationProblem[] problems = new CompilationProblem[jsrProblems.size()];
    int i = 0;
    for (final Diagnostic<? extends JavaFileObject> jsrProblem : jsrProblems) {
        problems[i++] = new Jsr199CompilationProblem(jsrProblem);

        if (problemHandler != null) {
            problemHandler.handle(problems[i - 1]);
        }
    }

    return new CompilationResult(problems);
}

From source file:org.drools.compiler.PackageBuilder.java

/**
 * Takes a given name and makes sure that its legal and doesn't already exist. If the file exists it increases counter appender untill it is unique.
 * /*from w w w . j a  v  a 2 s .co  m*/
 * @param packageName
 * @param name
 * @param ext
 * @return
 */
private String getUniqueLegalName(final String packageName, final String name, final String ext,
        final ResourceReader src) {
    // replaces all non alphanumeric or $ chars with _
    String newName = "Rule_" + name.replaceAll("[^\\w$]", "_");

    // make sure the class name does not exist, if it does increase the counter
    int counter = -1;
    boolean exists = true;
    while (exists) {

        counter++;
        final String fileName = packageName.replaceAll("\\.", "/") + newName + "_" + counter + ext;

        exists = src.isAvailable(fileName);
    }
    // we have duplicate file names so append counter
    if (counter >= 0) {
        newName = newName + "_" + counter;
    }

    return newName;
}