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
     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 ClassLoader loader;
    31 
    32     LdrRsrcs(ClassLoader loader) {
    33         this.loader = loader;
    34     }
    35 
    36     @Override
    37     public InputStream get(String name) throws IOException {
    38         return findSource(name).openStream();
    39     }
    40 
    41     @Override
    42     public String getModule(String name) throws IOException {
    43         final URL url = findSource(name);
    44 
    45         if (!"jar".equalsIgnoreCase(url.getProtocol())) {
    46             return null;
    47         }
    48 
    49         final String fullPathString = url.getPath();
    50         final int sepIndex = fullPathString.indexOf('!');
    51         final String jarPathString =
    52                 (sepIndex != -1) ? fullPathString.substring(0, sepIndex)
    53                                  : fullPathString;
    54         if (!jarPathString.endsWith(".jar")) {
    55             return null;
    56         }
    57 
    58         String moduleName =
    59                 jarPathString.substring(
    60                                   jarPathString.lastIndexOf('/') + 1,
    61                                   jarPathString.length() - 4);
    62         if (moduleName.endsWith("-SNAPSHOT")) {
    63             moduleName = moduleName.substring(
    64                                         0, moduleName.length() - 9);
    65         }
    66         final int dashIndex = moduleName.lastIndexOf('-');
    67         if (dashIndex != -1) {
    68             moduleName = moduleName.substring(0, dashIndex);
    69         }
    70 
    71         return moduleName;
    72     }
    73 
    74     private URL findSource(String name) throws IOException {
    75         Enumeration<URL> en = loader.getResources(name);
    76         URL u = null;
    77         while (en.hasMoreElements()) {
    78             u = en.nextElement();
    79         }
    80         if (u == null) {
    81             throw new IOException("Can't find " + name);
    82         }
    83         return u;
    84     }
    85 }