001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.transport.discovery.http;
018    
019    import java.net.URI;
020    
021    import org.eclipse.jetty.server.Server;
022    import org.eclipse.jetty.server.nio.SelectChannelConnector;
023    import org.eclipse.jetty.servlet.ServletContextHandler;
024    import org.eclipse.jetty.servlet.ServletHolder;
025    
026    public class EmbeddedJettyServer implements org.apache.activemq.Service {
027    
028        private HTTPDiscoveryAgent agent;
029        private Server server;
030        private SelectChannelConnector connector;
031        private DiscoveryRegistryServlet camelServlet = new DiscoveryRegistryServlet();
032        
033        public void start() throws Exception {
034            URI uri = new URI(agent.getRegistryURL());
035    
036            server = new Server();
037            ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SECURITY | ServletContextHandler.NO_SESSIONS);
038            
039            context.setContextPath("/");
040            ServletHolder holder = new ServletHolder();
041            holder.setServlet(camelServlet);
042            context.addServlet(holder, "/*");
043            server.setHandler(context);
044            server.start();
045            
046            int port = 80;
047            if( uri.getPort() >=0 ) {
048                port = uri.getPort();
049            }
050            
051            connector = new SelectChannelConnector();
052            connector.setPort(port);
053            server.addConnector(connector);
054            connector.start();
055        }
056    
057        public void stop() throws Exception {
058            if( connector!=null ) {
059                connector.stop();
060                connector = null;
061            }
062            if( server!=null ) {
063                server.stop();
064                server = null;
065            }
066        }
067    
068        public HTTPDiscoveryAgent getAgent() {
069            return agent;
070        }
071    
072        public void setAgent(HTTPDiscoveryAgent agent) {
073            this.agent = agent;
074        }
075        
076    
077    }