commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Emmanuel Bourg <ebo...@apache.org>
Subject Re: [Configuration] read different XML configuration files based on FQCN
Date Thu, 09 Jun 2005 21:29:47 GMT
Hi Phineas, if I understand well each Worker implementation share the
same ConfigurationFactory, and you'd like a different Configuration for
your workers on calling configurationFactory.getConfiguration(), right ?

You may try one of these alternatives:

1. have a ConfigurationFactory per worker:

public class WorkerA implements Worker {
    private Configuration configuration;

    public WorkerA() {
        ConfigurationFactory cf = new ConfigurationFactory();
        cf.setConfigurationFileName("configA.xml");
        configuration = cf.getConfiguration();
    }
}

and the content of configA.xml is:

<configuration>
  <system/>
  <xml fileName="implementationClass_A.xml"/>
</configuration>


2. let the worker factory assign the configuration to the worker:

public class WorkerFactory {
    public static Worker createWorker(Class type) {
        Worker worker = (Worker) Class.newInstance();

        CompositeConfiguration config = new CompositeConfiguration();
        config.addConfiguration(new SystemConfiguration());
        String file = type.getName() + ".xml";
        config.addConfiguration(new XMLConfiguration(file))
        worker.setConfiguration(config);

        return worker;
    }
}

Hope that helps.

Emmanuel Bourg


Phineas Fung wrote:
> Hi,
> 
> We have a few implementation classes that implements the same worker
> interface, that are returned by  a factory at runtime.
> 
> Every implementation class needs to read an XML configuration file (to be
> exact, a tree structure).  The XML hierarchy and fields in each
> configuration file are the same, but contents are different for each
> implementation class.
> 
> Unfortunately, all of these implementation classes will be used at the same
> time, and I cannot figure out how to let each implementation class read its
> own XML file.
> 
> If I do sth like this:
> 
> <configuration>
>   <system/>
>   <xml fileName="implementationClass_A.xml"/>
>   <xml fileName="implementationClass_B.xml"/>
>   <xml fileName="implementationClass_C.xml"/>
> </configuration>
> 
> The values in implementationClass_C.xml will be overridden by those in A or
> B.  What I want is:
> 
> Construct a ConfigurationRepository using the fully-qualified class name,
> i.e. "ImplementationClass_C", then it'll read configuration values from
> implementationClass_C.xml.  Another ConfigurationRepository using e.g.
> "ImplementationClass_B", then it'll read configuration values from
> implementationClass_B.xml.
> 
> Sample configuration files are:
> 
> implementationClass_A.xml
> 
> <config>
>     <cacheTree name="field1_field2_field3">
>         <level sortingOrder="100" field="field1"/>
>         <level sortingOrder="200" field="field2"/>
>         <level sortingOrder="300" field="field3"/>
>     </cacheTree>
> 
>     <cacheTree name="field1_field3">
>         <level sortingOrder="100" field="field1"/>
>         <level sortingOrder="200" field="field3"/>
>     </cacheTree>
> 
>     <getterMethod methodName="get_Field1" cacheTree="field1_field3"/>
>     <getterMethod methodName="get_Field1_Field2"
> cacheTree="field1_field2_field3"/>
>     <getterMethod methodName="get_Field1_Field3" cacheTree="field1_field3"/>
>     <getterMethod methodName="get_Field1_Field2_Field3"
> cacheTree="field1_field2_field3"/>
>     <getterMethod methodName="get_Field1_Field2_Field3_Field4"
> cacheTree="field1_field2_field3"/>
> </config>
> 
> implementationClass_B.xml
> 
> <config>
>     <cacheTree name="field1_field2">
>         <level sortingOrder="100" field="field1"/>
>         <level sortingOrder="200" field="field2"/>
>     </cacheTree>
> 
>     <getterMethod methodName="get_Field1" cacheTree="field1_field2"/>
>     <getterMethod methodName="get_Field1_Field2" cacheTree="field1_field2"/>
> </config>
> 
> Any thoughts will be greatly appreciated.
> 
> Thanks alot.
> 
> 
> Best Regards,
> Phineas

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


Mime
View raw message