1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.portal.service.impl;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.image.SpriteProcessorUtil;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.plugin.PluginPackage;
30  import com.liferay.portal.kernel.portlet.FriendlyURLMapper;
31  import com.liferay.portal.kernel.servlet.ServletContextUtil;
32  import com.liferay.portal.kernel.util.ContentTypes;
33  import com.liferay.portal.kernel.util.GetterUtil;
34  import com.liferay.portal.kernel.util.ListUtil;
35  import com.liferay.portal.kernel.util.ServerDetector;
36  import com.liferay.portal.kernel.util.StringPool;
37  import com.liferay.portal.kernel.util.Validator;
38  import com.liferay.portal.kernel.xml.Document;
39  import com.liferay.portal.kernel.xml.Element;
40  import com.liferay.portal.kernel.xml.QName;
41  import com.liferay.portal.kernel.xml.SAXReaderUtil;
42  import com.liferay.portal.model.CompanyConstants;
43  import com.liferay.portal.model.EventDefinition;
44  import com.liferay.portal.model.Portlet;
45  import com.liferay.portal.model.PortletApp;
46  import com.liferay.portal.model.PortletCategory;
47  import com.liferay.portal.model.PortletConstants;
48  import com.liferay.portal.model.PortletFilter;
49  import com.liferay.portal.model.PortletInfo;
50  import com.liferay.portal.model.PortletURLListener;
51  import com.liferay.portal.model.PublicRenderParameter;
52  import com.liferay.portal.model.impl.EventDefinitionImpl;
53  import com.liferay.portal.model.impl.PortletAppImpl;
54  import com.liferay.portal.model.impl.PortletFilterImpl;
55  import com.liferay.portal.model.impl.PortletImpl;
56  import com.liferay.portal.model.impl.PortletURLListenerImpl;
57  import com.liferay.portal.model.impl.PublicRenderParameterImpl;
58  import com.liferay.portal.service.base.PortletLocalServiceBaseImpl;
59  import com.liferay.portal.util.ContentUtil;
60  import com.liferay.portal.util.PortalInstances;
61  import com.liferay.portal.util.PortalUtil;
62  import com.liferay.portal.util.PortletKeys;
63  import com.liferay.portal.util.PropsValues;
64  import com.liferay.portal.util.WebAppPool;
65  import com.liferay.portal.util.WebKeys;
66  import com.liferay.portlet.PortletInstanceFactoryUtil;
67  import com.liferay.portlet.PortletPreferencesSerializer;
68  import com.liferay.portlet.PortletQNameUtil;
69  import com.liferay.util.bridges.jsp.JSPPortlet;
70  
71  import java.io.File;
72  
73  import java.util.ArrayList;
74  import java.util.HashMap;
75  import java.util.HashSet;
76  import java.util.Iterator;
77  import java.util.LinkedHashSet;
78  import java.util.List;
79  import java.util.Map;
80  import java.util.Properties;
81  import java.util.Set;
82  import java.util.concurrent.ConcurrentHashMap;
83  
84  import javax.portlet.PortletMode;
85  import javax.portlet.PreferencesValidator;
86  
87  import javax.servlet.ServletContext;
88  
89  /**
90   * <a href="PortletLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
91   *
92   * @author Brian Wing Shun Chan
93   * @author Raymond Augé
94   *
95   */
96  public class PortletLocalServiceImpl extends PortletLocalServiceBaseImpl {
97  
98      public Portlet deployRemotePortlet(Portlet portlet) {
99          PortletApp portletApp = _getPortletApp(StringPool.BLANK);
100 
101         portlet.setPortletApp(portletApp);
102 
103         Map<String, Portlet> portletsPool = _getPortletsPool();
104 
105         portletsPool.put(portlet.getPortletId(), portlet);
106 
107         _clearCaches();
108 
109         PortletCategory newPortletCategory = new PortletCategory();
110 
111         PortletCategory wsrpCategory = new PortletCategory(_WSRP_CATEGORY);
112 
113         newPortletCategory.addCategory(wsrpCategory);
114 
115         wsrpCategory.getPortletIds().add(portlet.getPortletId());
116 
117         long[] companyIds = PortalInstances.getCompanyIds();
118 
119         for (long companyId : companyIds) {
120             PortletCategory portletCategory = (PortletCategory)WebAppPool.get(
121                 String.valueOf(companyId), WebKeys.PORTLET_CATEGORY);
122 
123             if (portletCategory != null) {
124                 portletCategory.merge(newPortletCategory);
125             }
126             else {
127                 _log.error(
128                     "Unable to register remote portlet for company " +
129                         companyId + " because it does not exist");
130             }
131         }
132 
133         return portlet;
134     }
135 
136     public void destroyPortlet(Portlet portlet) {
137         Map<String, Portlet> portletsPool = _getPortletsPool();
138 
139         portletsPool.remove(portlet.getRootPortletId());
140 
141         PortletApp portletApp = portlet.getPortletApp();
142 
143         if (portletApp != null) {
144             _portletAppsPool.remove(portletApp.getServletContextName());
145         }
146 
147         _clearCaches();
148     }
149 
150     public PortletCategory getEARDisplay(String xml) throws SystemException {
151         try {
152             return _readLiferayDisplayXML(xml);
153         }
154         catch (Exception e) {
155             throw new SystemException(e);
156         }
157     }
158 
159     public PortletCategory getWARDisplay(String servletContextName, String xml)
160         throws SystemException {
161 
162         try {
163             return _readLiferayDisplayXML(servletContextName, xml);
164         }
165         catch (Exception e) {
166             throw new SystemException(e);
167         }
168     }
169 
170     public List<Portlet> getFriendlyURLMapperPortlets() {
171         List<Portlet> portlets = new ArrayList<Portlet>(
172             _friendlyURLMapperPortlets.size());
173 
174         Iterator<Map.Entry<String, Portlet>> itr =
175             _friendlyURLMapperPortlets.entrySet().iterator();
176 
177         while (itr.hasNext()) {
178             Map.Entry<String, Portlet> entry = itr.next();
179 
180             Portlet portlet = entry.getValue();
181 
182             FriendlyURLMapper friendlyURLMapper =
183                 portlet.getFriendlyURLMapperInstance();
184 
185             if (friendlyURLMapper != null) {
186                 portlets.add(portlet);
187             }
188         }
189 
190         return portlets;
191     }
192 
193     public List<FriendlyURLMapper> getFriendlyURLMappers() {
194         List<FriendlyURLMapper> friendlyURLMappers =
195             new ArrayList<FriendlyURLMapper>(_friendlyURLMapperPortlets.size());
196 
197         Iterator<Map.Entry<String, Portlet>> itr =
198             _friendlyURLMapperPortlets.entrySet().iterator();
199 
200         while (itr.hasNext()) {
201             Map.Entry<String, Portlet> entry = itr.next();
202 
203             Portlet portlet = entry.getValue();
204 
205             FriendlyURLMapper friendlyURLMapper =
206                 portlet.getFriendlyURLMapperInstance();
207 
208             if (friendlyURLMapper != null) {
209                 friendlyURLMappers.add(friendlyURLMapper);
210             }
211         }
212 
213         return friendlyURLMappers;
214     }
215 
216     public Portlet getPortletById(String portletId) {
217         Map<String, Portlet> portletsPool = _getPortletsPool();
218 
219         return portletsPool.get(portletId);
220     }
221 
222     public Portlet getPortletById(long companyId, String portletId)
223         throws SystemException {
224 
225         portletId = PortalUtil.getJsSafePortletId(portletId);
226 
227         Portlet portlet = null;
228 
229         Map<String, Portlet> companyPortletsPool = _getPortletsPool(companyId);
230 
231         String rootPortletId = PortletConstants.getRootPortletId(portletId);
232 
233         if (portletId.equals(rootPortletId)) {
234             portlet = companyPortletsPool.get(portletId);
235         }
236         else {
237             portlet = companyPortletsPool.get(rootPortletId);
238 
239             if (portlet != null) {
240                 portlet = portlet.getClonedInstance(portletId);
241             }
242         }
243 
244         if ((portlet == null) &&
245             (!portletId.equals(PortletKeys.LIFERAY_PORTAL))) {
246 
247             if (_portletsPool.isEmpty()) {
248                 if (_log.isDebugEnabled()) {
249                     _log.debug("No portlets are installed");
250                 }
251             }
252             else {
253                 if (_log.isInfoEnabled()) {
254                     _log.info(
255                         "Portlet not found for " + companyId + " " + portletId);
256                 }
257 
258                 portlet = new PortletImpl(CompanyConstants.SYSTEM, portletId);
259 
260                 portlet.setTimestamp(System.currentTimeMillis());
261 
262                 portlet.setPortletApp(_getPortletApp(StringPool.BLANK));
263 
264                 portlet.setPortletName(portletId);
265                 portlet.setDisplayName(portletId);
266                 portlet.setPortletClass(JSPPortlet.class.getName());
267 
268                 Map<String, String> initParams = portlet.getInitParams();
269 
270                 initParams.put(
271                     "view-jsp", "/html/portal/undeployed_portlet.jsp");
272 
273                 Set<String> mimeTypeModes = new HashSet<String>();
274 
275                 mimeTypeModes.add(PortletMode.VIEW.toString().toLowerCase());
276 
277                 portlet.getPortletModes().put(
278                     ContentTypes.TEXT_HTML, mimeTypeModes);
279 
280                 portlet.setPortletInfo(
281                     new PortletInfo(portletId, portletId, portletId));
282 
283                 if (portletId.indexOf("_INSTANCE_") != -1) {
284                     portlet.setInstanceable(true);
285                 }
286 
287                 portlet.setActive(true);
288                 portlet.setUndeployedPortlet(true);
289 
290                 companyPortletsPool.put(portletId, portlet);
291             }
292         }
293 
294         return portlet;
295     }
296 
297     public Portlet getPortletByStrutsPath(long companyId, String strutsPath)
298         throws SystemException {
299 
300         return getPortletById(companyId, _getPortletId(strutsPath));
301     }
302 
303     public List<Portlet> getPortlets() {
304         Map<String, Portlet> portletsPool = _getPortletsPool();
305 
306         return ListUtil.fromCollection(portletsPool.values());
307     }
308 
309     public List<Portlet> getPortlets(long companyId) throws SystemException {
310         return getPortlets(companyId, true, true);
311     }
312 
313     public List<Portlet> getPortlets(
314             long companyId, boolean showSystem, boolean showPortal)
315         throws SystemException {
316 
317         Map<String, Portlet> portletsPool = _getPortletsPool(companyId);
318 
319         List<Portlet> portlets = ListUtil.fromCollection(portletsPool.values());
320 
321         if (!showSystem || !showPortal) {
322             Iterator<Portlet> itr = portlets.iterator();
323 
324             while (itr.hasNext()) {
325                 Portlet portlet = itr.next();
326 
327                 if (showPortal &&
328                     portlet.getPortletId().equals(PortletKeys.PORTAL)) {
329 
330                 }
331                 else if (!showPortal &&
332                          portlet.getPortletId().equals(PortletKeys.PORTAL)) {
333 
334                     itr.remove();
335                 }
336                 else if (!showSystem && portlet.isSystem()) {
337                     itr.remove();
338                 }
339             }
340         }
341 
342         return portlets;
343     }
344 
345     public boolean hasPortlet(long companyId, String portletId)
346         throws SystemException {
347 
348         portletId = PortalUtil.getJsSafePortletId(portletId);
349 
350         Portlet portlet = null;
351 
352         Map<String, Portlet> companyPortletsPool = _getPortletsPool(companyId);
353 
354         String rootPortletId = PortletConstants.getRootPortletId(portletId);
355 
356         if (portletId.equals(rootPortletId)) {
357             portlet = companyPortletsPool.get(portletId);
358         }
359         else {
360             portlet = companyPortletsPool.get(rootPortletId);
361         }
362 
363         if (portlet == null) {
364             return false;
365         }
366         else {
367             return true;
368         }
369     }
370 
371     public void initEAR(
372         ServletContext servletContext, String[] xmls,
373         PluginPackage pluginPackage) {
374 
375         // Clear pools every time initEAR is called. See LEP-5452.
376 
377         _portletAppsPool.clear();
378         _portletsPool.clear();
379         _companyPortletsPool.clear();
380         _portletIdsByStrutsPath.clear();
381         _friendlyURLMapperPortlets.clear();
382 
383         Map<String, Portlet> portletsPool = _getPortletsPool();
384 
385         try {
386             List<String> servletURLPatterns = _readWebXML(xmls[4]);
387 
388             Set<String> portletIds = _readPortletXML(
389                 servletContext, xmls[0], portletsPool, servletURLPatterns,
390                 pluginPackage);
391 
392             portletIds.addAll(
393                 _readPortletXML(
394                     servletContext, xmls[1], portletsPool, servletURLPatterns,
395                     pluginPackage));
396 
397             Set<String> liferayPortletIds =
398                 _readLiferayPortletXML(xmls[2], portletsPool);
399 
400             liferayPortletIds.addAll(
401                 _readLiferayPortletXML(xmls[3], portletsPool));
402 
403             // Check for missing entries in liferay-portlet.xml
404 
405             Iterator<String> portletIdsItr = portletIds.iterator();
406 
407             while (portletIdsItr.hasNext()) {
408                 String portletId = portletIdsItr.next();
409 
410                 if (_log.isWarnEnabled() &&
411                     !liferayPortletIds.contains(portletId)) {
412 
413                     _log.warn(
414                         "Portlet with the name " + portletId +
415                             " is described in portlet.xml but does not " +
416                                 "have a matching entry in liferay-portlet.xml");
417                 }
418             }
419 
420             // Check for missing entries in portlet.xml
421 
422             Iterator<String> liferayPortletIdsItr =
423                 liferayPortletIds.iterator();
424 
425             while (liferayPortletIdsItr.hasNext()) {
426                 String portletId = liferayPortletIdsItr.next();
427 
428                 if (_log.isWarnEnabled() && !portletIds.contains(portletId)) {
429                     _log.warn(
430                         "Portlet with the name " + portletId +
431                             " is described in liferay-portlet.xml but does " +
432                                 "not have a matching entry in portlet.xml");
433                 }
434             }
435 
436             // Remove portlets that should not be included
437 
438             Iterator<Map.Entry<String, Portlet>> portletPoolsItr =
439                 portletsPool.entrySet().iterator();
440 
441             while (portletPoolsItr.hasNext()) {
442                 Map.Entry<String, Portlet> entry = portletPoolsItr.next();
443 
444                 Portlet portletModel = entry.getValue();
445 
446                 if (!portletModel.getPortletId().equals(PortletKeys.ADMIN) &&
447                     !portletModel.getPortletId().equals(
448                         PortletKeys.MY_ACCOUNT) &&
449                     !portletModel.isInclude()) {
450 
451                     portletPoolsItr.remove();
452                 }
453             }
454 
455             // Sprite images
456 
457             PortletApp portletApp = _getPortletApp(StringPool.BLANK);
458 
459             _setSpriteImages(servletContext, portletApp, "/html/icons/");
460         }
461         catch (Exception e) {
462             _log.error(e, e);
463         }
464     }
465 
466     public List<Portlet> initWAR(
467         String servletContextName, ServletContext servletContext, String[] xmls,
468         PluginPackage pluginPackage) {
469 
470         List<Portlet> portlets = new ArrayList<Portlet>();
471 
472         Map<String, Portlet> portletsPool = _getPortletsPool();
473 
474         try {
475             List<String> servletURLPatterns = _readWebXML(xmls[3]);
476 
477             Set<String> portletIds = _readPortletXML(
478                 servletContextName, servletContext, xmls[0], portletsPool,
479                 servletURLPatterns, pluginPackage);
480 
481             portletIds.addAll(
482                 _readPortletXML(
483                     servletContextName, servletContext, xmls[1], portletsPool,
484                     servletURLPatterns, pluginPackage));
485 
486             Set<String> liferayPortletIds = _readLiferayPortletXML(
487                 servletContextName, xmls[2], portletsPool);
488 
489             // Check for missing entries in liferay-portlet.xml
490 
491             Iterator<String> itr = portletIds.iterator();
492 
493             while (itr.hasNext()) {
494                 String portletId = itr.next();
495 
496                 if (_log.isWarnEnabled() &&
497                     !liferayPortletIds.contains(portletId)) {
498 
499                     _log.warn(
500                         "Portlet with the name " + portletId +
501                             " is described in portlet.xml but does not " +
502                                 "have a matching entry in liferay-portlet.xml");
503                 }
504             }
505 
506             // Check for missing entries in portlet.xml
507 
508             itr = liferayPortletIds.iterator();
509 
510             while (itr.hasNext()) {
511                 String portletId = itr.next();
512 
513                 if (_log.isWarnEnabled() && !portletIds.contains(portletId)) {
514                     _log.warn(
515                         "Portlet with the name " + portletId +
516                             " is described in liferay-portlet.xml but does " +
517                                 "not have a matching entry in portlet.xml");
518                 }
519             }
520 
521             // Return the new portlets
522 
523             itr = portletIds.iterator();
524 
525             while (itr.hasNext()) {
526                 String portletId = itr.next();
527 
528                 Portlet portlet = _getPortletsPool().get(portletId);
529 
530                 portlets.add(portlet);
531 
532                 PortletInstanceFactoryUtil.clear(portlet);
533             }
534 
535             // Sprite images
536 
537             PortletApp portletApp = _getPortletApp(servletContextName);
538 
539             _setSpriteImages(servletContext, portletApp, "/icons/");
540         }
541         catch (Exception e) {
542             _log.error(e, e);
543         }
544 
545         _clearCaches();
546 
547         return portlets;
548     }
549 
550     public Portlet newPortlet(long companyId, String portletId) {
551         return new PortletImpl(companyId, portletId);
552     }
553 
554     public Portlet updatePortlet(
555             long companyId, String portletId, String roles, boolean active)
556         throws SystemException {
557 
558         portletId = PortalUtil.getJsSafePortletId(portletId);
559 
560         Portlet portlet = portletPersistence.fetchByC_P(companyId, portletId);
561 
562         if (portlet == null) {
563             long id = counterLocalService.increment();
564 
565             portlet = portletPersistence.create(id);
566 
567             portlet.setCompanyId(companyId);
568             portlet.setPortletId(portletId);
569         }
570 
571         portlet.setRoles(roles);
572         portlet.setActive(active);
573 
574         portletPersistence.update(portlet, false);
575 
576         portlet = getPortletById(companyId, portletId);
577 
578         portlet.setRoles(roles);
579         portlet.setActive(active);
580 
581         return portlet;
582     }
583 
584     private void _clearCaches() {
585 
586         // Refresh security path to portlet id mapping for all portlets
587 
588         _portletIdsByStrutsPath.clear();
589 
590         // Refresh company portlets
591 
592         _companyPortletsPool.clear();
593     }
594 
595     private PortletApp _getPortletApp(String servletContextName) {
596         PortletApp portletApp = _portletAppsPool.get(servletContextName);
597 
598         if (portletApp == null) {
599             portletApp = new PortletAppImpl(servletContextName);
600 
601             _portletAppsPool.put(servletContextName, portletApp);
602         }
603 
604         return portletApp;
605     }
606 
607     private String _getPortletId(String securityPath) {
608         if (_portletIdsByStrutsPath.size() == 0) {
609             Iterator<Portlet> itr = _getPortletsPool().values().iterator();
610 
611             while (itr.hasNext()) {
612                 Portlet portlet = itr.next();
613 
614                 _portletIdsByStrutsPath.put(
615                     portlet.getStrutsPath(), portlet.getPortletId());
616             }
617         }
618 
619         String portletId = _portletIdsByStrutsPath.get(securityPath);
620 
621         if (Validator.isNull(portletId)) {
622             _log.error(
623                 "Struts path " + securityPath + " is not mapped to a portlet " +
624                     "in liferay-portlet.xml");
625         }
626 
627         return portletId;
628     }
629 
630     private List<Portlet> _getPortletsByPortletName(
631         String portletName, String servletContextName,
632         Map<String, Portlet> portletsPool) {
633 
634         List<Portlet> portlets = null;
635 
636         int pos = portletName.indexOf(StringPool.STAR);
637 
638         if (pos == -1) {
639             portlets = new ArrayList<Portlet>();
640 
641             String portletId = portletName;
642 
643             if (Validator.isNotNull(servletContextName)) {
644                 portletId =
645                     portletId + PortletConstants.WAR_SEPARATOR +
646                         servletContextName;
647             }
648 
649             portletId = PortalUtil.getJsSafePortletId(portletId);
650 
651             Portlet portlet = portletsPool.get(portletId);
652 
653             if (portlet != null) {
654                 portlets.add(portlet);
655             }
656 
657             return portlets;
658         }
659 
660         String portletNamePrefix = portletName.substring(0, pos);
661 
662         portlets = _getPortletsByServletContextName(
663             servletContextName, portletsPool);
664 
665         Iterator<Portlet> itr = portlets.iterator();
666 
667         while (itr.hasNext()) {
668             Portlet portlet = itr.next();
669 
670             if (!portlet.getPortletId().startsWith(portletNamePrefix)) {
671                 itr.remove();
672             }
673         }
674 
675         return portlets;
676     }
677 
678     private List<Portlet> _getPortletsByServletContextName(
679         String servletContextName, Map<String, Portlet> portletsPool) {
680 
681         List<Portlet> portlets = new ArrayList<Portlet>();
682 
683         Iterator<Map.Entry<String, Portlet>> itr =
684             portletsPool.entrySet().iterator();
685 
686         while (itr.hasNext()) {
687             Map.Entry<String, Portlet> entry = itr.next();
688 
689             String portletId = entry.getKey();
690             Portlet portlet = entry.getValue();
691 
692             if (Validator.isNotNull(servletContextName)) {
693                 if (portletId.endsWith(
694                         PortletConstants.WAR_SEPARATOR + servletContextName)) {
695 
696                     portlets.add(portlet);
697                 }
698             }
699             else {
700                 if (portletId.indexOf(PortletConstants.WAR_SEPARATOR) == -1) {
701                     portlets.add(portlet);
702                 }
703             }
704         }
705 
706         return portlets;
707     }
708 
709     private Map<String, Portlet> _getPortletsPool() {
710         return _portletsPool;
711     }
712 
713     private Map<String, Portlet> _getPortletsPool(long companyId)
714         throws SystemException {
715 
716         Map<String, Portlet> portletsPool = _companyPortletsPool.get(companyId);
717 
718         if (portletsPool == null) {
719             portletsPool = new ConcurrentHashMap<String, Portlet>();
720 
721             Map<String, Portlet> parentPortletsPool = _getPortletsPool();
722 
723             if (parentPortletsPool == null) {
724 
725                 // The Upgrade scripts sometimes try to access portlet
726                 // preferences before the portal's been initialized. Return an
727                 // empty pool.
728 
729                 return portletsPool;
730             }
731 
732             Iterator<Portlet> itr = parentPortletsPool.values().iterator();
733 
734             while (itr.hasNext()) {
735                 Portlet portlet = itr.next();
736 
737                 portlet = (Portlet)portlet.clone();
738 
739                 portlet.setCompanyId(companyId);
740 
741                 portletsPool.put(portlet.getPortletId(), portlet);
742             }
743 
744             itr = portletPersistence.findByCompanyId(companyId).iterator();
745 
746             while (itr.hasNext()) {
747                 Portlet portlet = itr.next();
748 
749                 Portlet portletModel = portletsPool.get(portlet.getPortletId());
750 
751                 // Portlet may be null if it exists in the database but its
752                 // portlet WAR is not yet loaded
753 
754                 if (portletModel != null) {
755                     portletModel.setPluginPackage(portlet.getPluginPackage());
756                     portletModel.setDefaultPluginSetting(
757                         portlet.getDefaultPluginSetting());
758                     portletModel.setRoles(portlet.getRoles());
759                     portletModel.setActive(portlet.getActive());
760                 }
761             }
762 
763             _companyPortletsPool.put(companyId, portletsPool);
764         }
765 
766         return portletsPool;
767     }
768 
769     private void _readLiferayDisplay(
770         String servletContextName, Element el, PortletCategory portletCategory,
771         Set<String> portletIds) {
772 
773         Iterator<Element> itr1 = el.elements("category").iterator();
774 
775         while (itr1.hasNext()) {
776             Element category = itr1.next();
777 
778             String name = category.attributeValue("name");
779 
780             PortletCategory curPortletCategory = new PortletCategory(name);
781 
782             portletCategory.addCategory(curPortletCategory);
783 
784             Set<String> curPortletIds = curPortletCategory.getPortletIds();
785 
786             Iterator<Element> itr2 = category.elements("portlet").iterator();
787 
788             while (itr2.hasNext()) {
789                 Element portlet = itr2.next();
790 
791                 String portletId = portlet.attributeValue("id");
792 
793                 if (Validator.isNotNull(servletContextName)) {
794                     portletId =
795                         portletId + PortletConstants.WAR_SEPARATOR +
796                             servletContextName;
797                 }
798 
799                 portletId = PortalUtil.getJsSafePortletId(portletId);
800 
801                 portletIds.add(portletId);
802                 curPortletIds.add(portletId);
803             }
804 
805             _readLiferayDisplay(
806                 servletContextName, category, curPortletCategory, portletIds);
807         }
808     }
809 
810     private PortletCategory _readLiferayDisplayXML(String xml)
811         throws Exception {
812 
813         return _readLiferayDisplayXML(null, xml);
814     }
815 
816     private PortletCategory _readLiferayDisplayXML(
817             String servletContextName, String xml)
818         throws Exception {
819 
820         PortletCategory portletCategory = new PortletCategory();
821 
822         if (xml == null) {
823             xml = ContentUtil.get(
824                 "com/liferay/portal/deploy/dependencies/liferay-display.xml");
825         }
826 
827         Document doc = SAXReaderUtil.read(xml, true);
828 
829         Element root = doc.getRootElement();
830 
831         Set<String> portletIds = new HashSet<String>();
832 
833         _readLiferayDisplay(
834             servletContextName, root, portletCategory, portletIds);
835 
836         // Portlets that do not belong to any categories should default to the
837         // Undefined category
838 
839         Set<String> undefinedPortletIds = new HashSet<String>();
840 
841         Iterator<Portlet> itr = _getPortletsPool().values().iterator();
842 
843         while (itr.hasNext()) {
844             Portlet portlet = itr.next();
845 
846             if (portlet.isRemote()) {
847                 _readRemoteDisplay(portlet, portletCategory);
848 
849                 continue;
850             }
851 
852             String portletId = portlet.getPortletId();
853 
854             PortletApp portletApp = portlet.getPortletApp();
855 
856             if ((servletContextName != null) && (portletApp.isWARFile()) &&
857                 (portletId.endsWith(
858                     PortletConstants.WAR_SEPARATOR +
859                         PortalUtil.getJsSafePortletId(servletContextName)) &&
860                 (!portletIds.contains(portletId)))) {
861 
862                 undefinedPortletIds.add(portletId);
863             }
864             else if ((servletContextName == null) &&
865                      (!portletApp.isWARFile()) &&
866                      (portletId.indexOf(
867                         PortletConstants.WAR_SEPARATOR) == -1) &&
868                      (!portletIds.contains(portletId))) {
869 
870                 undefinedPortletIds.add(portletId);
871             }
872         }
873 
874         if (undefinedPortletIds.size() > 0) {
875             PortletCategory undefinedCategory = new PortletCategory(
876                 "category.undefined");
877 
878             portletCategory.addCategory(undefinedCategory);
879 
880             undefinedCategory.getPortletIds().addAll(undefinedPortletIds);
881         }
882 
883         return portletCategory;
884     }
885 
886     private Set<String> _readLiferayPortletXML(
887             String xml, Map<String, Portlet> portletsPool)
888         throws Exception {
889 
890         return _readLiferayPortletXML(StringPool.BLANK, xml, portletsPool);
891     }
892 
893     private Set<String> _readLiferayPortletXML(
894             String servletContextName, String xml,
895             Map<String, Portlet> portletsPool)
896         throws Exception {
897 
898         Set<String> liferayPortletIds = new HashSet<String>();
899 
900         if (xml == null) {
901             return liferayPortletIds;
902         }
903 
904         Document doc = SAXReaderUtil.read(xml, true);
905 
906         Element root = doc.getRootElement();
907 
908         PortletApp portletApp = _getPortletApp(servletContextName);
909 
910         Map<String, String> roleMappers = new HashMap<String, String>();
911 
912         Iterator<Element> itr1 = root.elements("role-mapper").iterator();
913 
914         while (itr1.hasNext()) {
915             Element roleMapper = itr1.next();
916 
917             String roleName = roleMapper.elementText("role-name");
918             String roleLink = roleMapper.elementText("role-link");
919 
920             roleMappers.put(roleName, roleLink);
921         }
922 
923         Map<String, String> customUserAttributes =
924             portletApp.getCustomUserAttributes();
925 
926         itr1 = root.elements("custom-user-attribute").iterator();
927 
928         while (itr1.hasNext()) {
929             Element customUserAttribute = itr1.next();
930 
931             String customClass = customUserAttribute.elementText(
932                 "custom-class");
933 
934             Iterator<Element> itr2 = customUserAttribute.elements(
935                 "name").iterator();
936 
937             while (itr2.hasNext()) {
938                 Element nameEl = itr2.next();
939 
940                 String name = nameEl.getText();
941 
942                 customUserAttributes.put(name, customClass);
943             }
944         }
945 
946         itr1 = root.elements("portlet").iterator();
947 
948         while (itr1.hasNext()) {
949             Element portlet = itr1.next();
950 
951             String portletId = portlet.elementText("portlet-name");
952 
953             if (Validator.isNotNull(servletContextName)) {
954                 portletId =
955                     portletId + PortletConstants.WAR_SEPARATOR +
956                         servletContextName;
957             }
958 
959             portletId = PortalUtil.getJsSafePortletId(portletId);
960 
961             if (_log.isDebugEnabled()) {
962                 _log.debug("Reading portlet extension " + portletId);
963             }
964 
965             liferayPortletIds.add(portletId);
966 
967             Portlet portletModel = portletsPool.get(portletId);
968 
969             if (portletModel != null) {
970                 portletModel.setIcon(GetterUtil.getString(
971                     portlet.elementText("icon"), portletModel.getIcon()));
972                 portletModel.setVirtualPath(GetterUtil.getString(
973                     portlet.elementText("virtual-path"),
974                     portletModel.getVirtualPath()));
975                 portletModel.setStrutsPath(GetterUtil.getString(
976                     portlet.elementText("struts-path"),
977                     portletModel.getStrutsPath()));
978 
979                 if (Validator.isNotNull(
980                         portlet.elementText("configuration-path"))) {
981 
982                     _log.error(
983                         "The configuration-path element is no longer " +
984                             "supported. Use configuration-action-class " +
985                                 "instead.");
986                 }
987 
988                 portletModel.setConfigurationActionClass(GetterUtil.getString(
989                     portlet.elementText("configuration-action-class"),
990                     portletModel.getConfigurationActionClass()));
991                 portletModel.setIndexerClass(GetterUtil.getString(
992                     portlet.elementText("indexer-class"),
993                     portletModel.getIndexerClass()));
994                 portletModel.setOpenSearchClass(GetterUtil.getString(
995                     portlet.elementText("open-search-class"),
996                     portletModel.getOpenSearchClass()));
997                 portletModel.setSchedulerClass(GetterUtil.getString(
998                     portlet.elementText("scheduler-class"),
999                     portletModel.getSchedulerClass()));
1000                portletModel.setPortletURLClass(GetterUtil.getString(
1001                    portlet.elementText("portlet-url-class"),
1002                    portletModel.getPortletURLClass()));
1003
1004                portletModel.setFriendlyURLMapperClass(GetterUtil.getString(
1005                    portlet.elementText("friendly-url-mapper-class"),
1006                    portletModel.getFriendlyURLMapperClass()));
1007
1008                if (Validator.isNull(
1009                        portletModel.getFriendlyURLMapperClass())) {
1010
1011                    _friendlyURLMapperPortlets.remove(portletId);
1012                }
1013                else {
1014                    _friendlyURLMapperPortlets.put(portletId, portletModel);
1015                }
1016
1017                portletModel.setURLEncoderClass(GetterUtil.getString(
1018                    portlet.elementText("url-encoder-class"),
1019                    portletModel.getURLEncoderClass()));
1020                portletModel.setPortletDataHandlerClass(GetterUtil.getString(
1021                    portlet.elementText("portlet-data-handler-class"),
1022                    portletModel.getPortletDataHandlerClass()));
1023                portletModel.setPortletLayoutListenerClass(GetterUtil.getString(
1024                    portlet.elementText("portlet-layout-listener-class"),
1025                    portletModel.getPortletLayoutListenerClass()));
1026                portletModel.setPollerProcessorClass(GetterUtil.getString(
1027                    portlet.elementText("poller-processor-class"),
1028                    portletModel.getPollerProcessorClass()));
1029                portletModel.setPopMessageListenerClass(GetterUtil.getString(
1030                    portlet.elementText("pop-message-listener-class"),
1031                    portletModel.getPopMessageListenerClass()));
1032                portletModel.setSocialActivityInterpreterClass(
1033                    GetterUtil.getString(
1034                        portlet.elementText(
1035                            "social-activity-interpreter-class"),
1036                            portletModel.getSocialActivityInterpreterClass()));
1037                portletModel.setSocialRequestInterpreterClass(
1038                    GetterUtil.getString(
1039                        portlet.elementText(
1040                            "social-request-interpreter-class"),
1041                            portletModel.getSocialRequestInterpreterClass()));
1042                portletModel.setWebDAVStorageToken(GetterUtil.getString(
1043                    portlet.elementText("webdav-storage-token"),
1044                    portletModel.getWebDAVStorageToken()));
1045                portletModel.setWebDAVStorageClass(GetterUtil.getString(
1046                    portlet.elementText("webdav-storage-class"),
1047                    portletModel.getWebDAVStorageClass()));
1048                portletModel.setControlPanelEntryCategory(GetterUtil.getString(
1049                    portlet.elementText("control-panel-entry-category"),
1050                    portletModel.getControlPanelEntryCategory()));
1051                portletModel.setControlPanelEntryWeight(GetterUtil.getDouble(
1052                    portlet.elementText("control-panel-entry-weight"),
1053                    portletModel.getControlPanelEntryWeight()));
1054                portletModel.setControlPanelEntryClass(GetterUtil.getString(
1055                    portlet.elementText("control-panel-entry-class"),
1056                    portletModel.getControlPanelEntryClass()));
1057                portletModel.setPreferencesCompanyWide(GetterUtil.getBoolean(
1058                    portlet.elementText("preferences-company-wide"),
1059                    portletModel.isPreferencesCompanyWide()));
1060                portletModel.setPreferencesUniquePerLayout(
1061                    GetterUtil.getBoolean(
1062                        portlet.elementText("preferences-unique-per-layout"),
1063                        portletModel.isPreferencesUniquePerLayout()));
1064                portletModel.setPreferencesOwnedByGroup(GetterUtil.getBoolean(
1065                    portlet.elementText("preferences-owned-by-group"),
1066                    portletModel.isPreferencesOwnedByGroup()));
1067                portletModel.setUseDefaultTemplate(GetterUtil.getBoolean(
1068                    portlet.elementText("use-default-template"),
1069                    portletModel.isUseDefaultTemplate()));
1070                portletModel.setShowPortletAccessDenied(GetterUtil.getBoolean(
1071                    portlet.elementText("show-portlet-access-denied"),
1072                    portletModel.isShowPortletAccessDenied()));
1073                portletModel.setShowPortletInactive(GetterUtil.getBoolean(
1074                    portlet.elementText("show-portlet-inactive"),
1075                    portletModel.isShowPortletInactive()));
1076                portletModel.setActionURLRedirect(GetterUtil.getBoolean(
1077                    portlet.elementText("action-url-redirect"),
1078                    portletModel.isActionURLRedirect()));
1079                portletModel.setRestoreCurrentView(GetterUtil.getBoolean(
1080                    portlet.elementText("restore-current-view"),
1081                    portletModel.isRestoreCurrentView()));
1082                portletModel.setMaximizeEdit(GetterUtil.getBoolean(
1083                    portlet.elementText("maximize-edit"),
1084                    portletModel.isMaximizeEdit()));
1085                portletModel.setMaximizeHelp(GetterUtil.getBoolean(
1086                    portlet.elementText("maximize-help"),
1087                    portletModel.isMaximizeHelp()));
1088                portletModel.setPopUpPrint(GetterUtil.getBoolean(
1089                    portlet.elementText("pop-up-print"),
1090                    portletModel.isPopUpPrint()));
1091                portletModel.setLayoutCacheable(GetterUtil.getBoolean(
1092                    portlet.elementText("layout-cacheable"),
1093                    portletModel.isLayoutCacheable()));
1094                portletModel.setInstanceable(GetterUtil.getBoolean(
1095                    portlet.elementText("instanceable"),
1096                    portletModel.isInstanceable()));
1097                portletModel.setScopeable(GetterUtil.getBoolean(
1098                    portlet.elementText("scopeable"),
1099                    portletModel.isScopeable()));
1100                portletModel.setUserPrincipalStrategy(GetterUtil.getString(
1101                    portlet.elementText("user-principal-strategy"),
1102                    portletModel.getUserPrincipalStrategy()));
1103                portletModel.setPrivateRequestAttributes(GetterUtil.getBoolean(
1104                    portlet.elementText("private-request-attributes"),
1105                    portletModel.isPrivateRequestAttributes()));
1106                portletModel.setPrivateSessionAttributes(GetterUtil.getBoolean(
1107                    portlet.elementText("private-session-attributes"),
1108                    portletModel.isPrivateSessionAttributes()));
1109                portletModel.setRenderWeight(GetterUtil.getInteger(
1110                    portlet.elementText("render-weight"),
1111                    portletModel.getRenderWeight()));
1112                portletModel.setAjaxable(GetterUtil.getBoolean(
1113                    portlet.elementText("ajaxable"),
1114                    portletModel.isAjaxable()));
1115
1116                List<String> headerPortalCssList =
1117                    portletModel.getHeaderPortalCss();
1118
1119                Iterator<Element> itr2 = portlet.elements(
1120                    "header-portal-css").iterator();
1121
1122                while (itr2.hasNext()) {
1123                    Element headerPortalCssEl = itr2.next();
1124
1125                    headerPortalCssList.add(headerPortalCssEl.getText());
1126                }
1127
1128                List<String> headerPortletCssList =
1129                    portletModel.getHeaderPortletCss();
1130
1131                List<Element> list = new ArrayList<Element>();
1132
1133                list.addAll(portlet.elements("header-css"));
1134                list.addAll(portlet.elements("header-portlet-css"));
1135
1136                itr2 = list.iterator();
1137
1138                while (itr2.hasNext()) {
1139                    Element headerPortletCssEl = itr2.next();
1140
1141                    headerPortletCssList.add(headerPortletCssEl.getText());
1142                }
1143
1144                List<String> headerPortalJavaScriptList =
1145                    portletModel.getHeaderPortalJavaScript();
1146
1147                itr2 = portlet.elements("header-portal-javascript").iterator();
1148
1149                while (itr2.hasNext()) {
1150                    Element headerPortalJavaScriptEl = itr2.next();
1151
1152                    headerPortalJavaScriptList.add(
1153                        headerPortalJavaScriptEl.getText());
1154                }
1155
1156                List<String> headerPortletJavaScriptList =
1157                    portletModel.getHeaderPortletJavaScript();
1158
1159                list.clear();
1160
1161                list.addAll(portlet.elements("header-javascript"));
1162                list.addAll(portlet.elements("header-portlet-javascript"));
1163
1164                itr2 = list.iterator();
1165
1166                while (itr2.hasNext()) {
1167                    Element headerPortletJavaScriptEl = itr2.next();
1168
1169                    headerPortletJavaScriptList.add(
1170                        headerPortletJavaScriptEl.getText());
1171                }
1172
1173                List<String> footerPortalCssList =
1174                    portletModel.getFooterPortalCss();
1175
1176                itr2 = portlet.elements("footer-portal-css").iterator();
1177
1178                while (itr2.hasNext()) {
1179                    Element footerPortalCssEl = itr2.next();
1180
1181                    footerPortalCssList.add(footerPortalCssEl.getText());
1182                }
1183
1184                List<String> footerPortletCssList =
1185                    portletModel.getFooterPortletCss();
1186
1187                itr2 = portlet.elements("footer-portlet-css").iterator();
1188
1189                while (itr2.hasNext()) {
1190                    Element footerPortletCssEl = itr2.next();
1191
1192                    footerPortletCssList.add(footerPortletCssEl.getText());
1193                }
1194
1195                List<String> footerPortalJavaScriptList =
1196                    portletModel.getFooterPortalJavaScript();
1197
1198                itr2 = portlet.elements("footer-portal-javascript").iterator();
1199
1200                while (itr2.hasNext()) {
1201                    Element footerPortalJavaScriptEl = itr2.next();
1202
1203                    footerPortalJavaScriptList.add(
1204                        footerPortalJavaScriptEl.getText());
1205                }
1206
1207                List<String> footerPortletJavaScriptList =
1208                    portletModel.getFooterPortletJavaScript();
1209
1210                itr2 = portlet.elements("footer-portlet-javascript").iterator();
1211
1212                while (itr2.hasNext()) {
1213                    Element footerPortletJavaScriptEl = itr2.next();
1214
1215                    footerPortletJavaScriptList.add(
1216                        footerPortletJavaScriptEl.getText());
1217                }
1218
1219                portletModel.setCssClassWrapper(GetterUtil.getString(
1220                    portlet.elementText("css-class-wrapper"),
1221                    portletModel.getCssClassWrapper()));
1222                portletModel.setFacebookIntegration(GetterUtil.getString(
1223                    portlet.elementText("facebook-integration"),
1224                    portletModel.getFacebookIntegration()));
1225                portletModel.setAddDefaultResource(GetterUtil.getBoolean(
1226                    portlet.elementText("add-default-resource"),
1227                    portletModel.isAddDefaultResource()));
1228                portletModel.setSystem(GetterUtil.getBoolean(
1229                    portlet.elementText("system"),
1230                    portletModel.isSystem()));
1231                portletModel.setActive(GetterUtil.getBoolean(
1232                    portlet.elementText("active"),
1233                    portletModel.isActive()));
1234                portletModel.setInclude(GetterUtil.getBoolean(
1235                    portlet.elementText("include"),
1236                    portletModel.isInclude()));
1237
1238                if (!portletModel.isAjaxable() &&
1239                    (portletModel.getRenderWeight() < 1)) {
1240
1241                    portletModel.setRenderWeight(1);
1242                }
1243
1244                portletModel.getRoleMappers().putAll(roleMappers);
1245                portletModel.linkRoles();
1246            }
1247        }
1248
1249        return liferayPortletIds;
1250    }
1251
1252    private Set<String> _readPortletXML(
1253            ServletContext servletContext, String xml,
1254            Map<String, Portlet> portletsPool, List<String> servletURLPatterns,
1255            PluginPackage pluginPackage)
1256        throws Exception {
1257
1258        return _readPortletXML(
1259            StringPool.BLANK, servletContext, xml, portletsPool,
1260            servletURLPatterns, pluginPackage);
1261    }
1262
1263    private Set<String> _readPortletXML(
1264            String servletContextName, ServletContext servletContext,
1265            String xml, Map<String, Portlet> portletsPool,
1266            List<String> servletURLPatterns, PluginPackage pluginPackage)
1267        throws Exception {
1268
1269        Set<String> portletIds = new HashSet<String>();
1270
1271        if (xml == null) {
1272            return portletIds;
1273        }
1274
1275        boolean portletXMLValidate = PropsValues.PORTLET_XML_VALIDATE;
1276
1277        if (ServerDetector.isGeronimo() || ServerDetector.isResin()) {
1278            portletXMLValidate = false;
1279        }
1280
1281        Document doc = SAXReaderUtil.read(xml, portletXMLValidate);
1282
1283        Element root = doc.getRootElement();
1284
1285        PortletApp portletApp = _getPortletApp(servletContextName);
1286
1287        portletApp.getServletURLPatterns().addAll(servletURLPatterns);
1288
1289        Set<String> userAttributes = portletApp.getUserAttributes();
1290
1291        Iterator<Element> itr1 = root.elements("user-attribute").iterator();
1292
1293        while (itr1.hasNext()) {
1294            Element userAttribute = itr1.next();
1295
1296            String name = userAttribute.elementText("name");
1297
1298            userAttributes.add(name);
1299        }
1300
1301        String defaultNamespace = root.elementText("default-namespace");
1302
1303        if (Validator.isNotNull(defaultNamespace)) {
1304            portletApp.setDefaultNamespace(defaultNamespace);
1305        }
1306
1307        itr1 = root.elements("event-definition").iterator();
1308
1309        while (itr1.hasNext()) {
1310            Element eventDefinitionEl = itr1.next();
1311
1312            Element qNameEl = eventDefinitionEl.element("qname");
1313            Element nameEl = eventDefinitionEl.element("name");
1314            String valueType = eventDefinitionEl.elementText("value-type");
1315
1316            QName qName = PortletQNameUtil.getQName(
1317                qNameEl, nameEl, portletApp.getDefaultNamespace());
1318
1319            EventDefinition eventDefinition = new EventDefinitionImpl(
1320                qName, valueType, portletApp);
1321
1322            portletApp.addEventDefinition(eventDefinition);
1323        }
1324
1325        itr1 = root.elements("public-render-parameter").iterator();
1326
1327        while (itr1.hasNext()) {
1328            Element publicRenderParameterEl = itr1.next();
1329
1330            String identifier = publicRenderParameterEl.elementText(
1331                "identifier");
1332            Element qNameEl = publicRenderParameterEl.element("qname");
1333            Element nameEl = publicRenderParameterEl.element("name");
1334
1335            QName qName = PortletQNameUtil.getQName(
1336                qNameEl, nameEl, portletApp.getDefaultNamespace());
1337
1338            PublicRenderParameter publicRenderParameter =
1339                new PublicRenderParameterImpl(identifier, qName, portletApp);
1340
1341            portletApp.addPublicRenderParameter(publicRenderParameter);
1342        }
1343
1344        itr1 = root.elements("container-runtime-option").iterator();
1345
1346        while (itr1.hasNext()) {
1347            Element containerRuntimeOption = itr1.next();
1348
1349            String name = containerRuntimeOption.elementText("name");
1350
1351            List<String> values = new ArrayList<String>();
1352
1353            for (Element value : containerRuntimeOption.elements("value")) {
1354                values.add(value.getTextTrim());
1355            }
1356
1357            portletApp.getContainerRuntimeOptions().put(
1358                name, values.toArray(new String[values.size()]));
1359        }
1360
1361        long timestamp = ServletContextUtil.getLastModified(servletContext);
1362
1363        itr1 = root.elements("portlet").iterator();
1364
1365        while (itr1.hasNext()) {
1366            Element portlet = itr1.next();
1367
1368            String portletName = portlet.elementText("portlet-name");
1369
1370            String portletId = portletName;
1371
1372            if (Validator.isNotNull(servletContextName)) {
1373                portletId =
1374                    portletId + PortletConstants.WAR_SEPARATOR +
1375                        servletContextName;
1376            }
1377
1378            portletId = PortalUtil.getJsSafePortletId(portletId);
1379
1380            if (_log.isDebugEnabled()) {
1381                _log.debug("Reading portlet " + portletId);
1382            }
1383
1384            portletIds.add(portletId);
1385
1386            Portlet portletModel = portletsPool.get(portletId);
1387
1388            if (portletModel == null) {
1389                portletModel = new PortletImpl(
1390                    CompanyConstants.SYSTEM, portletId);
1391
1392                portletsPool.put(portletId, portletModel);
1393            }
1394
1395            portletModel.setTimestamp(timestamp);
1396
1397            portletModel.setPluginPackage(pluginPackage);
1398            portletModel.setPortletApp(portletApp);
1399
1400            portletModel.setPortletName(portletName);
1401            portletModel.setDisplayName(GetterUtil.getString(
1402                portlet.elementText("display-name"),
1403                portletModel.getDisplayName()));
1404            portletModel.setPortletClass(GetterUtil.getString(
1405                portlet.elementText("portlet-class")));
1406
1407            Iterator<Element> itr2 = portlet.elements("init-param").iterator();
1408
1409            while (itr2.hasNext()) {
1410                Element initParam = itr2.next();
1411
1412                portletModel.getInitParams().put(
1413                    initParam.elementText("name"),
1414                    initParam.elementText("value"));
1415            }
1416
1417            Element expirationCache = portlet.element("expiration-cache");
1418
1419            if (expirationCache != null) {
1420                portletModel.setExpCache(new Integer(GetterUtil.getInteger(
1421                    expirationCache.getText())));
1422            }
1423
1424            itr2 = portlet.elements("supports").iterator();
1425
1426            while (itr2.hasNext()) {
1427                Element supports = itr2.next();
1428
1429                String mimeType = supports.elementText("mime-type");
1430
1431                Set<String> mimeTypeModes =
1432                    portletModel.getPortletModes().get(mimeType);
1433
1434                if (mimeTypeModes == null) {
1435                    mimeTypeModes = new HashSet<String>();
1436
1437                    portletModel.getPortletModes().put(mimeType, mimeTypeModes);
1438                }
1439
1440                mimeTypeModes.add(PortletMode.VIEW.toString().toLowerCase());
1441
1442                Iterator<Element> itr3 = supports.elements(
1443                    "portlet-mode").iterator();
1444
1445                while (itr3.hasNext()) {
1446                    Element portletMode = itr3.next();
1447
1448                    mimeTypeModes.add(portletMode.getTextTrim().toLowerCase());
1449                }
1450            }
1451
1452            Set<String> supportedLocales = portletModel.getSupportedLocales();
1453
1454            //supportedLocales.add(
1455            //  LocaleUtil.toLanguageId(LocaleUtil.getDefault()));
1456
1457            itr2 = portlet.elements("supported-locale").iterator();
1458
1459            while (itr2.hasNext()) {
1460                Element supportedLocaleEl = itr2.next();
1461
1462                String supportedLocale = supportedLocaleEl.getText();
1463
1464                supportedLocales.add(supportedLocale);
1465            }
1466
1467            portletModel.setResourceBundle(
1468                portlet.elementText("resource-bundle"));
1469
1470            Element portletInfo = portlet.element("portlet-info");
1471
1472            String portletInfoTitle = null;
1473            String portletInfoShortTitle = null;
1474            String portletInfoKeyWords = null;
1475
1476            if (portletInfo != null) {
1477                portletInfoTitle = portletInfo.elementText("title");
1478                portletInfoShortTitle = portletInfo.elementText("short-title");
1479                portletInfoKeyWords = portletInfo.elementText("keywords");
1480            }
1481
1482            portletModel.setPortletInfo(new PortletInfo(
1483                portletInfoTitle, portletInfoShortTitle, portletInfoKeyWords));
1484
1485            Element portletPreferences = portlet.element("portlet-preferences");
1486
1487            String defaultPreferences = null;
1488            String preferencesValidator = null;
1489
1490            if (portletPreferences != null) {
1491                Element preferencesValidatorEl =
1492                    portletPreferences.element("preferences-validator");
1493
1494                if (preferencesValidatorEl != null) {
1495                    preferencesValidator = preferencesValidatorEl.getText();
1496
1497                    portletPreferences.remove(preferencesValidatorEl);
1498                }
1499
1500                defaultPreferences = portletPreferences.asXML();
1501            }
1502
1503            portletModel.setDefaultPreferences(defaultPreferences);
1504            portletModel.setPreferencesValidator(preferencesValidator);
1505
1506            if (!portletApp.isWARFile() &&
1507                Validator.isNotNull(preferencesValidator) &&
1508                PropsValues.PREFERENCE_VALIDATE_ON_STARTUP) {
1509
1510                try {
1511                    PreferencesValidator preferencesValidatorObj =
1512                        PortalUtil.getPreferencesValidator(portletModel);
1513
1514                    preferencesValidatorObj.validate(
1515                        PortletPreferencesSerializer.fromDefaultXML(
1516                            defaultPreferences));
1517                }
1518                catch (Exception e) {
1519                    if (_log.isWarnEnabled()) {
1520                        _log.warn(
1521                            "Portlet with the name " + portletId +
1522                                " does not have valid default preferences");
1523                    }
1524                }
1525            }
1526
1527            Set<String> unlikedRoles = portletModel.getUnlinkedRoles();
1528
1529            itr2 = portlet.elements("security-role-ref").iterator();
1530
1531            while (itr2.hasNext()) {
1532                Element role = itr2.next();
1533
1534                unlikedRoles.add(role.elementText("role-name"));
1535            }
1536
1537            itr2 = portlet.elements("supported-processing-event").iterator();
1538
1539            while (itr2.hasNext()) {
1540                Element supportedProcessingEvent = itr2.next();
1541
1542                Element qNameEl = supportedProcessingEvent.element("qname");
1543                Element nameEl = supportedProcessingEvent.element("name");
1544
1545                QName qName = PortletQNameUtil.getQName(
1546                    qNameEl, nameEl, portletApp.getDefaultNamespace());
1547
1548                portletModel.addProcessingEvent(qName);
1549            }
1550
1551            itr2 = portlet.elements("supported-publishing-event").iterator();
1552
1553            while (itr2.hasNext()) {
1554                Element supportedPublishingEvent = itr2.next();
1555
1556                Element qNameEl = supportedPublishingEvent.element("qname");
1557                Element nameEl = supportedPublishingEvent.element("name");
1558
1559                QName qName = PortletQNameUtil.getQName(
1560                    qNameEl, nameEl, portletApp.getDefaultNamespace());
1561
1562                portletModel.addPublishingEvent(qName);
1563            }
1564
1565            itr2 = portlet.elements(
1566                "supported-public-render-parameter").iterator();
1567
1568            while (itr2.hasNext()) {
1569                Element supportedPublicRenderParameter = itr2.next();
1570
1571                String identifier =
1572                    supportedPublicRenderParameter.getTextTrim();
1573
1574                PublicRenderParameter publicRenderParameter =
1575                    portletApp.getPublicRenderParameter(identifier);
1576
1577                if (publicRenderParameter == null) {
1578                    _log.error(
1579                        "Supported public render parameter references " +
1580                            "unnknown identifier " + identifier);
1581
1582                    continue;
1583                }
1584
1585                portletModel.addPublicRenderParameter(publicRenderParameter);
1586            }
1587        }
1588
1589        itr1 = root.elements("filter").iterator();
1590
1591        while (itr1.hasNext()) {
1592            Element filter = itr1.next();
1593
1594            String filterName = filter.elementText("filter-name");
1595            String filterClass = filter.elementText("filter-class");
1596
1597            Set<String> lifecycles = new LinkedHashSet<String>();
1598
1599            Iterator<Element> itr2 = filter.elements("lifecycle").iterator();
1600
1601            while (itr2.hasNext()) {
1602                Element lifecycle = itr2.next();
1603
1604                lifecycles.add(lifecycle.getText());
1605            }
1606
1607            Map<String, String> initParams = new HashMap<String, String>();
1608
1609            itr2 = filter.elements("init-param").iterator();
1610
1611            while (itr2.hasNext()) {
1612                Element initParam = itr2.next();
1613
1614                initParams.put(
1615                    initParam.elementText("name"),
1616                    initParam.elementText("value"));
1617            }
1618
1619            PortletFilter portletFilter = new PortletFilterImpl(
1620                filterName, filterClass, lifecycles, initParams, portletApp);
1621
1622            portletApp.addPortletFilter(portletFilter);
1623        }
1624
1625        itr1 = root.elements("filter-mapping").iterator();
1626
1627        while (itr1.hasNext()) {
1628            Element filterMapping = itr1.next();
1629
1630            String filterName = filterMapping.elementText("filter-name");
1631
1632            Iterator<Element> itr2 = filterMapping.elements(
1633                "portlet-name").iterator();
1634
1635            while (itr2.hasNext()) {
1636                Element portletNameEl = itr2.next();
1637
1638                String portletName = portletNameEl.getTextTrim();
1639
1640                PortletFilter portletFilter = portletApp.getPortletFilter(
1641                    filterName);
1642
1643                if (portletFilter == null) {
1644                    _log.error(
1645                        "Filter mapping references unnknown filter name " +
1646                            filterName);
1647
1648                    continue;
1649                }
1650
1651                List<Portlet> portletModels = _getPortletsByPortletName(
1652                    portletName, servletContextName, portletsPool);
1653
1654                if (portletModels.size() == 0) {
1655                    _log.error(
1656                        "Filter mapping with filter name " + filterName +
1657                            " references unnknown portlet name " + portletName);
1658                }
1659
1660                for (Portlet portletModel : portletModels) {
1661                    portletModel.getPortletFilters().put(
1662                        filterName, portletFilter);
1663                }
1664            }
1665        }
1666
1667        itr1 = root.elements("listener").iterator();
1668
1669        while (itr1.hasNext()) {
1670            Element listener = itr1.next();
1671
1672            String listenerClass = listener.elementText("listener-class");
1673
1674            PortletURLListener portletURLListener = new PortletURLListenerImpl(
1675                listenerClass, portletApp);
1676
1677            portletApp.addPortletURLListener(portletURLListener);
1678        }
1679
1680        return portletIds;
1681    }
1682
1683    private void _readRemoteDisplay(
1684        Portlet remotePortlet, PortletCategory portletCategory) {
1685
1686        PortletCategory newPortletCategory = new PortletCategory();
1687
1688        PortletCategory wsrpCategory = portletCategory.getCategory(
1689            _WSRP_CATEGORY);
1690
1691        if (wsrpCategory == null) {
1692            wsrpCategory = new PortletCategory(_WSRP_CATEGORY);
1693
1694            newPortletCategory.addCategory(wsrpCategory);
1695        }
1696
1697        wsrpCategory.getPortletIds().add(remotePortlet.getPortletId());
1698
1699        portletCategory.merge(newPortletCategory);
1700    }
1701
1702    private List<String> _readWebXML(String xml) throws Exception {
1703        List<String> servletURLPatterns = new ArrayList<String>();
1704
1705        if (xml == null) {
1706            return servletURLPatterns;
1707        }
1708
1709        Document doc = SAXReaderUtil.read(xml);
1710
1711        Element root = doc.getRootElement();
1712
1713        Iterator<Element> itr = root.elements("servlet-mapping").iterator();
1714
1715        while (itr.hasNext()) {
1716            Element servletMapping = itr.next();
1717
1718            String urlPattern = servletMapping.elementText("url-pattern");
1719
1720            servletURLPatterns.add(urlPattern);
1721        }
1722
1723        return servletURLPatterns;
1724
1725    }
1726
1727    private void _setSpriteImages(
1728            ServletContext servletContext, PortletApp portletApp,
1729            String resourcePath)
1730        throws Exception {
1731
1732        Set<String> resourcePaths = servletContext.getResourcePaths(
1733            resourcePath);
1734
1735        if (resourcePaths == null) {
1736            return;
1737        }
1738
1739        List<File> images = new ArrayList<File>(resourcePaths.size());
1740
1741        for (String curResourcePath : resourcePaths) {
1742            if (curResourcePath.endsWith(StringPool.SLASH)) {
1743                _setSpriteImages(servletContext, portletApp, curResourcePath);
1744            }
1745            else if (curResourcePath.endsWith(".png")) {
1746                String realPath = ServletContextUtil.getRealPath(
1747                    servletContext, curResourcePath);
1748
1749                if (realPath != null) {
1750                    images.add(new File(realPath));
1751                }
1752                else {
1753                    if (ServerDetector.isTomcat()) {
1754                        if (_log.isInfoEnabled()) {
1755                            _log.info(ServletContextUtil.LOG_INFO_SPRITES);
1756                        }
1757                    }
1758                    else {
1759                        _log.error(
1760                            "Real path for " + curResourcePath + " is null");
1761                    }
1762                }
1763            }
1764        }
1765
1766        String spriteFileName = ".sprite.png";
1767        String spritePropertiesFileName = ".sprite.properties";
1768        String spritePropertiesRootPath = ServletContextUtil.getRealPath(
1769            servletContext, StringPool.SLASH);
1770
1771        Properties spriteProperties = SpriteProcessorUtil.generate(
1772            images, spriteFileName, spritePropertiesFileName,
1773            spritePropertiesRootPath, 16, 16, 10240);
1774
1775        if (spriteProperties == null) {
1776            return;
1777        }
1778
1779        spriteFileName =
1780            resourcePath.substring(0, resourcePath.length()) + spriteFileName;
1781
1782        portletApp.setSpriteImages(spriteFileName, spriteProperties);
1783    }
1784
1785    private static final String _WSRP_CATEGORY = "category.wsrp";
1786
1787    private static Log _log =
1788         LogFactoryUtil.getLog(PortletLocalServiceImpl.class);
1789
1790    private static Map<String, PortletApp> _portletAppsPool =
1791        new ConcurrentHashMap<String, PortletApp>();
1792    private static Map<String, Portlet> _portletsPool =
1793        new ConcurrentHashMap<String, Portlet>();
1794    private static Map<Long, Map<String, Portlet>> _companyPortletsPool =
1795        new ConcurrentHashMap<Long, Map<String, Portlet>>();
1796    private static Map<String, String> _portletIdsByStrutsPath =
1797        new ConcurrentHashMap<String, String>();
1798    private static Map<String, Portlet> _friendlyURLMapperPortlets =
1799        new ConcurrentHashMap<String, Portlet>();
1800
1801}