Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 15 | package org.apache.felix.http.jetty; |
| 16 | |
| 17 | |
| 18 | import java.io.InputStream; |
| 19 | import java.net.MalformedURLException; |
| 20 | import java.net.URL; |
| 21 | import java.util.Dictionary; |
| 22 | import java.util.Enumeration; |
| 23 | import java.util.HashMap; |
| 24 | import java.util.HashSet; |
| 25 | import java.util.Hashtable; |
| 26 | import java.util.Map; |
| 27 | import java.util.Set; |
| 28 | |
| 29 | import javax.servlet.RequestDispatcher; |
| 30 | import javax.servlet.Servlet; |
| 31 | import javax.servlet.ServletContext; |
| 32 | import javax.servlet.ServletException; |
| 33 | |
| 34 | import org.mortbay.jetty.servlet.OsgiServletHandler; |
| 35 | import org.mortbay.jetty.servlet.OsgiServletHolder; |
| 36 | import org.mortbay.jetty.servlet.ServletHolder; |
| 37 | import org.osgi.service.http.HttpContext; |
| 38 | |
| 39 | |
| 40 | public 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 | { |
| 181 | return m_hdlr.getServletContext().getRealPath( path ); |
| 182 | } |
| 183 | |
| 184 | |
| 185 | public RequestDispatcher getRequestDispatcher( String uri ) |
| 186 | { |
| 187 | return m_hdlr.getServletContext().getRequestDispatcher( uri ); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | public URL getResource( String path ) throws MalformedURLException |
| 192 | { |
| 193 | return m_hdlr.getServletContext().getResource( path ); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | public InputStream getResourceAsStream( String path ) |
| 198 | { |
| 199 | return m_hdlr.getServletContext().getResourceAsStream( path ); |
| 200 | } |
| 201 | |
| 202 | |
| 203 | public String getServerInfo() |
| 204 | { |
| 205 | return m_hdlr.getServletContext().getServerInfo(); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | public Servlet getServlet( String servletName ) throws ServletException |
| 210 | { |
| 211 | return m_hdlr.getServletContext().getServlet( servletName ); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | public Enumeration getServletNames() |
| 216 | { |
| 217 | return m_hdlr.getServletContext().getServletNames(); |
| 218 | } |
| 219 | |
| 220 | |
| 221 | /* (non-Javadoc) |
| 222 | * @see javax.servlet.ServletContext#getServlets() |
| 223 | */ |
| 224 | public Enumeration getServlets() |
| 225 | { |
| 226 | return m_hdlr.getServletContext().getServlets(); |
| 227 | } |
| 228 | |
| 229 | |
| 230 | public void log( Exception exception, String message ) |
| 231 | { |
| 232 | m_hdlr.getServletContext().log( exception, message ); |
| 233 | } |
| 234 | |
| 235 | |
| 236 | public void log( String message, Throwable throwable ) |
| 237 | { |
| 238 | m_hdlr.getServletContext().log( message, throwable ); |
| 239 | } |
| 240 | |
| 241 | |
| 242 | public void log( String message ) |
| 243 | { |
| 244 | m_hdlr.getServletContext().log( message ); |
| 245 | } |
| 246 | |
| 247 | |
| 248 | public void setAttribute( String name, Object value ) |
| 249 | { |
| 250 | m_attributes.put( name, value ); |
| 251 | } |
| 252 | |
| 253 | |
| 254 | public Object getAttribute( String name ) |
| 255 | { |
| 256 | return m_attributes.get( name ); |
| 257 | } |
| 258 | |
| 259 | |
| 260 | public Enumeration getAttributeNames() |
| 261 | { |
| 262 | return m_attributes.keys(); |
| 263 | } |
| 264 | |
| 265 | |
| 266 | public void removeAttribute( String name ) |
| 267 | { |
| 268 | m_attributes.remove( name ); |
| 269 | } |
| 270 | } |