1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.portlet.PortletBag;
28  import com.liferay.portal.kernel.portlet.PortletBagPool;
29  import com.liferay.portal.kernel.util.JavaConstants;
30  import com.liferay.portal.kernel.util.LocaleUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.model.Portlet;
33  import com.liferay.portal.model.PortletApp;
34  import com.liferay.portal.model.PortletConstants;
35  import com.liferay.portal.model.PortletInfo;
36  import com.liferay.portal.model.PublicRenderParameter;
37  import com.liferay.portal.util.PortalInstances;
38  
39  import java.io.ByteArrayInputStream;
40  
41  import java.util.ArrayList;
42  import java.util.Collections;
43  import java.util.Enumeration;
44  import java.util.HashMap;
45  import java.util.HashSet;
46  import java.util.List;
47  import java.util.Locale;
48  import java.util.Map;
49  import java.util.PropertyResourceBundle;
50  import java.util.ResourceBundle;
51  import java.util.Set;
52  
53  import javax.portlet.PortletConfig;
54  import javax.portlet.PortletContext;
55  
56  import javax.xml.namespace.QName;
57  
58  /**
59   * <a href="PortletConfigImpl.java.html"><b><i>View Source</i></b></a>
60   *
61   * @author Brian Wing Shun Chan
62   *
63   */
64  public class PortletConfigImpl implements PortletConfig {
65  
66      public static final String RUNTIME_OPTION_ESCAPE_XML =
67          "javax.portlet.escapeXml";
68  
69      public static final String RUNTIME_OPTION_PORTAL_CONTEXT =
70          "com.liferay.portal.portalContext";
71  
72      public PortletConfigImpl(Portlet portlet, PortletContext portletContext) {
73          _portletApp = portlet.getPortletApp();
74          _portlet = portlet;
75          _portletName = portlet.getRootPortletId();
76  
77          int pos = _portletName.indexOf(PortletConstants.WAR_SEPARATOR);
78  
79          if (pos != -1) {
80              _portletName = _portletName.substring(0, pos);
81          }
82  
83          _portletContext = portletContext;
84          _bundlePool = new HashMap<String, ResourceBundle>();
85      }
86  
87      public Map<String, String[]> getContainerRuntimeOptions() {
88          return _portletApp.getContainerRuntimeOptions();
89      }
90  
91      public String getDefaultNamespace() {
92          return _portletApp.getDefaultNamespace();
93      }
94  
95      public String getInitParameter(String name) {
96          if (name == null) {
97              throw new IllegalArgumentException();
98          }
99  
100         return _portlet.getInitParams().get(name);
101     }
102 
103     public Enumeration<String> getInitParameterNames() {
104         return Collections.enumeration(_portlet.getInitParams().keySet());
105     }
106 
107     public PortletContext getPortletContext() {
108         return _portletContext;
109     }
110 
111     public String getPortletId() {
112         return _portlet.getPortletId();
113     }
114 
115     public String getPortletName() {
116         return _portletName;
117     }
118 
119     public Enumeration<QName> getProcessingEventQNames() {
120         return Collections.enumeration(
121             toJavaxQNames(_portlet.getProcessingEvents()));
122     }
123 
124     public Enumeration<String> getPublicRenderParameterNames() {
125         List<String> publicRenderParameterNames = new ArrayList<String>();
126 
127         for (PublicRenderParameter publicRenderParameter :
128                 _portlet.getPublicRenderParameters()) {
129 
130             publicRenderParameterNames.add(
131                 publicRenderParameter.getIdentifier());
132         }
133 
134         return Collections.enumeration(publicRenderParameterNames);
135     }
136 
137     public Enumeration<QName> getPublishingEventQNames() {
138         return Collections.enumeration(
139             toJavaxQNames(_portlet.getPublishingEvents()));
140     }
141 
142     public ResourceBundle getResourceBundle(Locale locale) {
143         String resourceBundleClassName = _portlet.getResourceBundle();
144 
145         if (Validator.isNull(resourceBundleClassName)) {
146             String poolId = _portlet.getPortletId();
147 
148             ResourceBundle bundle = _bundlePool.get(poolId);
149 
150             if (bundle == null) {
151                 StringBuilder sb = new StringBuilder();
152 
153                 try {
154                     PortletInfo portletInfo = _portlet.getPortletInfo();
155 
156                     sb.append(JavaConstants.JAVAX_PORTLET_TITLE);
157                     sb.append("=");
158                     sb.append(portletInfo.getTitle());
159                     sb.append("\n");
160 
161                     sb.append(JavaConstants.JAVAX_PORTLET_SHORT_TITLE);
162                     sb.append("=");
163                     sb.append(portletInfo.getShortTitle());
164                     sb.append("\n");
165 
166                     sb.append(JavaConstants.JAVAX_PORTLET_KEYWORDS);
167                     sb.append("=");
168                     sb.append(portletInfo.getKeywords());
169                     sb.append("\n");
170 
171                     bundle = new PropertyResourceBundle(
172                         new ByteArrayInputStream(sb.toString().getBytes()));
173                 }
174                 catch (Exception e) {
175                     _log.error(e, e);
176                 }
177 
178                 _bundlePool.put(poolId, bundle);
179             }
180 
181             return bundle;
182         }
183         else {
184             String poolId = _portlet.getPortletId() + "." + locale.toString();
185 
186             ResourceBundle bundle = _bundlePool.get(poolId);
187 
188             if (bundle == null) {
189                 if (!_portletApp.isWARFile() &&
190                     resourceBundleClassName.equals(
191                         StrutsResourceBundle.class.getName())) {
192 
193                     long companyId = PortalInstances.getDefaultCompanyId();
194 
195                     bundle = StrutsResourceBundle.getBundle(
196                         _portletName, companyId, locale);
197                 }
198                 else {
199                     PortletBag portletBag = PortletBagPool.get(
200                         _portlet.getRootPortletId());
201 
202                     bundle = portletBag.getResourceBundle(locale);
203                 }
204 
205                 bundle = new PortletResourceBundle(
206                     bundle, _portlet.getPortletInfo());
207 
208                 _bundlePool.put(poolId, bundle);
209             }
210 
211             return bundle;
212         }
213     }
214 
215     public Enumeration<Locale> getSupportedLocales() {
216         List<Locale> supportedLocales = new ArrayList<Locale>();
217 
218         for (String languageId : _portlet.getSupportedLocales()) {
219             supportedLocales.add(LocaleUtil.fromLanguageId(languageId));
220         }
221 
222         return Collections.enumeration(supportedLocales);
223     }
224 
225     public boolean isWARFile() {
226         return _portletApp.isWARFile();
227     }
228 
229     protected Set<javax.xml.namespace.QName> toJavaxQNames(
230         Set<com.liferay.portal.kernel.xml.QName> liferayQNames) {
231 
232         Set<QName> javaxQNames = new HashSet<QName>(liferayQNames.size());
233 
234         for (com.liferay.portal.kernel.xml.QName liferayQName :
235                 liferayQNames) {
236 
237             QName javaxQName = new QName(
238                 liferayQName.getNamespaceURI(), liferayQName.getLocalPart(),
239                 liferayQName.getNamespacePrefix());
240 
241             javaxQNames.add(javaxQName);
242         }
243 
244         return javaxQNames;
245     }
246 
247     private static Log _log = LogFactoryUtil.getLog(PortletConfigImpl.class);
248 
249     private PortletApp _portletApp;
250     private Portlet _portlet;
251     private String _portletName;
252     private PortletContext _portletContext;
253     private Map<String, ResourceBundle> _bundlePool;
254 
255 }