blob: beaae87bbbbfcf58d5443fffeef72467a17f0506 [file] [log] [blame]
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +00001/*
Richard S. Hall435c20c2006-09-28 20:11:35 +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
Richard S. Hallfe8e5602006-04-19 15:23:22 +00009 *
Richard S. Hall435c20c2006-09-28 20:11:35 +000010 * http://www.apache.org/licenses/LICENSE-2.0
Richard S. Hallfe8e5602006-04-19 15:23:22 +000011 *
Richard S. Hall435c20c2006-09-28 20:11:35 +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.
Richard S. Hallfe8e5602006-04-19 15:23:22 +000018 */
19package org.mortbay.jetty.servlet;
20
Richard S. Hallfe8e5602006-04-19 15:23:22 +000021
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +000022import java.io.IOException;
23import java.net.URL;
Felix Meschbergerc7ee0152008-03-26 09:24:35 +000024
Richard S. Hallfe8e5602006-04-19 15:23:22 +000025import javax.servlet.ServletException;
Richard S. Hallfe8e5602006-04-19 15:23:22 +000026import javax.servlet.http.HttpServletRequest;
27import javax.servlet.http.HttpServletResponse;
28
Felix Meschbergerc7ee0152008-03-26 09:24:35 +000029import org.apache.felix.http.jetty.Activator;
30import org.mortbay.util.LazyList;
31import org.osgi.service.log.LogService;
Richard S. Hallfe8e5602006-04-19 15:23:22 +000032
33
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +000034public class OsgiServletHandler extends ServletHandler
Richard S. Hallfe8e5602006-04-19 15:23:22 +000035{
Richard S. Hallfe8e5602006-04-19 15:23:22 +000036 // allow external adding of osgi servlet holder
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +000037 public void addOsgiServletHolder( String pathSpec, ServletHolder holder )
Richard S. Hallfe8e5602006-04-19 15:23:22 +000038 {
Felix Meschbergerc7ee0152008-03-26 09:24:35 +000039 super.addServletWithMapping( holder, pathSpec );
Richard S. Hallfe8e5602006-04-19 15:23:22 +000040 }
41
42
Felix Meschbergerc7ee0152008-03-26 09:24:35 +000043 public ServletHolder removeOsgiServletHolder( String pathSpec )
Richard S. Hallfe8e5602006-04-19 15:23:22 +000044 {
Felix Meschbergerc7ee0152008-03-26 09:24:35 +000045 ServletMapping oldMapping = null;
46 ServletMapping[] mappings = getServletMappings();
47 for ( int i = 0; i < mappings.length && oldMapping == null; i++ )
48 {
49 String[] pathSpecs = mappings[i].getPathSpecs();
50 for ( int j = 0; j < pathSpecs.length && oldMapping == null; j++ )
51 {
52 if ( pathSpec.equals( pathSpecs[j] ) )
53 {
54 oldMapping = mappings[i];
55 }
56 }
57 }
Richard S. Hallfe8e5602006-04-19 15:23:22 +000058
Felix Meschbergerc7ee0152008-03-26 09:24:35 +000059 if ( oldMapping == null )
60 {
61 return null;
62 }
Richard S. Hallfe8e5602006-04-19 15:23:22 +000063
Felix Meschbergerc7ee0152008-03-26 09:24:35 +000064 ServletHolder[] holders = getServlets();
65 if ( holders != null )
66 {
67 holders = ( ServletHolder[] ) holders.clone();
68 }
69
70 ServletHolder oldHolder = null;
71 for ( int i = 0; i < holders.length; i++ )
72 {
73 if ( oldMapping.getServletName().equals( holders[i].getName() ) )
74 {
75 oldHolder = holders[i];
76 }
77 }
78 if ( oldHolder == null )
79 {
80 return null;
81 }
82
83 try
84 {
85 setServlets( ( ServletHolder[] ) LazyList.removeFromArray( holders, oldHolder ) );
86 setServletMappings( ( ServletMapping[] ) LazyList.removeFromArray( mappings, oldMapping ) );
87
88 if (oldHolder.isStarted() && isStopped()) {
89 oldHolder.stop();
90 }
91
92 return ( ServletHolder ) oldHolder;
93 }
94 catch ( Exception e )
95 {
96 setServlets( holders );
97 if ( e instanceof RuntimeException )
98 throw ( RuntimeException ) e;
99 throw new RuntimeException( e );
100 }
Richard S. Hallfe8e5602006-04-19 15:23:22 +0000101 }
102
103
104 // override standard handler behaviour to return resource from OSGi
105 // HttpContext
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +0000106 public URL getResource( String uriInContext )
Richard S. Hallfe8e5602006-04-19 15:23:22 +0000107 {
Felix Meschbergerc7ee0152008-03-26 09:24:35 +0000108 Activator.debug( "OSGI ServletHandler getResource:" + uriInContext );
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +0000109 return null;
Richard S. Hallfe8e5602006-04-19 15:23:22 +0000110 }
111
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +0000112
Felix Meschbergerc7ee0152008-03-26 09:24:35 +0000113 public void handle( String target, HttpServletRequest request, HttpServletResponse response, int type )
114 throws IOException, ServletException
Richard S. Hallfe8e5602006-04-19 15:23:22 +0000115 {
Felix Meschbergerc7ee0152008-03-26 09:24:35 +0000116 Activator.debug( "dispatch path = " + target );
117 super.handle( target, request, response, type );
Richard S. Hallfe8e5602006-04-19 15:23:22 +0000118 }
Felix Meschbergerc7ee0152008-03-26 09:24:35 +0000119
Richard S. Hallfe8e5602006-04-19 15:23:22 +0000120}