[Mulgara-svn] r399 - branches/nw-interface/src/jar/query/java/org/mulgara/query/ast

pag at mulgara.org pag at mulgara.org
Wed Sep 5 20:11:47 UTC 2007


Author: pag
Date: 2007-09-05 15:11:46 -0500 (Wed, 05 Sep 2007)
New Revision: 399

Modified:
   branches/nw-interface/src/jar/query/java/org/mulgara/query/ast/Backup.java
Log:
Added execute functionality from ItqlInterpreter, and a static method for direct access into the backup function.

Modified: branches/nw-interface/src/jar/query/java/org/mulgara/query/ast/Backup.java
===================================================================
--- branches/nw-interface/src/jar/query/java/org/mulgara/query/ast/Backup.java	2007-08-31 08:23:13 UTC (rev 398)
+++ branches/nw-interface/src/jar/query/java/org/mulgara/query/ast/Backup.java	2007-09-05 20:11:46 UTC (rev 399)
@@ -11,12 +11,24 @@
  */
 package org.mulgara.query.ast;
 
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.rmi.NoSuchObjectException;
+import java.rmi.RemoteException;
+import java.rmi.server.UnicastRemoteObject;
 
 import org.apache.log4j.Logger;
 import org.mulgara.connection.Connection;
+import org.mulgara.query.QueryException;
 
+import edu.emory.mathcs.util.remote.io.RemoteOutputStream;
+import edu.emory.mathcs.util.remote.io.server.impl.RemoteOutputStreamSrvImpl;
+
 /**
  * Represents a command to back data up from a model.
  *
@@ -26,6 +38,8 @@
  * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
  */
 public class Backup extends DataTx {
+  
+  private static final String FILE = "file";
 
   /** The logger */
   static final Logger logger = Logger.getLogger(Backup.class.getName());
@@ -41,6 +55,7 @@
    */
   public Backup(URI source, URI destination) {
     super(source, destination);
+    if (!destination.getScheme().equals(FILE)) throw new IllegalArgumentException("Backups must be sent to a file");
     calcServerUri(source);
   }
 
@@ -51,17 +66,75 @@
     return serverUri;
   }
 
-  public Object execute(Connection conn) throws Exception {
-    // TODO Auto-generated method stub
+
+  /**
+   * Perform a backup on a server.
+   * @param conn The connection to talk to the server on.
+   * @return Nothing.
+   * @throws QueryException There was an error asking the server to perform the backup.
+   * @throws MalformedURLException The destination is not a valid file.
+   */
+  public Object execute(Connection conn) throws QueryException, MalformedURLException {
+    FileOutputStream fileOutputStream = null;
+    String destinationFile = this.getDestination().toURL().getPath();
+    try {
+      fileOutputStream = new FileOutputStream(destinationFile);
+    } catch (FileNotFoundException ex) {
+      throw new QueryException("File " + destinationFile + " cannot be created for backup. ", ex);
+    }
+
+    backup(conn, serverUri, fileOutputStream);
+
     return null;
   }
 
-  
+
   /**
+   * Public interface to perform a backup into an output stream.
+   * This is callable directly, without an AST interface.
+   * @param conn The connection to a server to be backed up.
+   * @param source The URI describing the graph on the server to back up.
+   * @param outputStream The output which will receive the data to be backed up.
+   * @throws QueryException There was an error asking the server to perform the backup.
+   */
+  public static void backup(Connection conn, URI source, OutputStream outputStream) throws QueryException {
+    // open and wrap the outputstream
+    RemoteOutputStreamSrvImpl srv = new RemoteOutputStreamSrvImpl(outputStream);
+
+    // prepare it for exporting
+    try {
+      UnicastRemoteObject.exportObject(srv);
+    } catch (RemoteException rex) {
+      throw new QueryException("Unable to backup "+ source + " to an output stream", rex);
+    }
+
+    outputStream = new RemoteOutputStream(srv);
+
+    // perform the backup
+    try {
+      conn.getSession().backup(source, outputStream);
+    } finally {
+      // cleanup the output
+      if (outputStream != null) {
+        try {
+          outputStream.close();
+        } catch (IOException ioe ) {};
+      }
+      // cleanup the RMI for the output stream
+      if (srv != null) {
+        try {
+          UnicastRemoteObject.unexportObject(srv, false);
+        } catch (NoSuchObjectException ex) {};
+      }
+    }
+  }
+
+
+  /**
    * Sets the server URI for this server operation.
    * @param uri The URI to determine the server URI from.
    */
-  private void calcServerUri(URI uri) {
+  private URI calcServerUri(URI uri) {
     assert serverUri != null;
     // check if backing up a graph or a server
     String fragment = uri.getFragment();
@@ -76,5 +149,6 @@
         throw new Error("Unable to truncate a fragment from a valid URI");
       }
     }
+    return serverUri;
   }
 }




More information about the Mulgara-svn mailing list