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,
013     *   software distributed under the License is distributed on an
014     *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *   KIND, either express or implied.  See the License for the
016     *   specific language governing permissions and limitations
017     *   under the License.
018     *
019     */
020    
021    package org.apache.directory.server.integration.http;
022    
023    
024    import java.io.FileInputStream;
025    import java.util.ArrayList;
026    import java.util.List;
027    import java.util.Set;
028    
029    import org.apache.directory.server.i18n.I18n;
030    import org.mortbay.jetty.Connector;
031    import org.mortbay.jetty.Handler;
032    import org.mortbay.jetty.Server;
033    import org.mortbay.jetty.nio.SelectChannelConnector;
034    import org.mortbay.jetty.webapp.WebAppContext;
035    import org.mortbay.xml.XmlConfiguration;
036    import org.slf4j.Logger;
037    import org.slf4j.LoggerFactory;
038    
039    
040    /**
041     * Class to start the jetty http server
042     * 
043     * @org.apache.xbean.XBean
044     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
045     * @version $Rev$, $Date$
046     */
047    public class HttpServer
048    {
049    
050        /** the jetty http server instance */
051        private Server jetty;
052    
053        /** jetty config file */
054        private String confFile;
055    
056        /** a collection to hold the configured web applications */
057        private Set<WebApp> webApps;
058    
059        /** the default port to be used when no configuration file is provided */
060        private int port = 8080;
061    
062        /** an internal flag to check the server configuration */
063        private boolean configured = false;
064    
065        private static final Logger LOG = LoggerFactory.getLogger( HttpServer.class );
066    
067    
068        public HttpServer()
069        {
070        }
071    
072    
073        /**
074         * starts the jetty http server
075         * 
076         * @throws Exception
077         */
078        public void start() throws Exception
079        {
080    
081            if ( confFile == null && ( webApps == null || webApps.isEmpty() ) )
082            {
083                LOG.warn( "Neither configuration file nor web apps were configured for the http server, skipping initialization." );
084                return;
085            }
086    
087            XmlConfiguration jettyConf = null;
088    
089            if ( confFile != null )
090            {
091                jettyConf = new XmlConfiguration( new FileInputStream( confFile ) );
092    
093                LOG.info( "configuring jetty http server from the configuration file {}", confFile );
094    
095                try
096                {
097                    jetty = new Server();
098                    jettyConf.configure( jetty );
099                    configured = true;
100                }
101                catch ( Exception e )
102                {
103                    LOG.error( I18n.err( I18n.ERR_120 ) );
104                    throw e;
105                }
106            }
107            else
108            {
109                LOG.info( "No configuration file set, looking for web apps" );
110                configureServerThroughCode();
111            }
112    
113            if ( configured )
114            {
115                LOG.info( "starting jetty http server" );
116                jetty.start();
117            }
118            else
119            {
120                jetty = null;
121                LOG.warn( "Error while configuring the http server, skipping the http server startup" );
122            }
123        }
124    
125    
126        /*
127         * configure the jetty server programmatically without using any configuration file 
128         */
129        private void configureServerThroughCode()
130        {
131            try
132            {
133                jetty = new Server();
134    
135                Connector connector = new SelectChannelConnector();
136                connector.setPort( port );
137                jetty.setConnectors( new Connector[]{ connector } );
138    
139                List<Handler> handlers = new ArrayList<Handler>();
140                for ( WebApp w : webApps )
141                {
142                    WebAppContext webapp = new WebAppContext();
143                    webapp.setWar( w.getWarFile() );
144                    webapp.setContextPath( w.getContextPath() );
145                    handlers.add( webapp );
146                }
147    
148                jetty.setHandlers( handlers.toArray( new Handler[ handlers.size() ] ) );
149                
150                configured = true;
151            }
152            catch ( Exception e )
153            {
154                LOG.error( I18n.err( I18n.ERR_121 ), e );
155            }
156    
157        }
158    
159    
160        /**
161         * stops the jetty http server
162         * 
163         * @throws Exception
164         */
165        public void stop() throws Exception
166        {
167            if ( jetty != null && jetty.isStarted() )
168            {
169                LOG.info( "stopping jetty http server" );
170                jetty.stop();
171            }
172        }
173    
174    
175        public void setConfFile( String confFile )
176        {
177            this.confFile = confFile;
178        }
179    
180    
181        public Set<WebApp> getWebApps()
182        {
183            return webApps;
184        }
185    
186    
187        public void setWebApps( Set<WebApp> webapps )
188        {
189            this.webApps = webapps;
190        }
191    
192    
193        public int getPort()
194        {
195            return port;
196        }
197    
198    
199        public void setPort( int port )
200        {
201            this.port = port;
202        }
203    
204    }