Permalink
Browse files

[SHRINKWRAP-85] Create ArchiveEventHandler mechanism to listen/receiv…

…e events
  • Loading branch information...
1 parent dcf5f87 commit eba21ae9b262717728b5ede542e632e0278f7167 @DavideD DavideD committed with ALRubinger Jan 18, 2012
@@ -185,6 +185,14 @@
T addAsDirectories(ArchivePath... paths) throws IllegalArgumentException;
/**
+ * Add an array of listeners for call back based.
+ *
+ * @param listener CallBack on add
+ * @return This archive
+ */
+ T addHandlers(ArchiveEventHandler... handlers);
+
+ /**
* Obtains the {@link Node} located at the specified path
*
* @param path
@@ -0,0 +1,53 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2012, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed 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.jboss.shrinkwrap.api;
+
+import org.jboss.shrinkwrap.api.asset.Asset;
+
+/**
+ * @author Davide D'Alto
+ */
+public class ArchiveEvent {
+
+ private final Asset asset;
+
+ private final ArchivePath path;
+
+ private Asset handledAsset;
+
+ public ArchiveEvent(ArchivePath path, Asset asset) {
+ this.path = path;
+ this.asset = asset;
+ this.handledAsset = asset;
+ }
+
+ public Asset getAsset() {
+ return asset;
+ }
+
+ public ArchivePath getPath() {
+ return path;
+ }
+
+ public Asset getHandledAsset() {
+ return handledAsset;
+ }
+
+ public void setHandledAsset(Asset handledAsset) {
+ this.handledAsset = handledAsset;
+ }
+}
@@ -0,0 +1,30 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2012, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed 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.jboss.shrinkwrap.api;
+
+import org.jboss.shrinkwrap.api.asset.Asset;
+
+public interface ArchiveEventHandler {
+
+ /**
+ * CallBack when a {@link Asset} is added to a specific {@link ArchivePath}.
+ *
+ * @param event {@link ArchiveEvent} generated adding an {@link Asset}.
+ */
+ void handle(ArchiveEvent event);
+
+}
@@ -16,8 +16,10 @@
*/
package org.jboss.shrinkwrap.impl.base;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
@@ -26,9 +28,11 @@
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.ArchivePaths;
+import org.jboss.shrinkwrap.api.ArchiveEvent;
import org.jboss.shrinkwrap.api.Configuration;
import org.jboss.shrinkwrap.api.Filter;
import org.jboss.shrinkwrap.api.IllegalArchivePathException;
+import org.jboss.shrinkwrap.api.ArchiveEventHandler;
import org.jboss.shrinkwrap.api.Node;
import org.jboss.shrinkwrap.api.asset.ArchiveAsset;
import org.jboss.shrinkwrap.api.asset.Asset;
@@ -62,6 +66,8 @@
*/
private final Map<ArchivePath, ArchiveAsset> nestedArchives = new ConcurrentHashMap<ArchivePath, ArchiveAsset>();
+ private List<ArchiveEventHandler> handlers = new ArrayList<ArchiveEventHandler>();
+
// -------------------------------------------------------------------------------------||
// Constructor ------------------------------------------------------------------------||
// -------------------------------------------------------------------------------------||
@@ -115,22 +121,7 @@ public T add(Asset asset, ArchivePath path) {
Validate.notNull(asset, "No asset was specified");
Validate.notNull(path, "No path was specified");
- // Check if it exists. If it doesn't, create it and add it.
- if (!contains(path)) {
- // Retrieve the parent
- NodeImpl parentNode = obtainParent(path.getParent());
-
- // Add the node to the content of the archive
- NodeImpl node = new NodeImpl(path, asset);
- content.put(path, node);
-
- // Add the new node to the parent as a child
- if (parentNode != null) {
- parentNode.addChild(node);
- }
- }
-
- return covariantReturn();
+ return addAsset(path, asset);
}
/**
@@ -185,21 +176,45 @@ public T addAsDirectory(final ArchivePath path) throws IllegalArgumentException
// Adjust the path to remove any trailing slash
ArchivePath adjustedPath = new BasicPath(PathUtil.optionallyRemoveFollowingSlash(path.get()));
+ return addAsset(adjustedPath, null);
+ }
- // Check if it exists. If it doesn't, create it and add it. The same with all the
- // non-existing parents
- if (!contains(adjustedPath)) {
- NodeImpl node = new NodeImpl(adjustedPath);
- content.put(adjustedPath, node);
+ private T addAsset(ArchivePath path, Asset asset) {
+ Asset handledAsset = invokeHandlers(path, asset);
+
+ // Check if it exists. If it doesn't, create it and add it.
+ if (!contains(path)) {
+ // Add the node to the content of the archive
+ NodeImpl node = new NodeImpl(path, handledAsset);
+ content.put(path, node);
+
+ // Add the new node to the parent as a child
+ NodeImpl parentNode = obtainParent(path.getParent());
+ if (parentNode != null) {
+ parentNode.addChild(node);
+ }
+ }
+ return covariantReturn();
+ }
- // retrieve the parent and add the node as a child
- NodeImpl parentNode = obtainParent(adjustedPath.getParent());
- if (parentNode != null) {
- parentNode.addChild(node);
- }
- }
+ /**
+ * {@inheritDoc}
+ * @see org.jboss.shrinkwrap.api.Archive#addListener(org.jboss.shrinkwrap.api.Filter, org.jboss.shrinkwrap.api.ArchiveEventHandler)
+ */
+ @Override
+ public T addHandlers(ArchiveEventHandler... handlers) {
+ for (ArchiveEventHandler handler : handlers) {
+ this.handlers.add(handler);
+ }
+ return covariantReturn();
+ }
- return covariantReturn();
+ private Asset invokeHandlers(ArchivePath path, Asset asset) {
+ final ArchiveEvent event = new ArchiveEvent(path, asset);
+ for (ArchiveEventHandler handler : handlers) {
+ handler.handle(event);
+ }
+ return event.getHandledAsset();
}
/**
@@ -28,6 +28,7 @@
import java.util.Map;
import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.ArchiveEventHandler;
import org.jboss.shrinkwrap.api.ArchiveFactory;
import org.jboss.shrinkwrap.api.ArchiveFormat;
import org.jboss.shrinkwrap.api.ArchivePath;
@@ -234,6 +235,19 @@ public T addAsDirectory(String path) throws IllegalArgumentException {
/**
* {@inheritDoc}
*
+ * @see org.jboss.shrinkwrap.api.Archive#addHandlers(org.jboss.shrinkwrap.api.Filter, org.jboss.shrinkwrap.api.ArchiveEventHandler)
+ */
+ @Override
+ public T addHandlers(ArchiveEventHandler... handlers) {
+ for (ArchiveEventHandler handler : handlers) {
+ this.getArchive().addHandlers(handler);
+ }
+ return covarientReturn();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
* @see org.jboss.shrinkwrap.api.Archive#merge(org.jboss.shrinkwrap.api.Archive)
*/
@Override
@@ -16,7 +16,10 @@
*/
package org.jboss.shrinkwrap.impl.base.test;
+import java.io.BufferedReader;
+import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
@@ -47,6 +50,8 @@
import org.jboss.shrinkwrap.impl.base.Validate;
import org.jboss.shrinkwrap.impl.base.io.IOUtil;
import org.jboss.shrinkwrap.impl.base.path.BasicPath;
+import org.jboss.shrinkwrap.impl.base.test.handler.ReplaceAssetHandler;
+import org.jboss.shrinkwrap.impl.base.test.handler.SimpleHandler;
import org.jboss.shrinkwrap.spi.ArchiveFormatAssociable;
import org.junit.After;
import org.junit.Test;
@@ -365,6 +370,107 @@ public void testAddEmptyDirectories() throws Exception {
TestCase.assertTrue(message + path3, archive.contains(path3));
}
+ @Test
+ public void testHandlerIsCalledWhenAddingDirectoriesWithArchivePath() throws Exception {
+ final SimpleHandler simpleHandler1 = new SimpleHandler();
+ final SimpleHandler simpleHandler2 = new SimpleHandler();
+ getArchive().addHandlers(simpleHandler1, simpleHandler2);
+
+ getArchive().addAsDirectories(ArchivePaths.create("/path/to/dir1"));
+
+ Assert.assertTrue("Handler not called", simpleHandler1.called);
+ Assert.assertTrue("Handler not called", simpleHandler2.called);
+ }
+
+ @Test
+ public void testHandlerIsCalledWhenAddingDirectoriesWithStringPath() throws Exception {
+ final SimpleHandler simpleHandler1 = new SimpleHandler();
+ final SimpleHandler simpleHandler2 = new SimpleHandler();
+ getArchive().addHandlers(simpleHandler1, simpleHandler2);
+
+ getArchive().addAsDirectories("/path/to/dir1");
+
+ Assert.assertTrue("Handler not called", simpleHandler1.called);
+ Assert.assertTrue("Handler not called", simpleHandler2.called);
+ }
+
+ @Test
+ public void testHandlerIsCalledWhenAddingAssetWithArchivePath() throws Exception {
+ final ReplaceAssetHandler handler1 = new ReplaceAssetHandler("unexpected");
+ final ReplaceAssetHandler handler2 = new ReplaceAssetHandler("EXPECTED");
+ getArchive().addHandlers(handler1, handler2);
+
+ final ArchivePath path = ArchivePaths.create("/path/to/dir/test1.txt");
+ final StringAsset asset = new StringAsset("Asset content");
+ getArchive().add(asset, path);
+
+ Assert.assertEquals("Handler not called", "EXPECTED", readStringAsset(path));
+ Assert.assertEquals("Wrong asset received by handler", asset, handler1.savedAsset);
+ Assert.assertEquals("Wrong asset received by handler", handler1.returnedAsset, handler2.savedAsset);
+ }
+
+ @Test
+ public void testHandlerIsCalledWhenAddingAssetWithArchivePathAndName() throws Exception {
+ final ReplaceAssetHandler handler1 = new ReplaceAssetHandler("unexpected");
+ final ReplaceAssetHandler handler2 = new ReplaceAssetHandler("EXPECTED");
+ getArchive().addHandlers(handler1, handler2);
+
+ final ArchivePath path = ArchivePaths.create("/path/to/dir");
+ final StringAsset asset = new StringAsset("Original");
+ getArchive().add(asset, path, "asset.txt");
+
+ String actual = readStringAsset(ArchivePaths.create(path, "/asset.txt"));
+ Assert.assertEquals("Handler not called", "EXPECTED", actual);
+ Assert.assertEquals("Wrong asset received by handler", asset, handler1.savedAsset);
+ Assert.assertEquals("Wrong asset received by handler", handler1.returnedAsset, handler2.savedAsset); }
+
+ @Test
+ public void testHandlerIsCalledWhenAddingAssetWithtStringPathAndName() throws Exception {
+ final ReplaceAssetHandler handler1 = new ReplaceAssetHandler("unexpected");
+ final ReplaceAssetHandler handler2 = new ReplaceAssetHandler("EXPECTED");
+ getArchive().addHandlers(handler1, handler2);
+
+ final ArchivePath path = ArchivePaths.create("/path/to/dir");
+ final StringAsset asset = new StringAsset("Original");
+ getArchive().add(asset, path.get(), "asset.txt");
+
+ String actual = readStringAsset(ArchivePaths.create(path, "asset.txt"));
+ Assert.assertEquals("Handler not called", "EXPECTED", actual);
+ Assert.assertEquals("Wrong asset received by handler", asset, handler1.savedAsset);
+ Assert.assertEquals("Wrong asset received by handler", handler1.returnedAsset, handler2.savedAsset);
+ }
+
+ @Test
+ public void testHandlerIsCalledWhenAddingAssetWithStringPath() throws Exception {
+ final ReplaceAssetHandler handler1 = new ReplaceAssetHandler("unexpected");
+ final ReplaceAssetHandler handler2 = new ReplaceAssetHandler("EXPECTED");
+ getArchive().addHandlers(handler1, handler2);
+
+ final ArchivePath path = ArchivePaths.create("/path/to/dir/test1.txt");
+ final StringAsset asset = new StringAsset("Original");
+ getArchive().add(asset, path.get());
+
+ Assert.assertEquals("Handler not called", "EXPECTED", readStringAsset(path));
+ Assert.assertEquals("Wrong asset received by handler", asset, handler1.savedAsset);
+ Assert.assertEquals("Wrong asset received by handler", handler1.returnedAsset, handler2.savedAsset);
+ }
+
+ @Test
+ public void testHandlerIsCalledWhenAddingAssetWithArchivePathAndExporter() throws Exception {
+ final ReplaceAssetHandler handler1 = new ReplaceAssetHandler("unexpected");
+ final ReplaceAssetHandler handler2 = new ReplaceAssetHandler("EXPECTED");
+ getArchive().addHandlers(handler1, handler2);
+
+ final ArchivePath path = ArchivePaths.create("/path/to/dir");
+ final Archive<JavaArchive> asset = ShrinkWrap
+ .create(JavaArchive.class, "asset.zip")
+ .add(new StringAsset("asset content"), "content.txt");
+ getArchive().add(asset, path, ZipExporter.class);
+
+ Assert.assertTrue("Handler not called", handler1.called);
+ Assert.assertEquals("Wrong asset received by handler", handler1.returnedAsset, handler2.savedAsset);
+ }
+
/**
* Ensures that {@link Archive#contains(String)} works as expected
*/
@@ -1290,4 +1396,8 @@ protected int numAssets(final Archive<?> archive) {
return assets;
}
+ private String readStringAsset(final ArchivePath path) throws IOException {
+ Asset addedAsset = getArchive().get(path).getAsset();
+ return new BufferedReader(new InputStreamReader(addedAsset.openStream())).readLine();
+ }
}
Oops, something went wrong.

0 comments on commit eba21ae

Please sign in to comment.