KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > core > impl > PortletContextImpl


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17
18  */

19
20 package org.apache.pluto.core.impl;
21
22 import java.net.MalformedURLException JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 import javax.portlet.PortletContext;
27 import javax.servlet.RequestDispatcher JavaDoc;
28
29 import org.apache.pluto.Environment;
30 import org.apache.pluto.core.InternalPortletContext;
31 import org.apache.pluto.om.portlet.PortletApplicationDefinition;
32
33 public class PortletContextImpl implements PortletContext, InternalPortletContext
34 {
35     private PortletApplicationDefinition portletApplicationDefinition;
36     private javax.servlet.ServletContext JavaDoc servletContext;
37
38     public PortletContextImpl(javax.servlet.ServletContext JavaDoc servletContext,
39                               PortletApplicationDefinition portletApplicationDefinition)
40     {
41         this.servletContext = servletContext;
42         this.portletApplicationDefinition = portletApplicationDefinition;
43     }
44
45     // javax.portlet.PortletContext implementation ------------------------------------------------
46
public String JavaDoc getServerInfo()
47     {
48         return Environment.getServerInfo();
49     }
50
51     public javax.portlet.PortletRequestDispatcher getRequestDispatcher(String JavaDoc path)
52     {
53         Map JavaDoc parms = parseQueryParams(path);
54         try {
55             RequestDispatcher JavaDoc rd = servletContext.getRequestDispatcher(path);
56             return rd == null?null:new PortletRequestDispatcherImpl(rd, parms);
57         } catch (Exception JavaDoc e) {
58             // need to catch exception because of tomcat 4.x bug
59
// tomcat throws an exception instead of return null
60
// if the path was not found
61
return null;
62         }
63
64     }
65
66     public javax.portlet.PortletRequestDispatcher getNamedDispatcher(String JavaDoc name)
67     {
68         javax.servlet.RequestDispatcher JavaDoc rd = servletContext.getNamedDispatcher(name);
69         return rd != null ? new PortletRequestDispatcherImpl(rd)
70                           : null;
71     }
72
73     public java.io.InputStream JavaDoc getResourceAsStream(String JavaDoc path)
74     {
75         return servletContext.getResourceAsStream(path);
76     }
77     
78     public int getMajorVersion()
79     {
80         return Environment.getMajorSpecificationVersion();
81     }
82
83     public int getMinorVersion()
84     {
85         return Environment.getMinorSpecificationVersion();
86     }
87
88     public String JavaDoc getMimeType(String JavaDoc file)
89     {
90         return servletContext.getMimeType(file);
91     }
92
93     public String JavaDoc getRealPath(String JavaDoc path)
94     {
95         return servletContext.getRealPath(path);
96     }
97
98     public java.util.Set JavaDoc getResourcePaths(String JavaDoc path)
99     {
100         return servletContext.getResourcePaths(path);
101     }
102
103     public java.net.URL JavaDoc getResource(String JavaDoc path) throws java.net.MalformedURLException JavaDoc
104     {
105         if (path == null || !path.startsWith("/"))
106         {
107             throw new MalformedURLException JavaDoc("path must start with a '/'");
108         }
109         return servletContext.getResource(path);
110     }
111
112     public java.lang.Object JavaDoc getAttribute(java.lang.String JavaDoc name)
113     {
114         if (name == null)
115         {
116             throw new IllegalArgumentException JavaDoc("Attribute name == null");
117         }
118
119         return servletContext.getAttribute(name);
120     }
121
122     public java.util.Enumeration JavaDoc getAttributeNames()
123     {
124         return servletContext.getAttributeNames();
125     }
126
127     public java.lang.String JavaDoc getInitParameter(java.lang.String JavaDoc name)
128     {
129         if (name == null)
130         {
131             throw new IllegalArgumentException JavaDoc("Parameter name == null");
132         }
133
134         return servletContext.getInitParameter(name);
135     }
136
137     public java.util.Enumeration JavaDoc getInitParameterNames()
138     {
139         return servletContext.getInitParameterNames();
140     }
141
142     public void log(java.lang.String JavaDoc msg)
143     {
144         servletContext.log(msg);
145     }
146
147     public void log(java.lang.String JavaDoc message, java.lang.Throwable JavaDoc throwable)
148     {
149         servletContext.log(message, throwable);
150     }
151
152     public void removeAttribute(java.lang.String JavaDoc name)
153     {
154         if (name == null)
155         {
156             throw new IllegalArgumentException JavaDoc("Attribute name == null");
157         }
158
159         servletContext.removeAttribute(name);
160     }
161
162     public void setAttribute(java.lang.String JavaDoc name, java.lang.Object JavaDoc object)
163     {
164         if (name == null)
165         {
166             throw new IllegalArgumentException JavaDoc("Attribute name == null");
167         }
168
169         servletContext.setAttribute(name, object);
170     }
171
172     public String JavaDoc getPortletContextName()
173     {
174         return servletContext.getServletContextName();
175     }
176     // --------------------------------------------------------------------------------------------
177

178     // org.apache.pluto.core.InternalPortletContext implementation --------------------------------
179
public javax.servlet.ServletContext JavaDoc getServletContext()
180     {
181         return servletContext;
182     }
183
184     public PortletApplicationDefinition getInternalPortletApplicationDefinition()
185     {
186         return portletApplicationDefinition;
187     }
188     // --------------------------------------------------------------------------------------------
189

190     private Map JavaDoc parseQueryParams(String JavaDoc path) {
191         Map JavaDoc map = new java.util.HashMap JavaDoc();
192         int idx = path.indexOf("?");
193         if(idx < 0) {
194             return null;
195         }
196         String JavaDoc parms = path.substring(idx+1);
197         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(parms, "&");
198         while(st.hasMoreTokens()) {
199             String JavaDoc pair = st.nextToken();
200             if(pair.indexOf("=")>0) {
201                 String JavaDoc key = pair.substring(0,pair.indexOf("="));
202                 String JavaDoc val = pair.substring(pair.indexOf("=")+1);
203                 map.put(key, new String JavaDoc[] {val});
204             }
205         }
206         return map;
207     }
208 }
209
210
Popular Tags