You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lens.apache.org by pr...@apache.org on 2015/08/11 14:20:31 UTC

[08/50] [abbrv] incubator-lens git commit: LENS-682 : Make XML marshalling errors descriptive on CLI

LENS-682 : Make XML marshalling errors descriptive on CLI


Project: http://git-wip-us.apache.org/repos/asf/incubator-lens/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-lens/commit/4c725266
Tree: http://git-wip-us.apache.org/repos/asf/incubator-lens/tree/4c725266
Diff: http://git-wip-us.apache.org/repos/asf/incubator-lens/diff/4c725266

Branch: refs/heads/current-release-line
Commit: 4c7252669fb07f5b38e6a6653cc26830ccfe6329
Parents: 44ec2af
Author: Rajat Khandelwal <pr...@apache.org>
Authored: Fri Jul 24 12:56:18 2015 +0530
Committer: Amareshwari Sriramadasu <am...@apache.org>
Committed: Fri Jul 24 12:56:18 2015 +0530

----------------------------------------------------------------------
 .../java/org/apache/lens/api/APIResult.java     |  48 ++++-
 .../apache/lens/client/LensMetadataClient.java  | 193 +++++--------------
 2 files changed, 90 insertions(+), 151 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/4c725266/lens-api/src/main/java/org/apache/lens/api/APIResult.java
----------------------------------------------------------------------
diff --git a/lens-api/src/main/java/org/apache/lens/api/APIResult.java b/lens-api/src/main/java/org/apache/lens/api/APIResult.java
index d048373..6b734a9 100644
--- a/lens-api/src/main/java/org/apache/lens/api/APIResult.java
+++ b/lens-api/src/main/java/org/apache/lens/api/APIResult.java
@@ -23,10 +23,7 @@ import java.io.StringWriter;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Marshaller;
-import javax.xml.bind.annotation.XmlAccessType;
-import javax.xml.bind.annotation.XmlAccessorType;
-import javax.xml.bind.annotation.XmlElement;
-import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.*;
 
 import lombok.AccessLevel;
 import lombok.Getter;
@@ -43,6 +40,7 @@ import lombok.NoArgsConstructor;
 @NoArgsConstructor(access = AccessLevel.PROTECTED)
 public class APIResult {
 
+  private static final APIResult SUCCESS = new APIResult(Status.SUCCEEDED, "");
   /**
    * The status.
    */
@@ -117,4 +115,46 @@ public class APIResult {
       return e.getMessage();
     }
   }
+
+  public static APIResult partial(int actual, int expected) {
+    return new APIResult(Status.PARTIAL, actual + " out of " + expected);
+  }
+
+  public static APIResult successOrPartialOrFailure(int actual, int expected) {
+    return successOrPartialOrFailure(actual, expected, null);
+  }
+
+  public static APIResult successOrPartialOrFailure(int actual, int expected, Exception e) {
+    if (actual == 0 && expected != 0) {
+      return failure(e);
+    }
+    if (actual < expected) {
+      return partial(actual, expected);
+    } else {
+      return success();
+    }
+  }
+
+  public static APIResult success() {
+    return SUCCESS;
+  }
+
+  public static APIResult failure(Exception e) {
+    String cause = extractCause(e);
+    return new APIResult(Status.FAILED, cause);
+  }
+
+  public static APIResult partial(Exception e) {
+    String cause = extractCause(e);
+    return new APIResult(Status.PARTIAL, cause);
+  }
+
+  private static String extractCause(Throwable e) {
+    String cause = null;
+    while ((cause == null || cause.isEmpty()) && e != null) {
+      cause = e.getMessage();
+      e = e.getCause();
+    }
+    return cause;
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/4c725266/lens-client/src/main/java/org/apache/lens/client/LensMetadataClient.java
----------------------------------------------------------------------
diff --git a/lens-client/src/main/java/org/apache/lens/client/LensMetadataClient.java b/lens-client/src/main/java/org/apache/lens/client/LensMetadataClient.java
index 4bec6ad..31666a0 100644
--- a/lens-client/src/main/java/org/apache/lens/client/LensMetadataClient.java
+++ b/lens-client/src/main/java/org/apache/lens/client/LensMetadataClient.java
@@ -24,7 +24,6 @@ import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URISyntaxException;
-import java.nio.charset.Charset;
 import java.util.Date;
 import java.util.List;
 
@@ -46,8 +45,6 @@ import org.glassfish.jersey.media.multipart.*;
 import org.xml.sax.SAXException;
 
 import com.google.common.base.Joiner;
-import com.google.common.io.Files;
-
 import lombok.extern.slf4j.Slf4j;
 
 @Slf4j
@@ -204,12 +201,8 @@ public class LensMetadataClient {
   public APIResult createCube(String cubeSpec) {
     try {
       return createCube(this.<XCube>readFromXML(cubeSpec));
-    } catch (JAXBException e) {
-      log.error("Unmarshalling error:", e);
-      return new APIResult(Status.FAILED, "Unmarshalling failed");
-    } catch (IOException e) {
-      log.error("File error:", e);
-      return new APIResult(Status.FAILED, "File not found");
+    } catch (JAXBException | IOException e) {
+      return failureAPIResult(e);
     }
   }
 
@@ -225,12 +218,8 @@ public class LensMetadataClient {
   public APIResult updateCube(String cubeName, String cubeSpec) {
     try {
       return updateCube(cubeName, this.<XCube>readFromXML(cubeSpec));
-    } catch (JAXBException e) {
-      log.error("Unmarshalling error:", e);
-      return new APIResult(Status.FAILED, "Unmarshalling failed");
-    } catch (IOException e) {
-      log.error("File error:", e);
-      return new APIResult(Status.FAILED, "File not found");
+    } catch (JAXBException | IOException e) {
+      return failureAPIResult(e);
     }
   }
 
@@ -298,12 +287,8 @@ public class LensMetadataClient {
   public APIResult createDimension(String dimSpec) {
     try {
       return createDimension(this.<XDimension>readFromXML(dimSpec));
-    } catch (JAXBException e) {
-      log.error("Unmarshalling error:", e);
-      return new APIResult(Status.FAILED, "Unmarshalling failed");
-    } catch (IOException e) {
-      log.error("File error:", e);
-      return new APIResult(Status.FAILED, "File not found");
+    } catch (JAXBException | IOException e) {
+      return failureAPIResult(e);
     }
   }
 
@@ -319,12 +304,8 @@ public class LensMetadataClient {
   public APIResult updateDimension(String dimName, String dimSpec) {
     try {
       return updateDimension(dimName, this.<XDimension>readFromXML(dimSpec));
-    } catch (JAXBException e) {
-      log.error("Unmarshalling error:", e);
-      return new APIResult(Status.FAILED, "Unmarshalling failed");
-    } catch (IOException e) {
-      log.error("File error:", e);
-      return new APIResult(Status.FAILED, "File not found");
+    } catch (JAXBException | IOException e) {
+      return failureAPIResult(e);
     }
   }
 
@@ -367,12 +348,8 @@ public class LensMetadataClient {
   public APIResult createNewStorage(String storage) {
     try {
       return createNewStorage(this.<XStorage>readFromXML(storage));
-    } catch (JAXBException e) {
-      log.error("Unmarshalling error:", e);
-      return new APIResult(Status.FAILED, "Unmarshalling failed");
-    } catch (IOException e) {
-      log.error("File error:", e);
-      return new APIResult(Status.FAILED, "File not found");
+    } catch (JAXBException | IOException e) {
+      return failureAPIResult(e);
     }
   }
 
@@ -397,12 +374,8 @@ public class LensMetadataClient {
   public APIResult updateStorage(String storageName, String storage) {
     try {
       return updateStorage(storageName, this.<XStorage>readFromXML(storage));
-    } catch (JAXBException e) {
-      log.error("Unmarshalling error:", e);
-      return new APIResult(Status.FAILED, "Unmarshalling failed");
-    } catch (IOException e) {
-      log.error("File error:", e);
-      return new APIResult(Status.FAILED, "File not found");
+    } catch (JAXBException | IOException e) {
+      return failureAPIResult(e);
     }
   }
 
@@ -489,38 +462,11 @@ public class LensMetadataClient {
   public APIResult createFactTable(String factSpec) {
     try {
       return createFactTable(this.<XFactTable>readFromXML(factSpec));
-    } catch (JAXBException e) {
-      log.error("Unmarshalling error:", e);
-      return new APIResult(Status.FAILED, "Unmarshalling failed");
-    } catch (IOException e) {
-      log.error("File error:", e);
-      return new APIResult(Status.FAILED, "File not found");
-    }
-  }
-
-  private String getContent(String path) {
-    try {
-      List<String> content = null;
-      if (path.startsWith("/")) {
-        // path provided is absolute path
-        content = Files.readLines(new File(path), Charset.defaultCharset());
-      } else {
-        // load from classpath
-        URI uri = Thread.currentThread().getContextClassLoader().getResource(path).toURI();
-        content = Files.readLines(new File(uri),
-          Charset.defaultCharset());
-      }
-      return Joiner.on("\n").skipNulls().join(content);
-    } catch (IOException e) {
-      log.error("IO Error reading content ", e);
-      throw new IllegalStateException(e);
-    } catch (URISyntaxException e) {
-      log.error("URI Error reading content ", e);
-      throw new IllegalStateException(e);
+    } catch (JAXBException | IOException e) {
+      return failureAPIResult(e);
     }
   }
 
-
   public APIResult updateFactTable(String factName, XFactTable table) {
     WebTarget target = getMetastoreWebTarget();
     APIResult result = target.path("facts").path(factName)
@@ -533,12 +479,8 @@ public class LensMetadataClient {
   public APIResult updateFactTable(String factName, String table) {
     try {
       return updateFactTable(factName, this.<XFactTable>readFromXML(table));
-    } catch (JAXBException e) {
-      log.error("Unmarshalling error:", e);
-      return new APIResult(Status.FAILED, "Unmarshalling failed");
-    } catch (IOException e) {
-      log.error("File error:", e);
-      return new APIResult(Status.FAILED, "File not found");
+    } catch (JAXBException | IOException e) {
+      return failureAPIResult(e);
     }
   }
 
@@ -586,12 +528,8 @@ public class LensMetadataClient {
   public APIResult addStorageToFactTable(String factname, String storageSpec) {
     try {
       return addStorageToFactTable(factname, this.<XStorageTableElement>readFromXML(storageSpec));
-    } catch (JAXBException e) {
-      log.error("Unmarshalling error:", e);
-      return new APIResult(Status.FAILED, "Unmarshalling failed");
-    } catch (IOException e) {
-      log.error("File error:", e);
-      return new APIResult(Status.FAILED, "File not found");
+    } catch (JAXBException | IOException e) {
+      return failureAPIResult(e);
     }
   }
 
@@ -703,12 +641,8 @@ public class LensMetadataClient {
   public APIResult createDimensionTable(String tableXml) {
     try {
       return createDimensionTable(this.<XDimensionTable>readFromXML(tableXml));
-    } catch (JAXBException e) {
-      log.error("Unmarshalling error:", e);
-      return new APIResult(Status.FAILED, "Unmarshalling failed");
-    } catch (IOException e) {
-      log.error("File error:", e);
-      return new APIResult(Status.FAILED, "File not found");
+    } catch (JAXBException | IOException e) {
+      return failureAPIResult(e);
     }
   }
 
@@ -728,14 +662,9 @@ public class LensMetadataClient {
       XDimensionTable dimensionTable = readFromXML(dimSpec);
       dimensionTable.setTableName(dimTblName);
       return updateDimensionTable(dimensionTable);
-    } catch (JAXBException e) {
-      log.error("Unmarshalling error:", e);
-      return new APIResult(Status.FAILED, "Unmarshalling failed");
-    } catch (IOException e) {
-      log.error("File error:", e);
-      return new APIResult(Status.FAILED, "File not found");
+    } catch (JAXBException | IOException e) {
+      return failureAPIResult(e);
     }
-
   }
 
   public APIResult dropDimensionTable(String table, boolean cascade) {
@@ -784,12 +713,8 @@ public class LensMetadataClient {
   public APIResult addStorageToDimTable(String dimTblName, String table) {
     try {
       return addStorageToDimTable(dimTblName, this.<XStorageTableElement>readFromXML(table));
-    } catch (JAXBException e) {
-      log.error("Unmarshalling error:", e);
-      return new APIResult(Status.FAILED, "Unmarshalling failed");
-    } catch (IOException e) {
-      log.error("File error:", e);
-      return new APIResult(Status.FAILED, "File not found");
+    } catch (JAXBException | IOException e) {
+      return failureAPIResult(e);
     }
   }
 
@@ -885,12 +810,8 @@ public class LensMetadataClient {
     String partitionSpec) {
     try {
       return addPartitionToDimensionTable(dimTblName, storage, (XPartition) readFromXML(partitionSpec));
-    } catch (JAXBException e) {
-      log.error("Unmarshalling error:", e);
-      return new APIResult(Status.FAILED, "Unmarshalling failed");
-    } catch (IOException e) {
-      log.error("File error:", e);
-      return new APIResult(Status.FAILED, "File not found");
+    } catch (JAXBException | IOException e) {
+      return failureAPIResult(e);
     }
   }
 
@@ -909,12 +830,8 @@ public class LensMetadataClient {
     String partitionsSpec) {
     try {
       return addPartitionsToDimensionTable(dimTblName, storage, (XPartitionList) readFromXML(partitionsSpec));
-    } catch (JAXBException e) {
-      log.error("Unmarshalling error:", e);
-      return new APIResult(Status.FAILED, "Unmarshalling failed");
-    } catch (IOException e) {
-      log.error("File error:", e);
-      return new APIResult(Status.FAILED, "File not found");
+    } catch (JAXBException | IOException e) {
+      return failureAPIResult(e);
     }
   }
 
@@ -933,12 +850,8 @@ public class LensMetadataClient {
     String partitionSpec) {
     try {
       return addPartitionToFactTable(fact, storage, (XPartition) readFromXML(partitionSpec));
-    } catch (JAXBException e) {
-      log.error("Unmarshalling error:", e);
-      return new APIResult(Status.FAILED, "Unmarshalling failed");
-    } catch (IOException e) {
-      log.error("File error:", e);
-      return new APIResult(Status.FAILED, "File not found");
+    } catch (JAXBException | IOException e) {
+      return failureAPIResult(e);
     }
   }
 
@@ -957,14 +870,11 @@ public class LensMetadataClient {
     String partitionsSpec) {
     try {
       return addPartitionsToFactTable(fact, storage, (XPartitionList) readFromXML(partitionsSpec));
-    } catch (JAXBException e) {
-      log.error("Unmarshalling error:", e);
-      return new APIResult(Status.FAILED, "Unmarshalling failed");
-    } catch (IOException e) {
-      log.error("File error:", e);
-      return new APIResult(Status.FAILED, "File not found");
+    } catch (JAXBException | IOException e) {
+      return failureAPIResult(e);
     }
   }
+
   public APIResult updatePartitionOfDimensionTable(String dimTblName, String storage,
     XPartition partition) {
     WebTarget target = getMetastoreWebTarget();
@@ -980,12 +890,8 @@ public class LensMetadataClient {
     String partitionSpec) {
     try {
       return updatePartitionOfDimensionTable(dimTblName, storage, (XPartition) readFromXML(partitionSpec));
-    } catch (JAXBException e) {
-      log.error("Unmarshalling error:", e);
-      return new APIResult(Status.FAILED, "Unmarshalling failed");
-    } catch (IOException e) {
-      log.error("File error:", e);
-      return new APIResult(Status.FAILED, "File not found");
+    } catch (JAXBException | IOException e) {
+      return failureAPIResult(e);
     }
   }
 
@@ -1004,12 +910,8 @@ public class LensMetadataClient {
     String partitionsSpec) {
     try {
       return updatePartitionsOfDimensionTable(dimTblName, storage, (XPartitionList) readFromXML(partitionsSpec));
-    } catch (JAXBException e) {
-      log.error("Unmarshalling error:", e);
-      return new APIResult(Status.FAILED, "Unmarshalling failed");
-    } catch (IOException e) {
-      log.error("File error:", e);
-      return new APIResult(Status.FAILED, "File not found");
+    } catch (JAXBException | IOException e) {
+      return failureAPIResult(e);
     }
   }
 
@@ -1028,12 +930,8 @@ public class LensMetadataClient {
     String partitionSpec) {
     try {
       return updatePartitionOfFactTable(fact, storage, (XPartition) readFromXML(partitionSpec));
-    } catch (JAXBException e) {
-      log.error("Unmarshalling error:", e);
-      return new APIResult(Status.FAILED, "Unmarshalling failed");
-    } catch (IOException e) {
-      log.error("File error:", e);
-      return new APIResult(Status.FAILED, "File not found");
+    } catch (JAXBException | IOException e) {
+      return failureAPIResult(e);
     }
   }
 
@@ -1052,12 +950,8 @@ public class LensMetadataClient {
     String partitionsSpec) {
     try {
       return updatePartitionsOfFactTable(fact, storage, (XPartitionList) readFromXML(partitionsSpec));
-    } catch (JAXBException e) {
-      log.error("Unmarshalling error:", e);
-      return new APIResult(Status.FAILED, "Unmarshalling failed");
-    } catch (IOException e) {
-      log.error("File error:", e);
-      return new APIResult(Status.FAILED, "File not found");
+    } catch (JAXBException | IOException e) {
+      return failureAPIResult(e);
     }
   }
 
@@ -1079,4 +973,9 @@ public class LensMetadataClient {
       .request(MediaType.APPLICATION_XML)
       .get(StringList.class).getElements();
   }
+
+  private APIResult failureAPIResult(Exception e) {
+    log.error("Failed", e);
+    return APIResult.failure(e);
+  }
 }