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. See LEP-5452.
246 
247         _portletAppsPool.clear();
248         _portletsPool.clear();
249         _companyPortletsPool.clear();
250         _portletIdsByStrutsPath.clear();
251         _friendlyURLMapperPortlets.clear();
252 
253         Map<String, Portlet> portletsPool = _getPortletsPool();
254 
255         try {
256             List<String> servletURLPatterns = _readWebXML(xmls[4]);
257 
258             Set<String> portletIds = _readPortletXML(
259                 xmls[0], portletsPool, servletURLPatterns, pluginPackage);
260 
261             portletIds.addAll(_readPortletXML(
262                 xmls[1], portletsPool, servletURLPatterns, pluginPackage));
263 
264             Set<String> liferayPortletIds =
265                 _readLiferayPortletXML(xmls[2], portletsPool);
266 
267             liferayPortletIds.addAll(
268                 _readLiferayPortletXML(xmls[3], portletsPool));
269 
270             // Check for missing entries in liferay-portlet.xml
271 
272             Iterator<String> portletIdsItr = portletIds.iterator();
273 
274             while (portletIdsItr.hasNext()) {
275                 String portletId = portletIdsItr.next();
276 
277                 if (_log.isWarnEnabled() &&
278                     !liferayPortletIds.contains(portletId)) {
279 
280                     _log.warn(
281                         "Portlet with the name " + portletId +
282                             " is described in portlet.xml but does not " +
283                                 "have a matching entry in liferay-portlet.xml");
284                 }
285             }
286 
287             // Check for missing entries in portlet.xml
288 
289             Iterator<String> liferayPortletIdsItr =
290                 liferayPortletIds.iterator();
291 
292             while (liferayPortletIdsItr.hasNext()) {
293                 String portletId = liferayPortletIdsItr.next();
294 
295                 if (_log.isWarnEnabled() && !portletIds.contains(portletId)) {
296                     _log.warn(
297                         "Portlet with the name " + portletId +
298                             " is described in liferay-portlet.xml but does " +
299                                 "not have a matching entry in portlet.xml");
300                 }
301             }
302 
303             // Remove portlets that should not be included
304 
305             Iterator<Map.Entry<String, Portlet>> portletPoolsItr =
306                 portletsPool.entrySet().iterator();
307 
308             while (portletPoolsItr.hasNext()) {
309                 Map.Entry<String, Portlet> entry = portletPoolsItr.next();
310 
311                 Portlet portletModel = entry.getValue();
312 
313                 if (!portletModel.getPortletId().equals(PortletKeys.ADMIN) &&
314                     !portletModel.getPortletId().equals(
315                         PortletKeys.MY_ACCOUNT) &&
316                     !portletModel.isInclude()) {
317 
318                     portletPoolsItr.remove();
319                 }
320             }
321         }
322         catch (Exception e) {
323             _log.error(e, e);
324         }
325     }
326 
327     public List<Portlet> initWAR(
328         String servletContextName, String[] xmls, PluginPackage pluginPackage) {
329 
330         List<Portlet> portlets = new ArrayList<Portlet>();
331 
332         Map<String, Portlet> portletsPool = _getPortletsPool();
333 
334         try {
335             List<String> servletURLPatterns = _readWebXML(xmls[3]);
336 
337             Set<String> portletIds = _readPortletXML(
338                 servletContextName, xmls[0], portletsPool, servletURLPatterns,
339                 pluginPackage);
340 
341             portletIds.addAll(_readPortletXML(
342                 servletContextName, xmls[1], portletsPool, servletURLPatterns,
343                 pluginPackage));
344 
345             Set<String> liferayPortletIds = _readLiferayPortletXML(
346                 servletContextName, xmls[2], portletsPool);
347 
348             // Check for missing entries in liferay-portlet.xml
349 
350             Iterator<String> itr = portletIds.iterator();
351 
352             while (itr.hasNext()) {
353                 String portletId = itr.next();
354 
355                 if (_log.isWarnEnabled() &&
356                     !liferayPortletIds.contains(portletId)) {
357 
358                     _log.warn(
359                         "Portlet with the name " + portletId +
360                             " is described in portlet.xml but does not " +
361                                 "have a matching entry in liferay-portlet.xml");
362                 }
363             }
364 
365             // Check for missing entries in portlet.xml
366 
367             itr = liferayPortletIds.iterator();
368 
369             while (itr.hasNext()) {
370                 String portletId = itr.next();
371 
372                 if (_log.isWarnEnabled() && !portletIds.contains(portletId)) {
373                     _log.warn(
374                         "Portlet with the name " + portletId +
375                             " is described in liferay-portlet.xml but does " +
376                                 "not have a matching entry in portlet.xml");
377                 }
378             }
379 
380             // Return the new portlets
381 
382             itr = portletIds.iterator();
383 
384             while (itr.hasNext()) {
385                 String portletId = itr.next();
386 
387                 Portlet portlet = _getPortletsPool().get(portletId);
388 
389                 portlets.add(portlet);
390             }
391         }
392         catch (Exception e) {
393             _log.error(e, e);
394         }
395 
396         _clearCaches();
397 
398         return portlets;
399     }
400 
401     public Portlet updatePortlet(
402             long companyId, String portletId, String roles, boolean active)
403         throws PortalException, SystemException {
404 
405         portletId = PortalUtil.getJsSafePortletId(portletId);
406 
407         Portlet portlet = portletPersistence.fetchByC_P(companyId, portletId);
408 
409         if (portlet == null) {
410             long id = counterLocalService.increment();
411 
412             portlet = portletPersistence.create(id);
413 
414             portlet.setCompanyId(companyId);
415             portlet.setPortletId(portletId);
416         }
417 
418         portlet.setRoles(roles);
419         portlet.setActive(active);
420 
421         portletPersistence.update(portlet, false);
422 
423         portlet = getPortletById(companyId, portletId);
424 
425         portlet.setRoles(roles);
426         portlet.setActive(active);
427 
428         return portlet;
429     }
430 
431     private void _clearCaches() {
432 
433         // Refresh security path to portlet id mapping for all portlets
434 
435         _portletIdsByStrutsPath.clear();
436 
437         // Refresh company portlets
438 
439         _companyPortletsPool.clear();
440     }
441 
442     private List<FriendlyURLMapper> _getFriendlyURLMappers() {
443         List<FriendlyURLMapper> friendlyURLMappers =
444             new ArrayList<FriendlyURLMapper>(
445                             _friendlyURLMapperPortlets.size());
446 
447         Iterator<Map.Entry<String, Portlet>> itr =
448             _friendlyURLMapperPortlets.entrySet().iterator();
449 
450         while (itr.hasNext()) {
451             Map.Entry<String, Portlet> entry = itr.next();
452 
453             Portlet portlet = entry.getValue();
454 
455             FriendlyURLMapper friendlyURLMapper =
456                 portlet.getFriendlyURLMapperInstance();
457 
458             if (friendlyURLMapper != null) {
459                 friendlyURLMappers.add(friendlyURLMapper);
460             }
461         }
462 
463         return friendlyURLMappers;
464     }
465 
466     private PortletApp _getPortletApp(String servletContextName) {
467         PortletApp portletApp = _portletAppsPool.get(servletContextName);
468 
469         if (portletApp == null) {
470             portletApp = new PortletAppImpl(servletContextName);
471 
472             _portletAppsPool.put(servletContextName, portletApp);
473         }
474 
475         return portletApp;
476     }
477 
478     private String _getPortletId(String securityPath) throws SystemException {
479         if (_portletIdsByStrutsPath.size() == 0) {
480             Iterator<Portlet> itr = _getPortletsPool().values().iterator();
481 
482             while (itr.hasNext()) {
483                 Portlet portlet = itr.next();
484 
485                 _portletIdsByStrutsPath.put(
486                     portlet.getStrutsPath(), portlet.getPortletId());
487             }
488         }
489 
490         String portletId = _portletIdsByStrutsPath.get(securityPath);
491 
492         if (Validator.isNull(portletId)) {
493             _log.error(
494                 "Struts path " + securityPath + " is not mapped to a portlet " +
495                     "in liferay-portlet.xml");
496         }
497 
498         return portletId;
499     }
500 
501     private List<Portlet> _getPortletsByPortletName(
502         String portletName, String servletContextName,
503         Map<String, Portlet> portletsPool) {
504 
505         List<Portlet> portlets = null;
506 
507         int pos = portletName.indexOf(StringPool.STAR);
508 
509         if (pos == -1) {
510             portlets = new ArrayList<Portlet>();
511 
512             String portletId = portletName;
513 
514             if (Validator.isNotNull(servletContextName)) {
515                 portletId =
516                     portletId + PortletImpl.WAR_SEPARATOR + servletContextName;
517             }
518 
519             portletId = PortalUtil.getJsSafePortletId(portletId);
520 
521             Portlet portlet = portletsPool.get(portletId);
522 
523             if (portlet != null) {
524                 portlets.add(portlet);
525             }
526 
527             return portlets;
528         }
529 
530         String portletNamePrefix = portletName.substring(0, pos);
531 
532         portlets = _getPortletsByServletContextName(
533             servletContextName, portletsPool);
534 
535         Iterator<Portlet> itr = portlets.iterator();
536 
537         while (itr.hasNext()) {
538             Portlet portlet = itr.next();
539 
540             if (!portlet.getPortletId().startsWith(portletNamePrefix)) {
541                 itr.remove();
542             }
543         }
544 
545         return portlets;
546     }
547 
548     private List<Portlet> _getPortletsByServletContextName(
549         String servletContextName, Map<String, Portlet> portletsPool) {
550 
551         List<Portlet> portlets = new ArrayList<Portlet>();
552 
553         Iterator<Map.Entry<String, Portlet>> itr =
554             portletsPool.entrySet().iterator();
555 
556         while (itr.hasNext()) {
557             Map.Entry<String, Portlet> entry = itr.next();
558 
559             String portletId = entry.getKey();
560             Portlet portlet = entry.getValue();
561 
562             if (Validator.isNotNull(servletContextName)) {
563                 if (portletId.endsWith(
564                         PortletImpl.WAR_SEPARATOR + servletContextName)) {
565 
566                     portlets.add(portlet);
567                 }
568             }
569             else {
570                 if (portletId.indexOf(PortletImpl.WAR_SEPARATOR) == -1) {
571                     portlets.add(portlet);
572                 }
573             }
574         }
575 
576         return portlets;
577     }
578 
579     private Map<String, Portlet> _getPortletsPool() {
580         return _portletsPool;
581     }
582 
583     private Map<String, Portlet> _getPortletsPool(long companyId)
584         throws SystemException {
585 
586         Map<String, Portlet> portletsPool = _companyPortletsPool.get(companyId);
587 
588         if (portletsPool == null) {
589             portletsPool = new ConcurrentHashMap<String, Portlet>();
590 
591             Map<String, Portlet> parentPortletsPool = _getPortletsPool();
592 
593             if (parentPortletsPool == null) {
594 
595                 // The Upgrade scripts sometimes try to access portlet
596                 // preferences before the portal's been initialized. Return an
597                 // empty pool.
598 
599                 return portletsPool;
600             }
601 
602             Iterator<Portlet> itr = parentPortletsPool.values().iterator();
603 
604             while (itr.hasNext()) {
605                 Portlet portlet = itr.next();
606 
607                 portlet = (Portlet)portlet.clone();
608 
609                 portlet.setCompanyId(companyId);
610 
611                 portletsPool.put(portlet.getPortletId(), portlet);
612             }
613 
614             itr = portletPersistence.findByCompanyId(companyId).iterator();
615 
616             while (itr.hasNext()) {
617                 Portlet portlet = itr.next();
618 
619                 Portlet portletModel = portletsPool.get(portlet.getPortletId());
620 
621                 // Portlet may be null if it exists in the database but its
622                 // portlet WAR is not yet loaded
623 
624                 if (portletModel != null) {
625                     portletModel.setPluginPackage(portlet.getPluginPackage());
626                     portletModel.setDefaultPluginSetting(
627                         portlet.getDefaultPluginSetting());
628                     portletModel.setRoles(portlet.getRoles());
629                     portletModel.setActive(portlet.getActive());
630                 }
631             }
632 
633             _companyPortletsPool.put(companyId, portletsPool);
634         }
635 
636         return portletsPool;
637     }
638 
639     private void _readLiferayDisplay(
640         String servletContextName, Element el, PortletCategory portletCategory,
641         Set<String> portletIds) {
642 
643         Iterator<Element> itr1 = el.elements("category").iterator();
644 
645         while (itr1.hasNext()) {
646             Element category = itr1.next();
647 
648             String name = category.attributeValue("name");
649 
650             PortletCategory curPortletCategory = new PortletCategory(name);
651 
652             portletCategory.addCategory(curPortletCategory);
653 
654             Set<String> curPortletIds = curPortletCategory.getPortletIds();
655 
656             Iterator<Element> itr2 = category.elements("portlet").iterator();
657 
658             while (itr2.hasNext()) {
659                 Element portlet = itr2.next();
660 
661                 String portletId = portlet.attributeValue("id");
662 
663                 if (Validator.isNotNull(servletContextName)) {
664                     portletId =
665                         portletId + PortletImpl.WAR_SEPARATOR +
666                             servletContextName;
667                 }
668 
669                 portletId = PortalUtil.getJsSafePortletId(portletId);
670 
671                 portletIds.add(portletId);
672                 curPortletIds.add(portletId);
673             }
674 
675             _readLiferayDisplay(
676                 servletContextName, category, curPortletCategory, portletIds);
677         }
678     }
679 
680     private PortletCategory _readLiferayDisplayXML(String xml)
681         throws DocumentException, IOException {
682 
683         return _readLiferayDisplayXML(null, xml);
684     }
685 
686     private PortletCategory _readLiferayDisplayXML(
687             String servletContextName, String xml)
688         throws DocumentException, IOException {
689 
690         PortletCategory portletCategory = new PortletCategory();
691 
692         if (xml == null) {
693             xml = ContentUtil.get(
694                 "com/liferay/portal/deploy/dependencies/liferay-display.xml");
695         }
696 
697         Document doc = DocumentUtil.readDocumentFromXML(xml, true);
698 
699         Element root = doc.getRootElement();
700 
701         Set<String> portletIds = new HashSet<String>();
702 
703         _readLiferayDisplay(
704             servletContextName, root, portletCategory, portletIds);
705 
706         // Portlets that do not belong to any categories should default to the
707         // Undefined category
708 
709         Set<String> undefinedPortletIds = new HashSet<String>();
710 
711         Iterator<Portlet> itr = _getPortletsPool().values().iterator();
712 
713         while (itr.hasNext()) {
714             Portlet portlet = itr.next();
715 
716             String portletId = portlet.getPortletId();
717 
718             PortletApp portletApp = portlet.getPortletApp();
719 
720             if ((servletContextName != null) && (portletApp.isWARFile()) &&
721                 (portletId.endsWith(
722                     PortletImpl.WAR_SEPARATOR +
723                         PortalUtil.getJsSafePortletId(servletContextName)) &&
724                 (!portletIds.contains(portletId)))) {
725 
726                 undefinedPortletIds.add(portletId);
727             }
728             else if ((servletContextName == null) &&
729                      (!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 = new PortletCategory(
739                 "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}