[java-metadata-aggregator] branch maint-0.9 updated: MDA-76 add FilesInDirectoryMultiOutputStrategy

Ian Young ian at iay.org.uk
Fri Aug 26 15:52:12 EDT 2016


This is an automated email from the git hooks/post-receive script.

iay pushed a commit to branch maint-0.9
in repository java-metadata-aggregator.

View the commit online:
http://git.shibboleth.net/view/?p=java-metadata-aggregator.git;a=commit;h=575d3c184599539a33a2cd4f929b229c4e707e1f

The following commit(s) were added to refs/heads/maint-0.9 by this push:
       new  575d3c1   MDA-76 add FilesInDirectoryMultiOutputStrategy
575d3c1 is described below

commit 575d3c184599539a33a2cd4f929b229c4e707e1f
Author: Ian Young <ian at iay.org.uk>
AuthorDate: Fri Aug 26 20:52:05 2016 +0100

    MDA-76 add FilesInDirectoryMultiOutputStrategy
    
    A MultiOutputSerializationStage strategy based on the Item's ItemId,
    possibly prefixed, transformed and suffixed, and used as a file name
    within a destination directory.
---
 .../FilesInDirectoryMultiOutputStrategy.java       | 204 +++++++++++++++++++++
 .../FilesInDirectoryMultiOutputStrategyTest.java   |  84 +++++++++
 2 files changed, 288 insertions(+)

diff --git a/aggregator-pipeline/src/main/java/net/shibboleth/metadata/pipeline/FilesInDirectoryMultiOutputStrategy.java b/aggregator-pipeline/src/main/java/net/shibboleth/metadata/pipeline/FilesInDirectoryMultiOutputStrategy.java
new file mode 100644
index 0000000..d7036d4
--- /dev/null
+++ b/aggregator-pipeline/src/main/java/net/shibboleth/metadata/pipeline/FilesInDirectoryMultiOutputStrategy.java
@@ -0,0 +1,204 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development, 
+ * Inc. (UCAID) under one or more contributor license agreements.  See the 
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID 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 net.shibboleth.metadata.pipeline;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.List;
+
+import javax.annotation.Nonnull;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Function;
+import com.google.common.base.Functions;
+
+import net.shibboleth.metadata.Item;
+import net.shibboleth.metadata.ItemId;
+import net.shibboleth.metadata.pipeline.MultiOutputSerializationStage.Destination;
+import net.shibboleth.utilities.java.support.annotation.constraint.NonnullAfterInit;
+import net.shibboleth.utilities.java.support.component.AbstractInitializableComponent;
+import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
+import net.shibboleth.utilities.java.support.component.ComponentSupport;
+import net.shibboleth.utilities.java.support.logic.Constraint;
+
+/**
+ * An output strategy for use with the {@link MultiOutputSerializationStage} which generates
+ * individual files within a directory.
+ * 
+ * The files are named by a combination of:
+ * 
+ * <ul>
+ * <li>an optional prefix string,
+ * <li>each item's {@link ItemId} transformed by an optional {@link Function},
+ * <li>an optional suffix string.
+ * </ul>
+ */
+public class FilesInDirectoryMultiOutputStrategy<T> extends AbstractInitializableComponent
+    implements MultiOutputSerializationStage.OutputStrategy<T> {
+    
+    /** Class logger. */
+    private final Logger log = LoggerFactory.getLogger(FilesInDirectoryMultiOutputStrategy.class);
+
+    private class FileDestination implements MultiOutputSerializationStage.Destination {
+
+        private final File file;
+
+        protected FileDestination(@Nonnull final File f) {
+            file = f;
+        }
+
+        @Override
+        public void close() throws IOException {
+        }
+
+        @Override
+        public OutputStream getOutputStream() throws IOException {
+            return new FileOutputStream(file);
+        }
+        
+    }
+    /** String to use as a prefix for file names generated by this strategy. Default value: empty string. */
+    @Nonnull private String namePrefix = "";
+    
+    /** {@link Function} to use to transform the {@link Item}'s {@link ItemId}. Default: identity transform. */
+    @Nonnull private Function<String, String> nameTransform = Functions.identity();
+
+    /** String to use as a suffix for file names generated by this strategy. Default value: empty string. */
+    @Nonnull private String nameSuffix = "";
+    
+    /** Directory into which to write files. */
+    @NonnullAfterInit private File directory;
+
+    /**
+     * @return Returns the namePrefix.
+     */
+    @Nonnull public String getNamePrefix() {
+        return namePrefix;
+    }
+
+    /**
+     * @param namePrefix The namePrefix to set.
+     */
+    public void setNamePrefix(@Nonnull final String namePrefix) {
+        ComponentSupport.ifDestroyedThrowDestroyedComponentException(this);
+        ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
+
+        this.namePrefix = Constraint.isNotNull(namePrefix, "name prefix may not be null");
+    }
+
+    /**
+     * @return Returns the nameTransform.
+     */
+    @Nonnull public Function<String, String> getNameTransform() {
+        return nameTransform;
+    }
+
+    /**
+     * @param nameTransform The nameTransform to set.
+     */
+    public void setNameTransform(@Nonnull final Function<String, String> nameTransform) {
+        ComponentSupport.ifDestroyedThrowDestroyedComponentException(this);
+        ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
+
+        this.nameTransform = Constraint.isNotNull(nameTransform,
+                "name transform may not be null");
+    }
+
+    /**
+     * @return Returns the nameSuffix.
+     */
+    @Nonnull public String getNameSuffix() {
+        return nameSuffix;
+    }
+
+    /**
+     * @param nameSuffix The nameSuffix to set.
+     */
+    public void setNameSuffix(@Nonnull final String nameSuffix) {
+        ComponentSupport.ifDestroyedThrowDestroyedComponentException(this);
+        ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
+
+        this.nameSuffix = Constraint.isNotNull(nameSuffix, "name suffix may not be null");
+    }
+
+    /**
+     * @return Returns the directory.
+     */
+    @Nonnull public File getDirectory() {
+        return directory;
+    }
+
+    /**
+     * @param directory The directory to set.
+     */
+    public void setDirectory(@Nonnull final File directory) {
+        ComponentSupport.ifDestroyedThrowDestroyedComponentException(this);
+        ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
+
+        this.directory = Constraint.isNotNull(directory, "directory may not be null");
+    }
+
+    @Override
+    public Destination getDestination(Item<T> item) throws StageProcessingException {
+        // Locate the item's identifier.
+        final List<ItemId> ids = item.getItemMetadata().get(ItemId.class);
+        if (ids.isEmpty()) {
+            throw new StageProcessingException("item has no ItemId to base a file name on");
+        }
+        final ItemId id = ids.get(0);
+
+        // Construct file name
+        final String name = namePrefix + nameTransform.apply(id.getId()) + nameSuffix;
+        log.debug("id mapped {} -> {}", id.getId(), name);
+        
+        // Locate the output file within the directory
+        final File outputFile = new File(directory, name);
+
+        return new FileDestination(outputFile);
+    }
+
+    @Override
+    protected void doDestroy() {
+        namePrefix = null;
+        nameTransform = null;
+        nameSuffix = null;
+        directory = null;
+
+        super.doDestroy();
+    }
+
+    @Override
+    protected void doInitialize() throws ComponentInitializationException {
+        super.doInitialize();
+
+        if (directory == null) {
+            throw new ComponentInitializationException("directory can not be null");
+        }
+
+        // Check that the directory can be written to.
+        if (!directory.canWrite()) {
+            throw new ComponentInitializationException("Can not write to parent directory of output files");
+        }
+
+    }
+
+}
diff --git a/aggregator-pipeline/src/test/java/net/shibboleth/metadata/pipeline/FilesInDirectoryMultiOutputStrategyTest.java b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/pipeline/FilesInDirectoryMultiOutputStrategyTest.java
new file mode 100644
index 0000000..1d65a52
--- /dev/null
+++ b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/pipeline/FilesInDirectoryMultiOutputStrategyTest.java
@@ -0,0 +1,84 @@
+
+package net.shibboleth.metadata.pipeline;
+
+import java.io.File;
+import java.io.OutputStream;
+
+import org.testng.annotations.Test;
+
+import com.google.common.base.Function;
+import com.google.common.io.Files;
+
+import junit.framework.Assert;
+import net.shibboleth.metadata.Item;
+import net.shibboleth.metadata.ItemId;
+import net.shibboleth.metadata.MockItem;
+
+public class FilesInDirectoryMultiOutputStrategyTest {
+
+    private void wipeDirectory(final File dir) {
+        for (final File file : dir.listFiles()) {
+            //System.out.println("delete: " + file.getAbsolutePath());
+            file.delete();
+        }
+        dir.delete();
+    }
+    
+    private void checkOneFile(final File dir, final String expectedName) {
+        final File[] files = dir.listFiles();
+        Assert.assertEquals(1, files.length);
+        Assert.assertEquals(expectedName, files[0].getName());
+    }
+
+    // Test with a prefix, a suffix, and an ID transform that doubles the name
+    @Test public void testFull() throws Exception {
+        final File tempDir = Files.createTempDir();
+        //System.out.println("temp dir: " + tempDir.getAbsolutePath());
+        
+        final FilesInDirectoryMultiOutputStrategy<String> strategy = new FilesInDirectoryMultiOutputStrategy<>();
+        strategy.setDirectory(tempDir);
+        strategy.setNamePrefix("pre");
+        strategy.setNameTransform(new Function<String, String>() {
+            @Override
+            public String apply(String input) {
+                return input + input;
+            }
+            
+        });
+        strategy.setNameSuffix(".txt");
+        strategy.initialize();
+        
+        final Item<String> item = new MockItem("mocked");
+        item.getItemMetadata().put(new ItemId("abc"));
+        final MultiOutputSerializationStage.Destination dest = strategy.getDestination(item);
+        final OutputStream os = dest.getOutputStream();
+        os.write('a');
+        os.close();
+        dest.close();
+        
+        checkOneFile(tempDir, "preabcabc.txt");
+        wipeDirectory(tempDir);
+    }
+
+    // Test with defaults
+    @Test public void testDefaults() throws Exception {
+        final File tempDir = Files.createTempDir();
+        //System.out.println("temp dir: " + tempDir.getAbsolutePath());
+
+        final FilesInDirectoryMultiOutputStrategy<String> strategy = new FilesInDirectoryMultiOutputStrategy<>();
+        strategy.setDirectory(tempDir);
+        strategy.initialize();
+        
+        final Item<String> item = new MockItem("mocked");
+        item.getItemMetadata().put(new ItemId("abc"));
+        final MultiOutputSerializationStage.Destination dest = strategy.getDestination(item);
+        final OutputStream os = dest.getOutputStream();
+        os.write('a');
+        os.close();
+        dest.close();
+        
+        checkOneFile(tempDir, "abc");
+        wipeDirectory(tempDir);
+    }
+
+}

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list