ICU-4511 fix icu data loading for websphere - prototype
X-SVN-Rev: 17582
This commit is contained in:
parent
ebfe32c0e3
commit
4ad4ae521e
@ -25,7 +25,9 @@ import java.util.Set;
|
||||
import java.util.Vector;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import com.ibm.icu.impl.URLHandler.URLVisitor;
|
||||
import com.ibm.icu.util.StringTokenizer;
|
||||
import com.ibm.icu.util.ULocale;
|
||||
import com.ibm.icu.util.UResourceBundle;
|
||||
@ -76,8 +78,6 @@ public abstract class ICUResourceBundle extends UResourceBundle{
|
||||
static {
|
||||
ClassLoader loader = ICUData.class.getClassLoader();
|
||||
if (loader == null) { // boot class loader
|
||||
// security check not performed if our class loader is null,
|
||||
// which it should be if ICUData's class loader was null
|
||||
loader = ClassLoader.getSystemClassLoader();
|
||||
}
|
||||
ICU_DATA_CLASS_LOADER = loader;
|
||||
@ -840,6 +840,9 @@ public abstract class ICUResourceBundle extends UResourceBundle{
|
||||
// Cache for getAvailableLocales
|
||||
private static SoftReference GET_AVAILABLE_CACHE;
|
||||
private static final ULocale[] createULocaleList(String baseName, ClassLoader root){
|
||||
// the canned list is a subset of all the available .res files, the idea is we don't export them
|
||||
// all. gotta be a better way to do this, since to add a locale you have to update this list,
|
||||
// and it's embedded in our binary resources.
|
||||
ICUResourceBundle bundle = (ICUResourceBundle) instantiateBundle(baseName, ICU_RESOURCE_INDEX, root, true);
|
||||
bundle = bundle.get(INSTALLED_LOCALES);
|
||||
int length = bundle.getSize();
|
||||
@ -875,69 +878,33 @@ public abstract class ICUResourceBundle extends UResourceBundle{
|
||||
}
|
||||
|
||||
private static final ArrayList createFullLocaleNameArray(final String baseName, final ClassLoader root){
|
||||
final ArrayList list = new ArrayList();
|
||||
|
||||
java.security.AccessController.
|
||||
ArrayList list = (ArrayList)java.security.AccessController.
|
||||
doPrivileged(new java.security.PrivilegedAction() {
|
||||
public Object run() {
|
||||
URL url = root.getResource(baseName);
|
||||
|
||||
if (!url.getProtocol().equalsIgnoreCase("jar")) {
|
||||
// assume a file
|
||||
File file = new File(url.getPath());
|
||||
File[] files = file.listFiles();
|
||||
if (files != null) {
|
||||
// then it's a directory...
|
||||
for (int i = 0; i < files.length; i++){
|
||||
if (!files[i].isDirectory()) {
|
||||
String name = files[i].getName();
|
||||
if (name.indexOf("res_index") < 0) {
|
||||
name = name.substring(0, name.lastIndexOf('.'));
|
||||
list.add(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// we failed to recognize the url!
|
||||
}
|
||||
} else {
|
||||
// otherwise its a jar file...
|
||||
try {
|
||||
String fileName = url.getPath();
|
||||
int ix = fileName.indexOf("!/");
|
||||
if (ix >= 0) {
|
||||
fileName = fileName.substring(ix + 2); // truncate after "!/"
|
||||
}
|
||||
JarURLConnection conn = (JarURLConnection)url.openConnection();
|
||||
JarFile jarFile = conn.getJarFile();
|
||||
Enumeration entries = jarFile.entries();
|
||||
while (entries.hasMoreElements()) {
|
||||
JarEntry entry = (JarEntry)entries.nextElement();
|
||||
if (!entry.isDirectory()) {
|
||||
String name = entry.getName();
|
||||
if (name.startsWith(fileName)) {
|
||||
name = name.substring(fileName.length() + 1);
|
||||
if (name.indexOf('/') == -1 && name.endsWith(".res")) {
|
||||
name = name.substring(0, name.lastIndexOf('.'));
|
||||
list.add(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
if (DEBUG){
|
||||
System.out.println("icurb jar error: " + e);
|
||||
Thread.dumpStack();
|
||||
}
|
||||
}
|
||||
}
|
||||
// WebSphere class loader will return null for a raw directory name without trailing slash
|
||||
URL url = root.getResource(baseName.endsWith("/") ? baseName : baseName + "/"); //
|
||||
|
||||
URLHandler handler = URLHandler.get(url);
|
||||
if (handler != null) {
|
||||
final ArrayList list = new ArrayList();
|
||||
URLVisitor v = new URLVisitor() {
|
||||
private Pattern p = Pattern.compile(".*\\.res");
|
||||
public void visit(String s) {
|
||||
if (p.matcher(s).matches() && !"res_index.res".equals(s)) {
|
||||
list.add(s.substring(0, s.length() - 4)); // strip '.res'
|
||||
}
|
||||
}
|
||||
};
|
||||
handler.guide(v, false);
|
||||
return list;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
return list;
|
||||
return list;
|
||||
}
|
||||
|
||||
private static Set createFullLocaleNameSet(String baseName) {
|
||||
|
205
icu4j/src/com/ibm/icu/impl/URLHandler.java
Normal file
205
icu4j/src/com/ibm/icu/impl/URLHandler.java
Normal file
@ -0,0 +1,205 @@
|
||||
/*
|
||||
******************************************************************************
|
||||
* Copyright (C) 2005, International Business Machines Corporation and *
|
||||
* others. All Rights Reserved. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
package com.ibm.icu.impl;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.JarURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
|
||||
public abstract class URLHandler {
|
||||
public static final String PROPNAME = "urlhandler.props";
|
||||
|
||||
private static final Map handlers;
|
||||
|
||||
private static final boolean DEBUG = ICUDebug.enabled("URLHandler");
|
||||
|
||||
static {
|
||||
Map h = null;
|
||||
try {
|
||||
InputStream is = URLHandler.class.getResourceAsStream(PROPNAME);
|
||||
if (is == null) {
|
||||
is = ClassLoader.getSystemClassLoader().getResourceAsStream(PROPNAME);
|
||||
}
|
||||
if (is != null) {
|
||||
Class[] params = { URL.class };
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(is));
|
||||
for (String line = br.readLine(); line != null; line = br.readLine()) {
|
||||
line = line.trim();
|
||||
if (line.length() == 0 || line.charAt(0) == '#') {
|
||||
continue;
|
||||
}
|
||||
int ix = line.indexOf('=');
|
||||
if (ix == -1) {
|
||||
if (DEBUG) System.err.println("bad urlhandler line: '" + line + "'");
|
||||
break;
|
||||
}
|
||||
String key = line.substring(0, ix).trim();
|
||||
String value = line.substring(ix+1).trim();
|
||||
|
||||
try {
|
||||
Class cl = Class.forName(value);
|
||||
Method m = cl.getDeclaredMethod("get", params);
|
||||
if (h == null) {
|
||||
h = new HashMap();
|
||||
}
|
||||
h.put(key, m);
|
||||
}
|
||||
catch (ClassNotFoundException e) {
|
||||
if (DEBUG) System.err.println(e);
|
||||
}
|
||||
catch(NoSuchMethodException e) {
|
||||
if (DEBUG) System.err.println(e);
|
||||
}
|
||||
catch(SecurityException e) {
|
||||
if (DEBUG) System.err.println(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
if (DEBUG) System.err.println(t);
|
||||
}
|
||||
handlers = h;
|
||||
}
|
||||
|
||||
public static URLHandler get(URL url) {
|
||||
if (url == null) {
|
||||
return null;
|
||||
}
|
||||
String protocol = url.getProtocol();
|
||||
if (handlers != null) {
|
||||
Method m = (Method)handlers.get(protocol);
|
||||
if (m != null) {
|
||||
try {
|
||||
URLHandler handler = (URLHandler)m.invoke(null, new Object[] { url });
|
||||
if (handler != null) {
|
||||
return handler;
|
||||
}
|
||||
}
|
||||
catch(IllegalAccessException e) {
|
||||
if (DEBUG) System.err.println(e);
|
||||
}
|
||||
catch(IllegalArgumentException e) {
|
||||
if (DEBUG) System.err.println(e);
|
||||
}
|
||||
catch(InvocationTargetException e) {
|
||||
if (DEBUG) System.err.println(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getDefault(url);
|
||||
}
|
||||
|
||||
protected static URLHandler getDefault(URL url) {
|
||||
String protocol = url.getProtocol();
|
||||
if (protocol.equals("file")) {
|
||||
return new FileURLHandler(url);
|
||||
} else if (protocol.equals("jar")) {
|
||||
return new JarURLHandler(url);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static class FileURLHandler extends URLHandler {
|
||||
File file;
|
||||
|
||||
FileURLHandler(URL url) {
|
||||
file = new File(url.getPath());
|
||||
if (!file.exists()) {
|
||||
if (DEBUG) System.err.println("file does not exist");
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
public void guide(URLVisitor v, boolean recurse) {
|
||||
if (file.isDirectory()) {
|
||||
process(v, recurse, file.listFiles());
|
||||
} else {
|
||||
v.visit(file.getName());
|
||||
}
|
||||
}
|
||||
|
||||
private void process(URLVisitor v, boolean recurse, File[] files) {
|
||||
for (int i = 0; i < files.length; i++) {
|
||||
File f = files[i];
|
||||
if (f.isDirectory()) {
|
||||
if (recurse) {
|
||||
process(v, recurse, f.listFiles());
|
||||
}
|
||||
} else {
|
||||
v.visit(f.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class JarURLHandler extends URLHandler {
|
||||
JarFile jarFile;
|
||||
String prefix;
|
||||
|
||||
JarURLHandler(URL url) {
|
||||
try {
|
||||
prefix = url.getPath();
|
||||
int ix = prefix.indexOf("!/");
|
||||
if (ix >= 0) {
|
||||
prefix = prefix.substring(ix + 2); // truncate after "!/"
|
||||
}
|
||||
JarURLConnection conn = (JarURLConnection)url.openConnection();
|
||||
jarFile = conn.getJarFile();
|
||||
}
|
||||
catch (Exception e) {
|
||||
if (DEBUG) System.err.println("icurb jar error: " + e);
|
||||
throw new IllegalArgumentException("jar error: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void guide(URLVisitor v, boolean recurse) {
|
||||
try {
|
||||
Enumeration entries = jarFile.entries();
|
||||
while (entries.hasMoreElements()) {
|
||||
JarEntry entry = (JarEntry)entries.nextElement();
|
||||
if (!entry.isDirectory()) { // skip just directory paths
|
||||
String name = entry.getName();
|
||||
if (name.startsWith(prefix)) {
|
||||
name = name.substring(prefix.length());
|
||||
int ix = name.lastIndexOf('/');
|
||||
if (ix != -1) {
|
||||
if (!recurse) {
|
||||
continue;
|
||||
}
|
||||
name = name.substring(ix+1);
|
||||
}
|
||||
v.visit(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
if (DEBUG) System.err.println("icurb jar error: " + e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void guide(URLVisitor visitor, boolean recurse);
|
||||
|
||||
public interface URLVisitor {
|
||||
void visit(String str);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user