001: /**
002: * $Id: ResourceLookup.java,v 1.2 2004/01/21 20:41:45 sathyakn Exp $
003: * Copyright © 2002 Sun Microsystems, Inc. All rights reserved.
004: *
005: * U.S. Government Rights - Commercial software. Government users are subject
006: * to the Sun Microsystems, Inc. standard license agreement and applicable
007: * provisions of the FAR and its supplements.
008: *
009: * Use is subject to license terms.
010: *
011: * This distribution may include materials developed by third parties.Sun, Sun
012: * Microsystems, the Sun logo and Java are trademarks or registered trademarks
013: * of Sun Microsystems, Inc. in the U.S. and other countries.
014: *
015: * Copyright © 2002 Sun Microsystems, Inc. Tous droits réservés.
016: *
017: * Droits du gouvernement américain, utlisateurs gouvernmentaux - logiciel
018: * commercial. Les utilisateurs gouvernmentaux sont soumis au contrat de licence
019: * standard de Sun Microsystems, Inc., ainsi qu aux dispositions en vigueur de
020: * la FAR [ (Federal Acquisition Regulations) et des suppléments à
021: * celles-ci.
022: *
023: * Distribué par des licences qui en restreignent l'utilisation.
024: *
025: * Cette distribution peut comprendre des composants développés pardes
026: * tierces parties.Sun, Sun Microsystems, le logo Sun et Java sont des marques
027: * de fabrique ou des marques déposées de Sun Microsystems, Inc. aux
028: * Etats-Unis et dans d'autres pays.
029: */package com.sun.mobile.util;
030:
031: import java.util.*;
032: import java.io.*;
033: import java.net.*;
034: import javax.servlet.*;
035: import com.iplanet.am.util.*;
036:
037: /**
038: * ResourceLookup is a partial replacement for the IS implementation of
039: * FileLookup. It performs the equivalent of "fstat" using ServletContext.getResource(),
040: * thus increasing web container independence. It also adds an additional qualifying
041: * parameter, "subComponent", which is intended to further qualify the "component"
042: * parameter, e.g. mail/exchange, cal/notes, ab/sun-one, etc.
043: *
044: * ResourceLookup should follow same look up pattern of Java Resource Bundle Lookup
045: * Failure to do so results in messages in mixed languages
046: * Java lookup mechansim is if platform locale is ja_JP
047: * Resource Bundle locale is zh_CN
048: * the lookup sequence is rb_zh_CN, rb_zh, rb_JA_JP, rb_ja, rb
049: * ie we can't fall back to rb if zh resource bundle is not found
050: * Hence We have added more steps to file lookup
051: */
052: public class ResourceLookup {
053:
054: private static Map resourceNameCache = new HashMap();
055:
056: /**
057: * The getFirstExisting() method will return the first existing file
058: * in the ordered search paths.
059: * @param type (an arbitrary profile-stored string)
060: * @param locale
061: * @param component
062: * @param subComponent
063: * @param clientPath
064: * @param filename
065: * @param resourceDir (absolute path of template base directory)
066: * @param enableCache (boolean on whether to cache previously returned files, restart required for changes when false)
067: * @return <code>String</code> first existing resource in the ordered search paths.
068: */
069: public static String getFirstExisting(ServletContext context,
070: String type, String locale, String component,
071: String subComponent, String clientPath, String filename,
072: String resourceDir, boolean enableCache) {
073: return getFirstExisting(context, type, locale, component,
074: subComponent, null, clientPath, filename, resourceDir,
075: enableCache);
076: }
077:
078: /**
079: * The getFirstExisting() method will return the first existing file
080: * in the ordered search paths.
081: * @param type (an arbitrary profile-stored string)
082: * @param locale
083: * @param component
084: * @param subComponent
085: * @param clientPath
086: * @param orgFilePath
087: * @param filename
088: * @param resourceDir (absolute path of template base directory)
089: * @param enableCache (boolean on whether to cache previously returned files, restart required for changes when false)
090: * @return <code>String</code> first existing resource in the ordered search paths.
091: */
092: public static String getFirstExisting(ServletContext context,
093: String type, String locale, String component,
094: String subComponent, String orgFilePath, String clientPath,
095: String filename, String resourceDir, boolean enableCache) {
096: String resourceName = null;
097:
098: //
099: // TODO: This is a temporary hack until an optimized implementation
100: // that supports the new "type" encoding is possible...
101: //
102: if (type != null) {
103: int i = type.indexOf(',');
104: if (i != -1) {
105: type = type.substring(0, i);
106: }
107: }
108:
109: String cacheKey = type + ":" + locale + ":" + component + ":"
110: + subComponent + ":" + orgFilePath + ":" + clientPath
111: + ":" + filename + ":" + resourceDir;
112:
113: if (enableCache) {
114: resourceName = (String) resourceNameCache.get(cacheKey);
115: if (resourceName != null) {
116: return resourceName;
117: }
118: }
119:
120: URL resourceUrl = null;
121:
122: if (subComponent != null && subComponent.length() != 0) {
123: String newClientPath;
124:
125: if (clientPath == null || clientPath.length() == 0) {
126: newClientPath = subComponent;
127: } else {
128: newClientPath = subComponent + "/" + clientPath;
129: }
130:
131: //
132: // Try lookup using subComponent...
133: //
134: try {
135: File[] orderedPaths = FileLookup.getOrderedPaths(type,
136: locale, component, orgFilePath, newClientPath,
137: filename);
138:
139: for (int i = 0; i < orderedPaths.length; i++) {
140: resourceName = resourceDir + "/"
141: + orderedPaths[i].toString();
142: try {
143: resourceUrl = context.getResource(resourceName);
144: } catch (Exception e) {
145: }
146: if (resourceUrl != null)
147: break;
148: }
149: } catch (Exception e) {
150: }
151: }
152:
153: if (resourceUrl == null) {
154: //
155: // Try lookup w/o subComponent...
156: //
157: try {
158: File[] orderedPaths = FileLookup.getOrderedPaths(type,
159: locale, component, orgFilePath, clientPath,
160: filename);
161:
162: for (int i = 0; i < orderedPaths.length; i++) {
163: resourceName = resourceDir + "/"
164: + orderedPaths[i].toString();
165: try {
166: resourceUrl = context.getResource(resourceName);
167: } catch (Exception e) {
168: }
169: if (resourceUrl != null)
170: break;
171: }
172: } catch (Exception e) {
173: }
174: }
175:
176: if (resourceUrl != null) {
177: if (enableCache) {
178: synchronized (resourceNameCache) {
179: resourceNameCache.put(cacheKey, resourceName);
180: }
181: }
182: } else {
183: return null;
184: }
185:
186: return resourceName;
187: }
188:
189: }
|