changeset 6:37fc9235b1cf

* support to check for existence of RRDs
author Peter Stamfest <peter@stamfest.at>
date Thu, 02 Sep 2010 06:17:59 +0200
parents e2df1d1a3af5
children 90a4a69c54c6
files src/net/stamfest/rrd/RRDCommandPool.java src/net/stamfest/rrd/RRDToolService.java src/net/stamfest/rrd/RRDp.java
diffstat 3 files changed, 177 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/src/net/stamfest/rrd/RRDCommandPool.java	Mon Aug 30 18:54:27 2010 +0200
+++ b/src/net/stamfest/rrd/RRDCommandPool.java	Thu Sep 02 06:17:59 2010 +0200
@@ -1,11 +1,29 @@
 package net.stamfest.rrd;
 
+class RRDPoolMember {
+    int requestcount = 0;
+    boolean inuse = false;
+    RRDCommand rrdp;
+}
+
+class RRDPoolMemberWrapper implements RRDCommand {
+    RRDPoolMember member;
+
+    public RRDPoolMemberWrapper(RRDPoolMember member) {
+	this.member = member;
+    }
+
+    @Override
+    public void finish() {
+    }
+
+    @Override
+    public CommandResult command(String[] cmd) throws Exception {
+	return member.rrdp.command(cmd);
+    }
+};
+
 public class RRDCommandPool implements RRDCommand {
-    class RRDPoolMember {
-	int requestcount = 0;
-	boolean inuse = false;
-	RRDCommand rrdp;
-    }
     
     private int poolsize = 4;
     private int maxRequestsPerMember = 100;
@@ -35,12 +53,11 @@
 	};
     }
 
-    @Override
-    public CommandResult command(String[] cmd) throws Exception {
+    
+    public RRDCommand getConnection() throws Exception {
+	if (finished) throw new Exception("Already finished");
 	RRDPoolMember member = null;
 
-	if (finished) throw new Exception("Already finished");
-
 	synchronized(this) {
 	    while (true) {
 		for (int i = 0 ; i < poolsize ; i++) {
@@ -67,11 +84,16 @@
 	    }
 	}
 	
-	CommandResult r = null;
-	if (member != null) {
-	    member.requestcount++;
-	    if (member.rrdp == null) member.rrdp = factory.createRRDCommand();
-	    r = member.rrdp.command(cmd);
+	member.requestcount++;
+	if (member.rrdp == null) member.rrdp = factory.createRRDCommand();
+	
+	
+	return new RRDPoolMemberWrapper(member);
+    }
+    
+    public void done(RRDCommand cmd) {
+	if (cmd instanceof RRDPoolMemberWrapper) {
+	    RRDPoolMember member = ((RRDPoolMemberWrapper) cmd).member;
 	    
 	    if (member.requestcount > maxRequestsPerMember) {
 		member.rrdp.finish();
@@ -84,8 +106,17 @@
 		notify();
 	    }
 	}
-	
-	return r;
+    }
+    
+    
+    @Override
+    public CommandResult command(String[] cmd) throws Exception {
+	RRDCommand rrdcmd = getConnection();
+	try {
+	    return rrdcmd.command(cmd);
+	} finally {
+	    done(rrdcmd);
+	}
     }
 
     public int getMaxRequestsPerMember() {
--- a/src/net/stamfest/rrd/RRDToolService.java	Mon Aug 30 18:54:27 2010 +0200
+++ b/src/net/stamfest/rrd/RRDToolService.java	Thu Sep 02 06:17:59 2010 +0200
@@ -1,6 +1,11 @@
 package net.stamfest.rrd;
 
+import java.io.BufferedReader;
+import java.io.File;
 import java.io.IOException;
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * 
@@ -16,26 +21,26 @@
  */
 
 public class RRDToolService implements RRD {
-    RRDCommand backend;
+    RRDCommandPool pool;
     
     public RRDToolService(String basedir) throws IOException {
-	this.backend = new RRDp(basedir);
+	this.pool = new RRDCommandPool(basedir);
     }
     
-    public RRDToolService(RRDCommand backend) {
-	this.backend = backend;
+    public RRDToolService(RRDCommandPool pool) {
+	this.pool = pool;
     }
     
     /* (non-Javadoc)
      * @see net.stamfest.rrd.RRD#info(java.lang.String)
      */
     public CommandResult info(String filename) throws Exception {
-	return backend.command(new String[] { "info", filename });
+	return pool.command(new String[] { "info", filename });
     }
 
     public CommandResult update(String filename, String arg) throws Exception {
 	String cmd[] = new String[] { "update", filename, arg };
-	return backend.command(cmd);
+	return pool.command(cmd);
     }
     
     public CommandResult update(String filename, String args[]) throws Exception {
@@ -45,7 +50,7 @@
 	for (int i = 0, j = 2 ; i < args.length ; i++, j++) {
 	    cmd[j] = args[i];
 	}
-	return backend.command(cmd);
+	return pool.command(cmd);
     }
     
     /* (non-Javadoc)
@@ -60,19 +65,80 @@
 	    cmd[j] = cmdin[i];
 	}
 
-	return backend.command(cmd);
+	return pool.command(cmd);
     }
 
     @Override
     protected void finalize() throws Throwable {
         super.finalize();
-        backend.finish();
+        pool.finish();
     }
 
+    static final String pwd[] = new String[] { "pwd" };
+    public static String getCwd(RRDCommand rrdcmd) throws Exception {
+	CommandResult r = rrdcmd.command(pwd);
+	if (! r.ok) throw new Exception("Problem executing 'pwd' command");
+	
+	return r.output.trim();
+    }
+    
+    public static boolean chdir(RRDCommand rrdcmd, String dir) throws Exception {
+	String cmd[] = new String[] { "cd", dir	};
+	CommandResult r = rrdcmd.command(cmd);
+	return r.ok;
+    }
+    
+    
+    private List<String> splitFilename(String filename) {
+	ArrayList<String> l = new ArrayList<String>();
+	int i,st = 0, len = filename.length();
+	
+	for (i = 0 ; i < len ; i++) {
+	    if (filename.charAt(i) == File.separatorChar) {
+		if (st != i) {
+		    String e = filename.substring(st, i);
+		    /* handle "." and ".." directory entries */
+		    if (! e.equals(".")) {
+			if (e.equals("..")) {
+			    if (l.size() > 0) l.remove(l.size() - 1);
+			} else {
+			    l.add(e);
+			}
+		    }
+		}
+		st = i + 1;
+	    }
+	}
+	return l;
+    }
+    
     @Override
     public CommandResult create(String filename, String[] args)
 	throws Exception
     {
+	// 	split filename 
+	List<String> pel = splitFilename(filename);
+	
+	RRDCommand rrdcmd = pool.getConnection();
+	String cwd = null;
+	try {
+	    cwd = getCwd(rrdcmd);
+	
+	    if (pel.size() > 0) pel.remove(pel.size() - 1);
+	    for (String pe : pel) { 
+		if (chdir(rrdcmd, pe)) continue;
+		if (!mkdir(rrdcmd, pe)) throw new Exception("Cannot mkdir directory");
+		chdir(rrdcmd, pe);
+	    }
+	} finally {
+	    try {
+		if (cwd != null) rrdcmd.command(new String[] { "cd", cwd });
+	    } finally {
+		pool.done(rrdcmd);
+	    }
+	}
+	
+	
 	String cmd[] = new String[args.length + 2];
 	cmd[0] = "create";
 	cmd[1] = filename;
@@ -81,6 +147,54 @@
 	    cmd[j] = args[i];
 	}
 
-	return backend.command(cmd);
+	return pool.command(cmd);
+    }
+    
+    public static boolean mkdir(RRDCommand rrdcmd, String dir) throws Exception {
+	CommandResult r = rrdcmd.command(new String[] { "mkdir", dir });
+	if (r == null) return false;
+	return r.ok;
     }
+
+    public boolean exists(String filename) throws Exception {
+	if (!filename.endsWith(".rrd")) 
+	    throw new Exception("Can only check for the existance of .rrd files");
+	
+	File f = new File(filename);
+	File dir = f.getParentFile();
+	
+	String dirstr = dir.getPath();
+	// line we await in the output of the ls command.
+	String await = "- " + f.getName();
+	
+	RRDCommand rrdcmd = pool.getConnection();
+	String current = null;
+	try {
+	    current = getCwd(rrdcmd);
+	    String cmd[] = new String[] { "ls" };
+
+	    if (! chdir(rrdcmd, dirstr)) return false;
+	
+	    CommandResult r = rrdcmd.command(cmd);
+	    // parse output - a directory listing
+	    if (! r.ok) return false;
+	    // System.err.println("ls OUTPUT:\n" + r.output);
+	    
+	    BufferedReader br = new BufferedReader(new StringReader(r.output));
+	    String line;
+	    while ((line = br.readLine()) != null) {
+		if (line.equals(await)) return true;
+	    }
+	    
+	    return false;	    
+	} finally {
+	    try {
+		if (current != null) chdir(rrdcmd, current);
+	    } finally {
+		pool.done(rrdcmd);
+	    }
+	    
+	}
+    }
+    
 }
--- a/src/net/stamfest/rrd/RRDp.java	Mon Aug 30 18:54:27 2010 +0200
+++ b/src/net/stamfest/rrd/RRDp.java	Thu Sep 02 06:17:59 2010 +0200
@@ -41,7 +41,7 @@
     static private Pattern okpat   = Pattern.compile("^OK u:([0-9.]+) s:([0-9.]+) r:([0-9.]+)"); 
     static private Pattern infoPat = Pattern.compile("^\\s*(.*?)\\s*=\\s*(.*?)\\s*$");
     protected synchronized CommandResult sendCommand(String command[]) throws Exception {
-	if (command == null || command.length <= 1) {
+	if (command == null || command.length == 0) {
 	    throw new IllegalArgumentException();
 	}
 	
@@ -120,6 +120,11 @@
 	}
 	r.output = sb.toString();
 	r.info = data;
+	
+	if (logger.isLoggable(Level.FINER)) {
+	    logger.finer(r.ok + ":" + r.error);
+	}
+	
 	return r;
     }