Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 1 | /* |
Clement Escoffier | 94e552a | 2009-03-30 07:40:36 +0000 | [diff] [blame] | 2 | * 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 Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 9 | * |
Clement Escoffier | 94e552a | 2009-03-30 07:40:36 +0000 | [diff] [blame] | 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 11 | * |
Clement Escoffier | 94e552a | 2009-03-30 07:40:36 +0000 | [diff] [blame] | 12 | * 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 Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 18 | */ |
| 19 | package org.apache.felix.http.jetty; |
| 20 | |
| 21 | |
Felix Meschberger | 3820020 | 2007-09-28 20:31:39 +0000 | [diff] [blame] | 22 | import java.io.IOException; |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 23 | import java.io.InputStream; |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 24 | import java.net.URL; |
Felix Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 25 | import java.util.Collections; |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 26 | import java.util.Dictionary; |
| 27 | import java.util.Enumeration; |
| 28 | import java.util.HashMap; |
| 29 | import java.util.HashSet; |
| 30 | import java.util.Hashtable; |
| 31 | import java.util.Map; |
| 32 | import java.util.Set; |
| 33 | |
| 34 | import javax.servlet.RequestDispatcher; |
| 35 | import javax.servlet.Servlet; |
| 36 | import javax.servlet.ServletContext; |
| 37 | import javax.servlet.ServletException; |
| 38 | |
Felix Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 39 | import org.mortbay.jetty.servlet.OsgiResourceHolder; |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 40 | import org.mortbay.jetty.servlet.OsgiServletHandler; |
| 41 | import org.mortbay.jetty.servlet.OsgiServletHolder; |
| 42 | import org.mortbay.jetty.servlet.ServletHolder; |
| 43 | import org.osgi.service.http.HttpContext; |
| 44 | |
| 45 | |
| 46 | public 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 Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 125 | void addResource( String alias, String path ) |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 126 | { |
| 127 | String wAlias = aliasWildcard( alias ); |
Rob Walker | 48cb243 | 2008-10-13 14:30:43 +0000 | [diff] [blame] | 128 | ServletHolder holder = new OsgiResourceHolder( m_hdlr, alias, path, this ); |
Felix Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 129 | m_hdlr.addOsgiServletHolder( wAlias, holder ); |
| 130 | Activator.debug( " adding resources for " + wAlias + " at: " + path ); |
| 131 | } |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 132 | |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 133 | |
Felix Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 134 | 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 Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 140 | { |
Felix Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 141 | 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 Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 164 | } |
| 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 Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 196 | public String getContextPath() |
| 197 | { |
| 198 | return m_hdlr.getServletContext().getContextPath(); |
| 199 | } |
| 200 | |
| 201 | |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 202 | 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 Meschberger | 3820020 | 2007-09-28 20:31:39 +0000 | [diff] [blame] | 216 | // 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 Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | |
Felix Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 247 | 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 Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 254 | public RequestDispatcher getRequestDispatcher( String uri ) |
| 255 | { |
| 256 | return m_hdlr.getServletContext().getRequestDispatcher( uri ); |
| 257 | } |
| 258 | |
| 259 | |
Felix Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 260 | 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 Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 271 | public String getServerInfo() |
| 272 | { |
| 273 | return m_hdlr.getServletContext().getServerInfo(); |
| 274 | } |
| 275 | |
| 276 | |
Felix Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 277 | public String getServletContextName() |
| 278 | { |
| 279 | if ( getMinorVersion() >= 3 ) |
| 280 | { |
| 281 | return m_hdlr.getServletContext().getServletContextName(); |
| 282 | } |
| 283 | |
| 284 | return null; |
| 285 | } |
| 286 | |
| 287 | |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 288 | 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 Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 300 | 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 Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 322 | /* (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 | } |