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