Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Jakarta-commons Wiki" 
for change notification.

The following page has been changed by MarcoMistroni:
http://wiki.apache.org/jakarta-commons/CommonsChainAndSpringFramework

New page:
{{{Below are the steps of a possble solution for using commons-chain with 
Struts, having the Catalog configured via Spring.

Write an implementation of org.apache.commons.chain.Catalog interface which 
must implement also org.springframework.context.ApplicationContextAware in 
order to retrieve commands from spring configuration file.
Below is a possible implementation 

public class SpringCatalog implements Catalog,ApplicationContextAware {
         
         private ApplicationContext appContext;
         
         public SpringCatalog() {
                 System.out.println("Instantiating Spring Catalog..");
         }
         
         public void addCommand(String name, Command command) {
                 // does nothing here..
         }
         
         public Command getCommand(String name) {
                 return (Command)appContext.getBean(name);
         }

       public Iterator getNames() {
                  Map commands = new HashMap();
                  try {
                          commands = appContext.getBeansOfType(
                                        org.apache.commons.chain.Command.class,
                                        true,
                                        true);
                  } catch(Exception e) {
                          System.err.println("Error in retrieving command 
names..");
                  }
                  return commands.keySet().iterator();
          }
          
         public void setApplicationContext(ApplicationContext ctx)
                throws ApplicationContextException, BeansException {
                  appContext = ctx;
         }
 }

* Declare your Catalog class and commands in your spring configuration file 
(usually it is applicationContext.xml). 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
"http://www.springframework.org/dtd/spring-beans.dtd";>
<beans>
        
        <!-- ========================= GENERAL DEFINITIONS 
========================= -->

  

        <!-- Configurer that replaces ${...} placeholders with values from 
properties files -->
        <!-- (in this case, mail and JDBC related properties) -->

     <bean id="persistenceManager" 
class="com.myapp.strutschain.bean.PersistenceManager"/>
     <bean name="catalog" class="com.myapp.strutschain.core.SpringCatalog"/>
     <bean name="login" class="org.apache.commons.chain.impl.ChainBase">
                <constructor-arg>
                        <list>
                                <ref bean="firstCmd"/>
                                <ref bean="secondCmd"/>
                        </list>
                </constructor-arg>
      </bean>
     <bean name="firstCmd"
            class="com.myapp.strutschain.command.PreLoginCommand"/>
    
     <bean name="secondCmd"
            class="com.myapp.strutschain.command.LoginCommand">
                <property name="persistenceManager">
                        <ref local="persistenceManager"/>
                </property>
     </bean>    
</beans>

* Declare the Spring plugIn in your struts configuration file

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextConfigLocation"
                    value="/WEB-INF/applicationContext.xml"/>
</plug-in>

* Write an Action class servers as base to retrieve commands

public class CommandAction extends ActionSupport {
    
    protected static String SUCCESS = "success";

    protected Command getCommand(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
            throws Exception {
        Catalog catalog = (Catalog) 
getWebApplicationContext().getBean("catalog");
         String name = mapping.getName();
         System.out.println("Retrieving command for action"+ name);
        Command command = catalog.getCommand(name);
        return command;
    }

    protected Context getContext(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
            throws Exception {
        ServletContext application = request.getSession()
                .getServletContext();
        Context context = new ServletWebContext(
                application, request, response);
        
        return context;
    }
    
    protected ActionForward findLocation(ActionMapping mapping,
            boolean stop) {
        if (stop) return mapping.getInputForward(); // Something failed
        return mapping.findForward(SUCCESS);
    }

    public ActionForward execute(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {
        Command command = getCommand(mapping, form, request, response);
        Context context = getContext(mapping, form, request, response);
        boolean stop = command.execute(context);
        ActionForward location = findLocation(mapping, stop);
        return location;
    }
}

}}}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to