commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Peter_F...@blm.gov
Subject [Chain] Parametizing commands?
Date Fri, 05 Sep 2008 16:49:41 GMT

I'm looking for a way to decouple the context keys used in a command from
the actual context keys used at runtime. Here's an example:

Here's a really simple command  that just concatenates two strings from the
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 other
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 result
is expected to be in FULL_NAME for the next step in processing. So now I
have to create two more commands: one to move FIRST_NAME to A and LAST_NAME
to B, so that the strings are in the right places for my Concat command,
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 (combining
DIRECTORY and FILE to create PATH, say) I have to create yet more commands
just to move things around so that I can re-use the command that does the
actual work. And in fact in the application I've been developing something
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 they
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 at
runtime. As an example, I might want to represent my chain something like
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 tell
there is no such option in Chain as it is right now.

It strikes me, having used Chain for a while, that I am probably not the
only one to have come across the general problem of having "fixed" keys in
Commands referring to "movable" data in Contexts. What I've described here
is a potential solution but I don't think it'll work because Chain doesn't
support being able to attach parameter sets to Command references. Is there
another solution out there?


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message