Migrating from Avalon database connections to DataSource

In James version 2.3, we expose database connections via the javax.sql.DataSource. This document describes how to update code using James previous database connectivity (Avalon's DataSourceComponent).

  1. Remove references to DataSourceSelector.
  2. Change references of DataSourceComponent to DataSource
  3. Add imports:
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import javax.sql.DataSource;
    
  4. Change how the datasource is looked up

    Replace:
        ComponentManager componentManager = (ComponentManager)getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER);
        // Get the DataSourceSelector service
        DataSourceSelector datasources = (DataSourceSelector)componentManager.lookup(DataSourceSelector.ROLE);
        // Get the data-source required.
        datasource = (DataSourceComponent)datasources.select(datasourceName);
    
    with
        InitialContext ctx = new InitialContext();
        datasource = (DataSource) ctx.lookup("java:comp/env/jdbc/" + datasourceName);
    
    Then update exception handling appropriately.
That's all there is to it. Enjoy!