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