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