Author: marrs
Date: Wed Nov 13 11:22:49 2013
New Revision: 1541473

URL: http://svn.apache.org/r1541473
Log:
ACE-428 Implemented all three points that were mentioned in the issue. Updated 
the tests as well to cover the small API change.

Modified:
    
ace/trunk/org.apache.ace.client.repository/src/org/apache/ace/client/repository/helper/base/ArtifactPreprocessorBase.java
    
ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/servlet/BundleServlet.java
    ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/storage/BundleStore.java
    
ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/storage/file/BundleFileStore.java
    
ace/trunk/org.apache.ace.obr/test/org/apache/ace/obr/servlet/MockBundleStore.java
    
ace/trunk/org.apache.ace.obr/test/org/apache/ace/obr/storage/file/BundleFileStoreTest.java

Modified: 
ace/trunk/org.apache.ace.client.repository/src/org/apache/ace/client/repository/helper/base/ArtifactPreprocessorBase.java
URL: 
http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.client.repository/src/org/apache/ace/client/repository/helper/base/ArtifactPreprocessorBase.java?rev=1541473&r1=1541472&r2=1541473&view=diff
==============================================================================
--- 
ace/trunk/org.apache.ace.client.repository/src/org/apache/ace/client/repository/helper/base/ArtifactPreprocessorBase.java
 (original)
+++ 
ace/trunk/org.apache.ace.client.repository/src/org/apache/ace/client/repository/helper/base/ArtifactPreprocessorBase.java
 Wed Nov 13 11:22:49 2013
@@ -101,15 +101,17 @@ public abstract class ArtifactPreprocess
         if (obrBase == null) {
             throw new IOException("There is no storage available for this 
artifact.");
         }
-        if ((name == null) || (input == null)) {
-            throw new IllegalArgumentException("None of the parameters can be 
null.");
+        if (name == null) {
+            throw new IllegalArgumentException("Name cannot be null.");
+        }
+        if (input == null) {
+            throw new IllegalArgumentException("Input stream cannot be null.");
         }
 
         OutputStream output = null;
         String location = null;
         try {
-
-            URL url = new URL(obrBase, "?filename=" + name);
+            URL url = new URL(obrBase, "?filename=" + name + "&replace=true");
             URLConnection connection = 
m_connectionFactory.createConnection(url);
             connection.setDoOutput(true);
             connection.setDoInput(true);

Modified: 
ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/servlet/BundleServlet.java
URL: 
http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/servlet/BundleServlet.java?rev=1541473&r1=1541472&r2=1541473&view=diff
==============================================================================
--- 
ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/servlet/BundleServlet.java 
(original)
+++ 
ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/servlet/BundleServlet.java 
Wed Nov 13 11:22:49 2013
@@ -126,8 +126,9 @@ public class BundleServlet extends HttpS
     protected void doPost(HttpServletRequest request, HttpServletResponse 
response) {
 
         String fileName = request.getParameter("filename");
+        boolean replace = 
Boolean.parseBoolean(request.getParameter("replace"));
         try {
-            String storePath = m_store.put(request.getInputStream(), fileName);
+            String storePath = m_store.put(request.getInputStream(), fileName, 
replace);
             if (storePath != null) {
                 sendCreated(request, response, storePath);
             }

Modified: 
ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/storage/BundleStore.java
URL: 
http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/storage/BundleStore.java?rev=1541473&r1=1541472&r2=1541473&view=diff
==============================================================================
--- 
ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/storage/BundleStore.java 
(original)
+++ 
ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/storage/BundleStore.java 
Wed Nov 13 11:22:49 2013
@@ -24,7 +24,7 @@ import org.osgi.service.cm.ManagedServic
  * under the License.
  */
 
-public interface BundleStore extends ManagedService {
+public interface BundleStore {
 
     /**
      * Returns an <code>InputStream</code> to the data of the specified 
resource.
@@ -36,17 +36,18 @@ public interface BundleStore extends Man
     public InputStream get(String filePath) throws IOException;
 
     /**
-     * Stores the specified resource in the store. For non OSGi bundles a 
valid filename must be specified that may
-     * contain a valid OSGi version.
-     * <br/><br/>
-     * Filename pattern: 
<code>&lt;filename&gt;[-&lt;version&gt;].&lt;extension&gt;<code>
+     * Stores the specified resource in the store. If the resource already 
existed, it
+     * will only be accepted if you either try to store exactly the same 
resource (byte-by-byte)
+     * or tell it to forcefully replace the resource. The latter should only 
be done with
+     * extreme care.
      *
      * @param fileName name of the resource, ignored if the resource is an 
OSGi bundle
-     * @param data The actual data of the resource.
-     * @return the filePath if the resource was successfully stored, 
<code>null</code> if the resource already existed
+     * @param data the actual data of the resource
+     * @param replace <code>true</code> to replace any existing resource with 
the same name
+     * @return the filePath if the resource was successfully stored, 
<code>null</code> if the resource already existed and was different
      * @throws java.io.IOException If there was a problem reading or writing 
the data of the resource.
      */
-    public String put(InputStream data, String fileName) throws IOException;
+    public String put(InputStream data, String fileName, boolean replace) 
throws IOException;
     
     /**
      * Removes the specified resource from the store.

Modified: 
ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/storage/file/BundleFileStore.java
URL: 
http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/storage/file/BundleFileStore.java?rev=1541473&r1=1541472&r2=1541473&view=diff
==============================================================================
--- 
ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/storage/file/BundleFileStore.java
 (original)
+++ 
ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/storage/file/BundleFileStore.java
 Wed Nov 13 11:22:49 2013
@@ -18,6 +18,7 @@
  */
 package org.apache.ace.obr.storage.file;
 
+import java.io.BufferedInputStream;
 import java.io.Closeable;
 import java.io.File;
 import java.io.FileInputStream;
@@ -89,7 +90,7 @@ public class BundleFileStore implements 
         return result;
     }
 
-    public String put(InputStream data, String fileName) throws IOException {
+    public String put(InputStream data, String fileName, boolean replace) 
throws IOException {
 
         if (fileName == null) {
             fileName = "";
@@ -109,8 +110,13 @@ public class BundleFileStore implements 
             throw new IOException("Failed to store resource (filename = " +  
fileName  + ")");
         }
         if (storeLocation.exists()) {
-            m_log.log(LogService.LOG_ERROR, "Resource already existed in OBR 
(filename = " +  fileName  + ")");
-            return null;
+               if (replace || compare(storeLocation, tempFile)) {
+                       m_log.log(LogService.LOG_DEBUG, "Exact same resource 
already existed in OBR (filename = " +  fileName  + ")");
+               }
+               else {
+                       m_log.log(LogService.LOG_ERROR, "Different resource 
with same name already existed in OBR (filename = " +  fileName  + ")");
+                       return null;
+               }
         }
 
         moveFile(tempFile, storeLocation);
@@ -122,7 +128,39 @@ public class BundleFileStore implements 
         return filePath;
     }
 
-    public boolean remove(String fileName) throws IOException {
+    /** Compares the contents of two files, returns <code>true</code> if 
they're exactly the same. */
+    private boolean compare(File first, File second) throws IOException {
+       BufferedInputStream bis = new BufferedInputStream(new 
FileInputStream(first));
+       BufferedInputStream bis2 = new BufferedInputStream(new 
FileInputStream(second));
+       int b1, b2;
+       try {
+               do {
+                       b1 = bis.read();
+                       b2 = bis2.read();
+                       if (b1 != b2) {
+                               return false;
+                       }
+               }
+               while (b1 != -1 && b2 != -1);
+               return (b1 == b2);
+       }
+       finally {
+               if (bis != null) {
+                       try {
+                               bis.close();
+                       }
+                       catch (IOException e) {}
+               }
+               if (bis2 != null) {
+                       try {
+                               bis2.close();
+                       }
+                       catch (IOException e) {}
+               }
+       }
+       }
+
+       public boolean remove(String fileName) throws IOException {
         File file = createFile(fileName);
         if (file.exists()) {
             if (file.delete()) {

Modified: 
ace/trunk/org.apache.ace.obr/test/org/apache/ace/obr/servlet/MockBundleStore.java
URL: 
http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.obr/test/org/apache/ace/obr/servlet/MockBundleStore.java?rev=1541473&r1=1541472&r2=1541473&view=diff
==============================================================================
--- 
ace/trunk/org.apache.ace.obr/test/org/apache/ace/obr/servlet/MockBundleStore.java
 (original)
+++ 
ace/trunk/org.apache.ace.obr/test/org/apache/ace/obr/servlet/MockBundleStore.java
 Wed Nov 13 11:22:49 2013
@@ -40,7 +40,7 @@ public class MockBundleStore implements 
         return m_outFile;
     }
 
-    public String put(InputStream data, String fileName) throws IOException {
+    public String put(InputStream data, String fileName, boolean replace) 
throws IOException {
         if (fileName.equals("NewFile")) {
             return "NewFile";
         }

Modified: 
ace/trunk/org.apache.ace.obr/test/org/apache/ace/obr/storage/file/BundleFileStoreTest.java
URL: 
http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.obr/test/org/apache/ace/obr/storage/file/BundleFileStoreTest.java?rev=1541473&r1=1541472&r2=1541473&view=diff
==============================================================================
--- 
ace/trunk/org.apache.ace.obr/test/org/apache/ace/obr/storage/file/BundleFileStoreTest.java
 (original)
+++ 
ace/trunk/org.apache.ace.obr/test/org/apache/ace/obr/storage/file/BundleFileStoreTest.java
 Wed Nov 13 11:22:49 2013
@@ -32,6 +32,7 @@ import java.util.Random;
 import java.util.jar.Attributes;
 import java.util.jar.JarOutputStream;
 import java.util.jar.Manifest;
+import java.util.zip.ZipEntry;
 
 import org.apache.ace.obr.metadata.MetadataGenerator;
 import org.apache.ace.obr.storage.file.constants.OBRFileStoreConstants;
@@ -250,25 +251,36 @@ public class BundleFileStoreTest {
     @Test(groups = { UNIT })
     public void putBundle() throws Exception {
         File bundle = createTmpResource("foo.bar", "1.0.0");
-        String filePath = m_bundleStore.put(new FileInputStream(bundle), null);
+        String filePath = m_bundleStore.put(new FileInputStream(bundle), null, 
false);
         assert filePath.equals("foo/foo.bar-1.0.0.jar") : "Path should be 
'foo/foo.bar-1.0.0.jar', was " + filePath;
         File file = new File(m_directory, filePath);
         assert file.exists();
     }
 
     @Test(groups = { UNIT })
-    public void putBundleDuplicate() throws Exception {
+    public void putBundleSameDuplicate() throws Exception {
         File bundle = createTmpResource("foo.bar", "1.0.0");
-        String filePath = m_bundleStore.put(new FileInputStream(bundle), null);
+        String filePath = m_bundleStore.put(new FileInputStream(bundle), null, 
false);
         assert filePath != null;
-        String filePath2 = m_bundleStore.put(new FileInputStream(bundle), 
null);
+        String filePath2 = m_bundleStore.put(new FileInputStream(bundle), 
null, false);
+        assert filePath2 != null;
+        assert filePath2.equals(filePath);
+    }
+
+    @Test(groups = { UNIT })
+    public void putBundleDifferentDuplicate() throws Exception {
+        File bundle = createTmpResource("foo.bar", "1.0.0", new byte[] {1});
+        File bundle2 = createTmpResource("foo.bar", "1.0.0", new byte[] {2});
+        String filePath = m_bundleStore.put(new FileInputStream(bundle), null, 
false);
+        assert filePath != null;
+        String filePath2 = m_bundleStore.put(new FileInputStream(bundle2), 
null, false);
         assert filePath2 == null;
     }
 
     @Test(groups = { UNIT }, expectedExceptions = { IOException.class }, 
expectedExceptionsMessageRegExp = "Not a valid bundle and no filename found.*")
     public void putBundleFail() throws Exception {
         File bundle = createTmpResource(null, "1.0.0");
-        String filePath = m_bundleStore.put(new FileInputStream(bundle), null);
+        String filePath = m_bundleStore.put(new FileInputStream(bundle), null, 
false);
         assert filePath.equals("foo/bar/foo.bar-1.0.0.jar") : "Path should be 
'foo/bar/foo.bar-1.0.0.jar', was " + filePath;
         File file = new File(m_directory, filePath);
         assert file.exists();
@@ -277,7 +289,7 @@ public class BundleFileStoreTest {
     @Test(groups = { UNIT })
     public void putRemoveArtifact() throws Exception {
         File bundle = createTmpResource(null, null);
-        String filePath = m_bundleStore.put(new FileInputStream(bundle), 
"foo.bar-2.3.7.test1.xxx");
+        String filePath = m_bundleStore.put(new FileInputStream(bundle), 
"foo.bar-2.3.7.test1.xxx", false);
         assert filePath.equals("foo/foo.bar-2.3.7.test1.xxx");
         File file = new File(m_directory, filePath);
         assert file.exists();
@@ -286,7 +298,7 @@ public class BundleFileStoreTest {
     @Test(groups = { UNIT })
     public void putArtifactDefaultVersion() throws Exception {
         File bundle = createTmpResource(null, null);
-        String filePath = m_bundleStore.put(new FileInputStream(bundle), 
"foo.bar.xxx");
+        String filePath = m_bundleStore.put(new FileInputStream(bundle), 
"foo.bar.xxx", false);
         assert filePath.equals("foo/foo.bar.xxx");
         File file = new File(m_directory, filePath);
         assert file.exists();
@@ -295,7 +307,7 @@ public class BundleFileStoreTest {
     @Test(groups = { UNIT })
     public void putArtifactMavenVersion() throws Exception {
         File bundle = createTmpResource(null, null);
-        String filePath = m_bundleStore.put(new FileInputStream(bundle), 
"foo.bar-2.3.7-test1.xxx");
+        String filePath = m_bundleStore.put(new FileInputStream(bundle), 
"foo.bar-2.3.7-test1.xxx", false);
         assert filePath.equals("foo/foo.bar-2.3.7-test1.xxx");
         File file = new File(m_directory, filePath);
         assert file.exists();
@@ -304,19 +316,19 @@ public class BundleFileStoreTest {
     @Test(groups = { UNIT }, expectedExceptions = { IOException.class }, 
expectedExceptionsMessageRegExp = "Not a valid bundle and no filename found.*")
     public void putArtifactFail1() throws Exception {
         File bundle = createTmpResource(null, null);
-        m_bundleStore.put(new FileInputStream(bundle), null);
+        m_bundleStore.put(new FileInputStream(bundle), null, false);
     }
 
     @Test(groups = { UNIT }, expectedExceptions = { IOException.class }, 
expectedExceptionsMessageRegExp = "Not a valid bundle and no filename found.*")
     public void putArtifactFail2() throws Exception {
         File bundle = createTmpResource(null, null);
-        m_bundleStore.put(new FileInputStream(bundle), "");
+        m_bundleStore.put(new FileInputStream(bundle), "", false);
     }
 
     @Test(groups = { UNIT })
     public void removeBundle() throws Exception {
         File bundle = createTmpResource("foo.bar", "1.0.0");
-        String filePath = m_bundleStore.put(new FileInputStream(bundle), null);
+        String filePath = m_bundleStore.put(new FileInputStream(bundle), null, 
false);
         File file = new File(m_directory, filePath);
         assert file.exists();
         assert m_bundleStore.remove(filePath);
@@ -333,7 +345,7 @@ public class BundleFileStoreTest {
     @Test(groups = { UNIT })
     public void removeArtifact() throws Exception {
         File bundle = createTmpResource(null, null);
-        String filePath = m_bundleStore.put(new FileInputStream(bundle), 
"foo.bar-2.3.7.test1.xxx");
+        String filePath = m_bundleStore.put(new FileInputStream(bundle), 
"foo.bar-2.3.7.test1.xxx", false);
         assert filePath.equals("foo/foo.bar-2.3.7.test1.xxx");
         File file = new File(m_directory, filePath);
         assert file.exists();
@@ -389,6 +401,10 @@ public class BundleFileStoreTest {
     }
     
     private File createTmpResource(String symbolicName, String version) throws 
IOException {
+       return createTmpResource(symbolicName, version, null);
+    }
+    
+    private File createTmpResource(String symbolicName, String version, byte[] 
data) throws IOException {
         File tmpFile = File.createTempFile("tmpbundle-", "jar");
         tmpFile.deleteOnExit();
         
@@ -401,6 +417,10 @@ public class BundleFileStoreTest {
             manifest.getMainAttributes().putValue(Constants.BUNDLE_VERSION, 
version);
         }
         JarOutputStream target = new JarOutputStream(new 
FileOutputStream(tmpFile), manifest);
+        if (data != null) {
+               target.putNextEntry(new ZipEntry("data"));
+               target.write(data, 0, data.length);
+        }
         target.close();
         return tmpFile;
     }


Reply via email to