You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by si...@apache.org on 2012/07/19 10:56:33 UTC

svn commit: r1363265 - in /commons/proper/chain/trunk: ./ core/src/main/java/org/apache/commons/chain2/ core/src/test/java/org/apache/commons/chain2/impl/ src/changes/

Author: simonetripodi
Date: Thu Jul 19 08:56:32 2012
New Revision: 1363265

URL: http://svn.apache.org/viewvc?rev=1363265&view=rev
Log:
[CHAIN-70] Add a small EDSL to simplify Chain setup and execution

Added:
    commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/ChainExecutor.java   (with props)
    commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/Chains.java   (with props)
    commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/CommandSetter.java   (with props)
    commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/ToExecutorCommandSetter.java   (with props)
    commons/proper/chain/trunk/core/src/test/java/org/apache/commons/chain2/impl/FluentInterfacesTestCase.java   (with props)
Modified:
    commons/proper/chain/trunk/RELEASE-NOTES.txt
    commons/proper/chain/trunk/src/changes/changes.xml

Modified: commons/proper/chain/trunk/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/commons/proper/chain/trunk/RELEASE-NOTES.txt?rev=1363265&r1=1363264&r2=1363265&view=diff
==============================================================================
--- commons/proper/chain/trunk/RELEASE-NOTES.txt (original)
+++ commons/proper/chain/trunk/RELEASE-NOTES.txt Thu Jul 19 08:56:32 2012
@@ -58,6 +58,7 @@ The Recommended Dependency Set for Chain
 NEW FEATURES
 =============
 
+ * [CHAIN-70] Add a small EDSL to simplify Chain setup and execution
  * [CHAIN-58] Update Chain Context interface to use K,V generics
  * [CHAIN-56] clever Context with generic type "auto-cast" feature.
  * [CHAIN-55] split the huge project in submodules.

Added: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/ChainExecutor.java
URL: http://svn.apache.org/viewvc/commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/ChainExecutor.java?rev=1363265&view=auto
==============================================================================
--- commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/ChainExecutor.java (added)
+++ commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/ChainExecutor.java Thu Jul 19 08:56:32 2012
@@ -0,0 +1,41 @@
+/*
+ * 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.chain2;
+
+import java.util.Map;
+
+/**
+ * Builder that allows continue adding a command in the target chain and execute it.
+ *
+ * @param <K> Context key type
+ * @param <V> Context value type
+ * @param <C> Type of the context associated with this chain executor
+ * @since 2.0
+ */
+public interface ChainExecutor<K, V, C extends Map<K, V>> extends CommandSetter<K, V, C, ChainExecutor<K, V, C>> {
+
+    /**
+     * Execute the processing represented by the target chain.
+     *
+     * @param context the context processed by the target chain
+     * @return true, if the processing of the target chain has been completed,
+     *         false otherwise
+     * @see Chain#execute(Map)
+     */
+    boolean execute(C context);
+
+}

Propchange: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/ChainExecutor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/ChainExecutor.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/ChainExecutor.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/Chains.java
URL: http://svn.apache.org/viewvc/commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/Chains.java?rev=1363265&view=auto
==============================================================================
--- commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/Chains.java (added)
+++ commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/Chains.java Thu Jul 19 08:56:32 2012
@@ -0,0 +1,90 @@
+/*
+ * 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.chain2;
+
+import java.util.Map;
+
+/**
+ * Simple fluent chain EDSL to simplify {@link Chain} instances invocation.
+ *
+ * @since 2.0
+ */
+public final class Chains {
+
+    /**
+     * Defines the target chain has to be invoked.
+     *
+     * @param <K> Context key type
+     * @param <V> Context value type
+     * @param <C> Type of the context associated with this command
+     * @param <CH> Type of the {@link Chain} to execute
+     * @param chain the chain instance reference to execute
+     * @return next chain builder
+     */
+    public static <K, V, C extends Map<K, V>, CH extends Chain<K, V, C>> ToExecutorCommandSetter<K, V, C> on(CH chain) {
+        return new DefaultCommandSetter<K, V, C>(checkNotNullArgument(chain, "Null Chain can not be executed"));
+    }
+
+    /**
+     * Private constructor, this class cannot be instantiated directly.
+     */
+    private Chains() {
+        // do nothing
+    }
+
+    private static class DefaultCommandSetter<K, V, C extends Map<K, V>> implements ToExecutorCommandSetter<K, V, C> {
+
+        private final Chain<K, V, C> chain;
+
+        public DefaultCommandSetter(Chain<K, V, C> chain) {
+            this.chain = chain;
+        }
+
+        public <CMD extends Command<K, V, C>> ChainExecutor<K, V, C> addCommand(CMD command) {
+            chain.addCommand(checkNotNullArgument(command, "Chain does not accept null Command instances"));
+            return new DefaultChainExecutor<K, V, C>(chain);
+        }
+
+    }
+
+    private static final class DefaultChainExecutor<K, V, C extends Map<K, V>> implements ChainExecutor<K, V, C> {
+
+        private final Chain<K, V, C> chain;
+
+        public DefaultChainExecutor(Chain<K, V, C> chain) {
+            this.chain = chain;
+        }
+
+        public <CMD extends Command<K, V, C>> ChainExecutor<K, V, C> addCommand(CMD command) {
+            chain.addCommand(checkNotNullArgument(command, "Chain does not accept null Command instances"));
+            return this;
+        }
+
+        public boolean execute(C context) {
+            return chain.execute(checkNotNullArgument(context, "Chain cannot be applied to a null context."));
+        }
+
+    }
+
+    private static <T> T checkNotNullArgument(T reference, String message) {
+        if (reference == null) {
+            throw new IllegalArgumentException(message);
+        }
+        return reference;
+    }
+
+}

Propchange: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/Chains.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/Chains.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/Chains.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/CommandSetter.java
URL: http://svn.apache.org/viewvc/commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/CommandSetter.java?rev=1363265&view=auto
==============================================================================
--- commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/CommandSetter.java (added)
+++ commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/CommandSetter.java Thu Jul 19 08:56:32 2012
@@ -0,0 +1,42 @@
+/*
+ * 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.chain2;
+
+import java.util.Map;
+
+/**
+ * Generic builder that allows adding commands to the target {@link Chain} has to be executed.
+ *
+ * @param <K> Context key type
+ * @param <V> Context value type
+ * @param <C> Type of the context associated with this command setter
+ * @param <CS> Type of the next chain builder
+ * @since 2.0
+ */
+public interface CommandSetter<K, V, C extends Map<K, V>, CS extends CommandSetter<K, V, C, CS>> {
+
+    /**
+     * Add the given command to the target {@link Chain} has to be executed.
+     *
+     * @param <CMD> Type of the command has to be added
+     * @param command the command has to be added in the target chain
+     * @return next chain builder
+     * @see Chain#addCommand(Command)
+     */
+    <CMD extends Command<K, V, C>> CS addCommand(CMD command);
+
+}

Propchange: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/CommandSetter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/CommandSetter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/CommandSetter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/ToExecutorCommandSetter.java
URL: http://svn.apache.org/viewvc/commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/ToExecutorCommandSetter.java?rev=1363265&view=auto
==============================================================================
--- commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/ToExecutorCommandSetter.java (added)
+++ commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/ToExecutorCommandSetter.java Thu Jul 19 08:56:32 2012
@@ -0,0 +1,32 @@
+/*
+ * 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.chain2;
+
+import java.util.Map;
+
+/**
+ * First builder that allows adding a command in the target chain.
+ *
+ * @param <K> Context key type
+ * @param <V> Context value type
+ * @param <C> Type of the context associated with this chain executor
+ * @since 2.0
+ */
+public interface ToExecutorCommandSetter<K, V, C extends Map<K, V>>
+    extends CommandSetter<K, V, C, ChainExecutor<K,V,C>> {
+
+}

Propchange: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/ToExecutorCommandSetter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/ToExecutorCommandSetter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/chain/trunk/core/src/main/java/org/apache/commons/chain2/ToExecutorCommandSetter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/chain/trunk/core/src/test/java/org/apache/commons/chain2/impl/FluentInterfacesTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/chain/trunk/core/src/test/java/org/apache/commons/chain2/impl/FluentInterfacesTestCase.java?rev=1363265&view=auto
==============================================================================
--- commons/proper/chain/trunk/core/src/test/java/org/apache/commons/chain2/impl/FluentInterfacesTestCase.java (added)
+++ commons/proper/chain/trunk/core/src/test/java/org/apache/commons/chain2/impl/FluentInterfacesTestCase.java Thu Jul 19 08:56:32 2012
@@ -0,0 +1,60 @@
+/*
+ * 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.chain2.impl;
+
+import static org.junit.Assert.assertTrue;
+import static org.apache.commons.chain2.Chains.on;
+
+import org.apache.commons.chain2.Context;
+import org.junit.Test;
+
+import org.apache.commons.chain2.Chain;
+
+public final class FluentInterfacesTestCase {
+
+    @Test(expected = IllegalArgumentException.class)
+    public void doesNotAcceptNullChain() {
+        on((Chain<String, Object, Context<String, Object>>) null);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void doesNotAcceptNullCommand() {
+        on(new ChainBase<String, Object, Context<String, Object>>())
+        .addCommand(null);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void doesNotAcceptNullContext() {
+        on(new ChainBase<String, Object, Context<String, Object>>())
+        .addCommand(new NonDelegatingFilter("3", "c"))
+        .execute(null);
+    }
+
+    @Test
+    public void justMakeSureChainIsExecuted() {
+        ContextBase context = new ContextBase();
+
+        on(new ChainBase<String, Object, Context<String, Object>>())
+        .addCommand(new DelegatingFilter("1", "a"))
+        .addCommand(new ExceptionFilter("2", "b"))
+        .addCommand(new NonDelegatingFilter("3", "c"))
+        .execute(context);
+
+        assertTrue(context.containsKey("log"));
+    }
+
+}

Propchange: commons/proper/chain/trunk/core/src/test/java/org/apache/commons/chain2/impl/FluentInterfacesTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/chain/trunk/core/src/test/java/org/apache/commons/chain2/impl/FluentInterfacesTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/chain/trunk/core/src/test/java/org/apache/commons/chain2/impl/FluentInterfacesTestCase.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: commons/proper/chain/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/chain/trunk/src/changes/changes.xml?rev=1363265&r1=1363264&r2=1363265&view=diff
==============================================================================
--- commons/proper/chain/trunk/src/changes/changes.xml (original)
+++ commons/proper/chain/trunk/src/changes/changes.xml Thu Jul 19 08:56:32 2012
@@ -41,6 +41,9 @@ The <action> type attribute can be add,u
 
   <body>
     <release version="2.0" description="Major release">
+      <action dev="simonetripodi" type="add" issue="CHAIN-70">
+        Add a small EDSL to simplify Chain setup and execution.
+      </action>
       <action dev="simonetripodi" type="add" issue="CHAIN-69" due-to="Elijah Zupancic">
         Fixed Checkstyle / PMD Warnings
       </action>