blob: 0a44ee475df9818123918d2c1cd5fc2e7ad7fd70 [file] [log] [blame]
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +00001/*
Clement Escoffier94e552a2009-03-30 07:40:36 +00002 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +00009 *
Clement Escoffier94e552a2009-03-30 07:40:36 +000010 * http://www.apache.org/licenses/LICENSE-2.0
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +000011 *
Clement Escoffier94e552a2009-03-30 07:40:36 +000012 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +000018 */
19package org.apache.felix.http.jetty;
20
21
Felix Meschberger38200202007-09-28 20:31:39 +000022import java.io.IOException;
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +000023import java.io.InputStream;
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +000024import java.net.URL;
Felix Meschbergerc7ee0152008-03-26 09:24:35 +000025import java.util.Collections;
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +000026import java.util.Dictionary;
27import java.util.Enumeration;
28import java.util.HashMap;
29import java.util.HashSet;
30import java.util.Hashtable;
31import java.util.Map;
32import java.util.Set;
33
34import javax.servlet.RequestDispatcher;
35import javax.servlet.Servlet;
36import javax.servlet.ServletContext;
37import javax.servlet.ServletException;
38
Felix Meschbergerc7ee0152008-03-26 09:24:35 +000039import org.mortbay.jetty.servlet.OsgiResourceHolder;
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +000040import org.mortbay.jetty.servlet.OsgiServletHandler;
41import org.mortbay.jetty.servlet.OsgiServletHolder;
42import org.mortbay.jetty.servlet.ServletHolder;
43import org.osgi.service.http.HttpContext;
44
45
46public class ServletContextGroup implements ServletContext
47{
48 /** global pool of all OSGi HttpContext that have been created */
49 private static Map m_contextMap = new HashMap();
50
51 /** global set of all servlet instances that have been registered */
52 private static Set m_servletSet = new HashSet();
53
54 private OsgiServletHandler m_hdlr = null;
55 private HttpContext m_osgiHttpContext = null;
56 private Hashtable m_attributes = null;
57
58
59 static void initializeStatics()
60 {
61 m_contextMap.clear();
62 m_servletSet.clear();
63 }
64
65
66 static ServletContextGroup getServletContextGroup( OsgiServletHandler hdlr, HttpContext osgiHttpContext )
67 {
68
69 ServletContextGroup grp = ( ServletContextGroup ) m_contextMap.get( osgiHttpContext );
70 if ( grp == null )
71 {
72 grp = new ServletContextGroup( hdlr, osgiHttpContext );
73 m_contextMap.put( osgiHttpContext, grp );
74 }
75
76 return grp;
77 }
78
79
80 static boolean isServletRegistered( Servlet servlet )
81 {
82 return m_servletSet.contains( servlet );
83 }
84
85
86 private ServletContextGroup( OsgiServletHandler hdlr, HttpContext osgiHttpContext )
87 {
88 init( hdlr, osgiHttpContext );
89 }
90
91
92 private void init( OsgiServletHandler hdlr, HttpContext osgiHttpContext )
93 {
94 m_hdlr = hdlr;
95 m_osgiHttpContext = osgiHttpContext;
96
97 m_attributes = new Hashtable();
98
99 m_contextMap.put( m_osgiHttpContext, this );
100 }
101
102
103 private void destroy()
104 {
105 m_contextMap.remove( m_osgiHttpContext );
106 }
107
108
109 public HttpContext getOsgiHttpContext()
110 {
111 return m_osgiHttpContext;
112 }
113
114
115 void addServlet( Servlet servlet, String alias, Dictionary params )
116 {
117 String wAlias = aliasWildcard( alias );
118 ServletHolder holder = new OsgiServletHolder( m_hdlr, servlet, wAlias, this, params );
119 m_hdlr.addOsgiServletHolder( wAlias, holder );
120 Activator.debug( " adding servlet instance: " + servlet );
121 m_servletSet.add( servlet );
122 }
123
124
Felix Meschbergerc7ee0152008-03-26 09:24:35 +0000125 void addResource( String alias, String path )
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +0000126 {
127 String wAlias = aliasWildcard( alias );
Rob Walker48cb2432008-10-13 14:30:43 +0000128 ServletHolder holder = new OsgiResourceHolder( m_hdlr, alias, path, this );
Felix Meschbergerc7ee0152008-03-26 09:24:35 +0000129 m_hdlr.addOsgiServletHolder( wAlias, holder );
130 Activator.debug( " adding resources for " + wAlias + " at: " + path );
131 }
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +0000132
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +0000133
Felix Meschbergerc7ee0152008-03-26 09:24:35 +0000134 void remove( String alias, boolean destroy )
135 {
136 String wAlias = aliasWildcard( alias );
137 ServletHolder holder = m_hdlr.removeOsgiServletHolder( wAlias );
138
139 if ( holder != null )
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +0000140 {
Felix Meschbergerc7ee0152008-03-26 09:24:35 +0000141 try
142 {
143 Servlet servlet = holder.getServlet();
144 if ( servlet != null )
145 {
146 Activator.debug( " removing servlet instance: " + servlet );
147 m_servletSet.remove( servlet );
148
149 if ( destroy )
150 {
151 servlet.destroy();
152 }
153
154 if ( m_servletSet.isEmpty() )
155 {
156 destroy();
157 }
158 }
159 }
160 catch ( ServletException se )
161 {
162 // may only be thrown if servlet in holder is null
163 }
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +0000164 }
165 }
166
167
168 private String aliasWildcard( String alias )
169 {
170 // add wilcard filter at the end of the alias to allow servlet to
171 // get requests which include sub-paths
172 return "/".equals( alias ) ? "/*" : alias + "/*";
173 }
174
175
176 // ServletContext interface for OSGi servlets
177
178 public ServletContext getContext( String contextName )
179 {
180 return m_hdlr.getServletContext().getContext( contextName );
181 }
182
183
184 public int getMajorVersion()
185 {
186 return m_hdlr.getServletContext().getMajorVersion();
187 }
188
189
190 public int getMinorVersion()
191 {
192 return m_hdlr.getServletContext().getMinorVersion();
193 }
194
195
Felix Meschbergerc7ee0152008-03-26 09:24:35 +0000196 public String getContextPath()
197 {
198 return m_hdlr.getServletContext().getContextPath();
199 }
200
201
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +0000202 public String getMimeType( String file )
203 {
204 String type = m_osgiHttpContext.getMimeType( file );
205 if ( type != null )
206 {
207 return type;
208 }
209
210 return m_hdlr.getServletContext().getMimeType( file );
211 }
212
213
214 public String getRealPath( String path )
215 {
Felix Meschberger38200202007-09-28 20:31:39 +0000216 // resources are contained in the bundle and thus are not
217 // available as normal files in the platform filesystem
218 return null;
219 }
220
221
222 public URL getResource( String path )
223 {
224 return m_osgiHttpContext.getResource( path );
225 }
226
227
228 public InputStream getResourceAsStream( String path )
229 {
230 URL res = getResource( path );
231 if ( res != null )
232 {
233 try
234 {
235 return res.openStream();
236 }
237 catch ( IOException ignore )
238 {
239 // might want to log, but actually don't care here
240 }
241 }
242
243 return null;
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +0000244 }
245
246
Felix Meschbergerc7ee0152008-03-26 09:24:35 +0000247 public Set getResourcePaths( String path )
248 {
249 // This is not implemented yet, might want to access the bundle entries
250 return null;
251 }
252
253
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +0000254 public RequestDispatcher getRequestDispatcher( String uri )
255 {
256 return m_hdlr.getServletContext().getRequestDispatcher( uri );
257 }
258
259
Felix Meschbergerc7ee0152008-03-26 09:24:35 +0000260 public RequestDispatcher getNamedDispatcher( String name )
261 {
262 if ( getMinorVersion() >= 2 )
263 {
264 return m_hdlr.getServletContext().getNamedDispatcher( name );
265 }
266
267 return null;
268 }
269
270
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +0000271 public String getServerInfo()
272 {
273 return m_hdlr.getServletContext().getServerInfo();
274 }
275
276
Felix Meschbergerc7ee0152008-03-26 09:24:35 +0000277 public String getServletContextName()
278 {
279 if ( getMinorVersion() >= 3 )
280 {
281 return m_hdlr.getServletContext().getServletContextName();
282 }
283
284 return null;
285 }
286
287
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +0000288 public Servlet getServlet( String servletName ) throws ServletException
289 {
290 return m_hdlr.getServletContext().getServlet( servletName );
291 }
292
293
294 public Enumeration getServletNames()
295 {
296 return m_hdlr.getServletContext().getServletNames();
297 }
298
299
Felix Meschbergerc7ee0152008-03-26 09:24:35 +0000300 public String getInitParameter( String name )
301 {
302 if ( getMinorVersion() >= 2 )
303 {
304 return m_hdlr.getServletContext().getInitParameter( name );
305 }
306
307 return null;
308 }
309
310
311 public Enumeration getInitParameterNames()
312 {
313 if ( getMinorVersion() >= 2 )
314 {
315 return m_hdlr.getServletContext().getInitParameterNames();
316 }
317
318 return Collections.enumeration( Collections.EMPTY_LIST );
319 }
320
321
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +0000322 /* (non-Javadoc)
323 * @see javax.servlet.ServletContext#getServlets()
324 */
325 public Enumeration getServlets()
326 {
327 return m_hdlr.getServletContext().getServlets();
328 }
329
330
331 public void log( Exception exception, String message )
332 {
333 m_hdlr.getServletContext().log( exception, message );
334 }
335
336
337 public void log( String message, Throwable throwable )
338 {
339 m_hdlr.getServletContext().log( message, throwable );
340 }
341
342
343 public void log( String message )
344 {
345 m_hdlr.getServletContext().log( message );
346 }
347
348
349 public void setAttribute( String name, Object value )
350 {
351 m_attributes.put( name, value );
352 }
353
354
355 public Object getAttribute( String name )
356 {
357 return m_attributes.get( name );
358 }
359
360
361 public Enumeration getAttributeNames()
362 {
363 return m_attributes.keys();
364 }
365
366
367 public void removeAttribute( String name )
368 {
369 m_attributes.remove( name );
370 }
371}