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