author | Lubomir Nerad <lubomir.nerad@oracle.com> |
Fri, 26 Apr 2013 18:48:34 +0200 | |
branch | closure |
changeset 1029 | b1fe994d4267 |
parent 1020 | a6bacea2518f |
child 1094 | 36961c9a009f |
permissions | -rw-r--r-- |
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 { |
lubomir@1029 | 30 |
private final String module; |
jaroslav@874 | 31 |
private final ClassLoader loader; |
jaroslav@874 | 32 |
|
jaroslav@874 | 33 |
LdrRsrcs(ClassLoader loader) { |
lubomir@1029 | 34 |
this(null, loader); |
lubomir@1029 | 35 |
} |
lubomir@1029 | 36 |
|
lubomir@1029 | 37 |
LdrRsrcs(String module, ClassLoader loader) { |
lubomir@1029 | 38 |
this.module = module; |
jaroslav@874 | 39 |
this.loader = loader; |
jaroslav@874 | 40 |
} |
jaroslav@874 | 41 |
|
jaroslav@874 | 42 |
@Override |
jaroslav@874 | 43 |
public InputStream get(String name) throws IOException { |
lubomir@1029 | 44 |
final URL url = findSource(name); |
lubomir@1029 | 45 |
if (url == null) { |
lubomir@1029 | 46 |
return null; |
lubomir@1029 | 47 |
} |
lubomir@1029 | 48 |
if (module != null) { |
lubomir@1029 | 49 |
final String resourceModule = getModule(url); |
lubomir@1029 | 50 |
if ((resourceModule != null) && !module.equals(resourceModule)) { |
lubomir@1029 | 51 |
return null; |
lubomir@1029 | 52 |
} |
lubomir@1029 | 53 |
} |
lubomir@1029 | 54 |
|
lubomir@1029 | 55 |
return url.openStream(); |
lubomir@1020 | 56 |
} |
lubomir@1020 | 57 |
|
lubomir@1029 | 58 |
private URL findSource(String name) throws IOException { |
lubomir@1029 | 59 |
Enumeration<URL> en = loader.getResources(name); |
lubomir@1029 | 60 |
URL u = null; |
lubomir@1029 | 61 |
while (en.hasMoreElements()) { |
lubomir@1029 | 62 |
u = en.nextElement(); |
lubomir@1029 | 63 |
} |
lubomir@1029 | 64 |
return u; |
lubomir@1029 | 65 |
} |
lubomir@1020 | 66 |
|
lubomir@1029 | 67 |
private static String getModule(URL url) throws IOException { |
lubomir@1020 | 68 |
if (!"jar".equalsIgnoreCase(url.getProtocol())) { |
lubomir@1020 | 69 |
return null; |
lubomir@1020 | 70 |
} |
lubomir@1020 | 71 |
|
lubomir@1020 | 72 |
final String fullPathString = url.getPath(); |
lubomir@1020 | 73 |
final int sepIndex = fullPathString.indexOf('!'); |
lubomir@1020 | 74 |
final String jarPathString = |
lubomir@1020 | 75 |
(sepIndex != -1) ? fullPathString.substring(0, sepIndex) |
lubomir@1020 | 76 |
: fullPathString; |
lubomir@1020 | 77 |
if (!jarPathString.endsWith(".jar")) { |
lubomir@1020 | 78 |
return null; |
lubomir@1020 | 79 |
} |
lubomir@1020 | 80 |
|
lubomir@1020 | 81 |
String moduleName = |
lubomir@1020 | 82 |
jarPathString.substring( |
lubomir@1020 | 83 |
jarPathString.lastIndexOf('/') + 1, |
lubomir@1020 | 84 |
jarPathString.length() - 4); |
lubomir@1020 | 85 |
if (moduleName.endsWith("-SNAPSHOT")) { |
lubomir@1020 | 86 |
moduleName = moduleName.substring( |
lubomir@1020 | 87 |
0, moduleName.length() - 9); |
lubomir@1020 | 88 |
} |
lubomir@1020 | 89 |
final int dashIndex = moduleName.lastIndexOf('-'); |
lubomir@1020 | 90 |
if (dashIndex != -1) { |
lubomir@1020 | 91 |
moduleName = moduleName.substring(0, dashIndex); |
lubomir@1020 | 92 |
} |
lubomir@1020 | 93 |
|
lubomir@1020 | 94 |
return moduleName; |
lubomir@1020 | 95 |
} |
jaroslav@874 | 96 |
} |