Coverage Report - org.apache.camel.component.http.JettyHttpComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
JettyHttpComponent
83% 
80% 
0
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.component.http;
 19  
 
 20  
 import java.util.HashMap;
 21  
 
 22  
 import org.mortbay.jetty.Connector;
 23  
 import org.mortbay.jetty.Server;
 24  
 import org.mortbay.jetty.nio.SelectChannelConnector;
 25  
 import org.mortbay.jetty.security.SslSocketConnector;
 26  
 import org.mortbay.jetty.servlet.Context;
 27  
 import org.mortbay.jetty.servlet.ServletHolder;
 28  
 
 29  
 /**
 30  
  * An HttpComponent which starts an embedded Jetty for to handle consuming from
 31  
  * http endpoints.
 32  
  * 
 33  
  * @version $Revision: 525142 $
 34  
  */
 35  2
 public class JettyHttpComponent extends HttpComponent {
 36  
         
 37  
         Server server;
 38  
         
 39  
         class ConnectorRef {
 40  
                 Connector connector;
 41  2
                 int refCount = 0;
 42  2
                 public ConnectorRef(Connector connector) {
 43  2
                         this.connector=connector;
 44  2
                         increment();
 45  2
                 }
 46  
                 public int increment() {
 47  2
                         return ++refCount;
 48  
                 }
 49  
                 public int decrement() {
 50  0
                         return --refCount;
 51  
                 }
 52  
         }
 53  
         
 54  2
         final HashMap<String, ConnectorRef> connectors = new HashMap<String, ConnectorRef>();
 55  
         
 56  
         
 57  
         
 58  
         
 59  
         @Override
 60  
         protected void doStart() throws Exception {
 61  2
                 server = createServer();
 62  2
                 super.doStart();
 63  2
         }
 64  
 
 65  
         private Server createServer() throws Exception {
 66  2
                 setCamelServlet(new CamelServlet());
 67  
                 
 68  2
                 Server server = new Server();
 69  2
                 Context context = new Context(Context.NO_SECURITY|Context.NO_SESSIONS);
 70  
                         
 71  2
                 context.setContextPath("/");
 72  2
                 ServletHolder holder = new ServletHolder();
 73  2
                 holder.setServlet(getCamelServlet());
 74  2
                 context.addServlet(holder, "/*");                
 75  2
                 server.setHandler(context);
 76  
                         
 77  2
                 server.start();
 78  2
                 return server;
 79  
         }
 80  
 
 81  
         @Override
 82  
         protected void doStop() throws Exception {
 83  2
                 for (ConnectorRef connectorRef : connectors.values()) {
 84  0
                         connectorRef.connector.stop();
 85  0
                 }
 86  2
                 connectors.clear();
 87  
                 
 88  2
                 server.stop();
 89  2
                 super.doStop();
 90  2
         }
 91  
 
 92  
         @Override
 93  
         public void connect(HttpConsumer consumer) throws Exception {
 94  
                 
 95  
                 // Make sure that there is a connector for the requested endpoint.
 96  2
                 HttpEndpoint endpoint = (HttpEndpoint) consumer.getEndpoint();
 97  2
                 String connectorKey = endpoint.getProtocol()+":"+endpoint.getPort();
 98  
                 
 99  2
                 synchronized(connectors) {
 100  2
                         ConnectorRef connectorRef = connectors.get(connectorKey);
 101  2
                         if( connectorRef == null ) {
 102  
                                 Connector connector;
 103  2
                                 if( "https".equals(endpoint.getProtocol()) ) {
 104  0
                                         connector = new SslSocketConnector();
 105  0
                                 } else {
 106  2
                                         connector = new SelectChannelConnector();
 107  
                                 }
 108  2
                                 connector.setPort(endpoint.getPort());
 109  2
                                 server.addConnector(connector);
 110  2
                                 connector.start();
 111  2
                                 connectorRef = new ConnectorRef(connector);
 112  2
                         } else {
 113  
                                 // ref track the connector
 114  0
                                 connectorRef.increment();
 115  
                         }
 116  2
                 }
 117  
                 
 118  2
                 super.connect(consumer);
 119  2
         }
 120  
         
 121  
         @Override
 122  
         public void disconnect(HttpConsumer consumer) throws Exception {
 123  2
                 super.disconnect(consumer);
 124  
                 
 125  
                 // If the connector is not needed anymore.. then stop it.
 126  2
                 HttpEndpoint endpoint = (HttpEndpoint) consumer.getEndpoint();
 127  2
                 String connectorKey = endpoint.getProtocol()+":"+endpoint.getPort();
 128  
                 
 129  2
                 synchronized(connectors) {
 130  2
                         ConnectorRef connectorRef = connectors.get(connectorKey);
 131  2
                         if( connectorRef != null ) {
 132  0
                                 if( connectorRef.decrement() == 0 ) {
 133  0
                                         server.removeConnector(connectorRef.connector);
 134  0
                                         connectorRef.connector.stop();
 135  0
                                         connectors.remove(connectorKey);
 136  
                                 }
 137  
                         }
 138  2
                 }
 139  2
         }
 140  
 }