001package ca.uhn.hl7v2.hoh.relay;
002
003import java.io.FileNotFoundException;
004
005import org.springframework.context.ApplicationContext;
006import org.springframework.context.support.FileSystemXmlApplicationContext;
007import org.springframework.util.Log4jConfigurer;
008
009import ca.uhn.hl7v2.hoh.util.IOUtils;
010import ca.uhn.hl7v2.hoh.util.VersionLogger;
011
012public class Launcher {
013        private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(Launcher.class);
014
015        private FileSystemXmlApplicationContext myAppContext;
016
017        public Launcher() {
018                this("conf" + IOUtils.FILE_PATH_SEP + "config.xml");
019        }
020
021        public Launcher(String configFile) {
022                long start = System.currentTimeMillis();
023
024                StringBuilder b = new StringBuilder();
025                b.append("HAPI HL7 over HTTP Relay is starting. This software is Licensed under the ");
026                b.append("Apache Software License (version 2.0) and is Copyright(c) ");
027                b.append("2012 University Health Network.");
028                ourLog.info(b.toString());
029
030                VersionLogger.init();
031
032                Runtime.getRuntime().addShutdownHook(new ShutdownHook());
033
034                myAppContext = new FileSystemXmlApplicationContext(configFile);
035                myAppContext.start();
036
037                long delay = System.currentTimeMillis() - start;
038                ourLog.info("HAPI HL7 over HTTP Relay started in {} ms", delay);
039        }
040
041        public ApplicationContext getAppCtx() {
042                return myAppContext;
043        }
044
045        public void shutdown() {
046                ourLog.info("Shutdown request detected, stopping all services");
047                if (myAppContext != null) {
048                        myAppContext.close();
049                        myAppContext = null;
050                }
051                ourLog.info("Shutdown request completed");
052        }
053
054        public static void main(String[] args) throws FileNotFoundException {
055                Log4jConfigurer.initLogging("file:conf" + IOUtils.FILE_PATH_SEP + "log4j.xml");
056
057                // System.setProperty("relay.port.in",
058                // RandomServerPortProvider.findFreePort() + "");
059                // System.setProperty("relay.port.out",
060                // RandomServerPortProvider.findFreePort() + "");
061                new Launcher();
062        }
063
064        private final class ShutdownHook extends Thread {
065                @Override
066                public void run() {
067                        shutdown();
068                }
069        }
070
071}