001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.ReleaseInfo;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.model.Portlet;
023    import com.liferay.portal.model.PortletApp;
024    
025    import java.io.InputStream;
026    
027    import java.net.MalformedURLException;
028    import java.net.URL;
029    
030    import java.util.Enumeration;
031    import java.util.Set;
032    
033    import javax.portlet.PortletContext;
034    import javax.portlet.PortletRequestDispatcher;
035    
036    import javax.servlet.RequestDispatcher;
037    import javax.servlet.ServletContext;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     * @author Brett Randall
042     */
043    public class PortletContextImpl implements PortletContext {
044    
045            public PortletContextImpl(Portlet portlet, ServletContext servletContext) {
046                    _portlet = portlet;
047                    _servletContext = servletContext;
048                    _servletContextName = GetterUtil.getString(
049                            _servletContext.getServletContextName());
050            }
051    
052            public Object getAttribute(String name) {
053                    if (name == null) {
054                            throw new IllegalArgumentException();
055                    }
056    
057                    return _servletContext.getAttribute(name);
058            }
059    
060            public Enumeration<String> getAttributeNames() {
061                    return _servletContext.getAttributeNames();
062            }
063    
064            public Enumeration<String> getContainerRuntimeOptions() {
065                    return null;
066            }
067    
068            public String getInitParameter(String name) {
069                    if (name == null) {
070                            throw new IllegalArgumentException();
071                    }
072    
073                    return _servletContext.getInitParameter(name);
074            }
075    
076            public Enumeration<String> getInitParameterNames() {
077                    return _servletContext.getInitParameterNames();
078            }
079    
080            public int getMajorVersion() {
081                    return _MAJOR_VERSION;
082            }
083    
084            public String getMimeType(String file) {
085                    return _servletContext.getMimeType(file);
086            }
087    
088            public int getMinorVersion() {
089                    return _MINOR_VERSION;
090            }
091    
092            public PortletRequestDispatcher getNamedDispatcher(String name) {
093                    RequestDispatcher requestDispatcher = null;
094    
095                    try {
096                            requestDispatcher = _servletContext.getNamedDispatcher(name);
097                    }
098                    catch (IllegalArgumentException iae) {
099                            return null;
100                    }
101    
102                    if (requestDispatcher != null) {
103                            return new PortletRequestDispatcherImpl(
104                                    requestDispatcher, true, this);
105                    }
106                    else {
107                            return null;
108                    }
109            }
110    
111            public Portlet getPortlet() {
112                    return _portlet;
113            }
114    
115            public String getPortletContextName() {
116                    return _servletContextName;
117            }
118    
119            public String getRealPath(String path) {
120                    return _servletContext.getRealPath(path);
121            }
122    
123            public PortletRequestDispatcher getRequestDispatcher(String path) {
124                    RequestDispatcher requestDispatcher = null;
125    
126                    try {
127                            requestDispatcher = _servletContext.getRequestDispatcher(path);
128                    }
129                    catch (IllegalArgumentException iae) {
130                            return null;
131                    }
132    
133                    if (requestDispatcher != null) {
134                            return new PortletRequestDispatcherImpl(
135                                    requestDispatcher, false, this, path);
136                    }
137                    else {
138                            return null;
139                    }
140            }
141    
142            public URL getResource(String path) throws MalformedURLException {
143                    if ((path == null) || (!path.startsWith(StringPool.SLASH))) {
144                            throw new MalformedURLException();
145                    }
146    
147                    return _servletContext.getResource(path);
148            }
149    
150            public InputStream getResourceAsStream(String path) {
151                    return _servletContext.getResourceAsStream(path);
152            }
153    
154            public Set<String> getResourcePaths(String path) {
155                    return _servletContext.getResourcePaths(path);
156            }
157    
158            public String getServerInfo() {
159                    return ReleaseInfo.getServerInfo();
160            }
161    
162            public ServletContext getServletContext() {
163                    return _servletContext;
164            }
165    
166            public boolean isWARFile() {
167                    PortletApp portletApp = _portlet.getPortletApp();
168    
169                    return portletApp.isWARFile();
170            }
171    
172            public void log(String msg) {
173                    if (_log.isInfoEnabled()) {
174                            _log.info(msg);
175                    }
176            }
177    
178            public void log(String msg, Throwable throwable) {
179                    if (_log.isInfoEnabled()) {
180                            _log.info(msg, throwable);
181                    }
182            }
183    
184            public void removeAttribute(String name) {
185                    if (name == null) {
186                            throw new IllegalArgumentException();
187                    }
188    
189                    _servletContext.removeAttribute(name);
190            }
191    
192            public void setAttribute(String name, Object obj) {
193                    if (name == null) {
194                            throw new IllegalArgumentException();
195                    }
196    
197                    _servletContext.setAttribute(name, obj);
198            }
199    
200            private static int _MAJOR_VERSION = 2;
201    
202            private static int _MINOR_VERSION = 0;
203    
204            private static Log _log = LogFactoryUtil.getLog(PortletContextImpl.class);
205    
206            private Portlet _portlet;
207            private ServletContext _servletContext;
208            private String _servletContextName;
209    
210    }