001: /*
002: * $RCSfile: PropertyUtil.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.1 $
009: * $Date: 2005/02/11 04:57:01 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.util;
013:
014: import java.io.File;
015: import java.io.FileInputStream;
016: import java.io.FileNotFoundException;
017: import java.io.InputStream;
018: import java.io.IOException;
019: import java.util.Hashtable;
020: import java.util.Iterator;
021: import java.util.Vector;
022: import java.util.PropertyResourceBundle;
023: import java.util.ResourceBundle;
024: import java.util.StringTokenizer;
025: import java.util.jar.JarEntry;
026: import java.util.jar.JarFile;
027: import java.net.URL;
028: import java.security.AccessController;
029: import java.security.PrivilegedAction;
030:
031: public class PropertyUtil {
032:
033: private static Hashtable bundles = new Hashtable();
034: private static String propertiesDir = "javax/media/jai";
035:
036: public static InputStream getFileFromClasspath(String path)
037: throws IOException, FileNotFoundException {
038: InputStream is;
039:
040: final String pathFinal = path;
041: final String sep = File.separator;
042: String tmpHome = null;
043: try {
044: tmpHome = System.getProperty("java.home");
045: } catch (Exception e) {
046: tmpHome = null; // Redundant
047: }
048: final String home = tmpHome;
049: final String urlHeader = tmpHome == null ? null : home + sep
050: + "lib" + sep;
051:
052: if (home != null) {
053: String libExtPath = urlHeader + "ext" + sep + path;
054: File libExtFile = new File(libExtPath);
055: try {
056: if (libExtFile.exists()) {
057: is = new FileInputStream(libExtFile);
058: if (is != null) {
059: return is;
060: }
061: }
062: } catch (java.security.AccessControlException e) {
063: // When the files are packed into jar files, the
064: // permission to access these files in a security environment
065: // isn't granted in the policy files in most of the cases.
066: // Thus, this java.security.AccessControlException is
067: // thrown. To continue the searching in the jar files,
068: // catch this exception and do nothing here.
069: // The fix of 4531516.
070: }
071: }
072:
073: is = PropertyUtil.class.getResourceAsStream("/" + path);
074: if (is != null) {
075: return is;
076: }
077:
078: // The above call doesn't work if the jai is an installed extension
079: // in the main jre/lib directory (as of 5-21-1999 the javaplugin
080: // doesn't look in the ext diretory which is a bug). We'll
081: // try to load the file from either $java_home/ext/jai_core.jar
082: // or $java_home/jai_core.jar. The ext is where it should be
083: // when the bug finally gets fixed.
084: PrivilegedAction p = new PrivilegedAction() {
085: public Object run() {
086: String localHome = null;
087: String localUrlHeader = null;
088: if (home != null) {
089: localHome = home;
090: localUrlHeader = urlHeader;
091: } else {
092: localHome = System.getProperty("java.home");
093: localUrlHeader = localHome + sep + "lib" + sep;
094: }
095: String filenames[] = {
096: localUrlHeader + "ext" + sep + "jai_core.jar",
097: localUrlHeader + "ext" + sep + "jai_codec.jar",
098: localUrlHeader + "jai_core.jar",
099: localUrlHeader + "jai_codec.jar" };
100:
101: for (int i = 0; i < filenames.length; i++) {
102: try {
103: InputStream tmpIS = getFileFromJar(
104: filenames[i], pathFinal);
105: if (tmpIS != null) {
106: return tmpIS;
107: }
108: } catch (Exception e) {
109: }
110: }
111:
112: return null;
113: }
114: };
115:
116: return (InputStream) AccessController.doPrivileged(p);
117: }
118:
119: private static InputStream getFileFromJar(String jarFilename,
120: String path) throws Exception {
121: // Look in jar file
122: JarFile f = null;
123: try {
124: f = new JarFile(jarFilename);
125: } catch (Exception e) {
126: }
127: JarEntry ent = f.getJarEntry(path);
128: if (ent != null) {
129: return f.getInputStream(ent);
130: }
131: return null;
132: }
133:
134: /** Get bundle from .properties files in javax/media/jai dir. */
135: private static ResourceBundle getBundle(String packageName) {
136: ResourceBundle bundle = null;
137:
138: InputStream in = null;
139: try {
140: in = getFileFromClasspath(propertiesDir + "/" + packageName
141: + ".properties");
142: if (in != null) {
143: bundle = new PropertyResourceBundle(in);
144: bundles.put(packageName, bundle);
145: return bundle;
146: }
147: } catch (Exception e) {
148: e.printStackTrace();
149: }
150:
151: return null;
152: }
153:
154: public static String getString(String packageName, String key) {
155: ResourceBundle b = (ResourceBundle) bundles.get(packageName);
156: if (b == null) {
157: b = getBundle(packageName);
158: }
159: return b.getString(key);
160: }
161:
162: /**
163: * Utility method to search the full list of property names for
164: * matches. If <code>propertyNames</code> is <code>null</code>
165: * then <code>null</code> is returned.
166: *
167: * @exception IllegalArgumentException if <code>prefix</code> is
168: * <code>null</code> and <code>propertyNames</code> is
169: * non-<code>null</code>.
170: */
171: public static String[] getPropertyNames(String[] propertyNames,
172: String prefix) {
173: if (propertyNames == null) {
174: return null;
175: } else if (prefix == null) {
176: throw new IllegalArgumentException(JaiI18N
177: .getString("PropertyUtil0"));
178: }
179:
180: prefix = prefix.toLowerCase();
181:
182: Vector names = new Vector();
183: for (int i = 0; i < propertyNames.length; i++) {
184: if (propertyNames[i].toLowerCase().startsWith(prefix)) {
185: names.addElement(propertyNames[i]);
186: }
187: }
188:
189: if (names.size() == 0) {
190: return null;
191: }
192:
193: // Copy the strings from the Vector over to a String array.
194: String prefixNames[] = new String[names.size()];
195: int count = 0;
196: for (Iterator it = names.iterator(); it.hasNext();) {
197: prefixNames[count++] = (String) it.next();
198: }
199:
200: return prefixNames;
201: }
202: }
|