001    package biz.hammurapi.sql.hsqldb;
002    
003    import org.hsqldb.Server;
004    import org.hsqldb.ServerConfiguration;
005    import org.hsqldb.ServerConstants;
006    import org.hsqldb.lib.FileUtil;
007    import org.hsqldb.persist.HsqlProperties;
008    
009    /**
010     * Helper class to start HSQLDB server as Windows service.
011     * @author Pavel
012     *
013     */
014    public class HsqldbService {
015            
016            private static Server server;
017    
018            /**
019             * @param args
020             */
021            public static void main(String[] args) throws Exception {
022                    if (args.length==0 || "start".equals(args[0])) {
023                            if (server==null) {
024                            String propsPath = FileUtil.getDefaultInstance().canonicalOrAbsolutePath("server");
025                            HsqlProperties fileProps = ServerConfiguration.getPropertiesFromFile(propsPath);
026                            HsqlProperties props = fileProps == null ? new HsqlProperties() : fileProps;
027                            HsqlProperties stringProps = HsqlProperties.argArrayToProps(args, ServerConstants.SC_KEY_PREFIX);
028    
029                            if (stringProps != null) {
030                                if (stringProps.getErrorKeys().length != 0) {
031                                    System.exit(2);
032                                }
033    
034                                props.addProperties(stringProps);
035                            }
036    
037                            ServerConfiguration.translateDefaultDatabaseProperty(props);
038                            ServerConfiguration.translateDefaultNoSystemExitProperty(props);
039    
040                            server = new Server();
041                        server.setProperties(props);
042                            server.start();                         
043                            } else {
044                                    System.err.println("Server is already running.");
045                                    System.exit(1);
046                            }
047                    }
048    
049                    if (args.length!=0 && "stop".equals(args[0])) {
050                            if (server==null) {
051                                    System.err.println("Server is not running.");
052                                    System.exit(1);                         
053                            } else {
054                                    server.shutdown();
055                            }                       
056                    }
057            }
058    
059    }