summaryrefslogtreecommitdiffstats
path: root/java/sca
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--java/sca/modules/endpoint-zookeeper/src/main/java/org/apache/tuscany/sca/endpoint/zookeeper/LocalZooKeeperServer.java148
-rw-r--r--java/sca/modules/endpoint-zookeeper/src/test/java/org/apache/tuscany/sca/endpoint/zookeeper/DistributedMapTestCase.java45
-rw-r--r--java/sca/modules/endpoint-zookeeper/src/test/java/org/apache/tuscany/sca/endpoint/zookeeper/LocalZooKeeperServerTestCase.java85
3 files changed, 278 insertions, 0 deletions
diff --git a/java/sca/modules/endpoint-zookeeper/src/main/java/org/apache/tuscany/sca/endpoint/zookeeper/LocalZooKeeperServer.java b/java/sca/modules/endpoint-zookeeper/src/main/java/org/apache/tuscany/sca/endpoint/zookeeper/LocalZooKeeperServer.java
new file mode 100644
index 0000000000..49bb46f983
--- /dev/null
+++ b/java/sca/modules/endpoint-zookeeper/src/main/java/org/apache/tuscany/sca/endpoint/zookeeper/LocalZooKeeperServer.java
@@ -0,0 +1,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);
+ }
+}
diff --git a/java/sca/modules/endpoint-zookeeper/src/test/java/org/apache/tuscany/sca/endpoint/zookeeper/DistributedMapTestCase.java b/java/sca/modules/endpoint-zookeeper/src/test/java/org/apache/tuscany/sca/endpoint/zookeeper/DistributedMapTestCase.java
new file mode 100644
index 0000000000..5489f20098
--- /dev/null
+++ b/java/sca/modules/endpoint-zookeeper/src/test/java/org/apache/tuscany/sca/endpoint/zookeeper/DistributedMapTestCase.java
@@ -0,0 +1,45 @@
+/*
+ * 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 org.junit.AfterClass;
+import org.junit.BeforeClass;
+
+/**
+ *
+ */
+public class DistributedMapTestCase {
+
+ /**
+ * @throws java.lang.Exception
+ */
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ }
+
+ /**
+ * @throws java.lang.Exception
+ */
+ @AfterClass
+ public static void tearDownAfterClass() throws Exception {
+ }
+
+}
diff --git a/java/sca/modules/endpoint-zookeeper/src/test/java/org/apache/tuscany/sca/endpoint/zookeeper/LocalZooKeeperServerTestCase.java b/java/sca/modules/endpoint-zookeeper/src/test/java/org/apache/tuscany/sca/endpoint/zookeeper/LocalZooKeeperServerTestCase.java
new file mode 100644
index 0000000000..51a4d7455e
--- /dev/null
+++ b/java/sca/modules/endpoint-zookeeper/src/test/java/org/apache/tuscany/sca/endpoint/zookeeper/LocalZooKeeperServerTestCase.java
@@ -0,0 +1,85 @@
+/*
+ * 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 org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.WatchedEvent;
+import org.apache.zookeeper.Watcher;
+import org.apache.zookeeper.ZooKeeper;
+import org.apache.zookeeper.Watcher.Event.KeeperState;
+import org.apache.zookeeper.ZooDefs.Ids;
+import org.apache.zookeeper.server.PurgeTxnLog;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class LocalZooKeeperServerTestCase implements Watcher {
+ private static LocalZooKeeperServer server;
+
+ /**
+ * @throws java.lang.Exception
+ */
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ server = new LocalZooKeeperServer();
+ }
+
+ @Test
+ public void testServer() throws Exception {
+ String[] args = new String[] {"8085", "target/zookeeper"};
+ Thread thread = server.folk(args);
+ ZooKeeper client = new ZooKeeper("localhost:8085", 500, this);
+ synchronized (this) {
+ wait(10000);
+ }
+ client.create("/test", "123".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
+ byte[] data = client.getData("/test", false, null);
+ Assert.assertEquals("123", new String(data));
+ client.close();
+ }
+
+ /**
+ * @throws java.lang.Exception
+ */
+ @AfterClass
+ public static void tearDownAfterClass() throws Exception {
+ if (server != null) {
+ server.shutdown();
+ File dir = new File("target/zookeeper");
+ PurgeTxnLog.purge(dir, dir, 3);
+ }
+ }
+
+ public void process(WatchedEvent event) {
+ System.out.println(event);
+ if (event.getPath() == null && event.getState() == KeeperState.SyncConnected) {
+ synchronized (this) {
+ notifyAll();
+ }
+ }
+ }
+
+}