enabled reading of metainfo.xml from app zip

git-svn-id: https://svn.apache.org/repos/asf/incubator/slider/trunk@1592638 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/slider-core/src/main/java/org/apache/slider/common/tools/SliderUtils.java b/slider-core/src/main/java/org/apache/slider/common/tools/SliderUtils.java
index 7abb461..fae3e3f 100644
--- a/slider-core/src/main/java/org/apache/slider/common/tools/SliderUtils.java
+++ b/slider-core/src/main/java/org/apache/slider/common/tools/SliderUtils.java
@@ -18,8 +18,11 @@
 
 package org.apache.slider.common.tools;
 
+import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
+import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.CommonConfigurationKeysPublic;
+import org.apache.hadoop.fs.FSDataInputStream;
 import org.apache.hadoop.fs.FileStatus;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.FileUtil;
@@ -54,9 +57,11 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.net.InetSocketAddress;
@@ -1317,7 +1322,7 @@
   /**
    * Add a subpath to an existing URL. This extends
    * the path, inserting a / between all entries
-   * if needed. 
+   * if needed.
    * @param base base path/URL
    * @param path subpath
    * @return base+"/"+subpath
@@ -1336,7 +1341,6 @@
     return fullpath.toString();
   }
 
-
   /**
    * Callable for async/scheduled halt
    */
@@ -1377,4 +1381,29 @@
     }
   }
 
+  public static InputStream getApplicationResourceInputStream(FileSystem fs,
+                                                       Path appPath,
+                                                       String entry)
+      throws IOException {
+    InputStream is = null;
+    FSDataInputStream appStream = fs.open(appPath);
+    ZipArchiveInputStream zis = new ZipArchiveInputStream(appStream);
+    ZipArchiveEntry zipEntry;
+    boolean done = false;
+    while (!done && (zipEntry = zis.getNextZipEntry()) != null) {
+      if (entry.equals(zipEntry.getName())) {
+        int size = (int) zipEntry.getSize();
+        byte[] content = new byte[size];
+        int offset = 0;
+        while (offset < size) {
+          offset += zis.read(content, offset, size - offset);
+        }
+        is = new ByteArrayInputStream(content);
+        done = true;
+      }
+    }
+
+    return is;
+  }
+
 }
diff --git a/slider-core/src/main/java/org/apache/slider/providers/agent/AgentProviderService.java b/slider-core/src/main/java/org/apache/slider/providers/agent/AgentProviderService.java
index a3a3309..ebc5f8f 100644
--- a/slider-core/src/main/java/org/apache/slider/providers/agent/AgentProviderService.java
+++ b/slider-core/src/main/java/org/apache/slider/providers/agent/AgentProviderService.java
@@ -43,6 +43,8 @@
 import org.apache.slider.providers.ProviderCore;
 import org.apache.slider.providers.ProviderRole;
 import org.apache.slider.providers.ProviderUtils;
+import org.apache.slider.providers.agent.application.metadata.Metainfo;
+import org.apache.slider.providers.agent.application.metadata.MetainfoParser;
 import org.apache.slider.server.appmaster.state.StateAccessForProviders;
 import org.apache.slider.server.appmaster.web.rest.agent.AgentCommandType;
 import org.apache.slider.server.appmaster.web.rest.agent.AgentRestOperations;
@@ -61,6 +63,7 @@
 
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.URI;
 import java.net.URL;
 import java.util.ArrayList;
@@ -217,6 +220,15 @@
                               getClusterInfoPropertyValue(OptionKeys.APPLICATION_NAME)));
   }
 
+  private Metainfo getApplicationMetainfo(SliderFileSystem fileSystem,
+                                      String appDef) throws IOException {
+    InputStream metainfoStream = SliderUtils.getApplicationResourceInputStream(
+        fileSystem.getFileSystem(), new Path(appDef), "metainfo.xml");
+    Metainfo metainfo = new MetainfoParser().parse(metainfoStream);
+
+    return metainfo;
+  }
+
   protected void setRoleHostMapping(String role, String host) {
     List<String> hosts = roleHostMapping.get(role);
     if (hosts == null) {
diff --git a/slider-core/src/test/java/org/apache/slider/common/tools/SliderUtilsTest.java b/slider-core/src/test/java/org/apache/slider/common/tools/SliderUtilsTest.java
new file mode 100644
index 0000000..ddad6ba
--- /dev/null
+++ b/slider-core/src/test/java/org/apache/slider/common/tools/SliderUtilsTest.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.slider.common.tools;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.InputStream;
+import java.net.URI;
+
+/**
+ *
+ */
+public class SliderUtilsTest {
+  protected static final Logger log =
+      LoggerFactory.getLogger(SliderUtilsTest.class);
+
+  @Test
+  public void testGetMetaInfoStreamFromZip () throws Exception {
+    Configuration configuration = new Configuration();
+    FileSystem fs = FileSystem.getLocal(configuration);
+    log.info("fs working dir is {}", fs.getWorkingDirectory().toString());
+    SliderFileSystem sliderFileSystem = new SliderFileSystem(fs, configuration);
+
+    InputStream stream = SliderUtils.getApplicationResourceInputStream(
+        sliderFileSystem.getFileSystem(),
+        new Path("target/test-classes/org/apache/slider/common/tools/test.zip"),
+        "metainfo.xml");
+    assert stream != null;
+    assert stream.available() > 0;
+  }
+}
diff --git a/slider-core/src/test/resources/org/apache/slider/common/tools/test.zip b/slider-core/src/test/resources/org/apache/slider/common/tools/test.zip
new file mode 100644
index 0000000..18acf1c
--- /dev/null
+++ b/slider-core/src/test/resources/org/apache/slider/common/tools/test.zip
Binary files differ