You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2009/08/25 19:01:22 UTC

svn commit: r807716 - in /sling/trunk/installer/osgi/installer/src: main/java/org/apache/sling/osgi/installer/ main/java/org/apache/sling/osgi/installer/impl/ test/java/org/apache/sling/osgi/installer/impl/

Author: bdelacretaz
Date: Tue Aug 25 17:01:21 2009
New Revision: 807716

URL: http://svn.apache.org/viewvc?rev=807716&view=rev
Log:
SLING-1078 - digests moved to InstallableResource, clients might need them to keep track of things

Added:
    sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/InstallableResourceTest.java   (with props)
Modified:
    sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/InstallableResource.java
    sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/RegisteredResourceImpl.java
    sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/DictionaryDigestTest.java
    sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/RegisteredResourceTest.java

Modified: sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/InstallableResource.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/InstallableResource.java?rev=807716&r1=807715&r2=807716&view=diff
==============================================================================
--- sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/InstallableResource.java (original)
+++ sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/InstallableResource.java Tue Aug 25 17:01:21 2009
@@ -18,7 +18,13 @@
  */
 package org.apache.sling.osgi.installer;
 
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
+import java.io.ObjectOutputStream;
+import java.math.BigInteger;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 import java.util.Dictionary;
 
 /** A piece of data that can be installed by the OSGi controller.
@@ -32,6 +38,7 @@
 	private final InputStream inputStream;
 	private final Dictionary<String, Object> dictionary;
 	private int priority;
+    public static final String DIGEST_TYPE = "MD5";
 	
 	/** Default resource priority */
 	public static final int DEFAULT_PRIORITY = 100;
@@ -78,7 +85,11 @@
 		this.extension = getExtension(url);
 		this.inputStream = null;
 		this.dictionary = d;
-        this.digest = null;
+		try {
+	        this.digest = computeDigest(d);
+		} catch(Exception e) {
+		    throw new IllegalStateException("Unexpected Exception while computing digest", e);
+		}
         this.priority = DEFAULT_PRIORITY;
 	}
 
@@ -137,4 +148,22 @@
     public void setPriority(int priority) {
         this.priority = priority;
     }
-}
+    
+    /** convert digest to readable string (http://www.javalobby.org/java/forums/t84420.html) */
+    public static String digestToString(MessageDigest d) {
+        final BigInteger bigInt = new BigInteger(1, d.digest());
+        return new String(bigInt.toString(16));
+    }
+    
+    /** Digest is needed to detect changes in data 
+     * @throws  */
+    public static String computeDigest(Dictionary<String, Object> data) throws IOException, NoSuchAlgorithmException {
+        final MessageDigest d = MessageDigest.getInstance(DIGEST_TYPE);
+        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        final ObjectOutputStream oos = new ObjectOutputStream(bos);
+        oos.writeObject(data);
+        bos.flush();
+        d.update(bos.toByteArray());
+        return digestToString(d);
+    }
+}
\ No newline at end of file

Modified: sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/RegisteredResourceImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/RegisteredResourceImpl.java?rev=807716&r1=807715&r2=807716&view=diff
==============================================================================
--- sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/RegisteredResourceImpl.java (original)
+++ sling/trunk/installer/osgi/installer/src/main/java/org/apache/sling/osgi/installer/impl/RegisteredResourceImpl.java Tue Aug 25 17:01:21 2009
@@ -76,7 +76,6 @@
     
     private final RegisteredResource.ResourceType resourceType;
 	
-	public static final String DIGEST_TYPE = "MD5";
     public static final String ENTITY_JAR_PREFIX = "jar:";
 	public static final String ENTITY_BUNDLE_PREFIX = "bundle:";
 	public static final String ENTITY_CONFIG_PREFIX = "config:";
@@ -94,6 +93,10 @@
     		priority = input.getPriority();
     		serialNumber = getNextSerialNumber();
     		
+            if(input.getDigest() == null || input.getDigest().length() == 0) {
+                throw new IllegalArgumentException("Missing digest: " + input);
+            }
+            
     		if(resourceType == RegisteredResource.ResourceType.BUNDLE) {
                 if(input.getInputStream() == null) {
                     throw new IllegalArgumentException("InputStream is required for BUNDLE resource type: " + input);
@@ -102,10 +105,6 @@
                 dataFile = getDataFile(ctx);
                 copyToLocalStorage(input.getInputStream(), dataFile);
                 digest = input.getDigest();
-                if(digest == null || digest.length() == 0) {
-                    throw new IllegalArgumentException(
-                            "Digest must be supplied for BUNDLE resource type: " + input);
-                }
                 setAttributesFromManifest();
                 final String name = (String)attributes.get(Constants.BUNDLE_SYMBOLICNAME); 
                 if(name == null) {
@@ -125,11 +124,7 @@
                 } else {
                     dictionary = readDictionary(input.getInputStream()); 
                 }
-                try {
-                    digest = computeDigest(dictionary);
-                } catch(NoSuchAlgorithmException nse) {
-                    throw new IOException("NoSuchAlgorithmException:" + DIGEST_TYPE);
-                }
+                digest = input.getDigest();
     		}
     	} finally {
     		if(input.getInputStream() != null) {
@@ -182,24 +177,6 @@
 		return digest;
 	}
 	
-    /** Digest is needed to detect changes in data 
-     * @throws  */
-    static String computeDigest(Dictionary<String, Object> data) throws IOException, NoSuchAlgorithmException {
-        final MessageDigest d = MessageDigest.getInstance(DIGEST_TYPE);
-        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        final ObjectOutputStream oos = new ObjectOutputStream(bos);
-        oos.writeObject(data);
-        bos.flush();
-        d.update(bos.toByteArray());
-        return digestToString(d);
-    }
-
-    /** convert digest to readable string (http://www.javalobby.org/java/forums/t84420.html) */
-    private static String digestToString(MessageDigest d) {
-        final BigInteger bigInt = new BigInteger(1, d.digest());
-        return new String(bigInt.toString(16));
-    }
-    
     /** Copy data to local storage */
 	private void copyToLocalStorage(InputStream data, File f) throws IOException {
 		final OutputStream os = new BufferedOutputStream(new FileOutputStream(f));

Modified: sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/DictionaryDigestTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/DictionaryDigestTest.java?rev=807716&r1=807715&r2=807716&view=diff
==============================================================================
--- sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/DictionaryDigestTest.java (original)
+++ sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/DictionaryDigestTest.java Tue Aug 25 17:01:21 2009
@@ -24,6 +24,8 @@
 import java.util.Dictionary;
 import java.util.Hashtable;
 
+import org.apache.sling.osgi.installer.InstallableResource;
+
 public class DictionaryDigestTest {
 	private void setTestData(Hashtable<String, Object> d) {
 		d.put("str", "value");
@@ -33,7 +35,7 @@
 	
 	private String testDigestChanged(Dictionary<String, Object> d, 
 			String oldDigest, int step, boolean shouldChange) throws Exception {
-		final String newDigest = RegisteredResourceImpl.computeDigest(d);
+		final String newDigest = InstallableResource.computeDigest(d);
 		if(shouldChange) {
 			assertTrue("Digest (" + newDigest + ") should have changed at step " + step, !newDigest.equals(oldDigest));
 		} else {
@@ -51,8 +53,8 @@
 		
 		assertEquals(
 				"Two dictionary with same values have the same key", 
-				RegisteredResourceImpl.computeDigest(d1),
-				RegisteredResourceImpl.computeDigest(d2)
+				InstallableResource.computeDigest(d1),
+				InstallableResource.computeDigest(d2)
 		);
 	}
 	

Added: sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/InstallableResourceTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/InstallableResourceTest.java?rev=807716&view=auto
==============================================================================
--- sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/InstallableResourceTest.java (added)
+++ sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/InstallableResourceTest.java Tue Aug 25 17:01:21 2009
@@ -0,0 +1,61 @@
+/*
+ * 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.sling.osgi.installer.impl;
+
+import java.io.ByteArrayInputStream;
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.apache.sling.osgi.installer.InstallableResource;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+public class InstallableResourceTest {
+    
+    @Test 
+    public void testDictionaryDigest() {
+        final Dictionary<String, Object> d = new Hashtable<String, Object>();
+        final InstallableResource r = new InstallableResource("x:url", d);
+        assertNotNull("Expected InstallableResource to compute its own digest", r.getDigest());
+    }
+    
+    @org.junit.Test public void testDictionaryDigestFromDictionaries() throws Exception {
+        final Hashtable<String, Object> d1 = new Hashtable<String, Object>();
+        final Hashtable<String, Object> d2 = new Hashtable<String, Object>();
+        
+        final String [] keys = { "foo", "bar", "something" };
+        for(int i=0 ; i < keys.length; i++) {
+            d1.put(keys[i], keys[i] + "." + keys[i]);
+        }
+        for(int i=keys.length - 1 ; i >= 0; i--) {
+            d2.put(keys[i], keys[i] + "." + keys[i]);
+        }
+        
+        final InstallableResource r1 = new InstallableResource("test:url1", d1);
+        final InstallableResource r2 = new InstallableResource("test:url1", d2);
+        
+        assertEquals(
+                "Two InstallableResource (Dictionary) with same values but different key orderings must have the same key", 
+                r1.getDigest(),
+                r2.getDigest()
+        );
+    }
+}

Propchange: sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/InstallableResourceTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/InstallableResourceTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/RegisteredResourceTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/RegisteredResourceTest.java?rev=807716&r1=807715&r2=807716&view=diff
==============================================================================
--- sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/RegisteredResourceTest.java (original)
+++ sling/trunk/installer/osgi/installer/src/test/java/org/apache/sling/osgi/installer/impl/RegisteredResourceTest.java Tue Aug 25 17:01:21 2009
@@ -85,7 +85,7 @@
         
         {
             final InputStream s = new ByteArrayInputStream("foo=bar\nother=2".getBytes());
-            final RegisteredResource r = new LocalFileRegisteredResource(new InstallableResource("test:1.properties", s, null));
+            final RegisteredResource r = new LocalFileRegisteredResource(new InstallableResource("test:1.properties", s, "digest1"));
             assertEquals(".properties URL creates a CONFIG resource", 
                     RegisteredResource.ResourceType.CONFIG, r.getResourceType());
             final InputStream rs = r.getInputStream();
@@ -132,50 +132,11 @@
         
         try {
             new LocalFileRegisteredResource(new InstallableResource("test:1.foo", in, null));
+            fail("With non-jar extension, expected an IllegalArgumentException as digest is null");
         } catch(IllegalArgumentException asExpected) {
-            fail("With non-jar extension, did not expect an IllegalArgumentException if digest is null");
         }
     }
     
-    @org.junit.Test public void testDictionaryDigestFromDictionaries() throws Exception {
-        final Hashtable<String, Object> d1 = new Hashtable<String, Object>();
-        final Hashtable<String, Object> d2 = new Hashtable<String, Object>();
-        
-        final String [] keys = { "foo", "bar", "something" };
-        for(int i=0 ; i < keys.length; i++) {
-            d1.put(keys[i], keys[i] + "." + keys[i]);
-        }
-        for(int i=keys.length - 1 ; i >= 0; i--) {
-            d2.put(keys[i], keys[i] + "." + keys[i]);
-        }
-        
-        final RegisteredResource r1 = new RegisteredResourceImpl(null, new InstallableResource("test:url1", d1));
-        final RegisteredResource r2 = new RegisteredResourceImpl(null, new InstallableResource("test:url1", d2));
-        
-        assertEquals(
-                "Two RegisteredResource (Dictionary) with same values but different key orderings must have the same key", 
-                r1.getDigest(),
-                r2.getDigest()
-        );
-    }
-    
-    @org.junit.Test public void testDictionaryDigestFromInputStreams() throws Exception {
-        final String d1 = "foo=bar\nsomething=another\n";
-        final String d2 = "something=another\nfoo=bar\n";
-        
-        final ByteArrayInputStream s1 = new ByteArrayInputStream(d1.getBytes());
-        final ByteArrayInputStream s2 = new ByteArrayInputStream(d2.getBytes());
-        
-        final RegisteredResource r1 = new RegisteredResourceImpl(null, new InstallableResource("test:url1.properties", s1, null));
-        final RegisteredResource r2 = new RegisteredResourceImpl(null, new InstallableResource("test:url2.properties", s2, null));
-        
-        assertEquals(
-                "Two RegisteredResource (InputStream) with same values but different key orderings must have the same key", 
-                r1.getDigest(),
-                r2.getDigest()
-        );
-    }
-    
     @org.junit.Test public void testBundleManifest() throws Exception {
         final File f = getTestBundle("testbundle-1.0.jar");
         final InstallableResource i = new InstallableResource("test:" + f.getAbsolutePath(), new FileInputStream(f), f.getName());
@@ -217,7 +178,7 @@
         };
         
         for(String url : goodOnes) {
-            final RegisteredResource r = new RegisteredResourceImpl(null, new InstallableResource(url, s, null));
+            final RegisteredResource r = new RegisteredResourceImpl(null, new InstallableResource(url, s, "digest1"));
             assertEquals("Expected scheme 'foo' for URL " + url, "foo", r.getUrlScheme());
         }
     }