struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Hookom, Jacob" <Jacob.Hoo...@redline.mckhboc.com>
Subject Struts and Commons Chain
Date Mon, 01 Dec 2003 20:43:51 GMT
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


Mime
View raw message