001package runwar;
002
003import java.io.File;
004import java.io.InputStream;
005import java.util.Properties;
006import java.util.ArrayList;
007import java.util.Arrays;
008import java.util.List;
009
010/**
011 * A bootstrap class for starting server using an embedded war
012 * 
013 */
014public final class RunEmbeddedWar {
015    private static String WAR_POSTFIX = ".war";
016    private static String WAR_NAME = "cfdistro";
017    private static String WAR_FILENAME = WAR_NAME + WAR_POSTFIX;
018
019    public static void main(String[] args) throws Exception {
020        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
021
022        String sConfigFile = "runwar.properties";
023        InputStream in = classLoader.getResourceAsStream(sConfigFile);
024        if (in == null) {
025            // File not found! (Manage the problem)
026        }
027        Properties props = new java.util.Properties();
028        props.load(in);
029        WAR_NAME = props.getProperty("war.name");
030        WAR_POSTFIX = ".war";
031        WAR_FILENAME = WAR_NAME + WAR_POSTFIX;
032        System.out.println(props.toString());
033        System.out.println("Starting...");
034
035        // File warFile = File.createTempFile(WAR_NAME + "-", WAR_POSTFIX);
036        File currentDir = new File(RunEmbeddedWar.class.getProtectionDomain().getCodeSource().getLocation().toURI())
037                .getParentFile();
038        File warFile = new File(currentDir.getCanonicalPath() + "/" + WAR_FILENAME);
039        File warDir = new File(currentDir.getCanonicalPath() + "/" + WAR_NAME);
040
041        if (warDir.exists()) {
042            System.out.println("Not extracting, as WAR directory already exists: " + warDir.getCanonicalPath());
043        } else {
044            warDir.mkdir();
045            System.out.println("Extracting " + WAR_FILENAME + " to " + warFile + " ...");
046            LaunchUtil.unzipInteralZip(classLoader, WAR_FILENAME, warDir, false);
047            System.out.println("Extracted " + WAR_FILENAME);
048            LaunchUtil.copyInternalFile(classLoader, "server.json", new File(currentDir,"server.json"));
049        }
050
051        System.out.println("Launching server...");
052
053        List<String> argsList = new ArrayList<String>();
054        if (args != null) {
055            argsList.addAll(Arrays.asList(args));
056        }
057        argsList.add("-c");
058        argsList.add(new File(currentDir,"server.json").getCanonicalPath());
059        System.out.println(argsList);
060
061        if (props.getProperty("open.url") != null) {
062            new Server(3);
063        }
064        Start.main(argsList.toArray(new String[argsList.size()]));
065//        System.exit(0);
066    }
067}