Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 1 | /* |
Richard S. Hall | 435c20c | 2006-09-28 20:11:35 +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 |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 9 | * |
Richard S. Hall | 435c20c | 2006-09-28 20:11:35 +0000 | [diff] [blame] | 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 11 | * |
Richard S. Hall | 435c20c | 2006-09-28 20:11:35 +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. |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 18 | */ |
| 19 | package org.mortbay.jetty.servlet; |
| 20 | |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 21 | |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 22 | import java.io.IOException; |
| 23 | import java.net.URL; |
Felix Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 24 | |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 25 | import javax.servlet.ServletException; |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 26 | import javax.servlet.http.HttpServletRequest; |
| 27 | import javax.servlet.http.HttpServletResponse; |
| 28 | |
Felix Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 29 | import org.apache.felix.http.jetty.Activator; |
| 30 | import org.mortbay.util.LazyList; |
| 31 | import org.osgi.service.log.LogService; |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 32 | |
| 33 | |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 34 | public class OsgiServletHandler extends ServletHandler |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 35 | { |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 36 | // allow external adding of osgi servlet holder |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 37 | public void addOsgiServletHolder( String pathSpec, ServletHolder holder ) |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 38 | { |
Felix Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 39 | super.addServletWithMapping( holder, pathSpec ); |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | |
Felix Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 43 | public ServletHolder removeOsgiServletHolder( String pathSpec ) |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 44 | { |
Felix Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 45 | 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. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 58 | |
Felix Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 59 | if ( oldMapping == null ) |
| 60 | { |
| 61 | return null; |
| 62 | } |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 63 | |
Felix Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 64 | 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. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | |
| 104 | // override standard handler behaviour to return resource from OSGi |
| 105 | // HttpContext |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 106 | public URL getResource( String uriInContext ) |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 107 | { |
Felix Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 108 | Activator.debug( "OSGI ServletHandler getResource:" + uriInContext ); |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 109 | return null; |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 112 | |
Felix Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 113 | public void handle( String target, HttpServletRequest request, HttpServletResponse response, int type ) |
| 114 | throws IOException, ServletException |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 115 | { |
Felix Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 116 | Activator.debug( "dispatch path = " + target ); |
| 117 | super.handle( target, request, response, type ); |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 118 | } |
Felix Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 119 | |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 120 | } |