james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stefano Bagnara <apa...@bago.org>
Subject Re: Accessing JdbcDataSource config in custom service
Date Mon, 01 Oct 2007 07:38:02 GMT
Kevin Jackson ha scritto:
> Hi,
> 
> Further to my request for help concerning removing spam mail after 15 days:
> 
> In my james config.xml I have:
> 
> <!-- configure to remove old mails from repository -->
>    <mailclean enabled="true">
>      <cleanup name="spams">
>        <!-- 24*60*60*1000 = 1 day -->
>        <interval>86400000</interval>
>        <mail-cleaner delay="15" schema="public" table="spams"/>
>      </cleanup>
>    </mailclean>
> 
> In my CleanMailRepoService code I have: (taken from FetchScheduler)
> 
> Configuration[] confs = conf.getChildren( "cleanup" );
>             for(int i=0; i<confs.length; i++) {
>             	Configuration conf = confs[i];
>                 String cleanName = conf.getAttribute( "name" );
>                 Integer interval = new Integer( conf.getChild(
> "interval" ).getValue() );
> 
>                 MailCleaner mc = new MailCleaner();
> 
>                 /* avalon specific initialization */
>                 ContainerUtil.enableLogging(
> mc,getLogger().getChildLogger( cleanName ) );
>                 ContainerUtil.service( mc,manager );
>                 ContainerUtil.configure( mc,conf );
> 
> and in my MailCleaner code I have:
> 
> public void configure(Configuration conf) throws ConfigurationException {
> 		Configuration jdbcConf = conf.getChild( "database-connections"
> ).getChild( "data-source" );
> 		ds = new JdbcDataSource();
> 		ds.configure( jdbcConf );
> 		Configuration mConf = conf.getChild( "mail-cleaner" );
> 		delay = mConf.getAttributeAsInteger( "delay" );
> 		schema = mConf.getAttribute( "schema" );
> 		table = mConf.getAttribute( "table" );
> 	}
> 
> Finally my CleanMailRepoService xinfo:
> 
> <blockinfo>
> 
>   <!-- section to describe block -->
>   <block>
>     <version>1.0</version>
>   </block>
> 
>   <!-- services that are offered by this block -->
>   <services/>
> 
>   <dependencies>
>     <dependency>
>       <service name="org.apache.avalon.cornerstone.services.datasources.DataSourceSelector"
> version="1.0"/>
>     </dependency>
>     <dependency>
>       <service name="org.apache.avalon.cornerstone.services.scheduler.TimeScheduler"
> version="1.0"/>
>     </dependency>
>   </dependencies>
> </blockinfo>
> 
> Now I want to access the <database-connections> config from the
> config.xml, but I can only access it if I duplicate that config in my
> <mailclean> section (and I don't want to do that)
> 
> Is there a way of injecting the JdbcDataSource into my code without
> having a duplicate block of config?

Do you need access to the database-connection block or simply you want
to manage repositories via the MailRepository interface?

In both cases you have to declare a new dependency in your xinfo file:

database-connection:
<dependency>
<service
name="org.apache.avalon.cornerstone.services.datasources.DataSourceSelector"
version="1.0" />
</depedency>

mailstore:
<dependency>
<service name="org.apache.avalon.cornerstone.services.store.Store"
version="1.0" />
</depedency>

Then you have to bind it in your assembly (inside the mailclean block):
<provide name="mailstore"
role="org.apache.avalon.cornerstone.services.store.Store"/>
or
<provide name="database-connections"
role="org.apache.avalon.cornerstone.services.datasources.DataSourceSelector"
/>

Then I suggest you to look at our "trunk" SpoolManagement block:
http://svn.apache.org/repos/asf/james/server/trunk/core-library/src/main/java/org/apache/james/management/SpoolManagement.java
http://svn.apache.org/repos/asf/james/server/trunk/core-library/src/main/java/org/apache/james/management/SpoolManagement.xinfo
This declare a depedency on mailstore, implements Serviceable and take a
reference to the Store.

It is specific to SpoolRepository, but you can do similar things for the
MailRepository (just use "MAIL" as repository type lookup key, instead
of "SPOOL").

Stefano

> Another option is not to perform any configuration in the
> CleanMailRepoService (but I have to do this to setup the scheduler).
> 
> Any advice on how to get my desired result in the most avalon-like way
> is much appreciated.
> 
> Thanks,
> Kev
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
> For additional commands, e-mail: server-dev-help@james.apache.org
> 
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message