/*
 * Copyright 2013-2014 eBay Software Foundation
 *
 * 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 com.kylinolap.rest;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

import org.apache.catalina.Context;
import org.apache.catalina.core.AprLifecycleListener;
import org.apache.catalina.core.StandardServer;
import org.apache.catalina.deploy.ErrorPage;
import org.apache.catalina.startup.Tomcat;
import org.apache.hadoop.util.Shell;

import com.kylinolap.rest.util.ClasspathUtil;

public class DebugTomcat {

    public static void main(String[] args) throws Exception {
        int port = 7070;
        if (args.length >= 1) {
            port = Integer.parseInt(args[0]);
        }

        // workaround for job submission from win to linux -- https://issues.apache.org/jira/browse/MAPREDUCE-4052
        if (Shell.WINDOWS) {
            {
                Field field = Shell.class.getDeclaredField("WINDOWS");
                field.setAccessible(true);
                Field modifiersField = Field.class.getDeclaredField("modifiers");
                modifiersField.setAccessible(true);
                modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
                field.set(null, false);
            }
            {
                Field field = java.io.File.class.getDeclaredField("pathSeparator");
                field.setAccessible(true);
                Field modifiersField = Field.class.getDeclaredField("modifiers");
                modifiersField.setAccessible(true);
                modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
                field.set(null, ":");
            }
        }

        ClasspathUtil.addClasspath(new File("../examples/test_case_data/sandbox").getAbsolutePath());
        String webBase = new File("../webapp/app").getAbsolutePath();

        Tomcat tomcat = new Tomcat();
        tomcat.setPort(port);
        tomcat.setBaseDir(".");

        // Add AprLifecycleListener
        StandardServer server = (StandardServer) tomcat.getServer();
        AprLifecycleListener listener = new AprLifecycleListener();
        server.addLifecycleListener(listener);

        Context webContext = tomcat.addWebapp("/kylin", webBase);
        ErrorPage notFound = new ErrorPage();
        notFound.setErrorCode(404);
        notFound.setLocation("/index.html");
        webContext.addErrorPage(notFound);
        webContext.addWelcomeFile("index.html");

        // tomcat start
        tomcat.start();
        tomcat.getServer().await();
    }

}

@@ -2378,16 +2378,264 @@
 ePath();
+%0A        %0A        if (new File(webBase, %22WEB-INF%22).exists() == false) %7B%0A            throw new RuntimeException(%22In order to launch Kylin web app from IDE, please make a symblink from webapp/app/WEB-INF to server/src/main/webapp/WEB-INF%22);%0A        %7D
 %0A%0A      

make DebugTomcat work as 2 static web and rest web app becomes one