001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet;
022:
023: import com.liferay.portal.kernel.util.JavaConstants;
024: import com.liferay.portal.kernel.util.StringMaker;
025: import com.liferay.portal.model.PortletInfo;
026: import com.liferay.portal.model.impl.PortletImpl;
027: import com.liferay.portal.servlet.PortletContextPool;
028: import com.liferay.portal.servlet.PortletContextWrapper;
029: import com.liferay.portal.util.PortalInstances;
030: import com.liferay.util.CollectionFactory;
031:
032: import java.io.ByteArrayInputStream;
033:
034: import java.util.Collections;
035: import java.util.Enumeration;
036: import java.util.Locale;
037: import java.util.Map;
038: import java.util.PropertyResourceBundle;
039: import java.util.ResourceBundle;
040:
041: import javax.portlet.PortletConfig;
042: import javax.portlet.PortletContext;
043:
044: /**
045: * <a href="PortletConfigImpl.java.html"><b><i>View Source</i></b></a>
046: *
047: * @author Brian Wing Shun Chan
048: *
049: */
050: public class PortletConfigImpl implements PortletConfig {
051:
052: public PortletConfigImpl(String portletName,
053: PortletContext portletCtx, Map params,
054: String resourceBundle, PortletInfo portletInfo) {
055:
056: _rootPortletId = PortletImpl.getRootPortletId(portletName);
057: _portletId = portletName;
058: _portletName = _rootPortletId;
059:
060: int pos = _portletName.indexOf(PortletImpl.WAR_SEPARATOR);
061:
062: if (pos != -1) {
063: _portletName = _portletName.substring(0, pos);
064:
065: _warFile = true;
066: }
067:
068: _portletCtx = portletCtx;
069: _params = params;
070: _resourceBundle = resourceBundle;
071: _portletInfo = portletInfo;
072: _bundlePool = CollectionFactory.getHashMap();
073: }
074:
075: public String getPortletId() {
076: return _portletId;
077: }
078:
079: public String getPortletName() {
080: return _portletName;
081: }
082:
083: public boolean isWARFile() {
084: return _warFile;
085: }
086:
087: public PortletContext getPortletContext() {
088: return _portletCtx;
089: }
090:
091: public ResourceBundle getResourceBundle(Locale locale) {
092: if (_resourceBundle == null) {
093: String poolId = _portletId;
094:
095: ResourceBundle bundle = (ResourceBundle) _bundlePool
096: .get(poolId);
097:
098: if (bundle == null) {
099: StringMaker sm = new StringMaker();
100:
101: try {
102: sm.append(JavaConstants.JAVAX_PORTLET_TITLE);
103: sm.append("=");
104: sm.append(_portletInfo.getTitle());
105: sm.append("\n");
106:
107: sm.append(JavaConstants.JAVAX_PORTLET_SHORT_TITLE);
108: sm.append("=");
109: sm.append(_portletInfo.getShortTitle());
110: sm.append("\n");
111:
112: sm.append(JavaConstants.JAVAX_PORTLET_KEYWORDS);
113: sm.append("=");
114: sm.append(_portletInfo.getKeywords());
115: sm.append("\n");
116:
117: bundle = new PropertyResourceBundle(
118: new ByteArrayInputStream(sm.toString()
119: .getBytes()));
120: } catch (Exception e) {
121: e.printStackTrace();
122: }
123:
124: _bundlePool.put(poolId, bundle);
125: }
126:
127: return bundle;
128: } else {
129: String poolId = _portletId + "." + locale.toString();
130:
131: ResourceBundle bundle = (ResourceBundle) _bundlePool
132: .get(poolId);
133:
134: if (bundle == null) {
135: if (!_warFile
136: && _resourceBundle
137: .equals(StrutsResourceBundle.class
138: .getName())) {
139:
140: long companyId = PortalInstances
141: .getDefaultCompanyId();
142:
143: bundle = StrutsResourceBundle.getBundle(
144: _portletName, companyId, locale);
145: } else {
146: PortletContextWrapper pcw = PortletContextPool
147: .get(_rootPortletId);
148:
149: bundle = pcw.getResourceBundle(locale);
150: }
151:
152: bundle = new PortletResourceBundle(bundle, _portletInfo);
153:
154: _bundlePool.put(poolId, bundle);
155: }
156:
157: return bundle;
158: }
159: }
160:
161: public String getInitParameter(String name) {
162: if (name == null) {
163: throw new IllegalArgumentException();
164: }
165:
166: return (String) _params.get(name);
167: }
168:
169: public Enumeration getInitParameterNames() {
170: return Collections.enumeration(_params.keySet());
171: }
172:
173: private String _rootPortletId;
174: private String _portletId;
175: private String _portletName;
176: private boolean _warFile;
177: private PortletContext _portletCtx;
178: private Map _params;
179: private String _resourceBundle;
180: private PortletInfo _portletInfo;
181: private Map _bundlePool;
182:
183: }
|