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.activemq.transport.discovery.http; 18 19 import java.net.URI; 20 21 import org.mortbay.jetty.Server; 22 import org.mortbay.jetty.nio.SelectChannelConnector; 23 import org.mortbay.jetty.servlet.Context; 24 import org.mortbay.jetty.servlet.ServletHolder; 25 26 public class EmbeddedJettyServer implements org.apache.activemq.Service { 27 28 private HTTPDiscoveryAgent agent; 29 private Server server; 30 private SelectChannelConnector connector; 31 private DiscoveryRegistryServlet camelServlet = new DiscoveryRegistryServlet(); 32 33 public void start() throws Exception { 34 URI uri = new URI(agent.getRegistryURL()); 35 36 server = new Server(); 37 Context context = new Context(Context.NO_SECURITY | Context.NO_SESSIONS); 38 39 context.setContextPath("/"); 40 ServletHolder holder = new ServletHolder(); 41 holder.setServlet(camelServlet); 42 context.addServlet(holder, "/*"); 43 server.setHandler(context); 44 server.start(); 45 46 int port = 80; 47 if( uri.getPort() >=0 ) { 48 port = uri.getPort(); 49 } 50 51 connector = new SelectChannelConnector(); 52 connector.setPort(port); 53 server.addConnector(connector); 54 connector.start(); 55 } 56 57 public void stop() throws Exception { 58 if( connector!=null ) { 59 connector.stop(); 60 connector = null; 61 } 62 if( server!=null ) { 63 server.stop(); 64 server = null; 65 } 66 } 67 68 public HTTPDiscoveryAgent getAgent() { 69 return agent; 70 } 71 72 public void setAgent(HTTPDiscoveryAgent agent) { 73 this.agent = agent; 74 } 75 76 77 }