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 com.iona.fuse.mb.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.server.handler.ContextHandler;
024    import org.eclipse.jetty.server.session.SessionHandler;
025    import org.eclipse.jetty.servlet.ServletHandler;
026    import org.eclipse.jetty.servlet.ServletHolder;
027    import org.eclipse.jetty.servlet.ServletMapping;
028    
029    public class EmbeddedJettyServer implements org.apache.activemq.Service {
030    
031        private HTTPDiscoveryAgent agent;
032        private Server server;
033        private SelectChannelConnector connector;
034        private DiscoveryRegistryServlet discoveryServlet = new DiscoveryRegistryServlet();
035        
036        public void start() throws Exception {
037            URI uri = new URI(agent.getRegistryURL());
038    
039            server = new Server();
040            ContextHandler contextHandler = new ContextHandler();
041            contextHandler.setContextPath("/");
042            contextHandler.setServer(server);
043    
044            SessionHandler sessionHandler = new SessionHandler();
045            contextHandler.setHandler(sessionHandler);
046    
047            ServletHandler servletHandler = new ServletHandler();
048            sessionHandler.setHandler(servletHandler);
049    
050            ServletHolder holder = new ServletHolder();
051            holder.setName("discoveryServlet");
052            holder.setClassName(DiscoveryRegistryServlet.class.getName());
053            servletHandler.setServlets(new ServletHolder[] {
054                holder
055            });
056    
057            ServletMapping mapping = new ServletMapping();
058            mapping.setServletName("discoveryServlet");
059            mapping.setPathSpec("/*");
060            servletHandler.setServletMappings(new ServletMapping[] {
061                mapping
062            });
063    
064            server.setHandler(contextHandler);
065            server.start();
066            
067            int port = 80;
068            if( uri.getPort() >=0 ) {
069                port = uri.getPort();
070            }
071            
072            connector = new SelectChannelConnector();
073            connector.setPort(port);
074            server.addConnector(connector);
075            connector.start();
076        }
077    
078        public void stop() throws Exception {
079            if( connector!=null ) {
080                connector.stop();
081                connector = null;
082            }
083            if( server!=null ) {
084                server.stop();
085                server = null;
086            }
087        }
088    
089        public HTTPDiscoveryAgent getAgent() {
090            return agent;
091        }
092    
093        public void setAgent(HTTPDiscoveryAgent agent) {
094            this.agent = agent;
095        }
096        
097    
098    }