001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet;
022:
023: import com.liferay.portal.model.Portlet;
024: import com.liferay.portal.model.impl.PortletImpl;
025: import com.liferay.portal.service.PortletLocalServiceUtil;
026: import com.liferay.portal.servlet.PortletContextPool;
027: import com.liferay.portal.servlet.PortletContextWrapper;
028: import com.liferay.util.CollectionFactory;
029:
030: import java.util.Iterator;
031: import java.util.Map;
032:
033: import javax.portlet.PortletConfig;
034: import javax.portlet.PortletContext;
035: import javax.portlet.PortletException;
036: import javax.portlet.UnavailableException;
037:
038: import javax.servlet.ServletContext;
039:
040: /**
041: * <a href="PortletInstanceFactory.java.html"><b><i>View Source</i></b></a>
042: *
043: * @author Brian Wing Shun Chan
044: *
045: */
046: public class PortletInstanceFactory {
047:
048: public static CachePortlet create(Portlet portlet,
049: ServletContext ctx) throws PortletException {
050:
051: return _instance._create(portlet, ctx);
052: }
053:
054: public static void destroy(Portlet portlet) {
055: _instance._destroy(portlet);
056: }
057:
058: private PortletInstanceFactory() {
059: _pool = CollectionFactory.getSyncHashMap();
060: }
061:
062: private CachePortlet _create(Portlet portlet, ServletContext ctx)
063: throws PortletException {
064:
065: boolean instanceable = false;
066:
067: if ((portlet.isInstanceable())
068: && (PortletImpl.getInstanceId(portlet.getPortletId()) != null)) {
069:
070: instanceable = true;
071: }
072:
073: Map portletInstances = (Map) _pool.get(portlet
074: .getRootPortletId());
075:
076: if (portletInstances == null) {
077: portletInstances = CollectionFactory.getSyncHashMap();
078:
079: _pool.put(portlet.getRootPortletId(), portletInstances);
080: }
081:
082: CachePortlet instanceCachePortletInstance = null;
083:
084: if (instanceable) {
085: instanceCachePortletInstance = (CachePortlet) portletInstances
086: .get(portlet.getPortletId());
087: }
088:
089: CachePortlet rootCachePortletInstance = null;
090:
091: if (instanceCachePortletInstance == null) {
092: rootCachePortletInstance = (CachePortlet) portletInstances
093: .get(portlet.getRootPortletId());
094:
095: if (rootCachePortletInstance == null) {
096: PortletConfig portletConfig = PortletConfigFactory
097: .create(portlet, ctx);
098:
099: if (portlet.isWARFile()) {
100: PortletContextWrapper pcw = PortletContextPool
101: .get(portlet.getRootPortletId());
102:
103: rootCachePortletInstance = _init(portlet,
104: portletConfig, pcw.getPortletInstance());
105: } else {
106: rootCachePortletInstance = _init(portlet,
107: portletConfig);
108: }
109:
110: portletInstances.put(portlet.getRootPortletId(),
111: rootCachePortletInstance);
112: }
113: }
114:
115: if (instanceable) {
116: if (instanceCachePortletInstance == null) {
117: javax.portlet.Portlet portletInstance = rootCachePortletInstance
118: .getPortletInstance();
119:
120: PortletConfig portletConfig = PortletConfigFactory
121: .create(portlet, ctx);
122:
123: PortletContext portletCtx = portletConfig
124: .getPortletContext();
125: Integer expCache = rootCachePortletInstance
126: .getExpCache();
127: boolean facesPortlet = rootCachePortletInstance
128: .isFacesPortlet();
129: boolean strutsPortlet = rootCachePortletInstance
130: .isStrutsPortlet();
131: boolean strutsBridgePortlet = rootCachePortletInstance
132: .isStrutsBridgePortlet();
133:
134: instanceCachePortletInstance = new CachePortlet(
135: portletInstance, portletConfig, portletCtx,
136: expCache, facesPortlet, strutsPortlet,
137: strutsBridgePortlet);
138:
139: portletInstances.put(portlet.getPortletId(),
140: instanceCachePortletInstance);
141: }
142: } else {
143: if (rootCachePortletInstance != null) {
144: instanceCachePortletInstance = rootCachePortletInstance;
145: }
146: }
147:
148: return instanceCachePortletInstance;
149: }
150:
151: private void _destroy(Portlet portlet) {
152: Map portletInstances = (Map) _pool.get(portlet
153: .getRootPortletId());
154:
155: if (portletInstances == null) {
156: return;
157: }
158:
159: Iterator itr = portletInstances.entrySet().iterator();
160:
161: while (itr.hasNext()) {
162: Map.Entry entry = (Map.Entry) itr.next();
163:
164: String portletId = (String) entry.getKey();
165: CachePortlet cachePortletInstance = (CachePortlet) entry
166: .getValue();
167:
168: if (PortletImpl.getInstanceId(portletId) == null) {
169: cachePortletInstance.destroy();
170:
171: break;
172: }
173: }
174:
175: _pool.remove(portlet.getRootPortletId());
176:
177: if (portlet.isWARFile()) {
178: PortletContextWrapper pcw = (PortletContextWrapper) PortletContextPool
179: .get(portlet.getRootPortletId());
180:
181: pcw.removePortletInstance();
182: }
183:
184: PortletConfigFactory.destroy(portlet);
185: PortletContextFactory.destroy(portlet);
186:
187: PortletLocalServiceUtil.destroyPortlet(portlet);
188: }
189:
190: private CachePortlet _init(Portlet portlet,
191: PortletConfig portletConfig) throws PortletException {
192:
193: return _init(portlet, portletConfig, null);
194: }
195:
196: private CachePortlet _init(Portlet portlet,
197: PortletConfig portletConfig,
198: javax.portlet.Portlet portletInstance)
199: throws PortletException {
200:
201: CachePortlet cachePortlet = null;
202:
203: try {
204: if (portletInstance == null) {
205: portletInstance = (javax.portlet.Portlet) Class
206: .forName(portlet.getPortletClass())
207: .newInstance();
208: }
209:
210: cachePortlet = new CachePortlet(portletInstance,
211: portletConfig.getPortletContext(), portlet
212: .getExpCache());
213:
214: cachePortlet.init(portletConfig);
215: } catch (ClassNotFoundException cnofe) {
216: throw new UnavailableException(cnofe.getMessage());
217: } catch (InstantiationException ie) {
218: throw new UnavailableException(ie.getMessage());
219: } catch (IllegalAccessException iae) {
220: throw new UnavailableException(iae.getMessage());
221: }
222:
223: return cachePortlet;
224: }
225:
226: private static PortletInstanceFactory _instance = new PortletInstanceFactory();
227:
228: private Map _pool;
229:
230: }
|