GridLab
Grid Application Toolkit

A simple API for Grid Applications
GAT

Menu



Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members  

File.java

Go to the documentation of this file.
00001 package org.gridlab.gat.io;
00002 
00003 import java.io.IOException;
00004 import java.io.Serializable;
00005 import java.net.URI;
00006 
00007 import org.gridlab.gat.GATContext;
00008 import org.gridlab.gat.Preferences;
00009 import org.gridlab.gat.engine.GATEngine;
00010 import org.gridlab.gat.io.cpi.FileCpi;
00011 import org.gridlab.gat.monitoring.Monitorable;
00012 import org.gridlab.gat.net.RemoteException;
00013 
00014 /**
00015  * An abstract representation of a physical file.
00016  * <p>
00017  * An instance of this class presents an abstract, system-independent view of a
00018  * physical file. User interfaces and operating systems use system-dependent
00019  * pathname strings to identify physical files. GAT, however, uses an operating
00020  * system independent pathname string to identify a physical file. A physical
00021  * file in GAT is identified by a URI.
00022  * <p>
00023  * An instance of this File class allows for various high-level operations to be
00024  * preformed on a physical file. For example, one can, with a single API call,
00025  * copy a physical file from one location to a second location, move a physical
00026  * file from one location to a second location, delete a physical file, and
00027  * preform various other operations on a physical file. The utility of this
00028  * high-level view of a physical file is multi-fold. The client of an instance
00029  * of this class does not have to concern themselves with the details of reading
00030  * every single byte of a physical file when all they wish to do is copy the
00031  * physical file to a new location. Similarly, a client does not have to deal
00032  * with all the various error states that can occur when moving a physical file (
00033  * Have all the various bytes been read correctly? Have all the various bytes
00034  * been saved correctly? Did the deletion of the original file proceed
00035  * correctly? ); the client simply has to call a single API call and the
00036  * physical file is moved.
00037  */
00038 public abstract class File extends java.io.File implements Monitorable, Serializable {
00039 
00040     protected URI location;
00041     protected GATContext gatContext;
00042     protected Preferences preferences;
00043 
00044     /**
00045      * Constructs a File instance which corresponds to the physical file
00046      * identified by the passed URI and whose access rights are determined
00047      * by the passed GATContext.
00048      * 
00049      * @param location
00050      *            A URI which represents the URI corresponding to the
00051      *            physical file.
00052      * @param gatContext
00053      *            A GATContext which is used to determine the access rights for
00054      *            this File.
00055      * @param preferences
00056      *            A Preferences which is used to determine the user's
00057      *            preferences for this File.
00058      * @throws java.lang.Exception
00059      *             Thrown upon creation problems
00060      */
00061     protected File(GATContext gatContext, Preferences preferences,
00062             URI location) {
00063         super(location);
00064         this.location = location;
00065         this.gatContext = gatContext;
00066         this.preferences = preferences;
00067     }
00068 
00069     /**
00070      * Constructs a File instance which corresponds to the physical file
00071      * identified by the passed URI and whose access rights are determined
00072      * by the passed GATContext.
00073      * 
00074      * @param location
00075      *            A URI which represents the URI corresponding to the
00076      *            physical file.
00077      * @param gatContext
00078      *            A GATContext which is used to determine the access rights for
00079      *            this File.
00080      * @throws java.lang.Exception
00081      *             Thrown upon creation problems
00082      */
00083     public static File create(GATContext gatContext, URI location) {
00084         return create(gatContext, null, location);
00085     }
00086 
00087     /**
00088      * Constructs a File instance which corresponds to the physical file
00089      * identified by the passed URI and whose access rights are determined
00090      * by the passed GATContext.
00091      * 
00092      * @param location
00093      *            A URI which represents the URI corresponding to the
00094      *            physical file.
00095      * @param gatContext
00096      *            A GATContext which is used to determine the access rights for
00097      *            this File.
00098      * @param preferences
00099      *            A Preferences which is used to determine the user's
00100      *            preferences for this File.
00101      * @throws java.lang.Exception
00102      *             Thrown upon creation problems
00103      */
00104     public static File create(GATContext gatContext, Preferences preferences,
00105             URI location) {
00106         
00107         // Make life a bit easier for the programmer:
00108         // If the URI does not have a scheme part, just consider it a local file.
00109         // The ctor of java.io.file does not accept this.
00110         URI tmpLocation = location;
00111         if(tmpLocation.getScheme() == null) {
00112             java.io.File tmp = new java.io.File(tmpLocation.toString());
00113             tmpLocation = tmp.toURI();
00114         }
00115         
00116         GATEngine gatEngine = GATEngine.getGATEngine();
00117         Object[] array = new Object[1];
00118         array[0] = tmpLocation;
00119         FileCpi f = (FileCpi) gatEngine.getAdaptor(FileCpi.class,
00120                 gatContext, preferences, array);
00121         return f;
00122     }
00123 
00124     /**
00125      * Tests this File for equality with the passed Object.
00126      * <p>
00127      * If the given object is not a File, then this method immediately returns
00128      * false.
00129      * <p>
00130      * If the given object is a File, then it is deemed equal to this instance
00131      * if a URI object constructed from this File's location and a URI
00132      * object constructed from the passed File's URI are equal as
00133      * determined by the Equals method of URI.
00134      * 
00135      * @param object
00136      *            The Object to test for equality
00137      * @return A boolean indicating equality
00138      */
00139     public boolean equals(Object object) {
00140         if (!(object instanceof org.gridlab.gat.io.File)) return false;
00141 
00142         org.gridlab.gat.io.File file = (org.gridlab.gat.io.File) object;
00143         return location.equals(file.getURI());
00144     }
00145 
00146     /**
00147      * This method returns the URI of this File
00148      * 
00149      * @return The URI of this File
00150      */
00151     public URI getURI() {
00152         return location;
00153     }
00154 
00155     /**
00156      * This method copies the physical file represented by this File instance to
00157      * a physical file identified by the passed URI.
00158      * 
00159      * @param loc
00160      *            The new location
00161      * @throws java.rmi.RemoteException
00162      *             Thrown upon problems accessing the remote instance
00163      * @throws java.io.IOException
00164      *             Upon non-remote IO problem
00165      */
00166     public abstract void copy(URI loc) throws IOException, RemoteException;
00167 
00168     /**
00169      * This method moves the physical file represented by this File instance to
00170      * a physical file identified by the passed URI.
00171      * 
00172      * @param location
00173      *            The URI to which to move the physical file corresponding
00174      *            to this File instance
00175      * @throws java.rmi.RemoteException
00176      *             Thrown upon problems accessing the remote instance
00177      * @throws java.io.IOException
00178      *             Upon non-remote IO problem
00179      */
00180     public void move(URI location) throws IOException {
00181         // Step 1: Copy the original file
00182         copy(location);
00183 
00184         // Step 2: Delete the original file
00185         delete();
00186 
00187         // Step 3: Update location
00188         this.location = location;
00189     }
00190 }