You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by Kevin Jackson <fo...@gmail.com> on 2007/10/01 08:32:20 UTC

Accessing JdbcDataSource config in custom service

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?

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


Re: Accessing JdbcDataSource config in custom service

Posted by Stefano Bagnara <ap...@bago.org>.
Kevin Jackson ha scritto:
> Hi,
> 
>> Do you need access to the database-connection block or simply you want
>> to manage repositories via the MailRepository interface?
> 
> I want access to the underlying db connection so that I can execute a
> preparedstatement, I don't want to treat the data as mail, just as a
> table

Then take a look at this trunk component:
http://svn.apache.org/repos/asf/james/server/trunk/core-library/src/main/java/org/apache/james/domain/JDBCDomainList.java
http://svn.apache.org/repos/asf/james/server/trunk/core-library/src/main/java/org/apache/james/domain/JDBCDomainList.xinfo

it declares a dependency on the DataSourceSelector and then use it to
make custom queries to a custom table.

Stefano

>> 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>
>>
> 
> Ok, I'll give this a try
> 
> Thanks,
> Kev



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


Re: Accessing JdbcDataSource config in custom service

Posted by Kevin Jackson <fo...@gmail.com>.
Hi,

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

I want access to the underlying db connection so that I can execute a
preparedstatement, I don't want to treat the data as mail, just as a
table

>
> 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>
>

Ok, I'll give this a try

Thanks,
Kev

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


Re: Accessing JdbcDataSource config in custom service

Posted by Stefano Bagnara <ap...@bago.org>.
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