You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@twill.apache.org by ch...@apache.org on 2014/04/26 10:16:20 UTC

git commit: (TWILL-76) Improve Location class be able to return LocationFactory that used to create the Location instance. It's convenient to create more Location by just passing a Location around.

Repository: incubator-twill
Updated Branches:
  refs/heads/master fc265f1a2 -> 60d157ff4


(TWILL-76) Improve Location class be able to return LocationFactory that used to create the Location instance. It's convenient to create more Location by just passing a Location around.

Signed-off-by: Terence Yim <ch...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/incubator-twill/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-twill/commit/60d157ff
Tree: http://git-wip-us.apache.org/repos/asf/incubator-twill/tree/60d157ff
Diff: http://git-wip-us.apache.org/repos/asf/incubator-twill/diff/60d157ff

Branch: refs/heads/master
Commit: 60d157ff46649069c0c1e14af6b485b558c96dd8
Parents: fc265f1
Author: Terence Yim <ch...@apache.org>
Authored: Sat Apr 26 00:59:30 2014 -0700
Committer: Terence Yim <ch...@apache.org>
Committed: Sat Apr 26 01:16:13 2014 -0700

----------------------------------------------------------------------
 .../apache/twill/filesystem/LocalLocation.java  | 20 ++++++++++++-----
 .../twill/filesystem/LocalLocationFactory.java  |  8 +++----
 .../org/apache/twill/filesystem/Location.java   |  7 ++++++
 .../apache/twill/filesystem/HDFSLocation.java   | 23 +++++++++++++-------
 .../twill/filesystem/HDFSLocationFactory.java   | 10 ++++-----
 5 files changed, 45 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-twill/blob/60d157ff/twill-common/src/main/java/org/apache/twill/filesystem/LocalLocation.java
----------------------------------------------------------------------
diff --git a/twill-common/src/main/java/org/apache/twill/filesystem/LocalLocation.java b/twill-common/src/main/java/org/apache/twill/filesystem/LocalLocation.java
index c4e3dbf..6dec09c 100644
--- a/twill-common/src/main/java/org/apache/twill/filesystem/LocalLocation.java
+++ b/twill-common/src/main/java/org/apache/twill/filesystem/LocalLocation.java
@@ -39,14 +39,17 @@ import java.util.UUID;
  */
 final class LocalLocation implements Location {
   private final File file;
+  private final LocalLocationFactory locationFactory;
 
   /**
    * Constructs a LocalLocation.
    *
+   * @param locationFactory The {@link LocationFactory} instance used to create this instance.
    * @param file to the file.
    */
-  LocalLocation(File file) {
+  LocalLocation(LocalLocationFactory locationFactory, File file) {
     this.file = file;
+    this.locationFactory = locationFactory;
   }
 
   /**
@@ -119,13 +122,13 @@ final class LocalLocation implements Location {
    */
   @Override
   public Location append(String child) throws IOException {
-    return new LocalLocation(new File(file, child));
+    return new LocalLocation(locationFactory, new File(file, child));
   }
 
   @Override
   public Location getTempFile(String suffix) throws IOException {
-    return new LocalLocation(
-      new File(file.getAbsolutePath() + "." + UUID.randomUUID() + (suffix == null ? TEMP_FILE_SUFFIX : suffix)));
+    String newName = file.getAbsolutePath() + "." + UUID.randomUUID() + (suffix == null ? TEMP_FILE_SUFFIX : suffix);
+    return new LocalLocation(locationFactory, new File(newName));
   }
 
   /**
@@ -177,7 +180,7 @@ final class LocalLocation implements Location {
     // destination will always be of the same type as this location
     boolean success = file.renameTo(((LocalLocation) destination).file);
     if (success) {
-      return new LocalLocation(((LocalLocation) destination).file);
+      return new LocalLocation(locationFactory, ((LocalLocation) destination).file);
     } else {
       return null;
     }
@@ -218,7 +221,7 @@ final class LocalLocation implements Location {
     ImmutableList.Builder<Location> result = ImmutableList.builder();
     if (files != null) {
       for (File file : files) {
-        result.add(new LocalLocation(file));
+        result.add(new LocalLocation(locationFactory, file));
       }
     } else if (!file.exists()) {
       throw new FileNotFoundException("File " + file + " does not exist.");
@@ -227,6 +230,11 @@ final class LocalLocation implements Location {
   }
 
   @Override
+  public LocationFactory getLocationFactory() {
+    return locationFactory;
+  }
+
+  @Override
   public boolean equals(Object o) {
     if (this == o) {
       return true;

http://git-wip-us.apache.org/repos/asf/incubator-twill/blob/60d157ff/twill-common/src/main/java/org/apache/twill/filesystem/LocalLocationFactory.java
----------------------------------------------------------------------
diff --git a/twill-common/src/main/java/org/apache/twill/filesystem/LocalLocationFactory.java b/twill-common/src/main/java/org/apache/twill/filesystem/LocalLocationFactory.java
index f44cd87..82847b2 100644
--- a/twill-common/src/main/java/org/apache/twill/filesystem/LocalLocationFactory.java
+++ b/twill-common/src/main/java/org/apache/twill/filesystem/LocalLocationFactory.java
@@ -40,19 +40,19 @@ public final class LocalLocationFactory implements LocationFactory {
 
   @Override
   public Location create(String path) {
-    return new LocalLocation(new File(basePath, path));
+    return new LocalLocation(this, new File(basePath, path));
   }
 
   @Override
   public Location create(URI uri) {
     if (uri.isAbsolute()) {
-      return new LocalLocation(new File(uri));
+      return new LocalLocation(this, new File(uri));
     }
-    return new LocalLocation(new File(basePath, uri.getPath()));
+    return new LocalLocation(this, new File(basePath, uri.getPath()));
   }
 
   @Override
   public Location getHomeLocation() {
-    return new LocalLocation(new File(System.getProperty("user.home")));
+    return new LocalLocation(this, new File(System.getProperty("user.home")));
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-twill/blob/60d157ff/twill-common/src/main/java/org/apache/twill/filesystem/Location.java
----------------------------------------------------------------------
diff --git a/twill-common/src/main/java/org/apache/twill/filesystem/Location.java b/twill-common/src/main/java/org/apache/twill/filesystem/Location.java
index db3a8e8..04edd27 100644
--- a/twill-common/src/main/java/org/apache/twill/filesystem/Location.java
+++ b/twill-common/src/main/java/org/apache/twill/filesystem/Location.java
@@ -167,4 +167,11 @@ public interface Location {
    *         An empty list is returned if this location is not a directory.
    */
   List<Location> list() throws IOException;
+
+  /**
+   * Returns the location factory used to create this instance.
+   *
+   * @return The {@link LocationFactory} instance for creating this instance.
+   */
+  LocationFactory getLocationFactory();
 }

http://git-wip-us.apache.org/repos/asf/incubator-twill/blob/60d157ff/twill-yarn/src/main/java/org/apache/twill/filesystem/HDFSLocation.java
----------------------------------------------------------------------
diff --git a/twill-yarn/src/main/java/org/apache/twill/filesystem/HDFSLocation.java b/twill-yarn/src/main/java/org/apache/twill/filesystem/HDFSLocation.java
index 59a371c..ed86cb8 100644
--- a/twill-yarn/src/main/java/org/apache/twill/filesystem/HDFSLocation.java
+++ b/twill-yarn/src/main/java/org/apache/twill/filesystem/HDFSLocation.java
@@ -40,16 +40,18 @@ import java.util.UUID;
 final class HDFSLocation implements Location {
   private final FileSystem fs;
   private final Path path;
+  private final HDFSLocationFactory locationFactory;
 
   /**
    * Constructs a HDFSLocation.
    *
-   * @param fs  An instance of {@link FileSystem}
+   * @param locationFactory The {@link HDFSLocationFactory} that creates this instance.
    * @param path of the file.
    */
-  HDFSLocation(FileSystem fs, Path path) {
-    this.fs = fs;
+  HDFSLocation(HDFSLocationFactory locationFactory, Path path) {
+    this.fs = locationFactory.getFileSystem();
     this.path = path;
+    this.locationFactory = locationFactory;
   }
 
   /**
@@ -108,14 +110,14 @@ final class HDFSLocation implements Location {
     if (child.startsWith("/")) {
       child = child.substring(1);
     }
-    return new HDFSLocation(fs, new Path(URI.create(path.toUri() + "/" + child)));
+    return new HDFSLocation(locationFactory, new Path(URI.create(path.toUri() + "/" + child)));
   }
 
   @Override
   public Location getTempFile(String suffix) throws IOException {
     Path path = new Path(
       URI.create(this.path.toUri() + "." + UUID.randomUUID() + (suffix == null ? TEMP_FILE_SUFFIX : suffix)));
-    return new HDFSLocation(fs, path);
+    return new HDFSLocation(locationFactory, path);
   }
 
   /**
@@ -161,11 +163,11 @@ final class HDFSLocation implements Location {
     // Destination will always be of the same type as this location.
     if (fs instanceof DistributedFileSystem) {
       ((DistributedFileSystem) fs).rename(path, ((HDFSLocation) destination).path, Options.Rename.OVERWRITE);
-      return new HDFSLocation(fs, new Path(destination.toURI()));
+      return new HDFSLocation(locationFactory, new Path(destination.toURI()));
     }
 
     if (fs.rename(path, ((HDFSLocation) destination).path)) {
-      return new HDFSLocation(fs, new Path(destination.toURI()));
+      return new HDFSLocation(locationFactory, new Path(destination.toURI()));
     } else {
       return null;
     }
@@ -207,7 +209,7 @@ final class HDFSLocation implements Location {
     if (statuses != null) {
       for (FileStatus status : statuses) {
         if (!Objects.equal(path, status.getPath())) {
-          result.add(new HDFSLocation(fs, status.getPath()));
+          result.add(new HDFSLocation(locationFactory, status.getPath()));
         }
       }
     }
@@ -215,6 +217,11 @@ final class HDFSLocation implements Location {
   }
 
   @Override
+  public LocationFactory getLocationFactory() {
+    return locationFactory;
+  }
+
+  @Override
   public boolean equals(Object o) {
     if (this == o) {
       return true;

http://git-wip-us.apache.org/repos/asf/incubator-twill/blob/60d157ff/twill-yarn/src/main/java/org/apache/twill/filesystem/HDFSLocationFactory.java
----------------------------------------------------------------------
diff --git a/twill-yarn/src/main/java/org/apache/twill/filesystem/HDFSLocationFactory.java b/twill-yarn/src/main/java/org/apache/twill/filesystem/HDFSLocationFactory.java
index fa79391..65146a8 100644
--- a/twill-yarn/src/main/java/org/apache/twill/filesystem/HDFSLocationFactory.java
+++ b/twill-yarn/src/main/java/org/apache/twill/filesystem/HDFSLocationFactory.java
@@ -58,24 +58,24 @@ public final class HDFSLocationFactory implements LocationFactory {
     if (path.startsWith("/")) {
       path = path.substring(1);
     }
-    return new HDFSLocation(fileSystem, new Path(fileSystem.getUri() + "/" + pathBase + "/" + path));
+    return new HDFSLocation(this, new Path(fileSystem.getUri() + "/" + pathBase + "/" + path));
   }
 
   @Override
   public Location create(URI uri) {
     if (!uri.toString().startsWith(fileSystem.getUri().toString())) {
       // It's a full URI
-      return new HDFSLocation(fileSystem, new Path(uri));
+      return new HDFSLocation(this, new Path(uri));
     }
     if (uri.isAbsolute()) {
-      return new HDFSLocation(fileSystem, new Path(fileSystem.getUri() + uri.getPath()));
+      return new HDFSLocation(this, new Path(fileSystem.getUri() + uri.getPath()));
     }
-    return new HDFSLocation(fileSystem, new Path(fileSystem.getUri() + "/" + pathBase + "/" + uri.getPath()));
+    return new HDFSLocation(this, new Path(fileSystem.getUri() + "/" + pathBase + "/" + uri.getPath()));
   }
 
   @Override
   public Location getHomeLocation() {
-    return new HDFSLocation(fileSystem, fileSystem.getHomeDirectory());
+    return new HDFSLocation(this, fileSystem.getHomeDirectory());
   }
 
   /**