1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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  }