package org.jfox.hsql;

import java.io.File;

import org.jfox.framework.extra.ActiveComponent;
import org.jfox.framework.extra.Managable;
import org.jfox.framework.extra.ManagementInfo;
import org.jfox.system.AbstractService;

/**
 * @author <a href="mailto:yangyong@ufsoft.com.cn">Young Yang</a>
 */

public class HsqlDBService extends AbstractService implements ActiveComponent, Managable {

    /**
     * db strage directory
     */
    private String storage = "db";

    /**
     * db name
     */
    private String name = "default";

    /**
     * Default port.
     */
    private int port = org.hsqldb.ServerConstants.SC_DEFAULT_HSQL_SERVER_PORT;

    org.hsqldb.Server server = new org.hsqldb.Server();

    protected void doInit() throws Exception {
        File dbpath = new File(context.getModuleDir(), storage);
        if(!dbpath.exists()) {
            dbpath.mkdirs();
        }

        if(!dbpath.isDirectory()) {
            logger.error("Failed to create db directory: " + dbpath);
            return;
        }
        File db = new File(dbpath, name);
        server.setLogWriter(null);
//        server.setErrWriter(null);
        server.setSilent(true);
        server.setNoSystemExit(true);
        server.setPort(port);
        

        server.setDatabasePath(0, db.toURL().toString());
    }

    protected void doStart() throws Exception {
        server.start();
    }

    protected void doStop() throws Exception {
        server.stop();
    }

    protected void doDestroy() throws Exception {

    }

    public void setDatabase(final String name) {
        this.name = name;
    }

    public String getDatabase() {
        return name;
    }

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

    public int getPort() {
        return port;
    }

    public String getDatabasePath() {
        return new File(context.getModuleDir(), storage).toString();
    }

    public void run() {

    }

    /**
     * start of DatabaseManager accesible from the http://localhost:8082
     */
    public void startDatabaseManager() {
        // Start DBManager in new thread
        new Thread() {
            public void run() {
                try {
                    String driver = "org.hsqldb.jdbcDriver";
                    String[] args;
                    args = new String[]{
                        "-driver", driver,
                        "-url", "jdbc:hsqldb:hsql://localhost:" + port,
                        "-user", "sa",
                        "-password", "",
                        "-dir", getDatabasePath(),
                        "-noexit"
                    };
                    org.hsqldb.util.DatabaseManagerSwing.main(args);
                }
                catch(Exception e) {
                    logger.error("Failed to start database manager", e);
                }
            }
        }.start();
    }


    public ManagementInfo getManagementInfo() {
        return ManagementInfo.exposePublic(this.getClass());
    }

    public static void main(String[] args) {

    }
}

