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 */
017package org.apache.activemq.transport.discovery.http;
018
019import java.net.URI;
020
021import org.eclipse.jetty.server.Server;
022import org.eclipse.jetty.servlet.ServletContextHandler;
023import org.eclipse.jetty.servlet.ServletHolder;
024
025public class EmbeddedJettyServer implements org.apache.activemq.Service {
026
027    private HTTPDiscoveryAgent agent;
028    private Server server;
029    private DiscoveryRegistryServlet camelServlet = new DiscoveryRegistryServlet();
030    
031    public void start() throws Exception {
032        URI uri = new URI(agent.getRegistryURL());
033
034        int port = 80;
035        if( uri.getPort() >=0 ) {
036            port = uri.getPort();
037        }
038        server = new Server(port);
039        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SECURITY | ServletContextHandler.NO_SESSIONS);
040        
041        context.setContextPath("/");
042        ServletHolder holder = new ServletHolder();
043        holder.setServlet(camelServlet);
044        context.addServlet(holder, "/*");
045        server.setHandler(context);
046        server.start();
047    }
048
049    public void stop() throws Exception {
050        if( server!=null ) {
051            server.stop();
052            server = null;
053        }
054    }
055
056    public HTTPDiscoveryAgent getAgent() {
057        return agent;
058    }
059
060    public void setAgent(HTTPDiscoveryAgent agent) {
061        this.agent = agent;
062    }
063    
064
065}