Search in sources :

Example 1 with ByteArrayResource

use of org.eclipse.ceylon.compiler.java.language.ByteArrayResource in project ceylon by eclipse.

the class ModuleImpl method resourceByPath.

@Override
@TypeInfo("ceylon.language::Resource")
public Resource resourceByPath(@Name("path") String path) {
    String fullPath = path;
    if (!fullPath.startsWith("/")) {
        String modPath = ("default".equals(getName())) ? "" : getName().replace('.', '/') + "/";
        fullPath = modPath + path;
    } else {
        fullPath = fullPath.substring(1);
    }
    fullPath = JVMModuleUtil.quoteJavaKeywordsInFilename(fullPath);
    // First lets ask the module manager for the contents of the resource
    RuntimeModuleManager moduleManager = Metamodel.getModuleManager();
    if (moduleManager != null) {
        RuntimeModelLoader modelLoader = moduleManager.getModelLoader();
        if (modelLoader != null) {
            byte[] contents = modelLoader.getContents(declaration, fullPath);
            if (contents != null) {
                URI uri = modelLoader.getContentUri(declaration, fullPath);
                return new ByteArrayResource(contents, uri);
            }
        }
    }
    // Second let's see if we can find the on-disk location of the module
    String moduleUnitFullPath = declaration.getUnit().getFullPath();
    if (moduleUnitFullPath != null) {
        final File car = new File(moduleUnitFullPath);
        // Then let's look inside the car
        try (ZipFile zip = new ZipFile(car)) {
            ZipEntry e = zip.getEntry(fullPath);
            if (e != null && !e.isDirectory()) {
                return new ZipResource(car, fullPath);
            }
        } catch (IOException ex) {
            throw new ceylon.language.Exception(new ceylon.language.String("Searching for resource " + path), ex);
        }
        // And finally as a fall-back let's look in the module's resource dir...
        final File target = new File(new File(car.getParentFile(), "module-resources"), fullPath);
        if (target.exists() && target.isFile() && target.canRead()) {
            return new FileResource(target);
        }
    }
    // One last shot: we might be in a fat jar
    try (InputStream stream = getClass().getClassLoader().getResourceAsStream(fullPath)) {
        if (stream != null) {
            byte[] buf = new byte[16384];
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            int bytesRead = stream.read(buf);
            while (bytesRead > 0) {
                bout.write(buf, 0, bytesRead);
                bytesRead = stream.read(buf);
            }
            return new ByteArrayResource(bout.toByteArray(), new URI("classpath:" + fullPath));
        }
    } catch (IOException | URISyntaxException ex) {
        throw new ceylon.language.Exception(new ceylon.language.String("Searching for resource " + path), ex);
    }
    return null;
}
Also used : RuntimeModuleManager(org.eclipse.ceylon.compiler.java.runtime.model.RuntimeModuleManager) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) FileResource(org.eclipse.ceylon.compiler.java.language.FileResource) ByteArrayResource(org.eclipse.ceylon.compiler.java.language.ByteArrayResource) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ZipResource(org.eclipse.ceylon.compiler.java.language.ZipResource) ZipFile(java.util.zip.ZipFile) RuntimeModelLoader(org.eclipse.ceylon.compiler.java.runtime.model.RuntimeModelLoader) ZipFile(java.util.zip.ZipFile) File(java.io.File) TypeInfo(org.eclipse.ceylon.compiler.java.metadata.TypeInfo)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1 ByteArrayResource (org.eclipse.ceylon.compiler.java.language.ByteArrayResource)1 FileResource (org.eclipse.ceylon.compiler.java.language.FileResource)1 ZipResource (org.eclipse.ceylon.compiler.java.language.ZipResource)1 TypeInfo (org.eclipse.ceylon.compiler.java.metadata.TypeInfo)1 RuntimeModelLoader (org.eclipse.ceylon.compiler.java.runtime.model.RuntimeModelLoader)1 RuntimeModuleManager (org.eclipse.ceylon.compiler.java.runtime.model.RuntimeModuleManager)1