You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ss...@apache.org on 2016/12/04 07:22:35 UTC

svn commit: r1772506 - in /sling: branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/ branches/testing/mocks/osgi-mock-1.x/src/test/java/org/apache/sling/testing/mock/osgi/ trunk/testing/mocks/osgi-mock/src/main/java/...

Author: sseifert
Date: Sun Dec  4 07:22:34 2016
New Revision: 1772506

URL: http://svn.apache.org/viewvc?rev=1772506&view=rev
Log:
SLING-6362 osgi-mock: ManifestScanner

Added:
    sling/branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/ManifestScanner.java   (with props)
    sling/branches/testing/mocks/osgi-mock-1.x/src/test/java/org/apache/sling/testing/mock/osgi/ManifestScannerTest.java   (with props)
    sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/ManifestScanner.java   (with props)
    sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/ManifestScannerTest.java   (with props)
Modified:
    sling/branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java
    sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java

Added: sling/branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/ManifestScanner.java
URL: http://svn.apache.org/viewvc/sling/branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/ManifestScanner.java?rev=1772506&view=auto
==============================================================================
--- sling/branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/ManifestScanner.java (added)
+++ sling/branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/ManifestScanner.java Sun Dec  4 07:22:34 2016
@@ -0,0 +1,93 @@
+/*
+ * 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.testing.mock.osgi;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Collection;
+import java.util.Enumeration;
+import java.util.LinkedHashSet;
+import java.util.Set;
+import java.util.jar.Attributes;
+import java.util.jar.JarFile;
+import java.util.jar.Manifest;
+
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Scan METAINF/MANIFEST.MF files.
+ */
+public final class ManifestScanner {
+    
+    private static final Logger log = LoggerFactory.getLogger(ManifestScanner.class);
+    
+    private ManifestScanner() {
+        // static methods only
+    }
+
+    /**
+     * Get all bundle header values stored in MANIFEST.MF files as attributes.
+     * Attributes values from all manifest files are collected, and values separated by "," are returned individually.
+     * The order of the values from each entry is preserved, but the overall order when multiple bundles define such an entry
+     * is not deterministic. Duplicate values are eliminated.
+     * @param attributeName Attribute / Bundle header name.
+     * @return List of values.
+     */
+    public static Collection<String> getValues(final String attributeName) {
+        Set<String> values = new LinkedHashSet<String>();
+        try {
+            Enumeration<URL> resEnum = ManifestScanner.class.getClassLoader().getResources(JarFile.MANIFEST_NAME);
+            while (resEnum.hasMoreElements()) {
+                try {
+                    URL url = (URL)resEnum.nextElement();
+                    InputStream is = url.openStream();
+                    if (is != null) {
+                        try {
+                            Manifest manifest = new Manifest(is);
+                            Attributes mainAttribs = manifest.getMainAttributes();
+                            String valueList = mainAttribs.getValue(attributeName);
+                            String[] valueArray = StringUtils.split(valueList, ",");
+                            if (valueArray != null) {
+                                for (String value : valueArray) {
+                                    if (!StringUtils.isBlank(value)) {
+                                        values.add(StringUtils.trim(value));
+                                    }
+                                }
+                            }
+                        }
+                        finally {
+                            is.close();
+                        }
+                    }
+                }
+                catch (Throwable ex) {
+                    log.warn("Unable to read JAR manifest.", ex);
+                }
+            }
+        }
+        catch (IOException ex) {
+            log.warn("Unable to read JAR manifests.", ex);
+        }
+        return values; 
+    }
+
+}

Propchange: sling/branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/ManifestScanner.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/ManifestScanner.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Sun Dec  4 07:22:34 2016
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: sling/branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/ManifestScanner.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: sling/branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java
URL: http://svn.apache.org/viewvc/sling/branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java?rev=1772506&r1=1772505&r2=1772506&view=diff
==============================================================================
--- sling/branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java (original)
+++ sling/branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java Sun Dec  4 07:22:34 2016
@@ -19,5 +19,5 @@
 /**
  * Mock implementation of selected OSGi APIs.
  */
-@aQute.bnd.annotation.Version("2.5")
+@aQute.bnd.annotation.Version("2.6")
 package org.apache.sling.testing.mock.osgi;

Added: sling/branches/testing/mocks/osgi-mock-1.x/src/test/java/org/apache/sling/testing/mock/osgi/ManifestScannerTest.java
URL: http://svn.apache.org/viewvc/sling/branches/testing/mocks/osgi-mock-1.x/src/test/java/org/apache/sling/testing/mock/osgi/ManifestScannerTest.java?rev=1772506&view=auto
==============================================================================
--- sling/branches/testing/mocks/osgi-mock-1.x/src/test/java/org/apache/sling/testing/mock/osgi/ManifestScannerTest.java (added)
+++ sling/branches/testing/mocks/osgi-mock-1.x/src/test/java/org/apache/sling/testing/mock/osgi/ManifestScannerTest.java Sun Dec  4 07:22:34 2016
@@ -0,0 +1,43 @@
+/*
+ * 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.testing.mock.osgi;
+
+import static org.junit.Assert.assertTrue;
+
+import java.util.Collection;
+
+import org.junit.Test;
+import org.osgi.framework.Constants;
+
+public class ManifestScannerTest {
+
+    /**
+     * Test some MANIFEST entries from commons-io:commons-io:2.4
+     */
+    @Test
+    public void testGetValues() {
+        Collection<String> bundleSymbolicNames = ManifestScanner.getValues(Constants.BUNDLE_SYMBOLICNAME);
+        assertTrue(bundleSymbolicNames.contains("org.apache.commons.io"));
+
+        Collection<String> includeResource = ManifestScanner.getValues("Include-Resource");
+        assertTrue(includeResource.contains("META-INF/LICENSE.txt=LICENSE.txt"));
+        assertTrue(includeResource.contains("META-INF/NOTICE.txt=NOTICE.txt"));
+    }
+
+}

Propchange: sling/branches/testing/mocks/osgi-mock-1.x/src/test/java/org/apache/sling/testing/mock/osgi/ManifestScannerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/branches/testing/mocks/osgi-mock-1.x/src/test/java/org/apache/sling/testing/mock/osgi/ManifestScannerTest.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Sun Dec  4 07:22:34 2016
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: sling/branches/testing/mocks/osgi-mock-1.x/src/test/java/org/apache/sling/testing/mock/osgi/ManifestScannerTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/ManifestScanner.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/ManifestScanner.java?rev=1772506&view=auto
==============================================================================
--- sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/ManifestScanner.java (added)
+++ sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/ManifestScanner.java Sun Dec  4 07:22:34 2016
@@ -0,0 +1,93 @@
+/*
+ * 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.testing.mock.osgi;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Collection;
+import java.util.Enumeration;
+import java.util.LinkedHashSet;
+import java.util.Set;
+import java.util.jar.Attributes;
+import java.util.jar.JarFile;
+import java.util.jar.Manifest;
+
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Scan METAINF/MANIFEST.MF files.
+ */
+public final class ManifestScanner {
+    
+    private static final Logger log = LoggerFactory.getLogger(ManifestScanner.class);
+    
+    private ManifestScanner() {
+        // static methods only
+    }
+
+    /**
+     * Get all bundle header values stored in MANIFEST.MF files as attributes.
+     * Attributes values from all manifest files are collected, and values separated by "," are returned individually.
+     * The order of the values from each entry is preserved, but the overall order when multiple bundles define such an entry
+     * is not deterministic. Duplicate values are eliminated.
+     * @param attributeName Attribute / Bundle header name.
+     * @return List of values.
+     */
+    public static Collection<String> getValues(final String attributeName) {
+        Set<String> values = new LinkedHashSet<String>();
+        try {
+            Enumeration<URL> resEnum = ManifestScanner.class.getClassLoader().getResources(JarFile.MANIFEST_NAME);
+            while (resEnum.hasMoreElements()) {
+                try {
+                    URL url = (URL)resEnum.nextElement();
+                    InputStream is = url.openStream();
+                    if (is != null) {
+                        try {
+                            Manifest manifest = new Manifest(is);
+                            Attributes mainAttribs = manifest.getMainAttributes();
+                            String valueList = mainAttribs.getValue(attributeName);
+                            String[] valueArray = StringUtils.split(valueList, ",");
+                            if (valueArray != null) {
+                                for (String value : valueArray) {
+                                    if (!StringUtils.isBlank(value)) {
+                                        values.add(StringUtils.trim(value));
+                                    }
+                                }
+                            }
+                        }
+                        finally {
+                            is.close();
+                        }
+                    }
+                }
+                catch (Throwable ex) {
+                    log.warn("Unable to read JAR manifest.", ex);
+                }
+            }
+        }
+        catch (IOException ex) {
+            log.warn("Unable to read JAR manifests.", ex);
+        }
+        return values; 
+    }
+
+}

Propchange: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/ManifestScanner.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/ManifestScanner.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Sun Dec  4 07:22:34 2016
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/ManifestScanner.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java?rev=1772506&r1=1772505&r2=1772506&view=diff
==============================================================================
--- sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java (original)
+++ sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java Sun Dec  4 07:22:34 2016
@@ -19,5 +19,5 @@
 /**
  * Mock implementation of selected OSGi APIs.
  */
-@org.osgi.annotation.versioning.Version("3.1")
+@org.osgi.annotation.versioning.Version("3.2")
 package org.apache.sling.testing.mock.osgi;

Added: sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/ManifestScannerTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/ManifestScannerTest.java?rev=1772506&view=auto
==============================================================================
--- sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/ManifestScannerTest.java (added)
+++ sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/ManifestScannerTest.java Sun Dec  4 07:22:34 2016
@@ -0,0 +1,43 @@
+/*
+ * 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.testing.mock.osgi;
+
+import static org.junit.Assert.assertTrue;
+
+import java.util.Collection;
+
+import org.junit.Test;
+import org.osgi.framework.Constants;
+
+public class ManifestScannerTest {
+
+    /**
+     * Test some MANIFEST entries from commons-io:commons-io:2.4
+     */
+    @Test
+    public void testGetValues() {
+        Collection<String> bundleSymbolicNames = ManifestScanner.getValues(Constants.BUNDLE_SYMBOLICNAME);
+        assertTrue(bundleSymbolicNames.contains("org.apache.commons.io"));
+
+        Collection<String> includeResource = ManifestScanner.getValues("Include-Resource");
+        assertTrue(includeResource.contains("META-INF/LICENSE.txt=LICENSE.txt"));
+        assertTrue(includeResource.contains("META-INF/NOTICE.txt=NOTICE.txt"));
+    }
+
+}

Propchange: sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/ManifestScannerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/ManifestScannerTest.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Sun Dec  4 07:22:34 2016
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/ManifestScannerTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain