commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject Re: [Chain] Parametizing commands?
Date Tue, 09 Sep 2008 20:19:45 GMT
It turns out that (as you probably know) it is possible to parametize
commands in the catalog XML file. (I should point out that the
documentation of the format of the XML file is either missing or in a
hard-to-find place; I couldn't find it and had to resort to pulling the
Digester rules apart to figure this out).

In any case, this means that it's possible to do what I was asking - but
it's not necessarily easy, so I've written a package to make this kind of
thing a whole lot simpler. The core class is an abstract class that allows
you to initialize mappings from the key names used in the code, to the
actual names used to access the context. In this way you can write
easily-reusable commands and provide the mappings in the XML.

In addition it supports hierarchies of contexts to make it easier to keep
complex structures organized, and like the keys the contexts can be mapped
to simple names.

I've been testing it and it shows a lot of promise; I'd like to submit the
code for consideration for inclusion in a future release of Commons-Chain
but I have no idea how to go about that; however I can email a source JAR
if anyone's interested.


"Niall Pemberton" <> wrote on 09/05/2008 06:57:31

> On Fri, Sep 5, 2008 at 5:49 PM,  <> wrote:
> >
> > I'm looking for a way to decouple the context keys used in a command
> > the actual context keys used at runtime. Here's an example:
> >
> > Here's a really simple command  that just concatenates two strings from
> > context - the keys are A and B and the concatenated output goes in C:
> >
> > public class Concat implements Command
> > {
> >    @Override
> >    public boolean execute(Context context) throws Exception
> >    {
> >        context.put("C", (String) context.get("A") + (String)
> > context.get("B"));
> >        return false;
> >    }
> > }
> >
> > Let's say I need to create a chain that uses this command, but the
> > commands leading up to this one leave the two strings that need to be
> > joined the context under the keys FIRST_NAME and LAST_NAME, and the
> > is expected to be in FULL_NAME for the next step in processing. So now
> > have to create two more commands: one to move FIRST_NAME to A and
> > to B, so that the strings are in the right places for my Concat
> > and then another to follow that moves C to LAST_NAME.
> >
> > That's not very nice - I now have three commands to do the job of one.
If I
> > need to use Concat somewhere else but using different keys again
> > DIRECTORY and FILE to create PATH, say) I have to create yet more
> > just to move things around so that I can re-use the command that does
> > actual work. And in fact in the application I've been developing
> > like a third of the commands in the app are just to do this kind of
> > data-shuffling, and I have command chains that are twice as long as
> > really should be.
> >
> > What I'd like to be able to do is provide some kind of mapping from the
> > "label" keys used by my command code, to the "real" keys that get used
> > runtime. As an example, I might want to represent my chain something
> > this in XML:
> >
> >  <command name="Concat">
> >    <map label="A" to="FIRST_NAME" />
> >    <map label="B" to="LAST_NAME" />
> >    <map label="C" to="FULL_NAME" />
> >  </command>
> >  <command name="Concat">
> >    <map label="A" to="DIRECTORY" />
> >    <map label="B" to="FILE" />
> >    <map label="C" to="PATH" />
> >  </command>
> >
> > I could do this myself *if* there was a way to parametize individual
> > command references in a chain something like this, but as far as I can
> > there is no such option in Chain as it is right now.
> The sample webapp(s) has an example of exactly this - theres an
> example "forward" command where the actual forward is specified as a
> property:
> http://svn.apache.

> java
> Then in the chain config you specify the fowards property:
> http://svn.apache.
> INF/chain-config.xml
> Niall
> > It strikes me, having used Chain for a while, that I am probably not
> > only one to have come across the general problem of having "fixed" keys
> > Commands referring to "movable" data in Contexts. What I've described
> > is a potential solution but I don't think it'll work because Chain
> > support being able to attach parameter sets to Command references. Is
> > another solution out there?
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message