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  
38  import java.io.ByteArrayInputStream;
39  
40  import java.util.ArrayList;
41  import java.util.Collections;
42  import java.util.Enumeration;
43  import java.util.HashMap;
44  import java.util.HashSet;
45  import java.util.List;
46  import java.util.Locale;
47  import java.util.Map;
48  import java.util.PropertyResourceBundle;
49  import java.util.ResourceBundle;
50  import java.util.Set;
51  
52  import javax.portlet.PortletConfig;
53  import javax.portlet.PortletContext;
54  
55  import javax.xml.namespace.QName;
56  
57  /**
58   * <a href="PortletConfigImpl.java.html"><b><i>View Source</i></b></a>
59   *
60   * @author Brian Wing Shun Chan
61   * @author Eduardo Lundgren
62   */
63  public class PortletConfigImpl implements PortletConfig {
64  
65      public static final String RUNTIME_OPTION_ESCAPE_XML =
66          "javax.portlet.escapeXml";
67  
68      public static final String RUNTIME_OPTION_PORTAL_CONTEXT =
69          "com.liferay.portal.portalContext";
70  
71      public PortletConfigImpl(Portlet portlet, PortletContext portletContext) {
72          _portletApp = portlet.getPortletApp();
73          _portlet = portlet;
74          _portletName = portlet.getRootPortletId();
75  
76          int pos = _portletName.indexOf(PortletConstants.WAR_SEPARATOR);
77  
78          if (pos != -1) {
79              _portletName = _portletName.substring(0, pos);
80          }
81  
82          _portletContext = portletContext;
83          _bundlePool = new HashMap<String, ResourceBundle>();
84      }
85  
86      public Map<String, String[]> getContainerRuntimeOptions() {
87          return _portletApp.getContainerRuntimeOptions();
88      }
89  
90      public String getDefaultNamespace() {
91          return _portletApp.getDefaultNamespace();
92      }
93  
94      public String getInitParameter(String name) {
95          if (name == null) {
96              throw new IllegalArgumentException();
97          }
98  
99          return _portlet.getInitParams().get(name);
100     }
101 
102     public Enumeration<String> getInitParameterNames() {
103         return Collections.enumeration(_portlet.getInitParams().keySet());
104     }
105 
106     public PortletContext getPortletContext() {
107         return _portletContext;
108     }
109 
110     public String getPortletId() {
111         return _portlet.getPortletId();
112     }
113 
114     public String getPortletName() {
115         return _portletName;
116     }
117 
118     public Enumeration<QName> getProcessingEventQNames() {
119         return Collections.enumeration(
120             toJavaxQNames(_portlet.getProcessingEvents()));
121     }
122 
123     public Enumeration<String> getPublicRenderParameterNames() {
124         List<String> publicRenderParameterNames = new ArrayList<String>();
125 
126         for (PublicRenderParameter publicRenderParameter :
127                 _portlet.getPublicRenderParameters()) {
128 
129             publicRenderParameterNames.add(
130                 publicRenderParameter.getIdentifier());
131         }
132 
133         return Collections.enumeration(publicRenderParameterNames);
134     }
135 
136     public Enumeration<QName> getPublishingEventQNames() {
137         return Collections.enumeration(
138             toJavaxQNames(_portlet.getPublishingEvents()));
139     }
140 
141     public ResourceBundle getResourceBundle(Locale locale) {
142         String resourceBundleClassName = _portlet.getResourceBundle();
143 
144         if (Validator.isNull(resourceBundleClassName)) {
145             String poolId = _portlet.getPortletId();
146 
147             ResourceBundle bundle = _bundlePool.get(poolId);
148 
149             if (bundle == null) {
150                 StringBuilder sb = new StringBuilder();
151 
152                 try {
153                     PortletInfo portletInfo = _portlet.getPortletInfo();
154 
155                     sb.append(JavaConstants.JAVAX_PORTLET_TITLE);
156                     sb.append("=");
157                     sb.append(portletInfo.getTitle());
158                     sb.append("\n");
159 
160                     sb.append(JavaConstants.JAVAX_PORTLET_SHORT_TITLE);
161                     sb.append("=");
162                     sb.append(portletInfo.getShortTitle());
163                     sb.append("\n");
164 
165                     sb.append(JavaConstants.JAVAX_PORTLET_KEYWORDS);
166                     sb.append("=");
167                     sb.append(portletInfo.getKeywords());
168                     sb.append("\n");
169 
170                     sb.append(JavaConstants.JAVAX_PORTLET_DESCRIPTION);
171                     sb.append("=");
172                     sb.append(portletInfo.getDescription());
173                     sb.append("\n");
174 
175                     bundle = new PropertyResourceBundle(
176                         new ByteArrayInputStream(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 }