You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bo...@apache.org on 2014/01/24 18:44:36 UTC

svn commit: r1561086 - in /commons/proper/compress/branches/compress-2.0/src: main/java/org/apache/commons/compress2/archivers/ main/resources/ main/resources/META-INF/ main/resources/META-INF/services/ test/java/org/apache/commons/compress2/archivers/

Author: bodewig
Date: Fri Jan 24 17:44:35 2014
New Revision: 1561086

URL: http://svn.apache.org/r1561086
Log:
discover archive formats via ServiceLoader

Added:
    commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/archivers/Archivers.java   (with props)
    commons/proper/compress/branches/compress-2.0/src/main/resources/
    commons/proper/compress/branches/compress-2.0/src/main/resources/META-INF/
    commons/proper/compress/branches/compress-2.0/src/main/resources/META-INF/services/
    commons/proper/compress/branches/compress-2.0/src/main/resources/META-INF/services/org.apache.commons.compress2.archivers.ArchiveFormat   (with props)
    commons/proper/compress/branches/compress-2.0/src/test/java/org/apache/commons/compress2/archivers/ArchiversTest.java   (with props)

Added: commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/archivers/Archivers.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/archivers/Archivers.java?rev=1561086&view=auto
==============================================================================
--- commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/archivers/Archivers.java (added)
+++ commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/archivers/Archivers.java Fri Jan 24 17:44:35 2014
@@ -0,0 +1,200 @@
+/*
+ * 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.commons.compress2.archivers;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.ServiceConfigurationError;
+import java.util.ServiceLoader;
+
+/**
+ * Loads ArchiveFormats defined as "services" from {@code
+ * META-INF/services/org.apache.commons.compress2.archivers.ArchiveFormat} and provides access to them.
+ *
+ * <p>Uses {@link java.util.ServiceLoader} under the covers but iterates over all formats found eagerly inside the
+ * constructor so errors are reported early.</p>
+ */
+public class Archivers implements Iterable<ArchiveFormat<? extends ArchiveEntry>> {
+    private final ServiceLoader<ArchiveFormat> formatLoader;
+    private Map<String, ArchiveFormat<? extends ArchiveEntry>> archivers;
+
+    /**
+     * Loads services using the current thread's context class loader.
+     * @throws ServiceConfigurationError if an error occurs reading a service file or instantiating a format
+     */
+    public Archivers() throws ServiceConfigurationError {
+        this(Thread.currentThread().getContextClassLoader());
+    }
+
+    /**
+     * Loads services using the given class loader.
+     * @throws ServiceConfigurationError if an error occurs reading a service file or instantiating a format
+     */
+    public Archivers(ClassLoader cl) throws ServiceConfigurationError {
+        this(ServiceLoader.load(ArchiveFormat.class, cl));
+    }
+
+    private Archivers(ServiceLoader<ArchiveFormat> loader) {
+        formatLoader = loader;
+        fillMap();
+    }
+
+    /**
+     * Clears the cached formats and rebuilds it.
+     *
+     * @see ServiceLoader#reload
+     */
+    public void reload() {
+        formatLoader.reload();
+        fillMap();
+    }
+
+    /**
+     * Iterator over all known formats.
+     */
+    public Iterator<ArchiveFormat<? extends ArchiveEntry>> iterator() {
+        return archivers.values().iterator();
+    }
+
+    /**
+     * Iterates over all known formats that can write archives.
+     */
+    public Iterable<ArchiveFormat<? extends ArchiveEntry>> getFormatsWithWriteSupport() {
+        return filter(WRITE_PREDICATE);
+    }
+
+    /**
+     * Iterates over all known formats that can write archives to channels.
+     */
+    public Iterable<ArchiveFormat<? extends ArchiveEntry>> getFormatsWithWriteSupportForChannels() {
+        return filter(WRITE_TO_CHANNEL_PREDICATE);
+    }
+
+    /**
+     * Iterates over all known formats that can read archives from channels.
+     */
+    public Iterable<ArchiveFormat<? extends ArchiveEntry>> getFormatsWithReadSupportForChannels() {
+        return filter(READ_FROM_CHANNEL_PREDICATE);
+    }
+
+    /**
+     * Iterates over all known formats that provide random access input.
+     */
+    public Iterable<ArchiveFormat<? extends ArchiveEntry>> getFormatsWithRandomAccessInput() {
+        return filter(RANDOM_ACCESS_PREDICATE);
+    }
+
+    /**
+     * Gets a format by its name.
+     * @param name the {@link ArchiveFormat#getName name} of the format.
+     * @return the ArchiveFormat instance or null if not format is known by that name
+     */
+    public ArchiveFormat getArchiveFormatByName(String name) {
+        return archivers.get(name);
+    }
+
+    private void fillMap() throws ServiceConfigurationError {
+        // TODO make that a TreeMap sorted for auto-detection order
+        Map<String, ArchiveFormat<? extends ArchiveEntry>> a =
+            new HashMap<String, ArchiveFormat<? extends ArchiveEntry>>();
+        for (ArchiveFormat<? extends ArchiveEntry> f : formatLoader) {
+            a.put(f.getName(), f);
+        }
+        archivers = Collections.unmodifiableMap(a);
+    }
+
+    private interface Predicate<T> { boolean matches(T t); }
+
+    private static final Predicate<ArchiveFormat<? extends ArchiveEntry>> WRITE_PREDICATE =
+        new Predicate<ArchiveFormat<? extends ArchiveEntry>>() {
+            public boolean matches(ArchiveFormat<? extends ArchiveEntry> a) {
+                return a.supportsWriting();
+            }
+        };
+
+    private static final Predicate<ArchiveFormat<? extends ArchiveEntry>> WRITE_TO_CHANNEL_PREDICATE =
+        new Predicate<ArchiveFormat<? extends ArchiveEntry>>() {
+            public boolean matches(ArchiveFormat<? extends ArchiveEntry> a) {
+                return a.supportsWritingToChannels();
+            }
+        };
+
+    private static final Predicate<ArchiveFormat<? extends ArchiveEntry>> READ_FROM_CHANNEL_PREDICATE =
+        new Predicate<ArchiveFormat<? extends ArchiveEntry>>() {
+            public boolean matches(ArchiveFormat<? extends ArchiveEntry> a) {
+                return a.supportsReadingFromChannels();
+            }
+        };
+
+    private static final Predicate<ArchiveFormat<? extends ArchiveEntry>> RANDOM_ACCESS_PREDICATE =
+        new Predicate<ArchiveFormat<? extends ArchiveEntry>>() {
+            public boolean matches(ArchiveFormat<? extends ArchiveEntry> a) {
+                return a.supportsRandomAccessInput();
+            }
+        };
+
+    private static final Predicate<ArchiveFormat<? extends ArchiveEntry>> AUTO_DETECTION_PREDICATE =
+        new Predicate<ArchiveFormat<? extends ArchiveEntry>>() {
+            public boolean matches(ArchiveFormat<? extends ArchiveEntry> a) {
+                return a.supportsAutoDetection();
+            }
+        };
+
+    private Iterable<ArchiveFormat<? extends ArchiveEntry>>
+        filter(final Predicate<ArchiveFormat<? extends ArchiveEntry>> p) {
+        return new Iterable<ArchiveFormat<? extends ArchiveEntry>>() {
+            public Iterator<ArchiveFormat<? extends ArchiveEntry>> iterator() {
+                return new FilteringIterator(Archivers.this.iterator(), p);
+            }
+        };
+    }
+
+    private static class FilteringIterator<T> implements Iterator<T> {
+        private final Iterator<T> i;
+        private final Predicate<? super T> filter;
+        private T lookAhead = null;
+        private FilteringIterator(Iterator<T> i, Predicate<? super T> filter) {
+            this.i = i;
+            this.filter = filter;
+        }
+        public void remove() {
+            i.remove();
+        }
+        public T next() {
+            if (lookAhead == null) {
+                throw new NoSuchElementException();
+            }
+            T next = lookAhead;
+            lookAhead = null;
+            return next;
+        }
+        public boolean hasNext() {
+            while (lookAhead == null && i.hasNext()) {
+                T next = i.next();
+                if (filter.matches(next)) {
+                    lookAhead = next;
+                }
+            }
+            return lookAhead != null;
+        }
+    }
+}

Propchange: commons/proper/compress/branches/compress-2.0/src/main/java/org/apache/commons/compress2/archivers/Archivers.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/compress/branches/compress-2.0/src/main/resources/META-INF/services/org.apache.commons.compress2.archivers.ArchiveFormat
URL: http://svn.apache.org/viewvc/commons/proper/compress/branches/compress-2.0/src/main/resources/META-INF/services/org.apache.commons.compress2.archivers.ArchiveFormat?rev=1561086&view=auto
==============================================================================
--- commons/proper/compress/branches/compress-2.0/src/main/resources/META-INF/services/org.apache.commons.compress2.archivers.ArchiveFormat (added)
+++ commons/proper/compress/branches/compress-2.0/src/main/resources/META-INF/services/org.apache.commons.compress2.archivers.ArchiveFormat Fri Jan 24 17:44:35 2014
@@ -0,0 +1 @@
+org.apache.commons.compress2.formats.ar.ArArchiveFormat

Propchange: commons/proper/compress/branches/compress-2.0/src/main/resources/META-INF/services/org.apache.commons.compress2.archivers.ArchiveFormat
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/compress/branches/compress-2.0/src/test/java/org/apache/commons/compress2/archivers/ArchiversTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/branches/compress-2.0/src/test/java/org/apache/commons/compress2/archivers/ArchiversTest.java?rev=1561086&view=auto
==============================================================================
--- commons/proper/compress/branches/compress-2.0/src/test/java/org/apache/commons/compress2/archivers/ArchiversTest.java (added)
+++ commons/proper/compress/branches/compress-2.0/src/test/java/org/apache/commons/compress2/archivers/ArchiversTest.java Fri Jan 24 17:44:35 2014
@@ -0,0 +1,78 @@
+/*
+ * 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.commons.compress2.archivers;
+
+
+import org.apache.commons.compress2.formats.ar.ArArchiveFormat;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class ArchiversTest {
+
+    @Test
+    public void shouldFindArArchiveFormatByName() {
+        ArchiveFormat<? extends ArchiveEntry> arFormat =
+            new Archivers().getArchiveFormatByName(ArArchiveFormat.AR_FORMAT_NAME);
+        Assert.assertNotNull(arFormat);
+        Assert.assertEquals(ArArchiveFormat.class, arFormat.getClass());
+    }
+
+    @Test
+    public void shouldFindArArchiveFormatWhenIterating() {
+        shouldFind(ArArchiveFormat.class, new Archivers());
+    }
+
+    @Test
+    public void shouldFindArArchiveFormatAsWritableFormat() {
+        shouldFind(ArArchiveFormat.class, new Archivers().getFormatsWithWriteSupport());
+    }
+
+    @Test
+    public void shouldFindArArchiveFormatAsChannelWritableFormat() {
+        shouldFind(ArArchiveFormat.class, new Archivers().getFormatsWithWriteSupportForChannels());
+    }
+
+    @Test
+    public void shouldFindArArchiveFormatAsChannelReadableFormat() {
+        shouldFind(ArArchiveFormat.class, new Archivers().getFormatsWithReadSupportForChannels());
+    }
+
+    @Test
+    public void shouldNotFindArArchiveFormatAsRandomAccessFormat() {
+        shouldNotFind(ArArchiveFormat.class, new Archivers().getFormatsWithRandomAccessInput());
+    }
+
+    private void shouldFind(Class<?> archiveFormat, Iterable<ArchiveFormat<? extends ArchiveEntry>> i) {
+        for (ArchiveFormat<? extends ArchiveEntry> a : i) {
+            if (archiveFormat.equals(a.getClass())) {
+                return;
+            }
+        }
+        Assert.fail("Expected to find " + archiveFormat);
+    }
+
+    private void shouldNotFind(Class<?> archiveFormat, Iterable<ArchiveFormat<? extends ArchiveEntry>> i) {
+        for (ArchiveFormat<? extends ArchiveEntry> a : i) {
+            if (archiveFormat.equals(a.getClass())) {
+                Assert.fail("Didn't expect to find " + archiveFormat);
+            }
+        }
+    }
+}

Propchange: commons/proper/compress/branches/compress-2.0/src/test/java/org/apache/commons/compress2/archivers/ArchiversTest.java
------------------------------------------------------------------------------
    svn:eol-style = native