This is an automated email from the ASF dual-hosted git repository.

geertjan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-netbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new 049d85f  Create empty war for application on the fly
     new ca080a4  Merge pull request #1118 from 
matthiasblaesing/remove_empty_war_again
049d85f is described below

commit 049d85f9a767c5703aab24845d710444f66526e1
Author: Matthias Bläsing <[email protected]>
AuthorDate: Sat Feb 2 22:34:28 2019 +0100

    Create empty war for application on the fly
    
    The empty.war was originally created by the build script, then it was
    committed to the repository (assumed accidentally). Instead of building
    the war at build time or putting it into the repository, this patch
    moves creation to runtime.
---
 enterprise/cloud.amazon/build.xml                  |  10 ---
 .../cloud.amazon/nbproject/project.properties      |   2 +-
 enterprise/cloud.amazon/nbproject/project.xml      |  14 +++++
 .../modules/cloud/amazon/AmazonInstance.java       |  49 +++++++++++++--
 .../modules/cloud/amazon/resources/empty.war       | Bin 2002 -> 0 bytes
 .../modules/cloud/amazon/AmazonInstanceTest.java   |  67 +++++++++++++++++++++
 6 files changed, 125 insertions(+), 17 deletions(-)

diff --git a/enterprise/cloud.amazon/build.xml 
b/enterprise/cloud.amazon/build.xml
index 14c6539..9acad6f 100644
--- a/enterprise/cloud.amazon/build.xml
+++ b/enterprise/cloud.amazon/build.xml
@@ -22,14 +22,4 @@
 <project basedir="." default="build" name="enterprise/cloud.amazon">
     <description>Builds, tests, and runs the project 
org.netbeans.modules.cloud.amazon</description>
     <import file="../../nbbuild/templates/projectized.xml"/>
-    <property name="resourcesdir" 
value="./src/org/netbeans/modules/cloud/amazon/resources/"/>
-
-    <war destfile = "${resourcesdir}/empty.war" webxml = 
"${resourcesdir}/web.xml">
-        <fileset dir="${resourcesdir}/">
-            <include name="welcome.jsp"/>
-        </fileset>
-        <classes dir="${resourcesdir}/">
-            <exclude name="**/*"/>
-        </classes>
-    </war>
 </project>
diff --git a/enterprise/cloud.amazon/nbproject/project.properties 
b/enterprise/cloud.amazon/nbproject/project.properties
index c0af75f..0f4ec98 100644
--- a/enterprise/cloud.amazon/nbproject/project.properties
+++ b/enterprise/cloud.amazon/nbproject/project.properties
@@ -14,5 +14,5 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
-javac.source=1.6
+javac.source=1.8
 javac.compilerargs=-Xlint -Xlint:-serial
diff --git a/enterprise/cloud.amazon/nbproject/project.xml 
b/enterprise/cloud.amazon/nbproject/project.xml
index dca571f..71e09e5 100644
--- a/enterprise/cloud.amazon/nbproject/project.xml
+++ b/enterprise/cloud.amazon/nbproject/project.xml
@@ -169,6 +169,20 @@
                     </run-dependency>
                 </dependency>
             </module-dependencies>
+            <test-dependencies>
+                <test-type>
+                    <name>unit</name>
+                    <test-dependency>
+                        
<code-name-base>org.netbeans.libs.junit4</code-name-base>
+                        <compile-dependency/>
+                    </test-dependency>
+                    <test-dependency>
+                        
<code-name-base>org.netbeans.modules.nbjunit</code-name-base>
+                        <recursive/>
+                        <compile-dependency/>
+                    </test-dependency>
+                </test-type>
+            </test-dependencies>
             <public-packages/>
         </data>
     </configuration>
diff --git 
a/enterprise/cloud.amazon/src/org/netbeans/modules/cloud/amazon/AmazonInstance.java
 
b/enterprise/cloud.amazon/src/org/netbeans/modules/cloud/amazon/AmazonInstance.java
index 04039d4..d432a84 100644
--- 
a/enterprise/cloud.amazon/src/org/netbeans/modules/cloud/amazon/AmazonInstance.java
+++ 
b/enterprise/cloud.amazon/src/org/netbeans/modules/cloud/amazon/AmazonInstance.java
@@ -32,7 +32,6 @@ import 
com.amazonaws.services.elasticbeanstalk.model.CheckDNSAvailabilityRequest
 import 
com.amazonaws.services.elasticbeanstalk.model.CheckDNSAvailabilityResult;
 import com.amazonaws.services.elasticbeanstalk.model.CreateApplicationRequest;
 import 
com.amazonaws.services.elasticbeanstalk.model.CreateApplicationVersionRequest;
-import 
com.amazonaws.services.elasticbeanstalk.model.CreateApplicationVersionResult;
 import com.amazonaws.services.elasticbeanstalk.model.CreateEnvironmentRequest;
 import 
com.amazonaws.services.elasticbeanstalk.model.DescribeEnvironmentsRequest;
 import 
com.amazonaws.services.elasticbeanstalk.model.DescribeEnvironmentsResult;
@@ -46,8 +45,11 @@ import com.amazonaws.services.s3.AmazonS3Client;
 import com.amazonaws.services.s3.model.AmazonS3Exception;
 import com.amazonaws.services.s3.model.ObjectMetadata;
 import com.amazonaws.services.s3.model.PutObjectRequest;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Date;
@@ -55,8 +57,13 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.concurrent.Callable;
 import java.util.concurrent.Future;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Manifest;
+import java.util.jar.Attributes;
 import java.util.logging.Level;
 import java.util.logging.Logger;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
 import javax.swing.SwingUtilities;
 import org.netbeans.api.server.ServerInstance;
 import 
org.netbeans.modules.cloud.common.spi.support.serverplugin.DeploymentStatus;
@@ -260,12 +267,14 @@ public class AmazonInstance {
             // this mean object does not exist in S3 - fine
         }
         if (!exist) {
-            InputStream is = 
AmazonInstance.class.getResourceAsStream("resources/empty.war");
-            s3.putObject(new PutObjectRequest(bucket, 
DEFAULT_EMPTY_APPLICATION, is, new ObjectMetadata()));
             try {
-                is.close();
+                s3.putObject(new PutObjectRequest(
+                        bucket,
+                        DEFAULT_EMPTY_APPLICATION,
+                        new ByteArrayInputStream(createEmptyWar()),
+                        new ObjectMetadata()));
             } catch (IOException ex) {
-                Exceptions.printStackTrace(ex);
+                LOG.log(Level.INFO, "Failed to create empty application war", 
ex);
             }
         }
         return new 
S3Location().withS3Bucket(bucket).withS3Key(DEFAULT_EMPTY_APPLICATION);
@@ -413,5 +422,33 @@ public class AmazonInstance {
         Future<T> f = AMAZON_RP.submit(callable);
         return f;
     }
-    
+
+    byte[] createEmptyWar() throws IOException {
+        // The empty war is about 2kB in size -- so build it in memory
+        ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
+        Manifest manifest = new Manifest();
+        manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, 
"1.0");
+        manifest.getMainAttributes().put(new Attributes.Name("Created-By"), 
"Apache NetBeans");
+        try (JarOutputStream jos = new JarOutputStream(baos, manifest)) {
+            copyResourceToJar(jos, "resources/welcome.jsp",  "welcome.jsp");
+            copyResourceToJar(jos, "resources/web.xml",  "WEB-INF/web.xml");
+        }
+        return baos.toByteArray();
+    }
+
+    private void copyResourceToJar(ZipOutputStream zos, String sourceFile, 
String outputPath) throws IOException {
+        try (InputStream welcomeIS = 
AmazonInstance.class.getResourceAsStream(sourceFile)) {
+            ZipEntry ze = new ZipEntry(outputPath);
+            zos.putNextEntry(ze);
+            copyStream(welcomeIS, zos);
+        }
+    }
+
+    private void copyStream(InputStream is, OutputStream os) throws 
IOException {
+        byte[] buffer = new byte[1024];
+        int read = 0;
+        while((read = is.read(buffer)) > 0) {
+            os.write(buffer, 0, read);
+        }
+    }
 }
diff --git 
a/enterprise/cloud.amazon/src/org/netbeans/modules/cloud/amazon/resources/empty.war
 
b/enterprise/cloud.amazon/src/org/netbeans/modules/cloud/amazon/resources/empty.war
deleted file mode 100644
index 719f01f..0000000
Binary files 
a/enterprise/cloud.amazon/src/org/netbeans/modules/cloud/amazon/resources/empty.war
 and /dev/null differ
diff --git 
a/enterprise/cloud.amazon/test/unit/src/org/netbeans/modules/cloud/amazon/AmazonInstanceTest.java
 
b/enterprise/cloud.amazon/test/unit/src/org/netbeans/modules/cloud/amazon/AmazonInstanceTest.java
new file mode 100644
index 0000000..dbc6d7b
--- /dev/null
+++ 
b/enterprise/cloud.amazon/test/unit/src/org/netbeans/modules/cloud/amazon/AmazonInstanceTest.java
@@ -0,0 +1,67 @@
+/*
+ * 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.netbeans.modules.cloud.amazon;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class AmazonInstanceTest {
+
+    public AmazonInstanceTest() {
+    }
+
+    @Test
+    public void testCreateEmptyWar() throws IOException {
+        AmazonInstance ai = new AmazonInstance("dommy", "dummy", "dummy", 
"dummy");
+        byte[] warData = ai.createEmptyWar();
+        Files.write(Paths.get("/home/matthias/test.zip"), warData, 
StandardOpenOption.CREATE, StandardOpenOption.WRITE);
+        Assert.assertNotNull(warData);
+        Assert.assertTrue(warData.length > 0);
+        boolean welcomePageFound = false;
+        boolean webXmlFound = false;
+        boolean manifestFound = false;
+        try(ZipInputStream zip = new ZipInputStream(new 
ByteArrayInputStream(warData))) {
+            ZipEntry ze;
+            while((ze = zip.getNextEntry()) != null) {
+                if("welcome.jsp".equals(ze.getName())) {
+                    welcomePageFound = streamContainsData(zip);
+                } else if ("WEB-INF/web.xml".equals(ze.getName())) {
+                    webXmlFound = streamContainsData(zip);
+                } if("META-INF/MANIFEST.MF".equals(ze.getName())) {
+                    manifestFound = streamContainsData(zip);
+                }
+            }
+        }
+        Assert.assertTrue(welcomePageFound);
+        Assert.assertTrue(webXmlFound);
+        Assert.assertTrue(manifestFound);
+    }
+
+    private boolean streamContainsData(InputStream is) throws IOException {
+        return is.read() >= 0;
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to