1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.ReleaseInfo;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.model.Portlet;
23  import com.liferay.portal.model.PortletApp;
24  
25  import java.io.InputStream;
26  
27  import java.net.MalformedURLException;
28  import java.net.URL;
29  
30  import java.util.Enumeration;
31  import java.util.Set;
32  
33  import javax.portlet.PortletContext;
34  import javax.portlet.PortletRequestDispatcher;
35  
36  import javax.servlet.RequestDispatcher;
37  import javax.servlet.ServletContext;
38  
39  /**
40   * <a href="PortletContextImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   * @author Brett Randall
44   */
45  public class PortletContextImpl implements PortletContext {
46  
47      public PortletContextImpl(Portlet portlet, ServletContext servletContext) {
48          _portlet = portlet;
49          _servletContext = servletContext;
50          _servletContextName = GetterUtil.getString(
51              _servletContext.getServletContextName());
52      }
53  
54      public Object getAttribute(String name) {
55          if (name == null) {
56              throw new IllegalArgumentException();
57          }
58  
59          return _servletContext.getAttribute(name);
60      }
61  
62      public Enumeration<String> getAttributeNames() {
63          return _servletContext.getAttributeNames();
64      }
65  
66      public Enumeration<String> getContainerRuntimeOptions() {
67          return null;
68      }
69  
70      public String getInitParameter(String name) {
71          if (name == null) {
72              throw new IllegalArgumentException();
73          }
74  
75          return _servletContext.getInitParameter(name);
76      }
77  
78      public Enumeration<String> getInitParameterNames() {
79          return _servletContext.getInitParameterNames();
80      }
81  
82      public int getMajorVersion() {
83          return _MAJOR_VERSION;
84      }
85  
86      public String getMimeType(String file) {
87          return _servletContext.getMimeType(file);
88      }
89  
90      public int getMinorVersion() {
91          return _MINOR_VERSION;
92      }
93  
94      public PortletRequestDispatcher getNamedDispatcher(String name) {
95          RequestDispatcher requestDispatcher = null;
96  
97          try {
98              requestDispatcher = _servletContext.getNamedDispatcher(name);
99          }
100         catch (IllegalArgumentException iae) {
101             return null;
102         }
103 
104         if (requestDispatcher != null) {
105             return new PortletRequestDispatcherImpl(
106                 requestDispatcher, true, this);
107         }
108         else {
109             return null;
110         }
111     }
112 
113     public Portlet getPortlet() {
114         return _portlet;
115     }
116 
117     public String getPortletContextName() {
118         return _servletContextName;
119     }
120 
121     public String getRealPath(String path) {
122         return _servletContext.getRealPath(path);
123     }
124 
125     public PortletRequestDispatcher getRequestDispatcher(String path) {
126         RequestDispatcher requestDispatcher = null;
127 
128         try {
129             requestDispatcher = _servletContext.getRequestDispatcher(path);
130         }
131         catch (IllegalArgumentException iae) {
132             return null;
133         }
134 
135         if (requestDispatcher != null) {
136             return new PortletRequestDispatcherImpl(
137                 requestDispatcher, false, this, path);
138         }
139         else {
140             return null;
141         }
142     }
143 
144     public URL getResource(String path) throws MalformedURLException {
145         if ((path == null) || (!path.startsWith(StringPool.SLASH))) {
146             throw new MalformedURLException();
147         }
148 
149         return _servletContext.getResource(path);
150     }
151 
152     public InputStream getResourceAsStream(String path) {
153         return _servletContext.getResourceAsStream(path);
154     }
155 
156     public Set<String> getResourcePaths(String path) {
157         return _servletContext.getResourcePaths(path);
158     }
159 
160     public String getServerInfo() {
161         return ReleaseInfo.getServerInfo();
162     }
163 
164     public ServletContext getServletContext() {
165         return _servletContext;
166     }
167 
168     public boolean isWARFile() {
169         PortletApp portletApp = _portlet.getPortletApp();
170 
171         return portletApp.isWARFile();
172     }
173 
174     public void log(String msg) {
175         if (_log.isInfoEnabled()) {
176             _log.info(msg);
177         }
178     }
179 
180     public void log(String msg, Throwable throwable) {
181         if (_log.isInfoEnabled()) {
182             _log.info(msg, throwable);
183         }
184     }
185 
186     public void removeAttribute(String name) {
187         if (name == null) {
188             throw new IllegalArgumentException();
189         }
190 
191         _servletContext.removeAttribute(name);
192     }
193 
194     public void setAttribute(String name, Object obj) {
195         if (name == null) {
196             throw new IllegalArgumentException();
197         }
198 
199         _servletContext.setAttribute(name, obj);
200     }
201 
202     private static int _MAJOR_VERSION = 2;
203 
204     private static int _MINOR_VERSION = 0;
205 
206     private static Log _log = LogFactoryUtil.getLog(PortletContextImpl.class);
207 
208     private Portlet _portlet;
209     private ServletContext _servletContext;
210     private String _servletContextName;
211 
212 }