001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     * 
010     *      http://www.apache.org/licenses/LICENSE-2.0
011     * 
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.oozie.test;
019    
020    import org.mortbay.jetty.Server;
021    import org.mortbay.jetty.servlet.FilterHolder;
022    import org.mortbay.jetty.servlet.ServletHolder;
023    import org.mortbay.jetty.servlet.Context;
024    
025    import java.net.InetAddress;
026    import java.net.ServerSocket;
027    
028    /**
029     * An embedded servlet container for testing purposes. <p/> It provides reduced functionality, it supports only
030     * Servlets. <p/> The servlet container is started in a free port.
031     */
032    public class EmbeddedServletContainer {
033        private Server server;
034        private String host = null;
035        private int port = -1;
036        private String contextPath;
037        Context context;
038    
039        /**
040         * Create a servlet container.
041         *
042         * @param contextPath context path for the servlet, it must not be prefixed or append with "/", for the default
043         * context use ""
044         */
045        public EmbeddedServletContainer(String contextPath) {
046            this.contextPath = contextPath;
047            server = new Server(0);
048            context = new Context();
049            context.setContextPath("/" + contextPath);
050            server.setHandler(context);
051        }
052    
053        /**
054         * Add a servlet to the container.
055         *
056         * @param servletPath servlet path for the servlet, it should be prefixed with '/", it may contain a wild card at
057         * the end.
058         * @param servletClass servlet class
059         */
060        public void addServletEndpoint(String servletPath, Class servletClass) {
061            context.addServlet(new ServletHolder(servletClass), servletPath);
062        }
063    
064        /**
065         * Add a filter to the container.
066         *
067         * @param filterPath path for the filter, it should be prefixed with '/", it may contain a wild card at
068         * the end.
069         * @param filterClass servlet class
070         */
071        public void addFilter(String filterPath, Class filterClass) {
072            context.addFilter(new FilterHolder(filterClass), filterPath, 0);
073        }
074    
075        /**
076         * Start the servlet container. <p/> The container starts on a free port.
077         *
078         * @throws Exception thrown if the container could not start.
079         */
080        public void start() throws Exception {
081            host = InetAddress.getLocalHost().getHostName();
082            ServerSocket ss = new ServerSocket(0);
083            port = ss.getLocalPort();
084            ss.close();
085            server.getConnectors()[0].setHost(host);
086            server.getConnectors()[0].setPort(port);
087            server.start();
088            System.out.println("Running embedded servlet container at: http://" + host + ":" + port);
089        }
090    
091        /**
092         * Return the hostname the servlet container is bound to.
093         *
094         * @return the hostname.
095         */
096        public String getHost() {
097            return host;
098        }
099    
100        /**
101         * Return the port number the servlet container is bound to.
102         *
103         * @return the port number.
104         */
105        public int getPort() {
106            return port;
107        }
108    
109        /**
110         * Return the full URL (including protocol, host, port, context path, servlet path) for the context path.
111         *
112         * @return URL to the context path.
113         */
114        public String getContextURL() {
115            return "http://" + host + ":" + port + "/" + contextPath;
116        }
117    
118        /**
119         * Return the full URL (including protocol, host, port, context path, servlet path) for a servlet path.
120         *
121         * @param servletPath the path which will be expanded to a full URL.
122         * @return URL to the servlet.
123         */
124        public String getServletURL(String servletPath) {
125            String path = servletPath;
126            if (path.endsWith("*")) {
127                path = path.substring(0, path.length() - 1);
128            }
129            return getContextURL() + path;
130        }
131    
132        /**
133         * Stop the servlet container.
134         */
135        public void stop() {
136            try {
137                server.stop();
138            }
139            catch (Exception e) {
140                // ignore exception
141            }
142    
143            try {
144                server.destroy();
145            }
146            catch (Exception e) {
147                // ignore exception
148            }
149    
150            host = null;
151            port = -1;
152        }
153    
154    }