1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet;
16  
17  import com.liferay.portal.model.Portlet;
18  
19  import java.util.Map;
20  import java.util.concurrent.ConcurrentHashMap;
21  
22  import javax.portlet.PortletConfig;
23  import javax.portlet.PortletContext;
24  
25  import javax.servlet.ServletContext;
26  
27  /**
28   * <a href="PortletConfigFactory.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class PortletConfigFactory {
33  
34      public static PortletConfig create(
35          Portlet portlet, ServletContext servletContext) {
36  
37          return _instance._create(portlet, servletContext);
38      }
39  
40      public static void destroy(Portlet portlet) {
41          _instance._destroy(portlet);
42      }
43  
44      private PortletConfigFactory() {
45          _pool = new ConcurrentHashMap<String, Map<String, PortletConfig>>();
46      }
47  
48      private PortletConfig _create(
49          Portlet portlet, ServletContext servletContext) {
50  
51          Map<String, PortletConfig> portletConfigs =
52              _pool.get(portlet.getRootPortletId());
53  
54          if (portletConfigs == null) {
55              portletConfigs = new ConcurrentHashMap<String, PortletConfig>();
56  
57              _pool.put(portlet.getRootPortletId(), portletConfigs);
58          }
59  
60          PortletConfig portletConfig = portletConfigs.get(
61              portlet.getPortletId());
62  
63          if (portletConfig == null) {
64              PortletContext portletContext =
65                  PortletContextFactory.create(portlet, servletContext);
66  
67              portletConfig = new PortletConfigImpl(portlet, portletContext);
68  
69              portletConfigs.put(portlet.getPortletId(), portletConfig);
70          }
71  
72          return portletConfig;
73      }
74  
75      private void _destroy(Portlet portlet) {
76          _pool.remove(portlet.getRootPortletId());
77      }
78  
79      private static PortletConfigFactory _instance = new PortletConfigFactory();
80  
81      private Map<String, Map<String, PortletConfig>> _pool;
82  
83  }