summaryrefslogtreecommitdiffstats
path: root/java/sca/modules/endpoint-zookeeper/src/main/java/org/apache/tuscany/sca/endpoint/zookeeper/LocalZooKeeperServer.java
blob: 49bb46f9832d6d86c5611544ae5bc4218996b23a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.tuscany.sca.endpoint.zookeeper;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

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.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException;

/**
 * This class starts and runs a standalone ZooKeeperServer.
 */
public class LocalZooKeeperServer {
    private static final Logger logger = Logger.getLogger(LocalZooKeeperServer.class.getName());
    private static final String USAGE = "Usage: LocalZooKeeperServer configfile | port datadir [ticktime] [maxcnxns]";
    private NIOServerCnxn.Factory cnxnFactory;
    private ZooKeeperServer zkServer;

    /**
     * 
     */
    public LocalZooKeeperServer() {
        super();
    }

    public Thread folk(final ServerConfig config) {
        Thread thread = new Thread() {
            public void run() {
                try {
                    LocalZooKeeperServer.this.run(config);
                } catch (IOException e) {
                    throw new IllegalStateException(e);
                }
            }
        };
        thread.start();
        return thread;
    }

    public Thread folk(final String[] args) {
        Thread thread = new Thread() {
            public void run() {
                try {
                    LocalZooKeeperServer.this.run(args);
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
        };
        thread.start();
        return thread;
    }

    /**
     * Run from a ServerConfig.
     * @param config ServerConfig to use.
     * @throws IOException
     */
    public void run(ServerConfig config) throws IOException {
        logger.info("Starting ZooKeeper server");
        try {
            // Note that this thread isn't going to be doing anything else,
            // so rather than spawning another thread, we will just call
            // run() in this thread.
            // create a file logger url from the command line args
            zkServer = new ZooKeeperServer();

            FileTxnSnapLog ftxn = new FileTxnSnapLog(new File(config.getDataLogDir()), new File(config.getDataDir()));
            zkServer.setTxnLogFactory(ftxn);
            zkServer.setTickTime(config.getTickTime());
            cnxnFactory = new NIOServerCnxn.Factory(config.getClientPort(), config.getMaxClientCnxns());
            cnxnFactory.startup(zkServer);
            cnxnFactory.join();
            if (zkServer.isRunning()) {
                zkServer.shutdown();
            }
        } catch (InterruptedException e) {
            // warn, but generally this is ok
            logger.log(Level.WARNING, "Server interrupted", e);
        }
    }

    public void run(String[] args) throws ConfigException, IOException {
        ServerConfig config = new ServerConfig();
        if (args.length == 1) {
            config.parse(args[0]);
        } else {
            config.parse(args);
        }
        run(config);
    }

    /**
     * Shutdown the serving instance
     */
    public void shutdown() {
        cnxnFactory.shutdown();
    }

    /*
     * Start up the ZooKeeper server.
     *
     * @param args the configfile or the port datadir [ticktime]
     */
    public static void main(String[] args) {
        LocalZooKeeperServer main = new LocalZooKeeperServer();
        try {
            main.run(args);
        } catch (IllegalArgumentException e) {
            logger.log(Level.SEVERE, "Invalid arguments, exiting abnormally", e);
            logger.info(USAGE);
            System.err.println(USAGE);
            System.exit(2);
        } catch (ConfigException e) {
            logger.log(Level.SEVERE, "Invalid config, exiting abnormally", e);
            System.err.println("Invalid config, exiting abnormally");
            System.exit(2);
        } catch (Exception e) {
            logger.log(Level.SEVERE, "Unexpected exception, exiting abnormally", e);
            System.exit(1);
        }
        logger.info("Exiting normally");
        System.exit(0);
    }
}