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.io.unsync.UnsyncByteArrayInputStream;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.portlet.PortletBag;
29  import com.liferay.portal.kernel.portlet.PortletBagPool;
30  import com.liferay.portal.kernel.util.JavaConstants;
31  import com.liferay.portal.kernel.util.LocaleUtil;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portal.model.Portlet;
34  import com.liferay.portal.model.PortletApp;
35  import com.liferay.portal.model.PortletConstants;
36  import com.liferay.portal.model.PortletInfo;
37  import com.liferay.portal.model.PublicRenderParameter;
38  
39  import java.util.ArrayList;
40  import java.util.Collections;
41  import java.util.Enumeration;
42  import java.util.HashMap;
43  import java.util.HashSet;
44  import java.util.List;
45  import java.util.Locale;
46  import java.util.Map;
47  import java.util.PropertyResourceBundle;
48  import java.util.ResourceBundle;
49  import java.util.Set;
50  
51  import javax.portlet.PortletConfig;
52  import javax.portlet.PortletContext;
53  
54  import javax.xml.namespace.QName;
55  
56  /**
57   * <a href="PortletConfigImpl.java.html"><b><i>View Source</i></b></a>
58   *
59   * @author Brian Wing Shun Chan
60   * @author Eduardo Lundgren
61   */
62  public class PortletConfigImpl implements PortletConfig {
63  
64      public static final String RUNTIME_OPTION_ESCAPE_XML =
65          "javax.portlet.escapeXml";
66  
67      public static final String RUNTIME_OPTION_PORTAL_CONTEXT =
68          "com.liferay.portal.portalContext";
69  
70      public PortletConfigImpl(Portlet portlet, PortletContext portletContext) {
71          _portletApp = portlet.getPortletApp();
72          _portlet = portlet;
73          _portletName = portlet.getRootPortletId();
74  
75          int pos = _portletName.indexOf(PortletConstants.WAR_SEPARATOR);
76  
77          if (pos != -1) {
78              _portletName = _portletName.substring(0, pos);
79          }
80  
81          _portletContext = portletContext;
82          _bundlePool = new HashMap<String, ResourceBundle>();
83      }
84  
85      public Map<String, String[]> getContainerRuntimeOptions() {
86          return _portletApp.getContainerRuntimeOptions();
87      }
88  
89      public String getDefaultNamespace() {
90          return _portletApp.getDefaultNamespace();
91      }
92  
93      public String getInitParameter(String name) {
94          if (name == null) {
95              throw new IllegalArgumentException();
96          }
97  
98          return _portlet.getInitParams().get(name);
99      }
100 
101     public Enumeration<String> getInitParameterNames() {
102         return Collections.enumeration(_portlet.getInitParams().keySet());
103     }
104 
105     public PortletContext getPortletContext() {
106         return _portletContext;
107     }
108 
109     public String getPortletId() {
110         return _portlet.getPortletId();
111     }
112 
113     public String getPortletName() {
114         return _portletName;
115     }
116 
117     public Enumeration<QName> getProcessingEventQNames() {
118         return Collections.enumeration(
119             toJavaxQNames(_portlet.getProcessingEvents()));
120     }
121 
122     public Enumeration<String> getPublicRenderParameterNames() {
123         List<String> publicRenderParameterNames = new ArrayList<String>();
124 
125         for (PublicRenderParameter publicRenderParameter :
126                 _portlet.getPublicRenderParameters()) {
127 
128             publicRenderParameterNames.add(
129                 publicRenderParameter.getIdentifier());
130         }
131 
132         return Collections.enumeration(publicRenderParameterNames);
133     }
134 
135     public Enumeration<QName> getPublishingEventQNames() {
136         return Collections.enumeration(
137             toJavaxQNames(_portlet.getPublishingEvents()));
138     }
139 
140     public ResourceBundle getResourceBundle(Locale locale) {
141         String resourceBundleClassName = _portlet.getResourceBundle();
142 
143         if (Validator.isNull(resourceBundleClassName)) {
144             String poolId = _portlet.getPortletId();
145 
146             ResourceBundle bundle = _bundlePool.get(poolId);
147 
148             if (bundle == null) {
149                 StringBuilder sb = new StringBuilder();
150 
151                 try {
152                     PortletInfo portletInfo = _portlet.getPortletInfo();
153 
154                     sb.append(JavaConstants.JAVAX_PORTLET_TITLE);
155                     sb.append("=");
156                     sb.append(portletInfo.getTitle());
157                     sb.append("\n");
158 
159                     sb.append(JavaConstants.JAVAX_PORTLET_SHORT_TITLE);
160                     sb.append("=");
161                     sb.append(portletInfo.getShortTitle());
162                     sb.append("\n");
163 
164                     sb.append(JavaConstants.JAVAX_PORTLET_KEYWORDS);
165                     sb.append("=");
166                     sb.append(portletInfo.getKeywords());
167                     sb.append("\n");
168 
169                     sb.append(JavaConstants.JAVAX_PORTLET_DESCRIPTION);
170                     sb.append("=");
171                     sb.append(portletInfo.getDescription());
172                     sb.append("\n");
173 
174                     bundle = new PropertyResourceBundle(
175                         new UnsyncByteArrayInputStream(
176                             sb.toString().getBytes()));
177                 }
178                 catch (Exception e) {
179                     _log.error(e, e);
180                 }
181 
182                 _bundlePool.put(poolId, bundle);
183             }
184 
185             return bundle;
186         }
187         else {
188             String poolId = _portlet.getPortletId() + "." + locale.toString();
189 
190             ResourceBundle bundle = _bundlePool.get(poolId);
191 
192             if (bundle == null) {
193                 if (!_portletApp.isWARFile() &&
194                     resourceBundleClassName.equals(
195                         StrutsResourceBundle.class.getName())) {
196 
197                     bundle = new StrutsResourceBundle(_portletName, locale);
198                 }
199                 else {
200                     PortletBag portletBag = PortletBagPool.get(
201                         _portlet.getRootPortletId());
202 
203                     bundle = portletBag.getResourceBundle(locale);
204                 }
205 
206                 bundle = new PortletResourceBundle(
207                     bundle, _portlet.getPortletInfo());
208 
209                 _bundlePool.put(poolId, bundle);
210             }
211 
212             return bundle;
213         }
214     }
215 
216     public Enumeration<Locale> getSupportedLocales() {
217         List<Locale> supportedLocales = new ArrayList<Locale>();
218 
219         for (String languageId : _portlet.getSupportedLocales()) {
220             supportedLocales.add(LocaleUtil.fromLanguageId(languageId));
221         }
222 
223         return Collections.enumeration(supportedLocales);
224     }
225 
226     public boolean isWARFile() {
227         return _portletApp.isWARFile();
228     }
229 
230     protected Set<javax.xml.namespace.QName> toJavaxQNames(
231         Set<com.liferay.portal.kernel.xml.QName> liferayQNames) {
232 
233         Set<QName> javaxQNames = new HashSet<QName>(liferayQNames.size());
234 
235         for (com.liferay.portal.kernel.xml.QName liferayQName :
236                 liferayQNames) {
237 
238             QName javaxQName = new QName(
239                 liferayQName.getNamespaceURI(), liferayQName.getLocalPart(),
240                 liferayQName.getNamespacePrefix());
241 
242             javaxQNames.add(javaxQName);
243         }
244 
245         return javaxQNames;
246     }
247 
248     private static Log _log = LogFactoryUtil.getLog(PortletConfigImpl.class);
249 
250     private PortletApp _portletApp;
251     private Portlet _portlet;
252     private String _portletName;
253     private PortletContext _portletContext;
254     private Map<String, ResourceBundle> _bundlePool;
255 
256 }