aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1386802 - in /aries/trunk/spi-fly/spi-fly-static-tool/src: main/java/org/apache/aries/spifly/statictool/ test/java/org/apache/aries/spifly/statictool/ test/java/org/apache/aries/spifly/statictool/bundle/
Date Mon, 17 Sep 2012 20:11:36 GMT
Author: davidb
Date: Mon Sep 17 20:11:36 2012
New Revision: 1386802

URL: http://svn.apache.org/viewvc?rev=1386802&view=rev
Log:
Add support for Require Capability to the static tool.

Added:
    aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/RequirementTest.java
    aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/bundle/Test3Class.java
Modified:
    aries/trunk/spi-fly/spi-fly-static-tool/src/main/java/org/apache/aries/spifly/statictool/Main.java
    aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/ConsumerTest.java
    aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/bundle/TestClass.java

Modified: aries/trunk/spi-fly/spi-fly-static-tool/src/main/java/org/apache/aries/spifly/statictool/Main.java
URL: http://svn.apache.org/viewvc/aries/trunk/spi-fly/spi-fly-static-tool/src/main/java/org/apache/aries/spifly/statictool/Main.java?rev=1386802&r1=1386801&r2=1386802&view=diff
==============================================================================
--- aries/trunk/spi-fly/spi-fly-static-tool/src/main/java/org/apache/aries/spifly/statictool/Main.java
(original)
+++ aries/trunk/spi-fly/spi-fly-static-tool/src/main/java/org/apache/aries/spifly/statictool/Main.java
Mon Sep 17 20:11:36 2012
@@ -26,6 +26,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.URL;
+import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
 import java.util.jar.Attributes;
@@ -40,6 +41,7 @@ import org.apache.aries.spifly.Streams;
 import org.apache.aries.spifly.Util;
 import org.apache.aries.spifly.WeavingData;
 import org.apache.aries.spifly.weaver.TCCLSetterVisitor;
+import org.apache.aries.util.manifest.ManifestHeaderProcessor.GenericMetadata;
 import org.objectweb.asm.ClassReader;
 import org.objectweb.asm.ClassWriter;
 import org.osgi.framework.Constants;
@@ -79,18 +81,33 @@ public class Main {
         File jarFile = new File(jarPath);
         File tempDir = new File(System.getProperty("java.io.tmpdir") + File.separator + jarFile.getName()
+ "_" + System.currentTimeMillis());
         Manifest manifest = unJar(jarFile, tempDir);
-        String consumerHeader = manifest.getMainAttributes().getValue(SpiFlyConstants.SPI_CONSUMER_HEADER);
-//        if (consumerHeader == null) {
-//            String reqCap = manifest.getMainAttributes().getValue(SpiFlyConstants.REQUIRE_CAPABILITY);
-//            sdasda
-//        }
+        String consumerHeaderVal = manifest.getMainAttributes().getValue(SpiFlyConstants.SPI_CONSUMER_HEADER);
+        String consumerHeaderKey = null;
+        if (consumerHeaderVal != null) {
+            consumerHeaderKey = SpiFlyConstants.SPI_CONSUMER_HEADER;
+        } else {
+            consumerHeaderVal = manifest.getMainAttributes().getValue(SpiFlyConstants.REQUIRE_CAPABILITY);
+            if (consumerHeaderVal != null) {
+                consumerHeaderKey = SpiFlyConstants.REQUIRE_CAPABILITY;
+            }
+        }
 
-        if (consumerHeader != null) {
+        if (consumerHeaderVal != null) {
             String bcp = manifest.getMainAttributes().getValue(Constants.BUNDLE_CLASSPATH);
-            weaveDir(tempDir, consumerHeader, bcp);
+            weaveDir(tempDir, consumerHeaderKey, consumerHeaderVal, bcp);
+
+            if (SpiFlyConstants.SPI_CONSUMER_HEADER.equals(consumerHeaderKey)) {
+                manifest.getMainAttributes().remove(new Attributes.Name(SpiFlyConstants.SPI_CONSUMER_HEADER));
+            } else {
+                // It's SpiFlyConstants.REQUIRE_CAPABILITY
+
+                // Take out the processor requirement, this probably needs to be improved
a little bit
+                String newConsumerHeaderVal = consumerHeaderVal.replaceAll(
+                        "osgi[.]extender;\\s*filter[:][=][\"]?[(]osgi[.]extender[=]osgi[.]serviceloader[.]processor[)][\"]?",
"");
+                manifest.getMainAttributes().putValue(SpiFlyConstants.REQUIRE_CAPABILITY,
newConsumerHeaderVal);
+            }
+            manifest.getMainAttributes().putValue(SpiFlyConstants.PROCESSED_SPI_CONSUMER_HEADER,
consumerHeaderVal);
 
-            manifest.getMainAttributes().remove(new Attributes.Name(SpiFlyConstants.SPI_CONSUMER_HEADER));
-            manifest.getMainAttributes().putValue(SpiFlyConstants.PROCESSED_SPI_CONSUMER_HEADER,
consumerHeader);
             // TODO if new packages needed then...
             extendImportPackage(manifest);
 
@@ -102,6 +119,29 @@ public class Main {
         delTree(tempDir);
     }
 
+    private static String serializeRequirement(GenericMetadata req) {
+        StringBuilder serialized = new StringBuilder();
+
+        serialized.append(req.getNamespace());
+        serialized.append(';');
+        for (Map.Entry<String, Object> attr : req.getAttributes().entrySet()) {
+            serialized.append(attr.getKey());
+            serialized.append('=');
+            serialized.append(attr.getValue());
+            serialized.append(';');
+        }
+
+        for (Map.Entry<String, String> dir : req.getDirectives().entrySet()) {
+            serialized.append(dir.getKey());
+            serialized.append(":=");
+            serialized.append(dir.getValue());
+            serialized.append(';');
+        }
+        serialized.deleteCharAt(serialized.length() -1);
+        serialized.append(',');
+        return serialized.toString();
+    }
+
     private static void extendImportPackage(Manifest manifest) throws IOException {
         String utilPkgVersion = getPackageVersion(Util.class);
 
@@ -147,9 +187,8 @@ public class Main {
         return new File(s);
     }
 
-    private static void weaveDir(File dir, String consumerHeader, String bundleClassPath)
throws Exception {
-        // TODO support Require-Capability in addition to SPI-Consumer
-        Set<WeavingData> wd = ConsumerHeaderProcessor.processHeader(SpiFlyConstants.SPI_CONSUMER_HEADER,
consumerHeader);
+    private static void weaveDir(File dir, String consumerHeaderKey, String consumerHeaderValue,
String bundleClassPath) throws Exception {
+        Set<WeavingData> wd = ConsumerHeaderProcessor.processHeader(consumerHeaderKey,
consumerHeaderValue);
 
         String dirName = dir.getAbsolutePath();
 
@@ -193,17 +232,17 @@ public class Main {
             for (String entry : bundleClassPath.split(",")) {
                 File jarFile = new File(dir, entry.trim());
                 if (jarFile.isFile()) {
-                    weaveBCPJar(jarFile, consumerHeader);
+                    weaveBCPJar(jarFile, consumerHeaderKey, consumerHeaderValue);
                 }
             }
         }
     }
 
-    private static void weaveBCPJar(File jarFile, String consumerHeader) throws Exception
{
+    private static void weaveBCPJar(File jarFile, String consumerHeaderKey, String consumerHeaderVal)
throws Exception {
         File tempDir = new File(System.getProperty("java.io.tmpdir") + File.separator + jarFile.getName()
+ "_" + System.currentTimeMillis());
         try {
             Manifest manifest = unJar(jarFile, tempDir);
-            weaveDir(tempDir, consumerHeader, null);
+            weaveDir(tempDir, consumerHeaderKey, consumerHeaderVal, null);
             if (!jarFile.delete()) {
                 throw new IOException("Could not replace file: " + jarFile);
             }

Modified: aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/ConsumerTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/ConsumerTest.java?rev=1386802&r1=1386801&r2=1386802&view=diff
==============================================================================
--- aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/ConsumerTest.java
(original)
+++ aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/ConsumerTest.java
Mon Sep 17 20:11:36 2012
@@ -18,7 +18,6 @@
  */
 package org.apache.aries.spifly.statictool;
 
-import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.net.URL;
@@ -32,6 +31,7 @@ import java.util.zip.ZipEntry;
 import org.apache.aries.spifly.SpiFlyConstants;
 import org.apache.aries.spifly.Streams;
 import org.apache.aries.spifly.statictool.bundle.Test2Class;
+import org.apache.aries.spifly.statictool.bundle.Test3Class;
 import org.apache.aries.spifly.statictool.bundle.TestClass;
 import org.junit.Assert;
 import org.junit.Test;
@@ -43,6 +43,8 @@ public class ConsumerTest {
         URL testClassURL = getClass().getResource("/" + testClassFileName);
         String test2ClassFileName = Test2Class.class.getName().replace('.', '/') + ".class";
         URL test2ClassURL = getClass().getResource("/" + test2ClassFileName);
+        String test3ClassFileName = Test3Class.class.getName().replace('.', '/') + ".class";
+        URL test3ClassURL = getClass().getResource("/" + test3ClassFileName);
 
         File jarFile = new File(System.getProperty("java.io.tmpdir") + "/testjar_" + System.currentTimeMillis()
+ ".jar");
         File expectedFile = null;
@@ -55,13 +57,14 @@ public class ConsumerTest {
             mainAttributes.putValue("Bundle-SymbolicName", "testbundle");
             mainAttributes.putValue("Foo", "Bar Bar");
             mainAttributes.putValue(SpiFlyConstants.SPI_CONSUMER_HEADER, Test2Class.class.getName()
+ "#getTCCL()");
-            ByteArrayOutputStream baos = new ByteArrayOutputStream();
-            mf.write(baos);
+
             JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile), mf);
             jos.putNextEntry(new ZipEntry(testClassFileName));
             Streams.pump(testClassURL.openStream(), jos);
             jos.putNextEntry(new ZipEntry(test2ClassFileName));
             Streams.pump(test2ClassURL.openStream(), jos);
+            jos.putNextEntry(new ZipEntry(test3ClassFileName));
+            Streams.pump(test3ClassURL.openStream(), jos);
             jos.close();
 
             Main.main(jarFile.getCanonicalPath());
@@ -76,6 +79,7 @@ public class ConsumerTest {
             Assert.assertEquals("2.0", expectedMF.getMainAttributes().getValue("Bundle-ManifestVersion"));
             Assert.assertEquals("testbundle", expectedMF.getMainAttributes().getValue("Bundle-SymbolicName"));
             Assert.assertEquals("Bar Bar", expectedMF.getMainAttributes().getValue("Foo"));
+            Assert.assertEquals("org.apache.aries.spifly;version=\"[1.0.0,1.1.0)\"", expectedMF.getMainAttributes().getValue("Import-Package"));
             Assert.assertNull(expectedMF.getMainAttributes().get(SpiFlyConstants.SPI_CONSUMER_HEADER));
 
             JarFile initialJarFile = new JarFile(jarFile);
@@ -84,8 +88,12 @@ public class ConsumerTest {
             Assert.assertFalse("The transformed class should be different", Arrays.equals(orgBytes,
transBytes));
 
             byte[] orgBytes2 = Streams.suck(initialJarFile.getInputStream(new ZipEntry(test2ClassFileName)));
-            byte[] nonTransBytes = Streams.suck(transformedJarFile.getInputStream(new ZipEntry(test2ClassFileName)));
-            Assert.assertArrayEquals(orgBytes2, nonTransBytes);
+            byte[] nonTransBytes2 = Streams.suck(transformedJarFile.getInputStream(new ZipEntry(test2ClassFileName)));
+            Assert.assertArrayEquals(orgBytes2, nonTransBytes2);
+
+            byte[] orgBytes3 = Streams.suck(initialJarFile.getInputStream(new ZipEntry(test3ClassFileName)));
+            byte[] nonTransBytes3 = Streams.suck(transformedJarFile.getInputStream(new ZipEntry(test3ClassFileName)));
+            Assert.assertArrayEquals(orgBytes3, nonTransBytes3);
 
             initialJarFile.close();
             transformedJarFile.close();

Added: aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/RequirementTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/RequirementTest.java?rev=1386802&view=auto
==============================================================================
--- aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/RequirementTest.java
(added)
+++ aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/RequirementTest.java
Mon Sep 17 20:11:36 2012
@@ -0,0 +1,113 @@
+/**
+ * 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.aries.spifly.statictool;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.net.URL;
+import java.util.Arrays;
+import java.util.jar.Attributes;
+import java.util.jar.JarFile;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Manifest;
+import java.util.zip.ZipEntry;
+
+import org.apache.aries.spifly.SpiFlyConstants;
+import org.apache.aries.spifly.Streams;
+import org.apache.aries.spifly.statictool.bundle.Test2Class;
+import org.apache.aries.spifly.statictool.bundle.Test3Class;
+import org.apache.aries.spifly.statictool.bundle.TestClass;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class RequirementTest {
+    @Test
+    public void testConsumerBundle() throws Exception {
+        String testClassFileName = TestClass.class.getName().replace('.', '/') + ".class";
+        URL testClassURL = getClass().getResource("/" + testClassFileName);
+        String test2ClassFileName = Test2Class.class.getName().replace('.', '/') + ".class";
+        URL test2ClassURL = getClass().getResource("/" + test2ClassFileName);
+        String test3ClassFileName = Test3Class.class.getName().replace('.', '/') + ".class";
+        URL test3ClassURL = getClass().getResource("/" + test3ClassFileName);
+
+        File jarFile = new File(System.getProperty("java.io.tmpdir") + "/testjar_" + System.currentTimeMillis()
+ ".jar");
+        File expectedFile = null;
+        try {
+            // Create the jarfile to be used for testing
+            Manifest mf = new Manifest();
+            Attributes mainAttributes = mf.getMainAttributes();
+            mainAttributes.putValue("Manifest-Version", "1.0");
+            mainAttributes.putValue("Bundle-ManifestVersion", "2.0");
+            mainAttributes.putValue("Bundle-SymbolicName", "testbundle");
+            mainAttributes.putValue("Foo", "Bar Bar");
+            mainAttributes.putValue("Import-Package", "org.foo.bar");
+            mainAttributes.putValue(SpiFlyConstants.REQUIRE_CAPABILITY,
+                    "osgi.serviceloader; filter:=\"(osgi.serviceloader=org.apache.aries.spifly.mysvc.SPIProvider)\";cardinality:=multiple,"
+
+                    "osgi.extender; filter:=\"(osgi.extender=osgi.serviceloader.processor)\"");
+
+            JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile), mf);
+            jos.putNextEntry(new ZipEntry(testClassFileName));
+            Streams.pump(testClassURL.openStream(), jos);
+            jos.putNextEntry(new ZipEntry(test2ClassFileName));
+            Streams.pump(test2ClassURL.openStream(), jos);
+            jos.putNextEntry(new ZipEntry(test3ClassFileName));
+            Streams.pump(test3ClassURL.openStream(), jos);
+            jos.close();
+
+            Main.main(jarFile.getCanonicalPath());
+
+            expectedFile = new File(jarFile.getParent(), jarFile.getName().replaceAll("[.]jar",
"_spifly.jar"));
+            Assert.assertTrue("A processed separate bundle should have been created", expectedFile.exists());
+            // Check manifest in generated bundle.
+            JarFile transformedJarFile = new JarFile(expectedFile);
+            Manifest expectedMF = transformedJarFile.getManifest();
+            Assert.assertEquals("1.0", expectedMF.getMainAttributes().getValue("Manifest-Version"));
+            Assert.assertEquals("2.0", expectedMF.getMainAttributes().getValue("Bundle-ManifestVersion"));
+            Assert.assertEquals("testbundle", expectedMF.getMainAttributes().getValue("Bundle-SymbolicName"));
+            Assert.assertEquals("Bar Bar", expectedMF.getMainAttributes().getValue("Foo"));
+            Assert.assertEquals("osgi.serviceloader; filter:=\"(osgi.serviceloader=org.apache.aries.spifly.mysvc.SPIProvider)\";cardinality:=multiple,",
+                    expectedMF.getMainAttributes().getValue(SpiFlyConstants.REQUIRE_CAPABILITY));
+            String importPackage = expectedMF.getMainAttributes().getValue("Import-Package");
+            Assert.assertTrue(
+                "org.foo.bar,org.apache.aries.spifly;version=\"[1.0.0,1.1.0)\"".equals(importPackage)
||
+                "org.apache.aries.spifly;version=\"[1.0.0,1.1.0)\",org.foo.bar".equals(importPackage));
+
+            JarFile initialJarFile = new JarFile(jarFile);
+            byte[] orgBytes = Streams.suck(initialJarFile.getInputStream(new ZipEntry(testClassFileName)));
+            byte[] nonTransBytes = Streams.suck(transformedJarFile.getInputStream(new ZipEntry(testClassFileName)));
+            Assert.assertArrayEquals(orgBytes, nonTransBytes);
+
+            byte[] orgBytes2 = Streams.suck(initialJarFile.getInputStream(new ZipEntry(test2ClassFileName)));
+            byte[] nonTransBytes2 = Streams.suck(transformedJarFile.getInputStream(new ZipEntry(test2ClassFileName)));
+            Assert.assertArrayEquals(orgBytes2, nonTransBytes2);
+
+            byte[] orgBytes3 = Streams.suck(initialJarFile.getInputStream(new ZipEntry(test3ClassFileName)));
+            byte[] transBytes3 = Streams.suck(transformedJarFile.getInputStream(new ZipEntry(test3ClassFileName)));
+            Assert.assertFalse("The transformed class should be different", Arrays.equals(orgBytes3,
transBytes3));
+
+            initialJarFile.close();
+            transformedJarFile.close();
+        } finally {
+            jarFile.delete();
+
+            if (expectedFile != null)
+                expectedFile.delete();
+        }
+    }
+}

Added: aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/bundle/Test3Class.java
URL: http://svn.apache.org/viewvc/aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/bundle/Test3Class.java?rev=1386802&view=auto
==============================================================================
--- aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/bundle/Test3Class.java
(added)
+++ aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/bundle/Test3Class.java
Mon Sep 17 20:11:36 2012
@@ -0,0 +1,30 @@
+/**
+ * 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.aries.spifly.statictool.bundle;
+
+import java.util.ServiceLoader;
+
+public class Test3Class {
+    public void doitToo() {
+        ServiceLoader<String> sl = ServiceLoader.load(String.class);
+        for (String s : sl) {
+            System.out.println("***: " + s);
+        }
+    }
+}

Modified: aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/bundle/TestClass.java
URL: http://svn.apache.org/viewvc/aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/bundle/TestClass.java?rev=1386802&r1=1386801&r2=1386802&view=diff
==============================================================================
--- aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/bundle/TestClass.java
(original)
+++ aries/trunk/spi-fly/spi-fly-static-tool/src/test/java/org/apache/aries/spifly/statictool/bundle/TestClass.java
Mon Sep 17 20:11:36 2012
@@ -18,6 +18,7 @@
  */
 package org.apache.aries.spifly.statictool.bundle;
 
+
 public class TestClass {
     public void doit() {
         Test2Class.getTCCL();



Mime
View raw message