http://git-wip-us.apache.org/repos/asf/zookeeper/blob/7291e47c/zookeeper-common/src/main/java/org/apache/zookeeper/ZooKeeperMain.java ---------------------------------------------------------------------- diff --git a/zookeeper-common/src/main/java/org/apache/zookeeper/ZooKeeperMain.java b/zookeeper-common/src/main/java/org/apache/zookeeper/ZooKeeperMain.java deleted file mode 100644 index 6ca538b..0000000 --- a/zookeeper-common/src/main/java/org/apache/zookeeper/ZooKeeperMain.java +++ /dev/null @@ -1,871 +0,0 @@ -/** - * 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.zookeeper; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Date; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.NoSuchElementException; - -import org.apache.yetus.audience.InterfaceAudience; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.apache.zookeeper.AsyncCallback.DataCallback; -import org.apache.zookeeper.ZooDefs.Ids; -import org.apache.zookeeper.data.ACL; -import org.apache.zookeeper.data.Id; -import org.apache.zookeeper.data.Stat; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * The command line client to ZooKeeper. - * - */ -@InterfaceAudience.Public -public class ZooKeeperMain { - private static final Logger LOG = LoggerFactory.getLogger(ZooKeeperMain.class); - static final Map<String,String> commandMap = new HashMap<String,String>( ); - - protected MyCommandOptions cl = new MyCommandOptions(); - protected HashMap<Integer,String> history = new HashMap<Integer,String>( ); - protected int commandCount = 0; - protected boolean printWatches = true; - - protected ZooKeeper zk; - protected String host = ""; - - public boolean getPrintWatches( ) { - return printWatches; - } - - static { - commandMap.put("connect", "host:port"); - commandMap.put("close",""); - commandMap.put("create", "[-s] [-e] path data acl"); - commandMap.put("delete","path [version]"); - commandMap.put("rmr","path"); - commandMap.put("set","path data [version]"); - commandMap.put("get","path [watch]"); - commandMap.put("ls","path [watch]"); - commandMap.put("ls2","path [watch]"); - commandMap.put("getAcl","path"); - commandMap.put("setAcl","path acl"); - commandMap.put("stat","path [watch]"); - commandMap.put("sync","path"); - commandMap.put("setquota","-n|-b val path"); - commandMap.put("listquota","path"); - commandMap.put("delquota","[-n|-b] path"); - commandMap.put("history",""); - commandMap.put("redo","cmdno"); - commandMap.put("printwatches", "on|off"); - commandMap.put("quit",""); - commandMap.put("addauth", "scheme auth"); - } - - static void usage() { - System.err.println("ZooKeeper -server host:port cmd args"); - for (Map.Entry<String, String> entry : commandMap.entrySet()) { - System.err.println("\t" + entry.getKey() + " " + entry.getValue()); - } - } - - private class MyWatcher implements Watcher { - public void process(WatchedEvent event) { - if (getPrintWatches()) { - ZooKeeperMain.printMessage("WATCHER::"); - ZooKeeperMain.printMessage(event.toString()); - } - } - } - - static private int getPermFromString(String permString) { - int perm = 0; - for (int i = 0; i < permString.length(); i++) { - switch (permString.charAt(i)) { - case 'r': - perm |= ZooDefs.Perms.READ; - break; - case 'w': - perm |= ZooDefs.Perms.WRITE; - break; - case 'c': - perm |= ZooDefs.Perms.CREATE; - break; - case 'd': - perm |= ZooDefs.Perms.DELETE; - break; - case 'a': - perm |= ZooDefs.Perms.ADMIN; - break; - default: - System.err - .println("Unknown perm type: " + permString.charAt(i)); - } - } - return perm; - } - - private static void printStat(Stat stat) { - System.err.println("cZxid = 0x" + Long.toHexString(stat.getCzxid())); - System.err.println("ctime = " + new Date(stat.getCtime()).toString()); - System.err.println("mZxid = 0x" + Long.toHexString(stat.getMzxid())); - System.err.println("mtime = " + new Date(stat.getMtime()).toString()); - System.err.println("pZxid = 0x" + Long.toHexString(stat.getPzxid())); - System.err.println("cversion = " + stat.getCversion()); - System.err.println("dataVersion = " + stat.getVersion()); - System.err.println("aclVersion = " + stat.getAversion()); - System.err.println("ephemeralOwner = 0x" - + Long.toHexString(stat.getEphemeralOwner())); - System.err.println("dataLength = " + stat.getDataLength()); - System.err.println("numChildren = " + stat.getNumChildren()); - } - - /** - * A storage class for both command line options and shell commands. - * - */ - static class MyCommandOptions { - - private Map<String,String> options = new HashMap<String,String>(); - private List<String> cmdArgs = null; - private String command = null; - public static final Pattern ARGS_PATTERN = Pattern.compile("\\s*([^\"\']\\S*|\"[^\"]*\"|'[^']*')\\s*"); - public static final Pattern QUOTED_PATTERN = Pattern.compile("^([\'\"])(.*)(\\1)$"); - - public MyCommandOptions() { - options.put("server", "localhost:2181"); - options.put("timeout", "30000"); - } - - public String getOption(String opt) { - return options.get(opt); - } - - public String getCommand( ) { - return command; - } - - public String getCmdArgument( int index ) { - return cmdArgs.get(index); - } - - public int getNumArguments( ) { - return cmdArgs.size(); - } - - public String[] getArgArray() { - return cmdArgs.toArray(new String[0]); - } - - /** - * Parses a command line that may contain one or more flags - * before an optional command string - * @param args command line arguments - * @return true if parsing succeeded, false otherwise. - */ - public boolean parseOptions(String[] args) { - List<String> argList = Arrays.asList(args); - Iterator<String> it = argList.iterator(); - - while (it.hasNext()) { - String opt = it.next(); - try { - if (opt.equals("-server")) { - options.put("server", it.next()); - } else if (opt.equals("-timeout")) { - options.put("timeout", it.next()); - } else if (opt.equals("-r")) { - options.put("readonly", "true"); - } - } catch (NoSuchElementException e){ - System.err.println("Error: no argument found for option " - + opt); - return false; - } - - if (!opt.startsWith("-")) { - command = opt; - cmdArgs = new ArrayList<String>( ); - cmdArgs.add( command ); - while (it.hasNext()) { - cmdArgs.add(it.next()); - } - return true; - } - } - return true; - } - - /** - * Breaks a string into command + arguments. - * @param cmdstring string of form "cmd arg1 arg2..etc" - * @return true if parsing succeeded. - */ - public boolean parseCommand( String cmdstring ) { - Matcher matcher = ARGS_PATTERN.matcher(cmdstring); - - List<String> args = new LinkedList<String>(); - while (matcher.find()) { - String value = matcher.group(1); - if (QUOTED_PATTERN.matcher(value).matches()) { - // Strip off the surrounding quotes - value = value.substring(1, value.length() - 1); - } - args.add(value); - } - if (args.isEmpty()){ - return false; - } - command = args.get(0); - cmdArgs = args; - return true; - } - } - - - /** - * Makes a list of possible completions, either for commands - * or for zk nodes if the token to complete begins with / - * - */ - - - protected void addToHistory(int i,String cmd) { - history.put(i, cmd); - } - - public static List<String> getCommands() { - return new LinkedList<String>(commandMap.keySet()); - } - - protected String getPrompt() { - return "[zk: " + host + "("+zk.getState()+")" + " " + commandCount + "] "; - } - - public static void printMessage(String msg) { - System.out.println("\n"+msg); - } - - protected void connectToZK(String newHost) throws InterruptedException, IOException { - if (zk != null && zk.getState().isAlive()) { - zk.close(); - } - host = newHost; - boolean readOnly = cl.getOption("readonly") != null; - zk = new ZooKeeper(host, - Integer.parseInt(cl.getOption("timeout")), - new MyWatcher(), readOnly); - } - - public static void main(String args[]) - throws KeeperException, IOException, InterruptedException - { - ZooKeeperMain main = new ZooKeeperMain(args); - main.run(); - } - - public ZooKeeperMain(String args[]) throws IOException, InterruptedException { - cl.parseOptions(args); - System.out.println("Connecting to " + cl.getOption("server")); - connectToZK(cl.getOption("server")); - //zk = new ZooKeeper(cl.getOption("server"), -// Integer.parseInt(cl.getOption("timeout")), new MyWatcher()); - } - - public ZooKeeperMain(ZooKeeper zk) { - this.zk = zk; - } - - @SuppressWarnings("unchecked") - void run() throws KeeperException, IOException, InterruptedException { - if (cl.getCommand() == null) { - System.out.println("Welcome to ZooKeeper!"); - - boolean jlinemissing = false; - // only use jline if it's in the classpath - try { - Class<?> consoleC = Class.forName("jline.ConsoleReader"); - Class<?> completorC = - Class.forName("org.apache.zookeeper.JLineZNodeCompletor"); - - System.out.println("JLine support is enabled"); - - Object console = - consoleC.getConstructor().newInstance(); - - Object completor = - completorC.getConstructor(ZooKeeper.class).newInstance(zk); - Method addCompletor = consoleC.getMethod("addCompletor", - Class.forName("jline.Completor")); - addCompletor.invoke(console, completor); - - String line; - Method readLine = consoleC.getMethod("readLine", String.class); - while ((line = (String)readLine.invoke(console, getPrompt())) != null) { - executeLine(line); - } - } catch (ClassNotFoundException e) { - LOG.debug("Unable to start jline", e); - jlinemissing = true; - } catch (NoSuchMethodException e) { - LOG.debug("Unable to start jline", e); - jlinemissing = true; - } catch (InvocationTargetException e) { - LOG.debug("Unable to start jline", e); - jlinemissing = true; - } catch (IllegalAccessException e) { - LOG.debug("Unable to start jline", e); - jlinemissing = true; - } catch (InstantiationException e) { - LOG.debug("Unable to start jline", e); - jlinemissing = true; - } - - if (jlinemissing) { - System.out.println("JLine support is disabled"); - BufferedReader br = - new BufferedReader(new InputStreamReader(System.in)); - - String line; - while ((line = br.readLine()) != null) { - executeLine(line); - } - } - } else { - // Command line args non-null. Run what was passed. - processCmd(cl); - } - } - - public void executeLine(String line) - throws InterruptedException, IOException, KeeperException { - if (!line.equals("")) { - cl.parseCommand(line); - addToHistory(commandCount,line); - processCmd(cl); - commandCount++; - } - } - - private static DataCallback dataCallback = new DataCallback() { - - public void processResult(int rc, String path, Object ctx, byte[] data, - Stat stat) { - System.out.println("rc = " + rc + " path = " + path + " data = " - + (data == null ? "null" : new String(data)) + " stat = "); - printStat(stat); - } - - }; - - /** - * trim the quota tree to recover unwanted tree elements - * in the quota's tree - * @param zk the zookeeper client - * @param path the path to start from and go up and see if their - * is any unwanted parent in the path. - * @return true if sucessful - * @throws KeeperException - * @throws IOException - * @throws InterruptedException - */ - private static boolean trimProcQuotas(ZooKeeper zk, String path) - throws KeeperException, IOException, InterruptedException - { - if (Quotas.quotaZookeeper.equals(path)) { - return true; - } - List<String> children = zk.getChildren(path, false); - if (children.size() == 0) { - zk.delete(path, -1); - String parent = path.substring(0, path.lastIndexOf('/')); - return trimProcQuotas(zk, parent); - } else { - return true; - } - } - - /** - * this method deletes quota for a node. - * @param zk the zookeeper client - * @param path the path to delete quota for - * @param bytes true if number of bytes needs to - * be unset - * @param numNodes true if number of nodes needs - * to be unset - * @return true if quota deletion is successful - * @throws KeeperException - * @throws IOException - * @throws InterruptedException - */ - public static boolean delQuota(ZooKeeper zk, String path, - boolean bytes, boolean numNodes) - throws KeeperException, IOException, InterruptedException - { - String parentPath = Quotas.quotaZookeeper + path; - String quotaPath = Quotas.quotaZookeeper + path + "/" + Quotas.limitNode; - if (zk.exists(quotaPath, false) == null) { - System.out.println("Quota does not exist for " + path); - return true; - } - byte[] data = null; - try { - data = zk.getData(quotaPath, false, new Stat()); - } catch(KeeperException.NoNodeException ne) { - System.err.println("quota does not exist for " + path); - return true; - } - StatsTrack strack = new StatsTrack(new String(data)); - if (bytes && !numNodes) { - strack.setBytes(-1L); - zk.setData(quotaPath, strack.toString().getBytes(), -1); - } else if (!bytes && numNodes) { - strack.setCount(-1); - zk.setData(quotaPath, strack.toString().getBytes(), -1); - } else if (bytes && numNodes) { - // delete till you can find a node with more than - // one child - List<String> children = zk.getChildren(parentPath, false); - /// delete the direct children first - for (String child: children) { - zk.delete(parentPath + "/" + child, -1); - } - // cut the tree till their is more than one child - trimProcQuotas(zk, parentPath); - } - return true; - } - - private static void checkIfParentQuota(ZooKeeper zk, String path) - throws InterruptedException, KeeperException - { - final String[] splits = path.split("/"); - String quotaPath = Quotas.quotaZookeeper; - for (String str: splits) { - if (str.length() == 0) { - // this should only be for the beginning of the path - // i.e. "/..." - split(path)[0] is empty string before first '/' - continue; - } - quotaPath += "/" + str; - List<String> children = null; - try { - children = zk.getChildren(quotaPath, false); - } catch(KeeperException.NoNodeException ne) { - LOG.debug("child removed during quota check", ne); - return; - } - if (children.size() == 0) { - return; - } - for (String child: children) { - if (Quotas.limitNode.equals(child)) { - throw new IllegalArgumentException(path + " has a parent " - + quotaPath + " which has a quota"); - } - } - } - } - - /** - * this method creates a quota node for the path - * @param zk the ZooKeeper client - * @param path the path for which quota needs to be created - * @param bytes the limit of bytes on this path - * @param numNodes the limit of number of nodes on this path - * @return true if its successful and false if not. - */ - public static boolean createQuota(ZooKeeper zk, String path, - long bytes, int numNodes) - throws KeeperException, IOException, InterruptedException - { - // check if the path exists. We cannot create - // quota for a path that already exists in zookeeper - // for now. - Stat initStat = zk.exists(path, false); - if (initStat == null) { - throw new IllegalArgumentException(path + " does not exist."); - } - // now check if their is already existing - // parent or child that has quota - - String quotaPath = Quotas.quotaZookeeper; - // check for more than 2 children -- - // if zookeeper_stats and zookeeper_qutoas - // are not the children then this path - // is an ancestor of some path that - // already has quota - String realPath = Quotas.quotaZookeeper + path; - try { - List<String> children = zk.getChildren(realPath, false); - for (String child: children) { - if (!child.startsWith("zookeeper_")) { - throw new IllegalArgumentException(path + " has child " + - child + " which has a quota"); - } - } - } catch(KeeperException.NoNodeException ne) { - // this is fine - } - - //check for any parent that has been quota - checkIfParentQuota(zk, path); - - // this is valid node for quota - // start creating all the parents - if (zk.exists(quotaPath, false) == null) { - try { - zk.create(Quotas.procZookeeper, null, Ids.OPEN_ACL_UNSAFE, - CreateMode.PERSISTENT); - zk.create(Quotas.quotaZookeeper, null, Ids.OPEN_ACL_UNSAFE, - CreateMode.PERSISTENT); - } catch(KeeperException.NodeExistsException ne) { - // do nothing - } - } - - // now create the direct children - // and the stat and quota nodes - String[] splits = path.split("/"); - StringBuilder sb = new StringBuilder(); - sb.append(quotaPath); - for (int i=1; i<splits.length; i++) { - sb.append("/" + splits[i]); - quotaPath = sb.toString(); - try { - zk.create(quotaPath, null, Ids.OPEN_ACL_UNSAFE , - CreateMode.PERSISTENT); - } catch(KeeperException.NodeExistsException ne) { - //do nothing - } - } - String statPath = quotaPath + "/" + Quotas.statNode; - quotaPath = quotaPath + "/" + Quotas.limitNode; - StatsTrack strack = new StatsTrack(null); - strack.setBytes(bytes); - strack.setCount(numNodes); - try { - zk.create(quotaPath, strack.toString().getBytes(), - Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); - StatsTrack stats = new StatsTrack(null); - stats.setBytes(0L); - stats.setCount(0); - zk.create(statPath, stats.toString().getBytes(), - Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); - } catch(KeeperException.NodeExistsException ne) { - byte[] data = zk.getData(quotaPath, false , new Stat()); - StatsTrack strackC = new StatsTrack(new String(data)); - if (bytes != -1L) { - strackC.setBytes(bytes); - } - if (numNodes != -1) { - strackC.setCount(numNodes); - } - zk.setData(quotaPath, strackC.toString().getBytes(), -1); - } - return true; - } - - protected boolean processCmd(MyCommandOptions co) - throws KeeperException, IOException, InterruptedException - { - try { - return processZKCmd(co); - } catch (IllegalArgumentException e) { - System.err.println("Command failed: " + e); - } catch (KeeperException.NoNodeException e) { - System.err.println("Node does not exist: " + e.getPath()); - } catch (KeeperException.NoChildrenForEphemeralsException e) { - System.err.println("Ephemerals cannot have children: " - + e.getPath()); - } catch (KeeperException.NodeExistsException e) { - System.err.println("Node already exists: " + e.getPath()); - } catch (KeeperException.NotEmptyException e) { - System.err.println("Node not empty: " + e.getPath()); - } catch (KeeperException.NotReadOnlyException e) { - System.err.println("Not a read-only call: " + e.getPath()); - }catch (KeeperException.InvalidACLException e) { - System.err.println("Acl is not valid : "+e.getPath()); - }catch (KeeperException.NoAuthException e) { - System.err.println("Authentication is not valid : "+e.getPath()); - }catch (KeeperException.BadArgumentsException e) { - System.err.println("Arguments are not valid : "+e.getPath()); - }catch (KeeperException.BadVersionException e) { - System.err.println("version No is not valid : "+e.getPath()); - } - return false; - } - - protected boolean processZKCmd(MyCommandOptions co) - throws KeeperException, IOException, InterruptedException - { - Stat stat = new Stat(); - String[] args = co.getArgArray(); - String cmd = co.getCommand(); - if (args.length < 1) { - usage(); - return false; - } - - if (!commandMap.containsKey(cmd)) { - usage(); - return false; - } - - boolean watch = args.length > 2; - String path = null; - List<ACL> acl = Ids.OPEN_ACL_UNSAFE; - LOG.debug("Processing " + cmd); - - if (cmd.equals("quit")) { - System.out.println("Quitting..."); - zk.close(); - System.exit(0); - } else if (cmd.equals("redo") && args.length >= 2) { - Integer i = Integer.decode(args[1]); - if (commandCount <= i || i < 0){ // don't allow redoing this redo - System.out.println("Command index out of range"); - return false; - } - cl.parseCommand(history.get(i)); - if (cl.getCommand().equals( "redo" )){ - System.out.println("No redoing redos"); - return false; - } - history.put(commandCount, history.get(i)); - processCmd( cl); - } else if (cmd.equals("history")) { - for (int i=commandCount - 10;i<=commandCount;++i) { - if (i < 0) continue; - System.out.println(i + " - " + history.get(i)); - } - } else if (cmd.equals("printwatches")) { - if (args.length == 1) { - System.out.println("printwatches is " + (printWatches ? "on" : "off")); - } else { - printWatches = args[1].equals("on"); - } - } else if (cmd.equals("connect")) { - if (args.length >=2) { - connectToZK(args[1]); - } else { - connectToZK(host); - } - } - - // Below commands all need a live connection - if (zk == null || !zk.getState().isAlive()) { - System.out.println("Not connected"); - return false; - } - - if (cmd.equals("create") && args.length >= 3) { - int first = 0; - CreateMode flags = CreateMode.PERSISTENT; - if ((args[1].equals("-e") && args[2].equals("-s")) - || (args[1]).equals("-s") && (args[2].equals("-e"))) { - first+=2; - flags = CreateMode.EPHEMERAL_SEQUENTIAL; - } else if (args[1].equals("-e")) { - first++; - flags = CreateMode.EPHEMERAL; - } else if (args[1].equals("-s")) { - first++; - flags = CreateMode.PERSISTENT_SEQUENTIAL; - } - if (args.length == first + 4) { - acl = parseACLs(args[first+3]); - } - path = args[first + 1]; - String newPath = zk.create(path, args[first+2].getBytes(), acl, - flags); - System.err.println("Created " + newPath); - } else if (cmd.equals("delete") && args.length >= 2) { - path = args[1]; - zk.delete(path, watch ? Integer.parseInt(args[2]) : -1); - } else if (cmd.equals("rmr") && args.length >= 2) { - path = args[1]; - ZKUtil.deleteRecursive(zk, path); - } else if (cmd.equals("set") && args.length >= 3) { - path = args[1]; - stat = zk.setData(path, args[2].getBytes(), - args.length > 3 ? Integer.parseInt(args[3]) : -1); - printStat(stat); - } else if (cmd.equals("aget") && args.length >= 2) { - path = args[1]; - zk.getData(path, watch, dataCallback, path); - } else if (cmd.equals("get") && args.length >= 2) { - path = args[1]; - byte data[] = zk.getData(path, watch, stat); - data = (data == null)? "null".getBytes() : data; - System.out.println(new String(data)); - printStat(stat); - } else if (cmd.equals("ls") && args.length >= 2) { - path = args[1]; - List<String> children = zk.getChildren(path, watch); - System.out.println(children); - } else if (cmd.equals("ls2") && args.length >= 2) { - path = args[1]; - List<String> children = zk.getChildren(path, watch, stat); - System.out.println(children); - printStat(stat); - } else if (cmd.equals("getAcl") && args.length >= 2) { - path = args[1]; - acl = zk.getACL(path, stat); - for (ACL a : acl) { - System.out.println(a.getId() + ": " - + getPermString(a.getPerms())); - } - } else if (cmd.equals("setAcl") && args.length >= 3) { - path = args[1]; - stat = zk.setACL(path, parseACLs(args[2]), - args.length > 4 ? Integer.parseInt(args[3]) : -1); - printStat(stat); - } else if (cmd.equals("stat") && args.length >= 2) { - path = args[1]; - stat = zk.exists(path, watch); - if (stat == null) { - throw new KeeperException.NoNodeException(path); - } - printStat(stat); - } else if (cmd.equals("listquota") && args.length >= 2) { - path = args[1]; - String absolutePath = Quotas.quotaZookeeper + path + "/" + Quotas.limitNode; - byte[] data = null; - try { - System.err.println("absolute path is " + absolutePath); - data = zk.getData(absolutePath, false, stat); - StatsTrack st = new StatsTrack(new String(data)); - System.out.println("Output quota for " + path + " " - + st.toString()); - - data = zk.getData(Quotas.quotaZookeeper + path + "/" + - Quotas.statNode, false, stat); - System.out.println("Output stat for " + path + " " + - new StatsTrack(new String(data)).toString()); - } catch(KeeperException.NoNodeException ne) { - System.err.println("quota for " + path + " does not exist."); - } - } else if (cmd.equals("setquota") && args.length >= 4) { - String option = args[1]; - String val = args[2]; - path = args[3]; - System.err.println("Comment: the parts are " + - "option " + option + - " val " + val + - " path " + path); - if ("-b".equals(option)) { - // we are setting the bytes quota - createQuota(zk, path, Long.parseLong(val), -1); - } else if ("-n".equals(option)) { - // we are setting the num quota - createQuota(zk, path, -1L, Integer.parseInt(val)); - } else { - usage(); - } - - } else if (cmd.equals("delquota") && args.length >= 2) { - //if neither option -n or -b is specified, we delete - // the quota node for thsi node. - if (args.length == 3) { - //this time we have an option - String option = args[1]; - path = args[2]; - if ("-b".equals(option)) { - delQuota(zk, path, true, false); - } else if ("-n".equals(option)) { - delQuota(zk, path, false, true); - } - } else if (args.length == 2) { - path = args[1]; - // we dont have an option specified. - // just delete whole quota node - delQuota(zk, path, true, true); - } else if (cmd.equals("help")) { - usage(); - } - } else if (cmd.equals("close")) { - zk.close(); - } else if (cmd.equals("sync") && args.length >= 2) { - path = args[1]; - zk.sync(path, new AsyncCallback.VoidCallback() { public void processResult(int rc, String path, Object ctx) { System.out.println("Sync returned " + rc); } }, null ); - } else if (cmd.equals("addauth") && args.length >=2 ) { - byte[] b = null; - if (args.length >= 3) - b = args[2].getBytes(); - - zk.addAuthInfo(args[1], b); - } else if (!commandMap.containsKey(cmd)) { - usage(); - } - return watch; - } - - private static String getPermString(int perms) { - StringBuilder p = new StringBuilder(); - if ((perms & ZooDefs.Perms.CREATE) != 0) { - p.append('c'); - } - if ((perms & ZooDefs.Perms.DELETE) != 0) { - p.append('d'); - } - if ((perms & ZooDefs.Perms.READ) != 0) { - p.append('r'); - } - if ((perms & ZooDefs.Perms.WRITE) != 0) { - p.append('w'); - } - if ((perms & ZooDefs.Perms.ADMIN) != 0) { - p.append('a'); - } - return p.toString(); - } - - private static List<ACL> parseACLs(String aclString) { - List<ACL> acl; - String acls[] = aclString.split(","); - acl = new ArrayList<ACL>(); - for (String a : acls) { - int firstColon = a.indexOf(':'); - int lastColon = a.lastIndexOf(':'); - if (firstColon == -1 || lastColon == -1 || firstColon == lastColon) { - System.err - .println(a + " does not have the form scheme:id:perm"); - continue; - } - ACL newAcl = new ACL(); - newAcl.setId(new Id(a.substring(0, firstColon), a.substring( - firstColon + 1, lastColon))); - newAcl.setPerms(getPermFromString(a.substring(lastColon + 1))); - acl.add(newAcl); - } - return acl; - } -}
http://git-wip-us.apache.org/repos/asf/zookeeper/blob/7291e47c/zookeeper-common/src/main/java/org/apache/zookeeper/ZooKeeperTestable.java ---------------------------------------------------------------------- diff --git a/zookeeper-common/src/main/java/org/apache/zookeeper/ZooKeeperTestable.java b/zookeeper-common/src/main/java/org/apache/zookeeper/ZooKeeperTestable.java deleted file mode 100644 index 775d1a2..0000000 --- a/zookeeper-common/src/main/java/org/apache/zookeeper/ZooKeeperTestable.java +++ /dev/null @@ -1,47 +0,0 @@ -/** - * 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.zookeeper; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -class ZooKeeperTestable implements Testable { - private static final Logger LOG = LoggerFactory - .getLogger(ZooKeeperTestable.class); - - private final ZooKeeper zooKeeper; - private final ClientCnxn clientCnxn; - - ZooKeeperTestable(ZooKeeper zooKeeper, ClientCnxn clientCnxn) { - this.zooKeeper = zooKeeper; - this.clientCnxn = clientCnxn; - } - - @Override - public void injectSessionExpiration() { - LOG.info("injectSessionExpiration() called"); - - clientCnxn.eventThread.queueEvent(new WatchedEvent( - Watcher.Event.EventType.None, - Watcher.Event.KeeperState.Expired, null)); - clientCnxn.eventThread.queueEventOfDeath(); - clientCnxn.sendThread.getClientCnxnSocket().wakeupCnxn(); - clientCnxn.state = ZooKeeper.States.CLOSED; - } -} http://git-wip-us.apache.org/repos/asf/zookeeper/blob/7291e47c/zookeeper-common/src/main/java/org/apache/zookeeper/common/AtomicFileOutputStream.java ---------------------------------------------------------------------- diff --git a/zookeeper-common/src/main/java/org/apache/zookeeper/common/AtomicFileOutputStream.java b/zookeeper-common/src/main/java/org/apache/zookeeper/common/AtomicFileOutputStream.java deleted file mode 100644 index 2584d3f..0000000 --- a/zookeeper-common/src/main/java/org/apache/zookeeper/common/AtomicFileOutputStream.java +++ /dev/null @@ -1,126 +0,0 @@ -/** - * 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.zookeeper.common; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.FilterOutputStream; -import java.io.IOException; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/* - * This code is originally from HDFS, see the similarly named files there - * in case of bug fixing, history, etc... - */ - -/** - * A FileOutputStream that has the property that it will only show up at its - * destination once it has been entirely written and flushed to disk. While - * being written, it will use a .tmp suffix. - * - * When the output stream is closed, it is flushed, fsynced, and will be moved - * into place, overwriting any file that already exists at that location. - * - * <b>NOTE</b>: on Windows platforms, it will not atomically replace the target - * file - instead the target file is deleted before this one is moved into - * place. - */ -public class AtomicFileOutputStream extends FilterOutputStream { - private static final String TMP_EXTENSION = ".tmp"; - - private final static Logger LOG = LoggerFactory - .getLogger(AtomicFileOutputStream.class); - - private final File origFile; - private final File tmpFile; - - public AtomicFileOutputStream(File f) throws FileNotFoundException { - // Code unfortunately must be duplicated below since we can't assign - // anything - // before calling super - super(new FileOutputStream(new File(f.getParentFile(), f.getName() - + TMP_EXTENSION))); - origFile = f.getAbsoluteFile(); - tmpFile = new File(f.getParentFile(), f.getName() + TMP_EXTENSION) - .getAbsoluteFile(); - } - - /** - * The default write method in FilterOutputStream does not call the write - * method of its underlying input stream with the same arguments. Instead - * it writes the data byte by byte, override it here to make it more - * efficient. - */ - @Override - public void write(byte b[], int off, int len) throws IOException { - out.write(b, off, len); - } - - @Override - public void close() throws IOException { - boolean triedToClose = false, success = false; - try { - flush(); - ((FileOutputStream) out).getChannel().force(true); - - triedToClose = true; - super.close(); - success = true; - } finally { - if (success) { - boolean renamed = tmpFile.renameTo(origFile); - if (!renamed) { - // On windows, renameTo does not replace. - if (!origFile.delete() || !tmpFile.renameTo(origFile)) { - throw new IOException( - "Could not rename temporary file " + tmpFile - + " to " + origFile); - } - } - } else { - if (!triedToClose) { - // If we failed when flushing, try to close it to not leak - // an FD - IOUtils.closeStream(out); - } - // close wasn't successful, try to delete the tmp file - if (!tmpFile.delete()) { - LOG.warn("Unable to delete tmp file " + tmpFile); - } - } - } - } - - /** - * Close the atomic file, but do not "commit" the temporary file on top of - * the destination. This should be used if there is a failure in writing. - */ - public void abort() { - try { - super.close(); - } catch (IOException ioe) { - LOG.warn("Unable to abort file " + tmpFile, ioe); - } - if (!tmpFile.delete()) { - LOG.warn("Unable to delete tmp file during abort " + tmpFile); - } - } -} http://git-wip-us.apache.org/repos/asf/zookeeper/blob/7291e47c/zookeeper-common/src/main/java/org/apache/zookeeper/common/IOUtils.java ---------------------------------------------------------------------- diff --git a/zookeeper-common/src/main/java/org/apache/zookeeper/common/IOUtils.java b/zookeeper-common/src/main/java/org/apache/zookeeper/common/IOUtils.java deleted file mode 100644 index 16aea4e..0000000 --- a/zookeeper-common/src/main/java/org/apache/zookeeper/common/IOUtils.java +++ /dev/null @@ -1,123 +0,0 @@ -/** - * 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.zookeeper.common; - -import java.io.Closeable; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.PrintStream; - -import org.slf4j.Logger; - -/* - * This code is originally from HDFS, see the similarly named files there - * in case of bug fixing, history, etc... - */ - -public class IOUtils { - /** - * Closes the stream ignoring {@link IOException}. Must only be called in - * cleaning up from exception handlers. - * - * @param stream - * the Stream to close - */ - public static void closeStream(Closeable stream) { - cleanup(null, stream); - } - - /** - * Close the Closeable objects and <b>ignore</b> any {@link IOException} or - * null pointers. Must only be used for cleanup in exception handlers. - * - * @param log - * the log to record problems to at debug level. Can be null. - * @param closeables - * the objects to close - */ - public static void cleanup(Logger log, Closeable... closeables) { - for (Closeable c : closeables) { - if (c != null) { - try { - c.close(); - } catch (IOException e) { - if (log != null) { - log.warn("Exception in closing " + c, e); - } - } - } - } - } - - /** - * Copies from one stream to another. - * - * @param in - * InputStrem to read from - * @param out - * OutputStream to write to - * @param buffSize - * the size of the buffer - * @param close - * whether or not close the InputStream and OutputStream at the - * end. The streams are closed in the finally clause. - */ - public static void copyBytes(InputStream in, OutputStream out, - int buffSize, boolean close) throws IOException { - try { - copyBytes(in, out, buffSize); - if (close) { - out.close(); - out = null; - in.close(); - in = null; - } - } finally { - if (close) { - closeStream(out); - closeStream(in); - } - } - } - - /** - * Copies from one stream to another. - * - * @param in - * InputStrem to read from - * @param out - * OutputStream to write to - * @param buffSize - * the size of the buffer - */ - public static void copyBytes(InputStream in, OutputStream out, int buffSize) - throws IOException { - PrintStream ps = out instanceof PrintStream ? (PrintStream) out : null; - byte buf[] = new byte[buffSize]; - int bytesRead = in.read(buf); - while (bytesRead >= 0) { - out.write(buf, 0, bytesRead); - if ((ps != null) && ps.checkError()) { - throw new IOException("Unable to write to output stream."); - } - bytesRead = in.read(buf); - } - } - -} http://git-wip-us.apache.org/repos/asf/zookeeper/blob/7291e47c/zookeeper-common/src/main/java/org/apache/zookeeper/common/PathTrie.java ---------------------------------------------------------------------- diff --git a/zookeeper-common/src/main/java/org/apache/zookeeper/common/PathTrie.java b/zookeeper-common/src/main/java/org/apache/zookeeper/common/PathTrie.java deleted file mode 100644 index 73053e0..0000000 --- a/zookeeper-common/src/main/java/org/apache/zookeeper/common/PathTrie.java +++ /dev/null @@ -1,293 +0,0 @@ - /** - * 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.zookeeper.common; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * a class that implements prefix matching for - * components of a filesystem path. the trie - * looks like a tree with edges mapping to - * the component of a path. - * example /ab/bc/cf would map to a trie - * / - * ab/ - * (ab) - * bc/ - * / - * (bc) - * cf/ - * (cf) - */ -public class PathTrie { - /** - * the logger for this class - */ - private static final Logger LOG = LoggerFactory.getLogger(PathTrie.class); - - /** - * the root node of PathTrie - */ - private final TrieNode rootNode ; - - static class TrieNode { - boolean property = false; - final HashMap<String, TrieNode> children; - TrieNode parent = null; - /** - * create a trienode with parent - * as parameter - * @param parent the parent of this trienode - */ - private TrieNode(TrieNode parent) { - children = new HashMap<String, TrieNode>(); - this.parent = parent; - } - - /** - * get the parent of this node - * @return the parent node - */ - TrieNode getParent() { - return this.parent; - } - - /** - * set the parent of this node - * @param parent the parent to set to - */ - void setParent(TrieNode parent) { - this.parent = parent; - } - - /** - * a property that is set - * for a node - making it - * special. - */ - void setProperty(boolean prop) { - this.property = prop; - } - - /** the property of this - * node - * @return the property for this - * node - */ - boolean getProperty() { - return this.property; - } - /** - * add a child to the existing node - * @param childName the string name of the child - * @param node the node that is the child - */ - void addChild(String childName, TrieNode node) { - synchronized(children) { - if (children.containsKey(childName)) { - return; - } - children.put(childName, node); - } - } - - /** - * delete child from this node - * @param childName the string name of the child to - * be deleted - */ - void deleteChild(String childName) { - synchronized(children) { - if (!children.containsKey(childName)) { - return; - } - TrieNode childNode = children.get(childName); - // this is the only child node. - if (childNode.getChildren().length == 1) { - childNode.setParent(null); - children.remove(childName); - } - else { - // their are more child nodes - // so just reset property. - childNode.setProperty(false); - } - } - } - - /** - * return the child of a node mapping - * to the input childname - * @param childName the name of the child - * @return the child of a node - */ - TrieNode getChild(String childName) { - synchronized(children) { - if (!children.containsKey(childName)) { - return null; - } - else { - return children.get(childName); - } - } - } - - /** - * get the list of children of this - * trienode. - * @param node to get its children - * @return the string list of its children - */ - String[] getChildren() { - synchronized(children) { - return children.keySet().toArray(new String[0]); - } - } - - /** - * get the string representation - * for this node - */ - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("Children of trienode: "); - synchronized(children) { - for (String str: children.keySet()) { - sb.append(" " + str); - } - } - return sb.toString(); - } - } - - /** - * construct a new PathTrie with - * a root node of / - */ - public PathTrie() { - this.rootNode = new TrieNode(null); - } - - /** - * add a path to the path trie - * @param path - */ - public void addPath(String path) { - if (path == null) { - return; - } - String[] pathComponents = path.split("/"); - TrieNode parent = rootNode; - String part = null; - if (pathComponents.length <= 1) { - throw new IllegalArgumentException("Invalid path " + path); - } - for (int i=1; i<pathComponents.length; i++) { - part = pathComponents[i]; - if (parent.getChild(part) == null) { - parent.addChild(part, new TrieNode(parent)); - } - parent = parent.getChild(part); - } - parent.setProperty(true); - } - - /** - * delete a path from the trie - * @param path the path to be deleted - */ - public void deletePath(String path) { - if (path == null) { - return; - } - String[] pathComponents = path.split("/"); - TrieNode parent = rootNode; - String part = null; - if (pathComponents.length <= 1) { - throw new IllegalArgumentException("Invalid path " + path); - } - for (int i=1; i<pathComponents.length; i++) { - part = pathComponents[i]; - if (parent.getChild(part) == null) { - //the path does not exist - return; - } - parent = parent.getChild(part); - LOG.info("{}",parent); - } - TrieNode realParent = parent.getParent(); - realParent.deleteChild(part); - } - - /** - * return the largest prefix for the input path. - * @param path the input path - * @return the largest prefix for the input path. - */ - public String findMaxPrefix(String path) { - if (path == null) { - return null; - } - if ("/".equals(path)) { - return path; - } - String[] pathComponents = path.split("/"); - TrieNode parent = rootNode; - List<String> components = new ArrayList<String>(); - if (pathComponents.length <= 1) { - throw new IllegalArgumentException("Invalid path " + path); - } - int i = 1; - String part = null; - StringBuilder sb = new StringBuilder(); - int lastindex = -1; - while((i < pathComponents.length)) { - if (parent.getChild(pathComponents[i]) != null) { - part = pathComponents[i]; - parent = parent.getChild(part); - components.add(part); - if (parent.getProperty()) { - lastindex = i-1; - } - } - else { - break; - } - i++; - } - for (int j=0; j< (lastindex+1); j++) { - sb.append("/" + components.get(j)); - } - return sb.toString(); - } - - /** - * clear all nodes - */ - public void clear() { - for(String child : rootNode.getChildren()) { - rootNode.deleteChild(child); - } - } -} http://git-wip-us.apache.org/repos/asf/zookeeper/blob/7291e47c/zookeeper-common/src/main/java/org/apache/zookeeper/common/PathUtils.java ---------------------------------------------------------------------- diff --git a/zookeeper-common/src/main/java/org/apache/zookeeper/common/PathUtils.java b/zookeeper-common/src/main/java/org/apache/zookeeper/common/PathUtils.java deleted file mode 100644 index 2a6c7ef..0000000 --- a/zookeeper-common/src/main/java/org/apache/zookeeper/common/PathUtils.java +++ /dev/null @@ -1,103 +0,0 @@ - /** - * 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.zookeeper.common; - - -/** - * Path related utilities - */ -public class PathUtils { - - /** validate the provided znode path string - * @param path znode path string - * @param isSequential if the path is being created - * with a sequential flag - * @throws IllegalArgumentException if the path is invalid - */ - public static void validatePath(String path, boolean isSequential) - throws IllegalArgumentException { - validatePath(isSequential? path + "1": path); - } - - /** - * Validate the provided znode path string - * @param path znode path string - * @throws IllegalArgumentException if the path is invalid - */ - public static void validatePath(String path) throws IllegalArgumentException { - if (path == null) { - throw new IllegalArgumentException("Path cannot be null"); - } - if (path.length() == 0) { - throw new IllegalArgumentException("Path length must be > 0"); - } - if (path.charAt(0) != '/') { - throw new IllegalArgumentException( - "Path must start with / character"); - } - if (path.length() == 1) { // done checking - it's the root - return; - } - if (path.charAt(path.length() - 1) == '/') { - throw new IllegalArgumentException( - "Path must not end with / character"); - } - - String reason = null; - char lastc = '/'; - char chars[] = path.toCharArray(); - char c; - for (int i = 1; i < chars.length; lastc = chars[i], i++) { - c = chars[i]; - - if (c == 0) { - reason = "null character not allowed @" + i; - break; - } else if (c == '/' && lastc == '/') { - reason = "empty node name specified @" + i; - break; - } else if (c == '.' && lastc == '.') { - if (chars[i-2] == '/' && - ((i + 1 == chars.length) - || chars[i+1] == '/')) { - reason = "relative paths not allowed @" + i; - break; - } - } else if (c == '.') { - if (chars[i-1] == '/' && - ((i + 1 == chars.length) - || chars[i+1] == '/')) { - reason = "relative paths not allowed @" + i; - break; - } - } else if (c > '\u0000' && c < '\u001f' - || c > '\u007f' && c < '\u009F' - || c > '\ud800' && c < '\uf8ff' - || c > '\ufff0' && c < '\uffff') { - reason = "invalid character @" + i; - break; - } - } - - if (reason != null) { - throw new IllegalArgumentException( - "Invalid path string \"" + path + "\" caused by " + reason); - } - } -} http://git-wip-us.apache.org/repos/asf/zookeeper/blob/7291e47c/zookeeper-common/src/main/java/org/apache/zookeeper/common/Time.java ---------------------------------------------------------------------- diff --git a/zookeeper-common/src/main/java/org/apache/zookeeper/common/Time.java b/zookeeper-common/src/main/java/org/apache/zookeeper/common/Time.java deleted file mode 100644 index 83e53f0..0000000 --- a/zookeeper-common/src/main/java/org/apache/zookeeper/common/Time.java +++ /dev/null @@ -1,52 +0,0 @@ -/** - * 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.zookeeper.common; - -import java.util.Date; - -public class Time { - /** - * Returns time in milliseconds as does System.currentTimeMillis(), - * but uses elapsed time from an arbitrary epoch more like System.nanoTime(). - * The difference is that if somebody changes the system clock, - * Time.currentElapsedTime will change but nanoTime won't. On the other hand, - * all of ZK assumes that time is measured in milliseconds. - * @return The time in milliseconds from some arbitrary point in time. - */ - public static long currentElapsedTime() { - return System.nanoTime() / 1000000; - } - - /** - * Explicitly returns system dependent current wall time. - * @return Current time in msec. - */ - public static long currentWallTime() { - return System.currentTimeMillis(); - } - - /** - * This is to convert the elapsedTime to a Date. - * @return A date object indicated by the elapsedTime. - */ - public static Date elapsedTimeToDate(long elapsedTime) { - long wallTime = currentWallTime() + elapsedTime - currentElapsedTime(); - return new Date(wallTime); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zookeeper/blob/7291e47c/zookeeper-common/src/main/java/org/apache/zookeeper/util/SecurityUtils.java ---------------------------------------------------------------------- diff --git a/zookeeper-common/src/main/java/org/apache/zookeeper/util/SecurityUtils.java b/zookeeper-common/src/main/java/org/apache/zookeeper/util/SecurityUtils.java deleted file mode 100644 index 67484e4..0000000 --- a/zookeeper-common/src/main/java/org/apache/zookeeper/util/SecurityUtils.java +++ /dev/null @@ -1,298 +0,0 @@ -/** - * 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.zookeeper.util; - -import java.security.Principal; -import java.security.PrivilegedActionException; -import java.security.PrivilegedExceptionAction; - -import javax.security.auth.Subject; -import javax.security.auth.callback.CallbackHandler; -import javax.security.sasl.Sasl; -import javax.security.sasl.SaslClient; -import javax.security.sasl.SaslException; -import javax.security.sasl.SaslServer; - -import org.apache.zookeeper.SaslClientCallbackHandler; -import org.apache.zookeeper.server.auth.KerberosName; -import org.ietf.jgss.GSSContext; -import org.ietf.jgss.GSSCredential; -import org.ietf.jgss.GSSException; -import org.ietf.jgss.GSSManager; -import org.ietf.jgss.GSSName; -import org.ietf.jgss.Oid; -import org.slf4j.Logger; - -public final class SecurityUtils { - - public static final String QUORUM_HOSTNAME_PATTERN = "_HOST"; - - /** - * Create an instance of a SaslClient. It will return null if there is an exception. - * - * @param subject subject - * @param servicePrincipal principal - * @param protocol name of the protocol for which the authentication is being performed - * @param serverName name of the server to authenticate to - * @param LOG logger - * @param entity can be either zookeeper client or quorum learner - * - * @return saslclient object - * @throws SaslException - */ - public static SaslClient createSaslClient(final Subject subject, - final String servicePrincipal, final String protocol, - final String serverName, final Logger LOG, final String entity) throws SaslException { - SaslClient saslClient; - // Use subject.getPrincipals().isEmpty() as an indication of which SASL - // mechanism to use: if empty, use DIGEST-MD5; otherwise, use GSSAPI. - if (subject.getPrincipals().isEmpty()) { - // no principals: must not be GSSAPI: use DIGEST-MD5 mechanism - // instead. - LOG.info("{} will use DIGEST-MD5 as SASL mechanism.", entity); - String[] mechs = { "DIGEST-MD5" }; - String username = (String) (subject.getPublicCredentials() - .toArray()[0]); - String password = (String) (subject.getPrivateCredentials() - .toArray()[0]); - // 'domain' parameter is hard-wired between the server and client - saslClient = Sasl.createSaslClient(mechs, username, protocol, - serverName, null, new SaslClientCallbackHandler(password, entity)); - return saslClient; - } else { // GSSAPI. - final Object[] principals = subject.getPrincipals().toArray(); - // determine client principal from subject. - final Principal clientPrincipal = (Principal) principals[0]; - boolean usingNativeJgss = Boolean - .getBoolean("sun.security.jgss.native"); - if (usingNativeJgss) { - // http://docs.oracle.com/javase/6/docs/technotes/guides/security/jgss/jgss-features.html - // """ - // In addition, when performing operations as a particular - // Subject, e.g. Subject.doAs(...) or - // Subject.doAsPrivileged(...), - // the to-be-used GSSCredential should be added to Subject's - // private credential set. Otherwise, the GSS operations will - // fail since no credential is found. - // """ - try { - GSSManager manager = GSSManager.getInstance(); - Oid krb5Mechanism = new Oid("1.2.840.113554.1.2.2"); - GSSCredential cred = manager.createCredential(null, - GSSContext.DEFAULT_LIFETIME, krb5Mechanism, - GSSCredential.INITIATE_ONLY); - subject.getPrivateCredentials().add(cred); - LOG.debug("Added private credential to {} principal name: '{}'", - entity, clientPrincipal); - } catch (GSSException ex) { - LOG.warn("Cannot add private credential to subject; " - + "authentication at the server may fail", ex); - } - } - final KerberosName clientKerberosName = new KerberosName( - clientPrincipal.getName()); - // assume that server and client are in the same realm (by default; - // unless the system property - // "zookeeper.server.realm" is set). - String serverRealm = System.getProperty("zookeeper.server.realm", - clientKerberosName.getRealm()); - KerberosName serviceKerberosName = new KerberosName( - servicePrincipal + "@" + serverRealm); - final String serviceName = serviceKerberosName.getServiceName(); - final String serviceHostname = serviceKerberosName.getHostName(); - final String clientPrincipalName = clientKerberosName.toString(); - try { - saslClient = Subject.doAs(subject, - new PrivilegedExceptionAction<SaslClient>() { - public SaslClient run() throws SaslException { - LOG.info("{} will use GSSAPI as SASL mechanism.", entity); - String[] mechs = { "GSSAPI" }; - LOG.debug("creating sasl client: {}={};service={};serviceHostname={}", - new Object[] { entity, clientPrincipalName, serviceName, serviceHostname }); - SaslClient saslClient = Sasl.createSaslClient( - mechs, clientPrincipalName, serviceName, - serviceHostname, null, - new SaslClientCallbackHandler(null, entity)); - return saslClient; - } - }); - return saslClient; - } catch (Exception e) { - LOG.error("Exception while trying to create SASL client", e); - return null; - } - } - } - - /** - * Create an instance of a SaslServer. It will return null if there is an exception. - * - * @param subject subject - * @param protocol protocol - * @param serverName server name - * @param callbackHandler login callback handler - * @param LOG logger - * @return sasl server object - */ - public static SaslServer createSaslServer(final Subject subject, - final String protocol, final String serverName, - final CallbackHandler callbackHandler, final Logger LOG) { - if (subject != null) { - // server is using a JAAS-authenticated subject: determine service - // principal name and hostname from zk server's subject. - if (subject.getPrincipals().size() > 0) { - try { - final Object[] principals = subject.getPrincipals() - .toArray(); - final Principal servicePrincipal = (Principal) principals[0]; - - // e.g. servicePrincipalNameAndHostname := - // "zookeeper/myhost.foo....@foo.com" - final String servicePrincipalNameAndHostname = servicePrincipal - .getName(); - - int indexOf = servicePrincipalNameAndHostname.indexOf("/"); - - // e.g. servicePrincipalName := "zookeeper" - final String servicePrincipalName = servicePrincipalNameAndHostname - .substring(0, indexOf); - - // e.g. serviceHostnameAndKerbDomain := - // "myhost.foo....@foo.com" - final String serviceHostnameAndKerbDomain = servicePrincipalNameAndHostname - .substring(indexOf + 1, - servicePrincipalNameAndHostname.length()); - - indexOf = serviceHostnameAndKerbDomain.indexOf("@"); - // e.g. serviceHostname := "myhost.foo.com" - final String serviceHostname = serviceHostnameAndKerbDomain - .substring(0, indexOf); - - // TODO: should depend on zoo.cfg specified mechs, but if - // subject is non-null, it can be assumed to be GSSAPI. - final String mech = "GSSAPI"; - - LOG.debug("serviceHostname is '" + serviceHostname + "'"); - LOG.debug("servicePrincipalName is '" + servicePrincipalName - + "'"); - LOG.debug("SASL mechanism(mech) is '" + mech + "'"); - - boolean usingNativeJgss = Boolean - .getBoolean("sun.security.jgss.native"); - if (usingNativeJgss) { - // http://docs.oracle.com/javase/6/docs/technotes/guides/security/jgss/jgss-features.html - // """ - // In addition, when performing operations as a - // particular - // Subject, e.g. Subject.doAs(...) or - // Subject.doAsPrivileged(...), the to-be-used - // GSSCredential should be added to Subject's - // private credential set. Otherwise, the GSS operations - // will fail since no credential is found. - // """ - try { - GSSManager manager = GSSManager.getInstance(); - Oid krb5Mechanism = new Oid("1.2.840.113554.1.2.2"); - GSSName gssName = manager.createName( - servicePrincipalName + "@" - + serviceHostname, - GSSName.NT_HOSTBASED_SERVICE); - GSSCredential cred = manager.createCredential( - gssName, GSSContext.DEFAULT_LIFETIME, - krb5Mechanism, GSSCredential.ACCEPT_ONLY); - subject.getPrivateCredentials().add(cred); - LOG.debug("Added private credential to service principal name: '{}'," - + " GSSCredential name: {}", servicePrincipalName, cred.getName()); - } catch (GSSException ex) { - LOG.warn("Cannot add private credential to subject; " - + "clients authentication may fail", ex); - } - } - try { - return Subject.doAs(subject, - new PrivilegedExceptionAction<SaslServer>() { - public SaslServer run() { - try { - SaslServer saslServer; - saslServer = Sasl.createSaslServer( - mech, servicePrincipalName, - serviceHostname, null, - callbackHandler); - return saslServer; - } catch (SaslException e) { - LOG.error("Zookeeper Server failed to create a SaslServer to interact with a client during session initiation: ", e); - return null; - } - } - }); - } catch (PrivilegedActionException e) { - // TODO: exit server at this point(?) - LOG.error("Zookeeper Quorum member experienced a PrivilegedActionException exception while creating a SaslServer using a JAAS principal context:", e); - } - } catch (IndexOutOfBoundsException e) { - LOG.error("server principal name/hostname determination error: ", e); - } - } else { - // JAAS non-GSSAPI authentication: assuming and supporting only - // DIGEST-MD5 mechanism for now. - // TODO: use 'authMech=' value in zoo.cfg. - try { - SaslServer saslServer = Sasl.createSaslServer("DIGEST-MD5", - protocol, serverName, null, callbackHandler); - return saslServer; - } catch (SaslException e) { - LOG.error("Zookeeper Quorum member failed to create a SaslServer to interact with a client during session initiation", e); - } - } - } - return null; - } - - /** - * Convert Kerberos principal name pattern to valid Kerberos principal name. - * If the principal name contains hostname pattern "_HOST" then it replaces - * with the given hostname, which should be fully-qualified domain name. - * - * @param principalConfig - * the Kerberos principal name conf value to convert - * @param hostname - * the fully-qualified domain name used for substitution - * @return converted Kerberos principal name - */ - public static String getServerPrincipal(String principalConfig, - String hostname) { - String[] components = getComponents(principalConfig); - if (components == null || components.length != 2 - || !components[1].equals(QUORUM_HOSTNAME_PATTERN)) { - return principalConfig; - } else { - return replacePattern(components, hostname); - } - } - - private static String[] getComponents(String principalConfig) { - if (principalConfig == null) - return null; - return principalConfig.split("[/]"); - } - - private static String replacePattern(String[] components, String hostname) { - return components[0] + "/" + hostname.toLowerCase(); - } -} http://git-wip-us.apache.org/repos/asf/zookeeper/blob/7291e47c/zookeeper-common/src/main/java/org/apache/zookeeper/version/util/VerGen.java ---------------------------------------------------------------------- diff --git a/zookeeper-common/src/main/java/org/apache/zookeeper/version/util/VerGen.java b/zookeeper-common/src/main/java/org/apache/zookeeper/version/util/VerGen.java deleted file mode 100644 index 7285a2b..0000000 --- a/zookeeper-common/src/main/java/org/apache/zookeeper/version/util/VerGen.java +++ /dev/null @@ -1,178 +0,0 @@ -/** - * 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.zookeeper.version.util; - -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class VerGen { - private static final String PACKAGE_NAME = "org.apache.zookeeper.version"; - private static final String TYPE_NAME = "Info"; - - static void printUsage() { - System.out.print("Usage:\tjava -cp <classpath> org.apache.zookeeper." - + "version.util.VerGen maj.min.micro[-qualifier] rev buildDate"); - System.exit(1); - } - - public static void generateFile(File outputDir, Version version, String rev, String buildDate) throws IOException - { - String path = PACKAGE_NAME.replaceAll("\\.", "/"); - File pkgdir = new File(outputDir, path); - if (!pkgdir.exists()) { - // create the pkg directory - boolean ret = pkgdir.mkdirs(); - if (!ret) { - System.out.println("Cannnot create directory: " + path); - System.exit(1); - } - } else if (!pkgdir.isDirectory()) { - // not a directory - System.out.println(path + " is not a directory."); - System.exit(1); - } - File file = new File(pkgdir, TYPE_NAME + ".java"); - FileWriter w = null; - try { - w = new FileWriter(file); - w.write("// Do not edit!\n// File generated by org.apache.zookeeper" - + ".version.util.VerGen.\n"); - w.write("/**\n"); - w.write("* Licensed to the Apache Software Foundation (ASF) under one\n"); - w.write("* or more contributor license agreements. See the NOTICE file\n"); - w.write("* distributed with this work for additional information\n"); - w.write("* regarding copyright ownership. The ASF licenses this file\n"); - w.write("* to you under the Apache License, Version 2.0 (the\n"); - w.write("* \"License\"); you may not use this file except in compliance\n"); - w.write("* with the License. You may obtain a copy of the License at\n"); - w.write("*\n"); - w.write("* http://www.apache.org/licenses/LICENSE-2.0\n"); - w.write("*\n"); - w.write("* Unless required by applicable law or agreed to in writing, software\n"); - w.write("* distributed under the License is distributed on an \"AS IS\" BASIS,\n"); - w.write("* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"); - w.write("* See the License for the specific language governing permissions and\n"); - w.write("* limitations under the License.\n"); - w.write("*/\n"); - w.write("\n"); - w.write("package " + PACKAGE_NAME + ";\n\n"); - w.write("public interface " + TYPE_NAME + " {\n"); - w.write(" int MAJOR=" + version.maj + ";\n"); - w.write(" int MINOR=" + version.min + ";\n"); - w.write(" int MICRO=" + version.micro + ";\n"); - w.write(" String QUALIFIER=" - + (version.qualifier == null ? null : - "\"" + version.qualifier + "\"") - + ";\n"); - if (rev.equals("-1")) { - System.out.println("Unknown REVISION number, using " + rev); - } - w.write(" int REVISION=-1; //TODO: remove as related to SVN VCS\n"); - w.write(" String REVISION_HASH=\"" + rev + "\";\n"); - w.write(" String BUILD_DATE=\"" + buildDate - + "\";\n"); - w.write("}\n"); - } finally { - if (w != null) { - try { - w.close(); - } catch (IOException e) { - System.out.println("Unable to close file writer" - + e.getMessage()); - } - } - } - } - - public static class Version { - public int maj; - public int min; - public int micro; - public String qualifier; - } - - public static Version parseVersionString(String input) { - Version result = new Version(); - - Pattern p = Pattern.compile("^(\\d+)\\.(\\d+)\\.(\\d+)((\\.\\d+)*)(-(.+))?$"); - Matcher m = p.matcher(input); - - if (!m.matches()) { - return null; - } - result.maj = Integer.parseInt(m.group(1)); - result.min = Integer.parseInt(m.group(2)); - result.micro = Integer.parseInt(m.group(3)); - if (m.groupCount() == 7) { - result.qualifier = m.group(7); - } else { - result.qualifier = null; - } - return result; - } - - /** - * Emits a org.apache.zookeeper.version.Info interface file with version and - * revision information constants set to the values passed in as command - * line parameters. The file is created in the current directory. <br> - * Usage: java org.apache.zookeeper.version.util.VerGen maj.min.micro[-qualifier] - * rev buildDate - * - * @param args - * <ul> - * <li>maj - major version number - * <li>min - minor version number - * <li>micro - minor minor version number - * <li>qualifier - optional qualifier (dash followed by qualifier text) - * <li>rev - current Git revision number - * <li>buildDate - date the build - * </ul> - */ - public static void main(String[] args) { - if (args.length != 3) - printUsage(); - try { - Version version = parseVersionString(args[0]); - if (version == null) { - System.err.println( - "Invalid version number format, must be \"x.y.z(-.*)?\""); - System.exit(1); - } - String rev = args[1]; - if (rev == null || rev.trim().isEmpty()) { - rev = "-1"; - } else { - rev = rev.trim(); - } - generateFile(new File("."), version, rev, args[2]); - } catch (NumberFormatException e) { - System.err.println( - "All version-related parameters must be valid integers!"); - throw e; - } catch (IOException e) { - System.out.println("Unable to generate version.Info file: " - + e.getMessage()); - System.exit(1); - } - } - -} http://git-wip-us.apache.org/repos/asf/zookeeper/blob/7291e47c/zookeeper-common/src/main/resources/lib/cobertura/README.txt ---------------------------------------------------------------------- diff --git a/zookeeper-common/src/main/resources/lib/cobertura/README.txt b/zookeeper-common/src/main/resources/lib/cobertura/README.txt deleted file mode 100644 index f5ba88f..0000000 --- a/zookeeper-common/src/main/resources/lib/cobertura/README.txt +++ /dev/null @@ -1,3 +0,0 @@ -Download the cobertura binary from the following location and unpack it into this directory. Run "cobertura-report" target from build.xml to generate coverage report. - -http://cobertura.sourceforge.net/download.html