You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Haytham Samad <sa...@gmail.com> on 2005/04/07 03:32:20 UTC

[chain] Getting null catalog when using LookupCommand

I keep getting a null catalog returned to me when I run this example
taken from an onjava article.  I am using Chains 1.0.  

Any idea why this is happening?

<catalog name="auto-sales">	
	<define    name="test-command"
			className="com.jadecove.chain.sample.TestPropertiesCommand"/>
			
	  <chain name="sell-vehicle">
		<command   id="GetCustomerInfo"
			className="com.jadecove.chain.sample.GetCustomerInfo"/>
		<command   id="TestDriveVehicle"
			className="com.jadecove.chain.sample.TestDriveVehicle"/>
		<command   id="NegotiateSale"
			className="com.jadecove.chain.sample.NegotiateSale"/>
		<command
			className="org.apache.commons.chain.generic.LookupCommand"
			catalogName="auto-sales"
			name="arrange-financing"
			optional="false"/>
		
		<command   id="CloseSale"
			className="com.jadecove.chain.sample.CloseSale"/>
		<test-command name="JoeSmith"/>
	  </chain>
	  <chain name="arrange-financing">
		<command   id="ArrangeFinancing"
			className="com.jadecove.chain.sample.ArrangeFinancingFilter"/>
	  </chain>
</catalog>

/**
 * Load and run the catalog.
 */
package com.jadecove.chain.sample;

import java.net.URL;

import org.apache.commons.chain.Catalog;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.apache.commons.chain.config.ConfigParser;
import org.apache.commons.chain.impl.CatalogFactoryBase;

/**
 * @author
 *
 */
public class CatalogLoader {
    
    private static final String CONFIG_FILE = 
        "/com/jadecove/chain/sample/chain-config.xml";
    private ConfigParser parser;
    private Catalog catalog;
    
    public CatalogLoader() {
        parser = new ConfigParser();
    }
    public Catalog getCatalog() throws Exception {
        if (catalog == null) {
            URL resource = getClass().getClassLoader().getResource(CONFIG_FILE);
                        
            URL configFile = this.getClass().getResource(CONFIG_FILE);
            if(configFile==null){
                System.out.println("Config file is null");
            }
            System.out.println("Resource path is " + configFile.getFile());
            parser.parse(this.getClass().getResource(CONFIG_FILE));
        }
        catalog = CatalogFactoryBase.getInstance().getCatalog();
        return catalog;
    }
    public static void main(String[] args) throws Exception {
        CatalogLoader loader = new CatalogLoader();
        Catalog sampleCatalog = loader.getCatalog();
        
        if(sampleCatalog==null){
            throw new NullPointerException("catalog not initialized correctly");
        }
        Command command = sampleCatalog.getCommand("sell-vehicle");
        Context ctx = new SellVehicleContext();
        command.execute(ctx);
    }
}

The call loader.getCatalog() always returns a null catalog.

Thanks



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


Re: [chain] When does a filter run?

Posted by Craig McClanahan <cr...@gmail.com>.
On 5/6/05, Samad Haytham <sa...@gmail.com> wrote:
> Question about how filters work as we are seeing behaviour that is a
> little different from what we expected.  We setup a filter as the
> second command in a 4 command chain.  Following is an example config
> file.
> 
> <chain name="myChain">
>   <command id="1"....
>    />
>   <command id="myfilter"..... />
>   <command id="3".../>
>   <command id="4" ...../>
> </chain>
> 
> If command 1 throws an Exception, myfilter's postprocess is never
> called.  Looking at the code, we realized that the filters are
> executed in reverse order from where an exception is thrown (or the
> chain ends).  Meaning that the filter's postprocess method is only
> called if the execute method is called.  What we wanted was a way to
> have one filter's postprocess method handle any exceptions that are
> thrown.  To enable that with how Chains currently works, we move our
> filter to the beginning of the chain.  Are there any other ways to do
> this?  Define a Filter (no matter where in the Chain - in our case it
> was the last command called JobEnd) and guarantee that its postprocess
> is called?

Filters are working as you describe (postprocess is only called if
execute is called), and that is as they are designed.  A typical use
case is that you want to allocate a resource (say, a JDBC connection)
that is needed by commands that will be executed later in the chain,
so you do this in the execute() method of a filter and then release it
in the postprocess() method.

If a command in the chain *prior* to this filter threw an exception,
you'd have never allocated the connection in the first place, so there
would be no reason to release it.

If you want a generalized exception catcher, either:

* Put this filter at the beginning of your chain

* Or, implement a customized version of Chain that traps any exception
  thrown while it is executing and passes it to some method for handling.

Craig


> 
> BTW, its been nice working with chains.  Any information as to what we
> can expect in the next release?
> 
> Thanks
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
>

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


[chain] When does a filter run?

Posted by Samad Haytham <sa...@gmail.com>.
Question about how filters work as we are seeing behaviour that is a
little different from what we expected.  We setup a filter as the
second command in a 4 command chain.  Following is an example config
file.

<chain name="myChain">
  <command id="1"....
   />
  <command id="myfilter"..... />
  <command id="3".../>
  <command id="4" ...../>
</chain>

If command 1 throws an Exception, myfilter's postprocess is never
called.  Looking at the code, we realized that the filters are
executed in reverse order from where an exception is thrown (or the
chain ends).  Meaning that the filter's postprocess method is only
called if the execute method is called.  What we wanted was a way to
have one filter's postprocess method handle any exceptions that are
thrown.  To enable that with how Chains currently works, we move our
filter to the beginning of the chain.  Are there any other ways to do
this?  Define a Filter (no matter where in the Chain - in our case it
was the last command called JobEnd) and guarantee that its postprocess
is called?

BTW, its been nice working with chains.  Any information as to what we
can expect in the next release?

Thanks

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