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

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

The newest version!
/**
 * Copyright 2008-2012 Bull S.A.S.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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 - 2025 Weber Informatics LLC | Privacy Policy