blob: 069de1f39dd0b9ead272ed6ebf0781932db58f9d [file] [log] [blame]
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +00001/*
2 * $Url: $
3 * $Id: $
4 *
5 * Copyright 1997-2005 Day Management AG
6 * Barfuesserplatz 6, 4001 Basel, Switzerland
7 * All Rights Reserved.
8 *
9 * This software is the confidential and proprietary information of
10 * Day Management AG, ("Confidential Information"). You shall not
11 * disclose such Confidential Information and shall use it only in
12 * accordance with the terms of the license agreement you entered into
13 * with Day.
14 */
15package org.apache.felix.http.jetty;
16
17
Felix Meschberger38200202007-09-28 20:31:39 +000018import java.io.IOException;
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +000019import java.io.InputStream;
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +000020import java.net.URL;
21import java.util.Dictionary;
22import java.util.Enumeration;
23import java.util.HashMap;
24import java.util.HashSet;
25import java.util.Hashtable;
26import java.util.Map;
27import java.util.Set;
28
29import javax.servlet.RequestDispatcher;
30import javax.servlet.Servlet;
31import javax.servlet.ServletContext;
32import javax.servlet.ServletException;
33
34import org.mortbay.jetty.servlet.OsgiServletHandler;
35import org.mortbay.jetty.servlet.OsgiServletHolder;
36import org.mortbay.jetty.servlet.ServletHolder;
37import org.osgi.service.http.HttpContext;
38
39
40public class ServletContextGroup implements ServletContext
41{
42 /** global pool of all OSGi HttpContext that have been created */
43 private static Map m_contextMap = new HashMap();
44
45 /** global set of all servlet instances that have been registered */
46 private static Set m_servletSet = new HashSet();
47
48 private OsgiServletHandler m_hdlr = null;
49 private HttpContext m_osgiHttpContext = null;
50 private Hashtable m_attributes = null;
51
52
53 static void initializeStatics()
54 {
55 m_contextMap.clear();
56 m_servletSet.clear();
57 }
58
59
60 static ServletContextGroup getServletContextGroup( OsgiServletHandler hdlr, HttpContext osgiHttpContext )
61 {
62
63 ServletContextGroup grp = ( ServletContextGroup ) m_contextMap.get( osgiHttpContext );
64 if ( grp == null )
65 {
66 grp = new ServletContextGroup( hdlr, osgiHttpContext );
67 m_contextMap.put( osgiHttpContext, grp );
68 }
69
70 return grp;
71 }
72
73
74 static boolean isServletRegistered( Servlet servlet )
75 {
76 return m_servletSet.contains( servlet );
77 }
78
79
80 private ServletContextGroup( OsgiServletHandler hdlr, HttpContext osgiHttpContext )
81 {
82 init( hdlr, osgiHttpContext );
83 }
84
85
86 private void init( OsgiServletHandler hdlr, HttpContext osgiHttpContext )
87 {
88 m_hdlr = hdlr;
89 m_osgiHttpContext = osgiHttpContext;
90
91 m_attributes = new Hashtable();
92
93 m_contextMap.put( m_osgiHttpContext, this );
94 }
95
96
97 private void destroy()
98 {
99 m_contextMap.remove( m_osgiHttpContext );
100 }
101
102
103 public HttpContext getOsgiHttpContext()
104 {
105 return m_osgiHttpContext;
106 }
107
108
109 void addServlet( Servlet servlet, String alias, Dictionary params )
110 {
111 String wAlias = aliasWildcard( alias );
112 ServletHolder holder = new OsgiServletHolder( m_hdlr, servlet, wAlias, this, params );
113 m_hdlr.addOsgiServletHolder( wAlias, holder );
114 Activator.debug( " adding servlet instance: " + servlet );
115 m_servletSet.add( servlet );
116 }
117
118
119 void removeServlet( String alias, boolean destroy )
120 {
121 String wAlias = aliasWildcard( alias );
122 OsgiServletHolder holder = m_hdlr.removeOsgiServletHolder( wAlias );
123 Servlet servlet = holder.getOsgiServlet();
124 Activator.debug( " removing servlet instance: " + servlet );
125 m_servletSet.remove( servlet );
126
127 if ( destroy )
128 {
129 servlet.destroy();
130 }
131
132 if ( m_servletSet.isEmpty() )
133 {
134 destroy();
135 }
136 }
137
138
139 private String aliasWildcard( String alias )
140 {
141 // add wilcard filter at the end of the alias to allow servlet to
142 // get requests which include sub-paths
143 return "/".equals( alias ) ? "/*" : alias + "/*";
144 }
145
146
147 // ServletContext interface for OSGi servlets
148
149 public ServletContext getContext( String contextName )
150 {
151 return m_hdlr.getServletContext().getContext( contextName );
152 }
153
154
155 public int getMajorVersion()
156 {
157 return m_hdlr.getServletContext().getMajorVersion();
158 }
159
160
161 public int getMinorVersion()
162 {
163 return m_hdlr.getServletContext().getMinorVersion();
164 }
165
166
167 public String getMimeType( String file )
168 {
169 String type = m_osgiHttpContext.getMimeType( file );
170 if ( type != null )
171 {
172 return type;
173 }
174
175 return m_hdlr.getServletContext().getMimeType( file );
176 }
177
178
179 public String getRealPath( String path )
180 {
Felix Meschberger38200202007-09-28 20:31:39 +0000181 // resources are contained in the bundle and thus are not
182 // available as normal files in the platform filesystem
183 return null;
184 }
185
186
187 public URL getResource( String path )
188 {
189 return m_osgiHttpContext.getResource( path );
190 }
191
192
193 public InputStream getResourceAsStream( String path )
194 {
195 URL res = getResource( path );
196 if ( res != null )
197 {
198 try
199 {
200 return res.openStream();
201 }
202 catch ( IOException ignore )
203 {
204 // might want to log, but actually don't care here
205 }
206 }
207
208 return null;
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +0000209 }
210
211
212 public RequestDispatcher getRequestDispatcher( String uri )
213 {
214 return m_hdlr.getServletContext().getRequestDispatcher( uri );
215 }
216
217
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +0000218 public String getServerInfo()
219 {
220 return m_hdlr.getServletContext().getServerInfo();
221 }
222
223
224 public Servlet getServlet( String servletName ) throws ServletException
225 {
226 return m_hdlr.getServletContext().getServlet( servletName );
227 }
228
229
230 public Enumeration getServletNames()
231 {
232 return m_hdlr.getServletContext().getServletNames();
233 }
234
235
236 /* (non-Javadoc)
237 * @see javax.servlet.ServletContext#getServlets()
238 */
239 public Enumeration getServlets()
240 {
241 return m_hdlr.getServletContext().getServlets();
242 }
243
244
245 public void log( Exception exception, String message )
246 {
247 m_hdlr.getServletContext().log( exception, message );
248 }
249
250
251 public void log( String message, Throwable throwable )
252 {
253 m_hdlr.getServletContext().log( message, throwable );
254 }
255
256
257 public void log( String message )
258 {
259 m_hdlr.getServletContext().log( message );
260 }
261
262
263 public void setAttribute( String name, Object value )
264 {
265 m_attributes.put( name, value );
266 }
267
268
269 public Object getAttribute( String name )
270 {
271 return m_attributes.get( name );
272 }
273
274
275 public Enumeration getAttributeNames()
276 {
277 return m_attributes.keys();
278 }
279
280
281 public void removeAttribute( String name )
282 {
283 m_attributes.remove( name );
284 }
285}