Return-Path:
A {@link Chain} represents a configured list of
+ * {@link Command}s that will be executed in order to perform processing
+ * on a specified {@link Context}. Each included {@link Command} will be
+ * executed in turn, until either one of them returns Note that {@link Chain} extends {@link Command}, so that the two can
+ * be used interchangeably when a {@link Command} is expected. This makes it
+ * easy to assemble workflows in a hierarchical manner by combining subchains
+ * into an overall processing chain. To protect applications from evolution of this interface, specialized
+ * implementations of {@link Chain} should generally be created by extending
+ * the provided base class {@link org.apache.dns.chain.impl.ChainBase})
+ * rather than directly implementing this interface. {@link Chain} implementations should be designed in a thread-safe
+ * manner, suitable for execution on multiple threads simultaneously. In
+ * general, this implies that the state information identifying which
+ * {@link Command} is currently being executed should be maintained in a
+ * local variable inside the Add a {@link Command} to the list of {@link Command}s that will
+ * be called in turn when this {@link Chain}'s Execute the processing represented by this {@link Chain} according
+ * to the following algorithm. A {@link Command} encapsulates a unit of processing work to be
+ * performed, whose purpose is to examine and/or modify the state of a
+ * transaction that is represented by a {@link Context}. Individual
+ * {@link Command}s can be assembled into a {@link Chain}, which allows
+ * them to either complete the required processing or delegate further
+ * processing to the next {@link Command} in the {@link Chain}. {@link Command} implementations should be designed in a thread-safe
+ * manner, suitable for inclusion in multiple {@link Chain}s that might be
+ * processed by different threads simultaneously. In general, this implies
+ * that {@link Command} classes should not maintain state information in
+ * instance variables. Instead, state information should be maintained via
+ * suitable modifications to the attributes of the {@link Context} that is
+ * passed to the {@link Command} implementations typically retrieve and store state
+ * information in the {@link Context} instance that is passed as a parameter
+ * to the And the operation of accessing the "input" information in the context
+ * would be executed by calling: instead of hard coding the attribute name. The use of the "Key"
+ * suffix on such property names is a useful convention to identify properties
+ * being used in this fashion, as opposed to JavaBeans properties that simply
+ * configure the internal operation of this {@link Command}. Execute a unit of processing work to be performed. This
+ * {@link Command} may either complete the required processing
+ * and return A {@link Context} represents the state information that is
+ * accessed and manipulated by the execution of a {@link Command} or a
+ * {@link Chain}. Specialized implementations of {@link Context} will
+ * typically add JavaBeans properties that contain typesafe accessors
+ * to information that is relevant to a particular use case for this
+ * context, and/or add operations that affect the state information
+ * that is saved in the context. Implementations of {@link Context} must also implement all of the
+ * required and optional contracts of the It is strongly recommended, but not required, that JavaBeans
+ * properties added to a particular {@link Context} implementation exhibit
+ * Attribute-Property Transparency. In other words,
+ * a value stored via a call to To protect applications from evolution of this interface, specialized
+ * implementations of {@link Context} should generally be created by extending
+ * the provided base class ({@link org.apache.dns.chain.impl.ContextBase})
+ * rather than directly implementing this interface. Applications should NOT assume that
+ * {@link Context} implementations, or the values stored in its
+ * attributes, may be accessed from multiple threads
+ * simultaneously unless this is explicitly documented for a particular
+ * implementation. A {@link Filter} is a specialized {@link Command} that also expects
+ * the {@link Chain} that is executing it to call the
+ * The most common use case for a {@link Filter}, as opposed to a
+ * {@link Command}, is where potentially expensive resources must be acquired
+ * and held until the processing of a particular request has been completed,
+ * even it execution is delegated to a subsequent {@link Command} via the
+ * Execute any cleanup activities, such as releasing resources that
+ * were acquired during the Convenience base class for {@link Chain} implementations. Construct a {@link Chain} with no configured {@link Command}s. Construct a {@link Chain} configured with the specified
+ * {@link Command}. Construct a {@link Chain} configured with the specified
+ * {@link Command}s. Construct a {@link Chain} configured with the specified
+ * {@link Command}s. The list of {@link Command}s configured for this {@link Chain}, in
+ * the order in which they may delegate processing to the remainder of
+ * the {@link Chain}. Flag indicating whether the configuration of our commands list
+ * has been frozen by a call to the Return an array of the configured {@link Command}s for this
+ * {@link Chain}. This method is package private, and is used only
+ * for the unit tests. Convenience base class for {@link Command} implementations. Convenience base class for {@link Context} implementations. In addition to the minimal functionality required by the {@link Context}
+ * interface, this class implements the recommended support for
+ * Attribute-Property Transparencytrue
,
+ * one of the executed {@link Command}s throws an exception,
+ * or the end of the chain has been reached. The {@link Chain} itself will
+ * return the return value of the last {@link Command} that was executed
+ * (if no exception was thrown), or rethrow the thrown exception.execute()
method, rather than
+ * in an instance variable. The {@link Command}s in a {@link Chain} may be
+ * configured (via calls to addCommand()
) at any time before
+ * the execute()
method of the {@link Chain} is first called.
+ * After that, the configuration of the {@link Chain} is frozen.execute()
+ * method is called. Once execute()
has been called
+ * at least once, it is no longer possible to add additional
+ * {@link Command}s; instead, an exception will be thrown.command
+ * is null
+ * @exception IllegalStateException if this {@link Chain} has already
+ * been executed at least once, so no further configuration is allowed
+ */
+ void addCommand(Command command);
+
+
+ /**
+ *
+ *
+ *
+ * @param context The {@link Context} to be processed by this
+ * {@link Chain}
+ *
+ * @exception Exception if thrown by one of the {@link Command}s
+ * in this {@link Chain} but not handled by a false
.execute()
method of each {@link Command}
+ * configured on this chain, in the order they were added via calls
+ * to the addCommand()
method, until the end of the
+ * configured {@link Command}s is encountered, or until one of
+ * the executed {@link Command}s returns true
+ * or throws an exception.execute()
methods, starting with the last one that
+ * was executed. If this {@link Command} instance is also a
+ * {@link Filter}, call its postprocess()
method,
+ * discarding any exception that is thrown.execute()
method
+ * was called threw an exception, rethrow that exception.execute()
+ * method of the last {@link Command} that was executed. This will be
+ * true
if the last {@link Command} indicated that
+ * processing of this {@link Context} has been completed, or
+ * false
if none of the called {@link Command}s
+ * returned true
.postprocess()
+ * method of a {@link Filter}
+ * @exception IllegalArgumentException if context
+ * is null
+ *
+ * @return true
if the processing of this {@link Context}
+ * has been completed, or false
if the processing
+ * of this {@link Context} should be delegated to a subsequent
+ * {@link Command} in an enclosing {@link Chain}
+ */
+ boolean execute(Context context) throws Exception;
+
+
+}
Propchange: directory/protocol-providers/dns/trunk/src/java/org/apache/dns/chain/Chain.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: directory/protocol-providers/dns/trunk/src/java/org/apache/dns/chain/Command.java
URL: http://svn.apache.org/viewcvs/directory/protocol-providers/dns/trunk/src/java/org/apache/dns/chain/Command.java?rev=265740&view=auto
==============================================================================
--- directory/protocol-providers/dns/trunk/src/java/org/apache/dns/chain/Command.java (added)
+++ directory/protocol-providers/dns/trunk/src/java/org/apache/dns/chain/Command.java Thu Sep 1 07:45:13 2005
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2003-2004 The Apache Software Foundation
+ *
+ * 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.apache.dns.chain;
+
+
+/**
+ * execute()
command.execute()
method, using particular keys into the
+ * Map
that can be acquired via
+ * Context.getAttributes()
. To improve interoperability of
+ * {@link Command} implementations, a useful design pattern is to expose the
+ * key values used as JavaBeans properties of the {@link Command}
+ * implementation class itself. For example, a {@link Command} that requires
+ * an input and an output key might implement the following properties:
+ * private String inputKey = "input";
+ * public String getInputKey() {
+ * return (this.inputKey);
+ * }
+ * public void setInputKey(String inputKey) {
+ * this.inputKey = inputKey;
+ * }
+ *
+ * private String outputKey = "output";
+ * public String getOutputKey() {
+ * return (this.outputKey);
+ * }
+ * public void setOutputKey(String outputKey) {
+ * this.outputKey = outputKey;
+ * }
+ *
+ *
+ *
+ * String input = (String) context.get(getInputKey());
+ *
+ *
+ * true
, or delegate remaining processing
+ * to the next {@link Command} in a {@link Chain} containing this
+ * {@link Command} by returning false
+ *
+ * @param context The {@link Context} to be processed by this
+ * {@link Command}
+ *
+ * @exception Exception general purpose exception return
+ * to indicate abnormal termination
+ * @exception IllegalArgumentException if context
+ * is null
+ *
+ * @return true
if the processing of this {@link Context}
+ * has been completed, or false
if the processing
+ * of this {@link Context} should be delegated to a subsequent
+ * {@link Command} in an enclosing {@link Chain}
+ */
+ boolean execute(Context context) throws Exception;
+
+
+}
Propchange: directory/protocol-providers/dns/trunk/src/java/org/apache/dns/chain/Command.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: directory/protocol-providers/dns/trunk/src/java/org/apache/dns/chain/Context.java
URL: http://svn.apache.org/viewcvs/directory/protocol-providers/dns/trunk/src/java/org/apache/dns/chain/Context.java?rev=265740&view=auto
==============================================================================
--- directory/protocol-providers/dns/trunk/src/java/org/apache/dns/chain/Context.java (added)
+++ directory/protocol-providers/dns/trunk/src/java/org/apache/dns/chain/Context.java Thu Sep 1 07:45:13 2005
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2003-2004 The Apache Software Foundation
+ *
+ * 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.apache.dns.chain;
+
+
+import java.util.Map;
+
+
+/**
+ * java.util.Map
+ * interface.setFoo(value)
should be visible
+ * by calling get("foo")
, and a value stored
+ * via a call to put("foo", value)
should be
+ * visible by calling getFoo()
. If your {@link Context}
+ * implementation class exhibits this featue, it becomes easier to reuse the
+ * implementation in multiple environments, without the need to cast to a
+ * particular implementation class in order to access the property getter
+ * and setter methods.postprocess()
method if it called the execute()
+ * method. This promise must be fulfilled in spite of any possible
+ * exceptions thrown by the execute()
method of this
+ * {@link Command}, or any subsequent {@link Command} whose
+ * execute()
method was called. The owning {@link Chain}
+ * must call the postprocess()
method of each {@link Filter}
+ * in a {@link Chain} in reverse order of the invocation of their
+ * execute()
methods.execute()
returning false
. A {@link Filter}
+ * can reliably release such resources in the postprocess()
+ * method, which is guaranteed to be called by the owning {@link Chain}.execute()
method of this
+ * {@link Filter} instance.Exception
(if any) that was thrown
+ * by the last {@link Command} that was executed; otherwise
+ * null
+ *
+ * @exception IllegalArgumentException if context
+ * is null
+ *
+ * @return If a non-null exception
was "handled" by this
+ * method (and therefore need not be rethrown), return true
;
+ * otherwise return false
+ */
+ boolean postprocess(Context context, Exception exception);
+
+
+}
Propchange: directory/protocol-providers/dns/trunk/src/java/org/apache/dns/chain/Filter.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: directory/protocol-providers/dns/trunk/src/java/org/apache/dns/chain/impl/ChainBase.java
URL: http://svn.apache.org/viewcvs/directory/protocol-providers/dns/trunk/src/java/org/apache/dns/chain/impl/ChainBase.java?rev=265740&view=auto
==============================================================================
--- directory/protocol-providers/dns/trunk/src/java/org/apache/dns/chain/impl/ChainBase.java (added)
+++ directory/protocol-providers/dns/trunk/src/java/org/apache/dns/chain/impl/ChainBase.java Thu Sep 1 07:45:13 2005
@@ -0,0 +1,223 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation
+ *
+ * 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.apache.dns.chain.impl;
+
+
+import java.util.Collection;
+import java.util.Iterator;
+
+import org.apache.dns.chain.Chain;
+import org.apache.dns.chain.Command;
+import org.apache.dns.chain.Context;
+import org.apache.dns.chain.Filter;
+
+
+/**
+ * command
+ * is null
+ */
+ public ChainBase(Command command) {
+
+ addCommand(command);
+
+ }
+
+
+ /**
+ * commands
,
+ * or one of the individual {@link Command} elements,
+ * is null
+ */
+ public ChainBase(Command[] commands) {
+
+ if (commands == null) {
+ throw new IllegalArgumentException();
+ }
+ for (int i = 0; i < commands.length; i++) {
+ addCommand(commands[i]);
+ }
+
+ }
+
+
+ /**
+ * commands
,
+ * or one of the individual {@link Command} elements,
+ * is null
+ */
+ public ChainBase(Collection commands) {
+
+ if (commands == null) {
+ throw new IllegalArgumentException();
+ }
+ Iterator elements = commands.iterator();
+ while (elements.hasNext()) {
+ addCommand((Command) elements.next());
+ }
+
+ }
+
+
+ // ----------------------------------------------------- Instance Variables
+
+
+ /**
+ * execute()
method.Map
,
+ * with the key being the name of the property itself.
IMPLEMENTATION NOTE - Because empty
is a
+ * read-only property defined by the Map
interface, it may not
+ * be utilized as an attribute key or property name.
Initialize the contents of this {@link Context} by copying the
+ * values from the specified Map
. Any keys in map
+ * that correspond to local properties will cause the setter method for
+ * that property to be called.
The PropertyDescriptor
s for all JavaBeans properties
+ * of this {@link Context} implementation class, keyed by property name.
+ * This collection is allocated only if there are any JavaBeans
+ * properties.
The same PropertyDescriptor
s as an array.
Distinguished singleton value that is stored in the map for each
+ * key that is actually a property. This value is used to ensure that
+ * equals()
comparisons will always fail.
Zero-length array of parameter values for calling property getters. + *
+ */ + private static Object[] zeroParams = new Object[0]; + + + // ------------------------------------------------------------- Map Methods + + + /** + *Override the default Map
behavior to clear all keys and
+ * values except those corresponding to JavaBeans properties.
Override the default Map
behavior to return
+ * true
if the specified value is present in either the
+ * underlying Map
or one of the local property values.
Override the default Map
behavior to return a
+ * Set
that meets the specified default behavior except
+ * for attempts to remove the key for a property of the {@link Context}
+ * implementation class, which will throw
+ * UnsupportedOperationException
.
Override the default Map
behavior to return the value
+ * of a local property if the specified key matches a local property name.
+ *
IMPLEMENTATION NOTE - If the specified
+ * key
identifies a write-only property, null
+ * will arbitrarily be returned, in order to avoid difficulties implementing
+ * the contracts of the Map
interface.
Override the default Map
behavior to return
+ * true
if the underlying Map
only contains
+ * key-value pairs for local properties (if any).
Override the default Map
behavior to return a
+ * Set
that meets the specified default behavior except
+ * for attempts to remove the key for a property of the {@link Context}
+ * implementation class, which will throw
+ * UnsupportedOperationException
.
Override the default Map
behavior to set the value
+ * of a local property if the specified key matches a local property name.
+ *
Override the default Map
behavior to call the
+ * put()
method individually for each key-value pair
+ * in the specified Map
.
Map
containing key-value pairs to store
+ * (or replace)
+ *
+ * @exception IllegalArgumentException if an exception is thrown
+ * reading or wrting a local property value
+ * @exception UnsupportedOperationException if a local property does not
+ * have both a read method and a write method
+ */
+ public void putAll(Map map) {
+
+ Iterator pairs = map.entrySet().iterator();
+ while (pairs.hasNext()) {
+ Map.Entry pair = (Map.Entry) pairs.next();
+ put(pair.getKey(), pair.getValue());
+ }
+
+ }
+
+
+ /**
+ * Override the default Map
behavior to throw
+ * UnsupportedOperationException
on any attempt to
+ * remove a key that is the name of a local property.
key
matches the name of a local property
+ */
+ public Object remove(Object key) {
+
+ // Case 1 -- no local properties
+ if (descriptors == null) {
+ return (super.remove(key));
+ }
+
+ // Case 2 -- this is a local property
+ if (key != null) {
+ PropertyDescriptor descriptor =
+ (PropertyDescriptor) descriptors.get(key);
+ if (descriptor != null) {
+ throw new UnsupportedOperationException
+ ("Local property '" + key + "' cannot be removed");
+ }
+ }
+
+ // Case 3 -- remove from underlying Map
+ return (super.remove(key));
+
+ }
+
+
+ /**
+ * Override the default Map
behavior to return a
+ * Collection
that meets the specified default behavior except
+ * for attempts to remove the key for a property of the {@link Context}
+ * implementation class, which will throw
+ * UnsupportedOperationException
.
Eliminate the specified property descriptor from the list of
+ * property descriptors in pd
.
Return an Iterator
over the set of Map.Entry
+ * objects representing our key-value pairs.
Return a Map.Entry
for the specified key value, if it
+ * is present; otherwise, return null
.
Customize the contents of our underlying Map
so that
+ * it contains keys corresponding to all of the JavaBeans properties of
+ * the {@link Context} implementation class.
Get and return the value for the specified property.
+ * + * @param descriptorPropertyDescriptor
for the
+ * specified property
+ *
+ * @exception IllegalArgumentException if an exception is thrown
+ * reading this local property value
+ * @exception UnsupportedOperationException if this local property does not
+ * have a read method.
+ */
+ private Object readProperty(PropertyDescriptor descriptor) {
+
+ try {
+ Method method = descriptor.getReadMethod();
+ if (method == null) {
+ throw new UnsupportedOperationException
+ ("Property '" + descriptor.getName()
+ + "' is not readable");
+ }
+ return (method.invoke(this, zeroParams));
+ } catch (Exception e) {
+ throw new UnsupportedOperationException
+ ("Exception reading property '" + descriptor.getName()
+ + "': " + e.getMessage());
+ }
+
+ }
+
+
+ /**
+ * Remove the specified key-value pair, if it exists, and return
+ * true
. If this pair does not exist, return
+ * false
.
Return an Iterator
over the set of values in this
+ * Map
.
Set the value for the specified property.
+ * + * @param descriptorPropertyDescriptor
for the
+ * specified property
+ * @param value The new value for this property (must be of the
+ * correct type)
+ *
+ * @exception IllegalArgumentException if an exception is thrown
+ * writing this local property value
+ * @exception UnsupportedOperationException if this local property does not
+ * have a write method.
+ */
+ private void writeProperty(PropertyDescriptor descriptor, Object value) {
+
+ try {
+ Method method = descriptor.getWriteMethod();
+ if (method == null) {
+ throw new UnsupportedOperationException
+ ("Property '" + descriptor.getName()
+ + "' is not writeable");
+ }
+ method.invoke(this, new Object[] { value });
+ } catch (Exception e) {
+ throw new UnsupportedOperationException
+ ("Exception writing property '" + descriptor.getName()
+ + "': " + e.getMessage());
+ }
+
+ }
+
+
+ // --------------------------------------------------------- Private Classes
+
+
+ /**
+ * Private implementation of Set
that implements the
+ * semantics required for the value returned by entrySet()
.
Private implementation of Iterator
for the
+ * Set
returned by entrySet()
.
Private implementation of Map.Entry
for each item in
+ * EntrySetImpl
.
Private implementation of Collection
that implements the
+ * semantics required for the value returned by values()
.
Private implementation of Iterator
for the
+ * Collection
returned by values()
.