rt/vm/src/main/java/org/apidesign/vm4brwsr/LdrRsrcs.java
author Lubomir Nerad <lubomir.nerad@oracle.com>
Thu, 25 Apr 2013 16:17:48 +0200
branchclosure
changeset 1020 a6bacea2518f
parent 874 2bcbe348dbec
child 1029 b1fe994d4267
permissions -rw-r--r--
Initial structure for extension modules
jaroslav@874
     1
/**
jaroslav@874
     2
 * Back 2 Browser Bytecode Translator
jaroslav@874
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@874
     4
 *
jaroslav@874
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@874
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@874
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@874
     8
 *
jaroslav@874
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@874
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@874
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@874
    12
 * GNU General Public License for more details.
jaroslav@874
    13
 *
jaroslav@874
    14
 * You should have received a copy of the GNU General Public License
jaroslav@874
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@874
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@874
    17
 */
jaroslav@874
    18
package org.apidesign.vm4brwsr;
jaroslav@874
    19
jaroslav@874
    20
import java.io.IOException;
jaroslav@874
    21
import java.io.InputStream;
jaroslav@874
    22
import java.net.URL;
jaroslav@874
    23
import java.util.Enumeration;
jaroslav@874
    24
jaroslav@874
    25
/** Implementation of Resources that delegates to some class loader.
jaroslav@874
    26
 *
jaroslav@874
    27
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@874
    28
 */
jaroslav@874
    29
final class LdrRsrcs implements Bck2Brwsr.Resources {
jaroslav@874
    30
    private final ClassLoader loader;
jaroslav@874
    31
jaroslav@874
    32
    LdrRsrcs(ClassLoader loader) {
jaroslav@874
    33
        this.loader = loader;
jaroslav@874
    34
    }
jaroslav@874
    35
jaroslav@874
    36
    @Override
jaroslav@874
    37
    public InputStream get(String name) throws IOException {
lubomir@1020
    38
        return findSource(name).openStream();
lubomir@1020
    39
    }
lubomir@1020
    40
lubomir@1020
    41
    @Override
lubomir@1020
    42
    public String getModule(String name) throws IOException {
lubomir@1020
    43
        final URL url = findSource(name);
lubomir@1020
    44
lubomir@1020
    45
        if (!"jar".equalsIgnoreCase(url.getProtocol())) {
lubomir@1020
    46
            return null;
lubomir@1020
    47
        }
lubomir@1020
    48
lubomir@1020
    49
        final String fullPathString = url.getPath();
lubomir@1020
    50
        final int sepIndex = fullPathString.indexOf('!');
lubomir@1020
    51
        final String jarPathString =
lubomir@1020
    52
                (sepIndex != -1) ? fullPathString.substring(0, sepIndex)
lubomir@1020
    53
                                 : fullPathString;
lubomir@1020
    54
        if (!jarPathString.endsWith(".jar")) {
lubomir@1020
    55
            return null;
lubomir@1020
    56
        }
lubomir@1020
    57
lubomir@1020
    58
        String moduleName =
lubomir@1020
    59
                jarPathString.substring(
lubomir@1020
    60
                                  jarPathString.lastIndexOf('/') + 1,
lubomir@1020
    61
                                  jarPathString.length() - 4);
lubomir@1020
    62
        if (moduleName.endsWith("-SNAPSHOT")) {
lubomir@1020
    63
            moduleName = moduleName.substring(
lubomir@1020
    64
                                        0, moduleName.length() - 9);
lubomir@1020
    65
        }
lubomir@1020
    66
        final int dashIndex = moduleName.lastIndexOf('-');
lubomir@1020
    67
        if (dashIndex != -1) {
lubomir@1020
    68
            moduleName = moduleName.substring(0, dashIndex);
lubomir@1020
    69
        }
lubomir@1020
    70
lubomir@1020
    71
        return moduleName;
lubomir@1020
    72
    }
lubomir@1020
    73
lubomir@1020
    74
    private URL findSource(String name) throws IOException {
jaroslav@874
    75
        Enumeration<URL> en = loader.getResources(name);
jaroslav@874
    76
        URL u = null;
jaroslav@874
    77
        while (en.hasMoreElements()) {
jaroslav@874
    78
            u = en.nextElement();
jaroslav@874
    79
        }
jaroslav@874
    80
        if (u == null) {
jaroslav@874
    81
            throw new IOException("Can't find " + name);
jaroslav@874
    82
        }
lubomir@1020
    83
        return u;
jaroslav@874
    84
    }
jaroslav@874
    85
}