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.servlet.PortletContextPool;
025: import com.liferay.portal.servlet.PortletContextWrapper;
026: import com.liferay.portal.velocity.VelocityContextPool;
027: import com.liferay.util.CollectionFactory;
028:
029: import java.util.Iterator;
030: import java.util.Map;
031:
032: import javax.portlet.PortletContext;
033:
034: import javax.servlet.ServletContext;
035:
036: /**
037: * <a href="PortletContextFactory.java.html"><b><i>View Source</i></b></a>
038: *
039: * @author Brian Wing Shun Chan
040: *
041: */
042: public class PortletContextFactory {
043:
044: public static PortletContext create(Portlet portlet,
045: ServletContext ctx) {
046: return _instance._create(portlet, ctx);
047: }
048:
049: public static void destroy(Portlet portlet) {
050: _instance._destroy(portlet);
051: }
052:
053: private PortletContextFactory() {
054: _pool = CollectionFactory.getSyncHashMap();
055: }
056:
057: private PortletContext _create(Portlet portlet, ServletContext ctx) {
058: Map portletContexts = (Map) _pool.get(portlet
059: .getRootPortletId());
060:
061: if (portletContexts == null) {
062: portletContexts = CollectionFactory.getSyncHashMap();
063:
064: _pool.put(portlet.getRootPortletId(), portletContexts);
065: }
066:
067: PortletContext portletContext = (PortletContext) portletContexts
068: .get(portlet.getPortletId());
069:
070: if (portletContext == null) {
071: if (portlet.isWARFile()) {
072: PortletContextWrapper pcw = PortletContextPool
073: .get(portlet.getRootPortletId());
074:
075: //String mainPath = (String)ctx.getAttribute(WebKeys.MAIN_PATH);
076:
077: ctx = pcw.getServletContext();
078:
079: // Context path for the portal must be passed to individual
080: // portlets
081:
082: //ctx.setAttribute(WebKeys.MAIN_PATH, mainPath);
083: }
084:
085: portletContext = new PortletContextImpl(portlet, ctx);
086:
087: VelocityContextPool.put(portletContext
088: .getPortletContextName(), ctx);
089:
090: portletContexts.put(portlet.getPortletId(), portletContext);
091: }
092:
093: return portletContext;
094: }
095:
096: private void _destroy(Portlet portlet) {
097: Map portletContexts = (Map) _pool.remove(portlet
098: .getRootPortletId());
099:
100: if (portletContexts == null) {
101: return;
102: }
103:
104: Iterator itr = portletContexts.entrySet().iterator();
105:
106: if (itr.hasNext()) {
107: Map.Entry entry = (Map.Entry) itr.next();
108:
109: PortletContext portletContext = (PortletContext) entry
110: .getValue();
111:
112: VelocityContextPool.remove(portletContext
113: .getPortletContextName());
114: }
115: }
116:
117: private static PortletContextFactory _instance = new PortletContextFactory();
118:
119: private Map _pool;
120:
121: }
|