Package net.sf.jruby.rails.asyncweb.server

Source Code of net.sf.jruby.rails.asyncweb.server.StandAlone

package net.sf.jruby.rails.asyncweb.server;

import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.safehaus.asyncweb.container.ContainerLifecycleException;
import org.safehaus.asyncweb.container.ServiceContainer;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.io.FileSystemResource;

public class StandAlone {

    private static final String DEFAULT_PORT = "3000";

    public static void main(String[] args) {
        StandAlone app = new StandAlone();
        Options opts = new Options();

        try {
            opts.addOption(buildConfigOption());
            opts.addOption(buildPortOption());
            opts.addOption(buildHelpOption());

            CommandLine cl = new BasicParser().parse(opts, args);

            if (cl.hasOption('h')) {
                printHelpAndExit(opts);
            }
            if (cl.getArgs().length == 1) {
                app.setRailsRoot(cl.getArgs()[0]);
            } else {
                printHelpAndExit(opts);
            }
            if (cl.hasOption('c')) {
                app.setConfig(cl.getOptionValue('c'));
            } else {
                app.setConfig(System.getProperty("rails.asyncweb.default.config"));
            }
            if (cl.hasOption('p')) {
                app.setPort(cl.getOptionValue('p'));
            } else {
                app.setPort(DEFAULT_PORT);
            }

            app.start();
            // TODO stop()

        } catch (ParseException e) {
            printHelpAndExit(opts);
        }

    }

    private static Option buildConfigOption() {
        Option optConfig = new Option("c", "config", true, "set config file 'file'.");
        optConfig.setArgName("file");
        return optConfig;
    }

    private static Option buildHelpOption() {
        return new Option("h", "help", false, "show this message.");
    }

    private static Option buildPortOption() {
        Option optPort = new Option("p", "port", true, "runs rails on the specific port.");
        optPort.setArgName("port");
        return optPort;
    }

    private static void printHelpAndExit(Options opts) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("jruby-rails [OPTION]... RAILS_ROOT", opts);

        System.exit(1);
    }

    private String config = null;

    private ConfigurableApplicationContext context = null;

    private String port = null;

    private String railsRoot = null;

    public void setConfig(String config) {
        this.config = config;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public void setRailsRoot(String railsRoot) {
        this.railsRoot = railsRoot;
    }

    public void start() {
        try {
            System.setProperty("rails.port", port);
            System.setProperty("rails.root", railsRoot);

            FileSystemResource resource = new FileSystemResource(config);
            XmlBeanFactory factory = new XmlBeanFactory(resource);

            PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
            configurer.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_FALLBACK);
            configurer.postProcessBeanFactory(factory);

            context = new GenericApplicationContext(factory);
            context.refresh();

            ServiceContainer container = (ServiceContainer) factory.getBean("container");

            container.start();
        } catch (BeansException e) {
            throw new RuntimeException(e);
        } catch (ContainerLifecycleException e) {
            throw new RuntimeException(e);
        }
    }

    public void stop() {
        context.close();
    }
}
TOP

Related Classes of net.sf.jruby.rails.asyncweb.server.StandAlone

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.