001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.pluto.internal.impl;
018:
019: import java.util.HashMap;
020: import java.util.Locale;
021: import java.util.Map;
022: import java.util.MissingResourceException;
023: import java.util.ResourceBundle;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.apache.pluto.PortletWindow;
028: import org.apache.pluto.core.ContainerInvocation;
029: import org.apache.pluto.descriptors.portlet.PortletDD;
030: import org.apache.pluto.descriptors.portlet.PortletInfoDD;
031: import org.apache.pluto.spi.optional.PortletInfoService;
032: import org.apache.pluto.util.StringManager;
033:
034: /**
035: * Factory object used to create Portlet Resource Bundles.
036: *
037: */
038: class ResourceBundleFactory {
039:
040: private static final Log LOG = LogFactory
041: .getLog(ResourceBundleFactory.class);
042:
043: private static final StringManager EXCEPTIONS = StringManager
044: .getManager(ResourceBundleFactory.class.getPackage()
045: .getName());
046:
047: /**
048: * The default (no local) resources bundle for
049: * this bundle.
050: */
051: private InlinePortletResourceBundle defaultBundle;
052:
053: /**
054: * All of the previously loaded bundles.
055: */
056: private final Map bundles = new HashMap();
057:
058: /**
059: * The name of the bundle.
060: */
061: private final String bundleName;
062:
063: public ResourceBundleFactory(PortletDD dd) {
064: bundleName = dd.getResourceBundle();
065: if (LOG.isDebugEnabled()) {
066: LOG.debug("Resource Bundle Created: " + bundleName);
067: }
068:
069: PortletInfoDD info = dd.getPortletInfo();
070:
071: PortletInfoService infoService = getPortletInfoService();
072: PortletWindow window = getWindow();
073:
074: if (info != null) {
075: String title = infoService == null ? info.getTitle()
076: : infoService.getTitle(window);
077: String shortTitle = infoService == null ? info
078: .getShortTitle() : infoService
079: .getShortTitle(window);
080: String keywords = infoService == null ? info.getKeywords()
081: : infoService.getKeywords(window);
082:
083: defaultBundle = new InlinePortletResourceBundle(title,
084: shortTitle, keywords);
085: } else {
086: defaultBundle = new InlinePortletResourceBundle(
087: new Object[][] { { "a", "b" } });
088: }
089: }
090:
091: public ResourceBundle getResourceBundle(Locale locale) {
092: if (LOG.isDebugEnabled()) {
093: LOG.debug("Resource Bundle: " + bundleName + " : " + locale
094: + " requested. ");
095: }
096:
097: // If allready loaded for this local, return immediately!
098: if (bundles.containsKey(locale)) {
099: return (ResourceBundle) bundles.get(locale);
100: }
101:
102: try {
103: ResourceBundle bundle = null;
104: if (bundleName != null) {
105: ClassLoader loader = Thread.currentThread()
106: .getContextClassLoader();
107: bundle = ResourceBundle.getBundle(bundleName, locale,
108: loader);
109: bundles.put(locale, new CombinedPortletResourceBundle(
110: defaultBundle, bundle));
111: } else {
112: bundles.put(locale, defaultBundle);
113: }
114: } catch (MissingResourceException mre) {
115: if (bundleName != null && LOG.isWarnEnabled()) {
116: LOG.info(EXCEPTIONS.getString(
117: "warning.resourcebundle.notfound", bundleName,
118: mre.getMessage()));
119: }
120: if (LOG.isDebugEnabled()) {
121: LOG.debug("Using default bundle for locale (" + locale
122: + ").");
123: }
124: bundles.put(locale, defaultBundle);
125: }
126: return (ResourceBundle) bundles.get(locale);
127: }
128:
129: private PortletInfoService getPortletInfoService() {
130: ContainerInvocation invocation = ContainerInvocation
131: .getInvocation();
132: if (invocation != null) {
133: return invocation.getPortletContainer()
134: .getOptionalContainerServices()
135: .getPortletInfoService();
136: }
137: return null;
138: }
139:
140: private PortletWindow getWindow() {
141: ContainerInvocation invocation = ContainerInvocation
142: .getInvocation();
143: if (invocation != null) {
144: return invocation.getPortletWindow();
145: }
146: return null;
147: }
148: }
|