You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Hookom, Jacob" <Ja...@redline.mckhboc.com> on 2003/12/01 21:43:51 UTC
Struts and Commons Chain
I was looking at chain's implementation, instead of executing the commands
with the visitor pattern, wouldn't a more powerful/flexible method be to
implement it using a filter pattern?
See:
http://java.sun.com/blueprints/corej2eepatterns/Patterns/InterceptingFilter.
html
Sample Chain:
CommandServletStart
CommandExceptionHandler
CommandStoreState
CommandExceptionThrower
--
CommandStart.execute(Context ctx, Chain chain) throws Exception
{
ServletContext srvCtx = (ServletContext) ctx;
String usrId = srvCtx.getRequestScope().getParameter("id"));
String newName = srvCtx.getRequestScope().getParameter("name"));
// get user and save state
User user = SomeService.getUser(usrId);
String oldName = user.getName();
// set new state and execute
try
{
user.setName(newName);
ctx.setAttribute("user", user);
chain.executeNext(ctx);
}
catch (Exception e)
{
// on error, rollback change
user.setName(oldName);
throw e;
}
}
--
CommandExceptionHandler.execute(Context ctx, Chain chain) throws Exception
{
try
{
chain.executeNext(ctx);
}
catch (SQLException e)
{
chain.executeNext(ctx);
}
// let unhandled exceptions go up
}
CommandExceptionThrower.execute(Context ctx, Chain chain) throws Exception
{
throw new SQLException();
}
With this, you would not have to change your inheritance to add behavior
(going from a command to a filter). The given command would already know
best on how to handle itself and since it calls the next item in the chain,
we can also wrap the context to take care of situations where we want to
have listener type behavior.
The chain could also allow for runtime manipulation by the commands, to
insert additional commands to handle errors.
chain.insert("SomeCommand").insert("AnotherCommand).executeNext(ctx);
--------
Jacob Hookom
Senior Programmer/Analyst
McKesson Medical-Surgical
Golden Valley, MN
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org
Re: Struts and Commons Chain
Posted by Vic Cekvenich <ce...@basebeans.com>.
> This is very interesting I think.
> .V
>
> Hookom, Jacob wrote:
>
>> I was looking at chain's implementation, instead of executing the
>> commands
>> with the visitor pattern, wouldn't a more powerful/flexible method be to
>> implement it using a filter pattern?
>>
>> See:
>> http://java.sun.com/blueprints/corej2eepatterns/Patterns/InterceptingFilter.
>>
>> html
>>
>>
>> Sample Chain:
>> CommandServletStart
>> CommandExceptionHandler
>> CommandStoreState
>> CommandExceptionThrower
>>
>> --
>>
>> CommandStart.execute(Context ctx, Chain chain) throws Exception
>> {
>> ServletContext srvCtx = (ServletContext) ctx;
>> String usrId = srvCtx.getRequestScope().getParameter("id"));
>> String newName = srvCtx.getRequestScope().getParameter("name"));
>>
>> // get user and save state
>> User user = SomeService.getUser(usrId);
>> String oldName = user.getName();
>>
>> // set new state and execute
>> try
>> {
>> user.setName(newName);
>> ctx.setAttribute("user", user);
>> chain.executeNext(ctx);
>> }
>> catch (Exception e)
>> {
>> // on error, rollback change
>> user.setName(oldName);
>> throw e;
>> }
>> }
>>
>> --
>>
>> CommandExceptionHandler.execute(Context ctx, Chain chain) throws
>> Exception
>> {
>> try
>> {
>> chain.executeNext(ctx);
>> }
>> catch (SQLException e)
>> {
>> chain.executeNext(ctx);
>> }
>> // let unhandled exceptions go up
>> }
>>
>> CommandExceptionThrower.execute(Context ctx, Chain chain) throws
>> Exception
>> {
>> throw new SQLException();
>> }
>>
>> With this, you would not have to change your inheritance to add behavior
>> (going from a command to a filter). The given command would already know
>> best on how to handle itself and since it calls the next item in the
>> chain,
>> we can also wrap the context to take care of situations where we want to
>> have listener type behavior.
>>
>> The chain could also allow for runtime manipulation by the commands, to
>> insert additional commands to handle errors.
>>
>> chain.insert("SomeCommand").insert("AnotherCommand).executeNext(ctx);
>>
>> --------
>> Jacob Hookom
>> Senior Programmer/Analyst
>> McKesson Medical-Surgical
>> Golden Valley, MN
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org