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.List;
46  import java.util.Locale;
47  import java.util.Map;
48  import java.util.PropertyResourceBundle;
49  import java.util.ResourceBundle;
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(_portlet.getProcessingEvents());
119     }
120 
121     public Enumeration<String> getPublicRenderParameterNames() {
122         List<String> publicRenderParameterNames = new ArrayList<String>();
123 
124         for (PublicRenderParameter publicRenderParameter :
125                 _portlet.getPublicRenderParameters()) {
126 
127             publicRenderParameterNames.add(
128                 publicRenderParameter.getIdentifier());
129         }
130 
131         return Collections.enumeration(publicRenderParameterNames);
132     }
133 
134     public Enumeration<QName> getPublishingEventQNames() {
135         return Collections.enumeration(_portlet.getPublishingEvents());
136     }
137 
138     public ResourceBundle getResourceBundle(Locale locale) {
139         String resourceBundleClassName = _portlet.getResourceBundle();
140 
141         if (Validator.isNull(resourceBundleClassName)) {
142             String poolId = _portlet.getPortletId();
143 
144             ResourceBundle bundle = _bundlePool.get(poolId);
145 
146             if (bundle == null) {
147                 StringBuilder sb = new StringBuilder();
148 
149                 try {
150                     PortletInfo portletInfo = _portlet.getPortletInfo();
151 
152                     sb.append(JavaConstants.JAVAX_PORTLET_TITLE);
153                     sb.append("=");
154                     sb.append(portletInfo.getTitle());
155                     sb.append("\n");
156 
157                     sb.append(JavaConstants.JAVAX_PORTLET_SHORT_TITLE);
158                     sb.append("=");
159                     sb.append(portletInfo.getShortTitle());
160                     sb.append("\n");
161 
162                     sb.append(JavaConstants.JAVAX_PORTLET_KEYWORDS);
163                     sb.append("=");
164                     sb.append(portletInfo.getKeywords());
165                     sb.append("\n");
166 
167                     sb.append(JavaConstants.JAVAX_PORTLET_DESCRIPTION);
168                     sb.append("=");
169                     sb.append(portletInfo.getDescription());
170                     sb.append("\n");
171 
172                     bundle = new PropertyResourceBundle(
173                         new ByteArrayInputStream(sb.toString().getBytes()));
174                 }
175                 catch (Exception e) {
176                     _log.error(e, e);
177                 }
178 
179                 _bundlePool.put(poolId, bundle);
180             }
181 
182             return bundle;
183         }
184         else {
185             String poolId = _portlet.getPortletId() + "." + locale.toString();
186 
187             ResourceBundle bundle = _bundlePool.get(poolId);
188 
189             if (bundle == null) {
190                 if (!_portletApp.isWARFile() &&
191                     resourceBundleClassName.equals(
192                         StrutsResourceBundle.class.getName())) {
193 
194                     long companyId = PortalInstances.getDefaultCompanyId();
195 
196                     bundle = StrutsResourceBundle.getBundle(
197                         _portletName, companyId, 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     private static Log _log = LogFactoryUtil.getLog(PortletConfigImpl.class);
231 
232     private PortletApp _portletApp;
233     private Portlet _portlet;
234     private String _portletName;
235     private PortletContext _portletContext;
236     private Map<String, ResourceBundle> _bundlePool;
237 
238 }