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