1
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
64 public class PortletConfigImpl implements PortletConfig {
65
66 public static final String RUNTIME_OPTION_ESCAPE_XML =
67 "javax.portlet.escapeXml";
68
69 public PortletConfigImpl(Portlet portlet, PortletContext portletContext) {
70 _portletApp = portlet.getPortletApp();
71 _portlet = portlet;
72 _portletName = portlet.getRootPortletId();
73
74 int pos = _portletName.indexOf(PortletConstants.WAR_SEPARATOR);
75
76 if (pos != -1) {
77 _portletName = _portletName.substring(0, pos);
78 }
79
80 _portletContext = portletContext;
81 _bundlePool = new HashMap<String, ResourceBundle>();
82 }
83
84 public Map<String, String[]> getContainerRuntimeOptions() {
85 return _portletApp.getContainerRuntimeOptions();
86 }
87
88 public String getDefaultNamespace() {
89 return _portletApp.getDefaultNamespace();
90 }
91
92 public String getInitParameter(String name) {
93 if (name == null) {
94 throw new IllegalArgumentException();
95 }
96
97 return _portlet.getInitParams().get(name);
98 }
99
100 public Enumeration<String> getInitParameterNames() {
101 return Collections.enumeration(_portlet.getInitParams().keySet());
102 }
103
104 public PortletContext getPortletContext() {
105 return _portletContext;
106 }
107
108 public String getPortletId() {
109 return _portlet.getPortletId();
110 }
111
112 public String getPortletName() {
113 return _portletName;
114 }
115
116 public Enumeration<QName> getProcessingEventQNames() {
117 return Collections.enumeration(
118 toJavaxQNames(_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(
136 toJavaxQNames(_portlet.getPublishingEvents()));
137 }
138
139 public ResourceBundle getResourceBundle(Locale locale) {
140 String resourceBundleClassName = _portlet.getResourceBundle();
141
142 if (Validator.isNull(resourceBundleClassName)) {
143 String poolId = _portlet.getPortletId();
144
145 ResourceBundle bundle = _bundlePool.get(poolId);
146
147 if (bundle == null) {
148 StringBuilder sb = new StringBuilder();
149
150 try {
151 PortletInfo portletInfo = _portlet.getPortletInfo();
152
153 sb.append(JavaConstants.JAVAX_PORTLET_TITLE);
154 sb.append("=");
155 sb.append(portletInfo.getTitle());
156 sb.append("\n");
157
158 sb.append(JavaConstants.JAVAX_PORTLET_SHORT_TITLE);
159 sb.append("=");
160 sb.append(portletInfo.getShortTitle());
161 sb.append("\n");
162
163 sb.append(JavaConstants.JAVAX_PORTLET_KEYWORDS);
164 sb.append("=");
165 sb.append(portletInfo.getKeywords());
166 sb.append("\n");
167
168 bundle = new PropertyResourceBundle(
169 new ByteArrayInputStream(sb.toString().getBytes()));
170 }
171 catch (Exception e) {
172 _log.error(e, e);
173 }
174
175 _bundlePool.put(poolId, bundle);
176 }
177
178 return bundle;
179 }
180 else {
181 String poolId = _portlet.getPortletId() + "." + locale.toString();
182
183 ResourceBundle bundle = _bundlePool.get(poolId);
184
185 if (bundle == null) {
186 if (!_portletApp.isWARFile() &&
187 resourceBundleClassName.equals(
188 StrutsResourceBundle.class.getName())) {
189
190 long companyId = PortalInstances.getDefaultCompanyId();
191
192 bundle = StrutsResourceBundle.getBundle(
193 _portletName, companyId, locale);
194 }
195 else {
196 PortletBag portletBag = PortletBagPool.get(
197 _portlet.getRootPortletId());
198
199 bundle = portletBag.getResourceBundle(locale);
200 }
201
202 bundle = new PortletResourceBundle(
203 bundle, _portlet.getPortletInfo());
204
205 _bundlePool.put(poolId, bundle);
206 }
207
208 return bundle;
209 }
210 }
211
212 public Enumeration<Locale> getSupportedLocales() {
213 List<Locale> supportedLocales = new ArrayList<Locale>();
214
215 for (String languageId : _portlet.getSupportedLocales()) {
216 supportedLocales.add(LocaleUtil.fromLanguageId(languageId));
217 }
218
219 return Collections.enumeration(supportedLocales);
220 }
221
222 public boolean isWARFile() {
223 return _portletApp.isWARFile();
224 }
225
226 protected Set<javax.xml.namespace.QName> toJavaxQNames(
227 Set<com.liferay.portal.kernel.xml.QName> liferayQNames) {
228
229 Set<QName> javaxQNames = new HashSet<QName>(liferayQNames.size());
230
231 for (com.liferay.portal.kernel.xml.QName liferayQName :
232 liferayQNames) {
233
234 QName javaxQName = new QName(
235 liferayQName.getNamespaceURI(), liferayQName.getLocalPart(),
236 liferayQName.getNamespacePrefix());
237
238 javaxQNames.add(javaxQName);
239 }
240
241 return javaxQNames;
242 }
243
244 private static Log _log = LogFactoryUtil.getLog(PortletConfigImpl.class);
245
246 private PortletApp _portletApp;
247 private Portlet _portlet;
248 private String _portletName;
249 private PortletContext _portletContext;
250 private Map<String, ResourceBundle> _bundlePool;
251
252 }