/*
 * Copyright 2013 Alex Lin.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.opoo.press.tool;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.NCSARequestLog;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.ContextHandler;
import org.mortbay.jetty.handler.DefaultHandler;
import org.mortbay.jetty.handler.HandlerList;
import org.mortbay.jetty.handler.RequestLogHandler;
import org.mortbay.jetty.handler.ResourceHandler;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.opoo.press.Site;

/**
 * Using jetty server as preview server.
 * 
 * @author Alex Lin
 *
 */
public class PreviewServer {
	private static final int MAX_IDLE_TIME = 30000;
	private static final Log log = LogFactory.getLog(PreviewServer.class);
	
	/**
	 * Create a jetty server and start it.
	 * 
	 * <p>这个操作会阻塞当前进程。所以当结合预览监听器使用时，必须先启动
	 * 监听器，再启动 Jetty Server。</p>
	 * @param site
	 * @param port
	 * @throws Exception
	 */
    public void start(Site site, int port) throws Exception{
    	
    	Server server = new Server();
        server.setStopAtShutdown( true );

        Connector defaultConnector = getDefaultConnector(null, port);
        server.setConnectors( new Connector[] { defaultConnector } );

        NCSARequestLog requestLog = new NCSARequestLog();
        RequestLogHandler logHandler = new RequestLogHandler();
        logHandler.setRequestLog(requestLog);
        
        //contextPath = site.root
        String root = site.getRoot();
		if("".equals(root)){
			ResourceHandler resource_handler = new ResourceHandler();
			// resource_handler.setDirectoriesListed(true);
			resource_handler.setWelcomeFiles(new String[] { "index.html" });

			// resource_handler.setResourceBase("target/public");
			resource_handler.setResourceBase(site.getDestination().getPath());
			String base = resource_handler.getResourceBase();
			log.info("Server resource base: " + base);
			
			HandlerList handlers = new HandlerList();
			handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler()});
			//server.setHandler(handlers);
			server.setHandlers(new Handler[]{handlers, logHandler});
			
			log.info( "Starting Jetty on http://localhost:" + port + "/" );
		}else{
			log.info("Using " + ContextHandler.class.getName());
			ContextHandler contextHandler = new ContextHandler();
			contextHandler.setContextPath(root);
			contextHandler.setHandler(new ResourceHandler());
			contextHandler.setResourceBase(site.getDestination().getPath());
			//server.setHandler(contextHandler);
			server.setHandlers(new Handler[]{contextHandler, logHandler});
			
			log.info( "Starting Jetty on http://localhost:" + port + root );
		}
		
        try{
            server.start();
        }
        catch ( Exception e ){
            throw new Exception( "Error executing Jetty: " + e.getMessage(), e );
        }

        // Watch it
        try{
            server.getThreadPool().join();
        }
        catch ( InterruptedException e ){
            log.warn( "Jetty was interrupted", e );
        }
    }
    
    /**
     * 
     * @param host localhost, 127.0.0.1 or 0.0.0.0.
     * @param port
     * @return jetty connector
     */
    private Connector getDefaultConnector(String host, int port){
        Connector connector = new SelectChannelConnector();
        if(host != null){
        	connector.setHost(host);
        }
        connector.setPort( port );
        connector.setMaxIdleTime( MAX_IDLE_TIME );
        return connector;
    }
}

@@ -1936,67 +1936,10 @@
     
-NCSARequestLog requestLog = new NCSARequestLog();%0D%0A        
+//
 Requ
@@ -1965,20 +1965,22 @@
 ndler = 
-new 
+create
 RequestL
@@ -1993,55 +1993,12 @@
 ler(
-);%0D%0A        logHandler.setRequestLog(requestLog
+site
 );%0D%0A
@@ -2653,26 +2653,24 @@
 er()%7D);%0D%0A%09%09%09
-//
 server.setHa
@@ -2683,24 +2683,26 @@
 handlers);%0D%0A
+//
 %09%09%09server.se
@@ -3115,18 +3115,16 @@
 ));%0D%0A%09%09%09
-//
 server.s
@@ -3147,24 +3147,26 @@
 tHandler);%0D%0A
+//
 %09%09%09server.se
@@ -3663,32 +3663,32 @@
 rrupted%22, e );%0D%0A
-
         %7D%0D%0A    %7D
@@ -3691,24 +3691,480 @@
    %7D%0D%0A    %0D%0A
+    RequestLogHandler createRequestLogHandler(Site site)%7B%0D%0A    %09boolean showRequestLog = Boolean.getBoolean(%22requestLog%22);%0D%0A    %09if(!showRequestLog)%7B%0D%0A    %09%09return null;%0D%0A    %09%7D%0D%0A    %09%0D%0A    %09NCSARequestLog requestLog = new NCSARequestLog();%0D%0A        requestLog.setLogDateFormat(null);%0D%0A        %0D%0A        RequestLogHandler logHandler = new RequestLogHandler();%0D%0A        logHandler.setRequestLog(requestLog);%0D%0A    %09%0D%0A        return logHandler;%0D%0A    %7D%0D%0A    %0D%0A
     /**%0D%0A   

Remove request log in Previewer.