All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.jasmine.zookeeper.ZooKeeper Maven / Gradle / Ivy

There is a newer version: 1.0.36
Show newest version
/**
 * JASMINe
 * Copyright (C) 2010 Bull S.A.S.
 * Contact: [email protected]
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id: ZooKeeper.java 5575 2010-09-23 11:33:18Z veyjul $
 * --------------------------------------------------------------------------
 */
package org.ow2.jasmine.zookeeper;

import java.io.File;

import org.apache.log4j.PropertyConfigurator;
import org.apache.zookeeper.server.NIOServerCnxn;
import org.apache.zookeeper.server.ServerConfig;
import org.apache.zookeeper.server.ZooKeeperServer;
import org.apache.zookeeper.server.persistence.FileTxnSnapLog;

import org.ow2.util.log.Log;
import org.ow2.util.log.LogFactory;

/**
 * Class that creates a ZooKeeper configuration an launch a ZooKeeperServer using this configuration
 * 
 * @author veyj
 */
public class ZooKeeper extends Thread {

    /**
     * Logger
     */
    private static Log logger = LogFactory.getLog(ZooKeeper.class);

    /**
     * Default zookeeper port
     */
    public final static String DEFAULT_ZOOKEEPER_PORT = "2181";

    /**
     * Default ZooKeeper Snapshot directory
     */
    public final static String DEFAULT_ZOOKEEPER_SNAPSHOT_DIRECTORY = "~/snapshotZoo";

    /**
     * Zookeeper configuration
     */
    private String conf[];

    /**
     * NIOServerCnxn factory
     */
    private NIOServerCnxn.Factory cnxnFactory;

    /**
     * Default constructor with no parameters, will use default port and default snapshot directory
     */
    public ZooKeeper() {
        conf = new String[2];
        conf[0] = DEFAULT_ZOOKEEPER_PORT;
        conf[1] = DEFAULT_ZOOKEEPER_SNAPSHOT_DIRECTORY;
    }

    /**
     * Constructor with the port and the snapshot directory specified
     * 
     * @param port
     *            the zookeeper port
     * @param snapshotDirectory
     *            the zookeeper snapshot directory
     */
    public ZooKeeper(String port, String snapshotDirectory) {
        conf = new String[2];
        conf[0] = port;
        conf[1] = snapshotDirectory;
    }

    /**
     * start the zookeeper server
     */
    public void startup() {
        deleteSnapshotDirectory();
        this.start();
    }

    /**
     * Delete the snasphot directory
     */
    private void deleteSnapshotDirectory() {
        File snapshotDir = new File(conf[1]);
        logger.info("Deleting snapshot directory");
        try {
            deleteDirectory(snapshotDir);
        } catch (Exception e) {
            logger.info("Impossible to delete snapshot directory, maybe it does not exists");
        }
    }

    /**
     * Delete a directory
     * 
     * @param f
     *            the directory to delete
     */
    private void deleteDirectory(File f) {
        if (f.isFile()) {
            logger.error("Cannot delete this directory {0} because it is a file", f.getAbsolutePath());
        } else {
            String rootPath = f.getAbsolutePath();
            File child;
            File[] childList = f.listFiles();
            if (childList.length == 0) {
                f.delete();
            } else {
                for (int i = 0; i < childList.length; i++) {
                    child = childList[i];
                    if (child.isDirectory()) {
                        deleteDirectory(child);
                    } else {
                        deleteFile(child);
                    }
                }
                f.delete();
            }
        }
    }

    /**
     * Delete a file
     * 
     * @param f
     *            the file to delete
     */
    private void deleteFile(File f) {
        if (f.isDirectory()) {
            logger.error("Cannot delete this file {0} because it is a directory", f.getAbsolutePath());
        } else {
            f.delete();
        }
    }

    /**
     * stop the zookeeper server
     */
    public void shutdown() {
        logger.info("Stopping Zookeeper");
        cnxnFactory.shutdown();
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Thread#run()
     */
    public void run() {
        PropertyConfigurator.configure(this.getClass().getClassLoader().getResource(("log4j.properties")));
        try {
            logger.info("Zookeeper starting on port {0}", conf[0]);
            ZooKeeperServer zkServer = new ZooKeeperServer();

            ServerConfig config = new ServerConfig();
            config.parse(conf);

            FileTxnSnapLog ftxn = new FileTxnSnapLog(new File(config.getDataLogDir()), new File(config.getDataDir()));
            zkServer.setTxnLogFactory(ftxn);
            zkServer.setTickTime(config.getTickTime());
            zkServer.setMinSessionTimeout(config.getMinSessionTimeout());
            zkServer.setMaxSessionTimeout(config.getMaxSessionTimeout());
            cnxnFactory = new NIOServerCnxn.Factory(config.getClientPortAddress(), config.getMaxClientCnxns());
            cnxnFactory.startup(zkServer);
            cnxnFactory.join();
            if (zkServer.isRunning()) {
                zkServer.shutdown();
            }
        } catch (Exception e) {
            logger.error("Error while starting ZooKeeper");
            e.printStackTrace();
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy