001: /**
002: * $Id: NetFileResource.java,v 1.13 2005/11/30 11:26:39 ss150821 Exp $
003: * Copyright 2002 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.netfile.servlet.java2;
014:
015: import java.util.*;
016: import com.sun.portal.log.common.PortalLogger;
017: import java.util.logging.*;
018:
019: import java.text.MessageFormat;
020: import com.sun.portal.netfile.shared.*;
021:
022: public class NetFileResource {
023:
024: private static Logger logger = PortalLogger
025: .getLogger(NetFileResource.class);
026: private java.util.HashMap ht_resource_bundle_entry_set;
027: private String s_bundle;
028: private String s_locale;
029: private static java.util.HashMap ht_locales = new java.util.HashMap(
030: 6);
031:
032: public NetFileResource() {
033: }
034:
035: public NetFileResource(String bundle, String locale)
036: throws NetFileException {
037: this ();
038: if ((bundle == null) || (locale == null)) {
039: //throw new NetFileException("null bundle name passed");
040: throw new NetFileException();
041: }
042: s_bundle = bundle;
043: s_locale = locale;
044: ht_resource_bundle_entry_set = getLocalisedBundle(bundle,
045: s_locale);
046: }
047:
048: public NetFileResource(String bundle) throws NetFileException {
049: this ();
050: if (bundle == null) {
051: //throw new NetFileException("null bundle name passed");
052: throw new NetFileException();
053: }
054: String s_locale = Locale.getDefault().toString();
055: if ((s_locale == null) || (s_locale.equals(""))) {
056: //throw new NetFileException("There is no key as com.iplanet.am.locale in AMConfig.properties");
057: throw new NetFileException();
058: }
059:
060: s_bundle = bundle;
061: this .s_locale = s_locale;
062: ht_resource_bundle_entry_set = getLocalisedBundle(bundle,
063: s_locale);
064: }
065:
066: private synchronized HashMap getLocalisedBundle(String bundle,
067: String locale) throws NetFileException {
068: HashMap ht_locale_specific_bundles = getLocaleHashMap(locale);
069: HashMap ht_locale_specific_bundle = (HashMap) (ht_locale_specific_bundles
070: .get(bundle));
071:
072: if (ht_locale_specific_bundle == null) {
073: // logger.info("HashMap containing bundle "+bundle+" not found for locale "+locale);
074: Object[] params0 = { bundle, " not found for locale ",
075: locale };
076: logger.log(Level.INFO, "PSSRNF_CSPNSJ2091", params0);
077: ht_locale_specific_bundle = populatei18nBucket(bundle,
078: locale);
079: ht_locale_specific_bundles.put(bundle,
080: ht_locale_specific_bundle);
081: SerializedRequestProcessor.setResourceMap(locale, bundle,
082: this );
083: } else {
084: // logger.info("HashMap containing bundle "+bundle+" found for locale "+locale);
085: Object[] params1 = { bundle, " found for locale ", locale };
086: logger.log(Level.INFO, "PSSRNF_CSPNSJ2092", params1);
087: }
088: return ht_locale_specific_bundle;
089: }
090:
091: private HashMap getLocaleHashMap(String locale)
092: throws NetFileException {
093: try {
094: HashMap ht_locale_specific_bundles = (HashMap) (ht_locales
095: .get(locale));
096: if (ht_locale_specific_bundles == null) {
097: // logger.info("HashMap containing bundles for locale "+locale+" not found");
098: Object[] params2 = { locale, " not found" };
099: logger.log(Level.INFO, "PSSRNF_CSPNSJ2093", params2);
100: ht_locale_specific_bundles = new HashMap(2);
101: } else {
102: // logger.info("HashMap containing bundles for locale "+locale+" found");
103: Object[] params3 = { locale, " found" };
104: logger.log(Level.INFO, "PSSRNF_CSPNSJ2094", params3);
105: }
106: ht_locales.put(locale, ht_locale_specific_bundles);
107: return ht_locale_specific_bundles;
108: } catch (Exception e) {
109: // logger.log(Level.SEVERE, "Exception in searching for HashMap containg bundles in a locale", e);
110: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ2095");
111: //throw new NetFileException(e);
112: throw new NetFileException();
113: }
114: }
115:
116: public String getString(String s_key) {
117: Object o_value = ht_resource_bundle_entry_set.get(s_key);
118: if (o_value == null) {
119: // logger.severe("Key "+s_key+" does not exist in bundle "+s_bundle+" for locale "+s_locale);
120: Object[] params5 = { s_key, " does not exist in bundle ",
121: s_bundle, " for locale ", s_locale };
122: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ2096", params5);
123: return s_key;
124: }
125: return ((String) (o_value));
126: }
127:
128: public String getString(String key, Object[] args) {
129: MessageFormat mf = new MessageFormat("");
130: Locale loc = getLocale(s_locale);
131: mf.setLocale(loc);
132: mf.applyPattern(getString(key));
133:
134: return mf.format(args, new StringBuffer(), null).toString();
135: }
136:
137: public HashMap populatei18nBucket(String bundle, String locale)
138: throws NetFileException {
139: HashMap bucket = new HashMap();
140: try {
141: Object key = null;
142: Object value = null;
143: Locale loc = getLocale(locale);
144: java.util.ResourceBundle resBundle = java.util.ResourceBundle
145: .getBundle(bundle, loc);
146:
147: for (Enumeration e = resBundle.getKeys(); e
148: .hasMoreElements();) {
149: key = e.nextElement();
150: value = resBundle.getObject((String) key);
151: bucket.put(key, value);
152: }
153: } catch (Exception e) {
154: //throw new NetFileException(e);
155: throw new NetFileException();
156: }
157: return bucket;
158: }
159:
160: private Locale getLocale(String locale) {
161: if ((locale == null) || (locale.equals(""))) {
162: locale = Locale.getDefault().toString();
163: }
164:
165: StringTokenizer locst = new StringTokenizer(locale, "_");
166: String lang = (locst.hasMoreTokens() ? locst.nextToken() : "");
167: String country = (locst.hasMoreTokens() ? locst.nextToken()
168: : "");
169: String variant = (locst.hasMoreTokens() ? locst.nextToken()
170: : "");
171:
172: while (locst.hasMoreTokens()) {
173: variant += "_" + locst.nextToken();
174: }
175:
176: Locale loc = new java.util.Locale(lang, country, variant);
177:
178: return loc;
179: }
180:
181: }
|