1   /**
2    * Copyright (c) 2000-2007 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.portlet;
24  
25  import com.liferay.portal.kernel.util.StringMaker;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.model.Portlet;
28  import com.liferay.util.CollectionFactory;
29  
30  import java.util.Map;
31  
32  import javax.portlet.PortletConfig;
33  import javax.portlet.PortletContext;
34  
35  import javax.servlet.ServletContext;
36  
37  /**
38   * <a href="PortletConfigFactory.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class PortletConfigFactory {
44  
45      public static PortletConfig create(Portlet portlet, ServletContext ctx) {
46          return _instance._create(portlet, ctx);
47      }
48  
49      public static void destroy(Portlet portlet) {
50          _instance._destroy(portlet);
51      }
52  
53      private PortletConfigFactory() {
54          _pool = CollectionFactory.getSyncHashMap();
55      }
56  
57      private PortletConfig _create(Portlet portlet, ServletContext ctx) {
58          String poolId = PortletConfigFactory.class.getName();
59  
60          if (!portlet.isWARFile()) {
61              StringMaker sm = new StringMaker();
62  
63              sm.append(poolId);
64              sm.append(StringPool.PERIOD);
65              sm.append(portlet.getCompanyId());
66  
67              poolId = sm.toString();
68          }
69  
70          Map map = (Map)_pool.get(poolId);
71  
72          if (map == null) {
73              map = CollectionFactory.getSyncHashMap();
74  
75              _pool.put(poolId, map);
76          }
77  
78          Map portletConfigs = (Map)map.get(portlet.getRootPortletId());
79  
80          if (portletConfigs == null) {
81              portletConfigs = CollectionFactory.getSyncHashMap();
82  
83              map.put(portlet.getRootPortletId(), portletConfigs);
84          }
85  
86          PortletConfig portletConfig =
87              (PortletConfig)portletConfigs.get(portlet.getPortletId());
88  
89          if (portletConfig == null) {
90              PortletContext portletCtx =
91                  PortletContextFactory.create(portlet, ctx);
92  
93              portletConfig = new PortletConfigImpl(
94                  portlet.getPortletId(), portletCtx, portlet.getInitParams(),
95                  portlet.getResourceBundle(), portlet.getPortletInfo());
96  
97              portletConfigs.put(portlet.getPortletId(), portletConfig);
98          }
99  
100         return portletConfig;
101     }
102 
103     private void _destroy(Portlet portlet) {
104         String poolId = PortletConfigFactory.class.getName();
105 
106         if (!portlet.isWARFile()) {
107             StringMaker sm = new StringMaker();
108 
109             sm.append(poolId);
110             sm.append(".");
111             sm.append(portlet.getCompanyId());
112 
113             poolId = sm.toString();
114         }
115 
116         Map map = (Map)_pool.get(poolId);
117 
118         if (map == null) {
119             return;
120         }
121 
122         map.remove(portlet.getRootPortletId());
123     }
124 
125     private static PortletConfigFactory _instance = new PortletConfigFactory();
126 
127     private Map _pool;
128 
129 }