001: /*
002: *******************************************************************************
003: * Copyright (C) 2004-2005, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: *
007: * Created on Feb 4, 2004
008: *
009: */
010: package com.ibm.icu.impl;
011:
012: import java.io.InputStream;
013: import java.net.URL;
014: import java.security.AccessController;
015: import java.security.PrivilegedAction;
016: import java.util.MissingResourceException;
017:
018: /**
019: * Provides access to ICU data files as InputStreams. Implements security checking.
020: */
021: public final class ICUData {
022: /*
023: * Return a URL to the ICU resource names resourceName. The
024: * resource name should either be an absolute path, or a path relative to
025: * com.ibm.icu.impl (e.g., most likely it is 'data/foo'). If required
026: * is true, throw an MissingResourceException instead of returning a null result.
027: */
028: public static boolean exists(final String resourceName) {
029: URL i = null;
030: if (System.getSecurityManager() != null) {
031: i = (URL) AccessController
032: .doPrivileged(new PrivilegedAction() {
033: public Object run() {
034: return ICUData.class
035: .getResource(resourceName);
036: }
037: });
038: } else {
039: i = ICUData.class.getResource(resourceName);
040: }
041: return i != null;
042: }
043:
044: private static InputStream getStream(final Class root,
045: final String resourceName, boolean required) {
046: InputStream i = null;
047:
048: if (System.getSecurityManager() != null) {
049: i = (InputStream) AccessController
050: .doPrivileged(new PrivilegedAction() {
051: public Object run() {
052: return root
053: .getResourceAsStream(resourceName);
054: }
055: });
056: } else {
057: i = root.getResourceAsStream(resourceName);
058: }
059:
060: if (i == null && required) {
061: throw new MissingResourceException("could not locate data "
062: + resourceName, root.getPackage().getName(),
063: resourceName);
064: }
065: return i;
066: }
067:
068: private static InputStream getStream(final ClassLoader loader,
069: final String resourceName, boolean required) {
070: InputStream i = null;
071: if (System.getSecurityManager() != null) {
072: i = (InputStream) AccessController
073: .doPrivileged(new PrivilegedAction() {
074: public Object run() {
075: return loader
076: .getResourceAsStream(resourceName);
077: }
078: });
079: } else {
080: i = loader.getResourceAsStream(resourceName);
081: }
082: if (i == null && required) {
083: throw new MissingResourceException("could not locate data",
084: loader.toString(), resourceName);
085: }
086: return i;
087: }
088:
089: public static InputStream getStream(ClassLoader loader,
090: String resourceName) {
091: return getStream(loader, resourceName, false);
092: }
093:
094: public static InputStream getRequiredStream(ClassLoader loader,
095: String resourceName) {
096: return getStream(loader, resourceName, true);
097: }
098:
099: /*
100: * Convenience override that calls getStream(ICUData.class, resourceName, false);
101: */
102: public static InputStream getStream(String resourceName) {
103: return getStream(ICUData.class, resourceName, false);
104: }
105:
106: /*
107: * Convenience method that calls getStream(ICUData.class, resourceName, true).
108: */
109: public static InputStream getRequiredStream(String resourceName) {
110: return getStream(ICUData.class, resourceName, true);
111: }
112:
113: /*
114: * Convenience override that calls getStream(root, resourceName, false);
115: */
116: public static InputStream getStream(Class root, String resourceName) {
117: return getStream(root, resourceName, false);
118: }
119:
120: /*
121: * Convenience method that calls getStream(root, resourceName, true).
122: */
123: public static InputStream getRequiredStream(Class root,
124: String resourceName) {
125: return getStream(root, resourceName, true);
126: }
127: }
|