avalon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Paulo Gaspar" <paulo.gas...@krankikom.de>
Subject RE: Divergence from Avalon (was Re: [RT] Is Poolable Harmful?)
Date Sat, 12 Jan 2002 04:03:55 GMT
Hi Peter,

> -----Original Message-----
> From: Peter Donald [mailto:peter@apache.org]
> Sent: Saturday, January 12, 2002 1:53 AM
> 
> 
> On Sat, 12 Jan 2002 11:50, Paulo Gaspar wrote:
> > > > I sent you the FACTORY code. The component has NO configuration
> > > > points and in this case is not even dependent on the framework.
> > >
> > > Actually it has many configuration points and is not dependent on
> > > framework
> > > ;) Rather than expose one public method to configure service 
> you expose N
> > > different setters and each of these setters is another hinge point.
> >
> > No. For configuration I only expose setters in an object that is used
> > ONLY to collect the configuration values. Usually this object is from
> > an inner class - inner to the factory class.
> >
> > That inner object is NOT the component. It is part of the factory.
> 
> Time for a code snippet I think ;) How does the configuration 
> values get from your "adaptor" object to your real component?

Ok, this hint means that you want _me_ to fetch the snippet from that post
I made ages ago... (It sure took time to find, you lazy b******!)

The snippet is just a bit after the quote and the "Have fun".

Brief legend:
  DbFactory    - the custom component factory;

  CfgBean      - the mentioned inner class. Not really a javabean 
                 since I am a lazy b****** (too) and the Configurator
                 uses field introspection if the "IFieldBasedConfig"
                 marker interface "marks" this configuration bean;

  cfg          - The configuration bean, which is an instance of the
                 CfgBean class;

  Connection   - The component... yes, a pooled java.sql.Connection!;

  m_dbConnPool - The database Connection pool.


Then, the database pool is created by this bit:

// Notice the use of "cfg"... just holding the values

    public void customInitialize()
    throws Exception
    {   IDbConnFactory dbConnFactory =          
                new DriverManagerConnectionFactory(cfg.driver, cfg.url, cfg.user, cfg.password);

        m_dbConnPool = new DbConnectionPool(logger(), dbConnFactory);
        m_dbConnPool.setUseBorrowValidation(false);
        m_dbConnPool.setUseIdleValidation(false);
        m_dbConnPool.setUseReturnValidation(false);
        m_dbConnPool.initialize();
    }


 
> -- 
> Cheers,
> 
> Pete
> 
> --------------------------------------------
> Sorry, I forgot to take my medication today.
> --------------------------------------------

I noticed!
=;o)


Have fun, the code snippet follows,
Paulo Gaspar


AND NOW... the sample:

// Here goes a simple DB component factory.
// This one is missing a load of parameterization but makes a better 
// example. I added some comments just for this mail!
// =:o)


import java.sql.Connection;

import javax.sql.DataSource;

import krankikom.sql.pool.IDbConnFactory;
import krankikom.sql.pool.dbcfactories.DriverManagerConnectionFactory;
import krankikom.sql.pool.PoolingDataSource;
import krankikom.sql.pool.DbConnectionPool;

import krankikom.activity.AbstractLoggableActivity;
import krankikom.log.ILogger;
import krankikom.framework.component.IComponentFactory;

import krankikom.framework.configuration.configurator.*;


public class DbFactory
    extends AbstractLoggableActivity
    implements IAutoConfigurable, IComponentFactory

// IAutoConfigurable exposes the autoConfigXXX() methods
// that the Configurator will use to "auto-configure" this
// factory.

{
    // The IFieldBasedConfig marker tells the Configurator
    // to use field introspection instead of the standard
    // JavaBean introspection. 
    // (I am too lazy to find writting setXXX() methods 
    //  interesting for this case.)

    public class CfgBean implements IFieldBasedConfig
    {
        public String name;
        public String driver;
        public String url;
        public String user;
        public String password;


        // Silly debug helper
        public String toString()
        { return getClass().getName()
                 + "{ " + fmtCell("*name",   name)
                        + fmtCell("*driver", driver)
                        + fmtCell("*url",    url)
                        + fmtCell("*user",   user)
                        + fmtCell("*pwd",    "*****")
                + " }";
        }
    }

    public CfgBean cfg = new CfgBean();

    private DbConnectionPool   m_dbConnPool;

    public DbFactory()
    {   super();
    }


    //*** IComponentFactory stuff: ***

    public Class createdClass()
    {   return java.sql.Connection.class;
    }

    public boolean isComponentThreadSafe()
    {   return false;
    }

    public Object newInstance()
    throws Exception
    {   return m_dbConnPool.newDataSource();
    }

    public void release(Object i_object)
    throws Exception
    {
        if (null != i_object)
            ((PoolingDataSource)i_object).close();
    }


    //*** IAutoConfigurable stuff: ***
    public Object autoConfigBean()
    {   return cfg;
    }

    public void autoConfigInitialize()
    {}

    public void autoConfigPropertiesFinalize()
    {  
        // I still want to see a sign that it is working!!! =;o)
        System.out.println("CfgBean => " + cfg);
    }

    public void autoConfigFinalize()
    {
    }



    //*** Life cycle ***

    // This customXXX() are called by the lifecycle methods 
    // at AbstractLoggableActivity which implements the
    // Initializable, Disposable, Startable and Logabble
    // interfaces with a bunch of checks to make sure I am 
    // doing things right and also logs on "INFO" level when
    // he performs the life cycle methods. UFFFfffff!


    public void customInitialize()
    throws Exception
    {   IDbConnFactory dbConnFactory =
                new DriverManagerConnectionFactory(cfg.driver, cfg.url, cfg.user, cfg.password);
        m_dbConnPool = new DbConnectionPool(logger(), dbConnFactory);
        m_dbConnPool.setUseBorrowValidation(false);
        m_dbConnPool.setUseIdleValidation(false);
        m_dbConnPool.setUseReturnValidation(false);
        m_dbConnPool.initialize();
    }


    public void customStart()
    throws Exception
    {   m_dbConnPool.start();
    }

    public void customStop()
    throws Exception
    {   m_dbConnPool.stop();
    }

    public void customDispose()
    throws Exception
    {   m_dbConnPool.dispose();
    }

    // Silly debug helpers
    protected static String fmtCell(final String i_name, final String i_value)
    {   return "[" + i_name + "=" + String.valueOf(i_value) + "]";
    }

    public String toString()
    {   return cfg.toString();
    }
}


--
To unsubscribe, e-mail:   <mailto:avalon-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-dev-help@jakarta.apache.org>


Mime
View raw message