| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 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 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 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 |
|
|
| 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 |
|
|
| 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 |
|
|
| 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 |
|
} |