Freight Train
Niclas Hedhman
niclas at hedhman.org
Fri May 6 05:03:56 EDT 2005
On Friday 06 May 2005 13:18, David Leangen wrote:
> XML works for me. I just don't understand your approach
> or what's going on in your mind. Please explain it. Configuration support
> is a big deal for me.
I should let Steve do it.
Effectively;
FT Contexts are totally wrapping the Configuration and Parameter concept in
Metro classic.
Let's say you had the following configuration snippet before;
<connections active="mysql-dev">
<connection name="mysql-dev" >
<url>jdbc:mysql:dev.hedhman.org/appdb</url>
<user>niclas</user>
<password>1234</password>
</connection>
<connection name="oracle-staging" >
<url>jdbc:oracle:staging.hedhman.org/appdb</url>
<user>steve</user>
<password>1234</password>
</connection>
<connection name="oracle-production" >
<url>jdbc:oracle:production.hedhman.org/appdb</url>
<user>david</user>
<password>1234</password>
</connection>
</connections>
You would end up with roughly the following coding in the component itself;
public class MyClass
{
private String m_DbUrl;
private String m_DbUser;
private String m_DbPassword;
public MyClass( Configuration conf )
throws ConfigurationException
{
Configuration conns = cond.getChild( "connections" );
String active = conns.getAttribute( "active" );
Configuration activeConf = findActiveConnection( conns, active );
configureConnection( activeConf );
initializeDatabase();
}
private void findActiveConnection( Configuration conf, String active )
throws ConfigurationException
{
Configuration[] connections = conf.getChildren( "connection" );
for( int i=0 ; i < connections.length ; i++ )
{
Configuration conn = connections[i];
String name = conn.getAttribute( "name" );
if( name.equals( active ) )
return conn;
}
return null;
}
private void configureConnection( Configuration active )
throws ConfigurationException
{
m_DBUrl = active.getChild( "url" ).getValue();
m_DbUser = active.getChild( "user" ).getValue();
m_DbPassword = active.getChild( "password" ).getValue();
}
:
}
And if anything is not absolutely correct, you will get a
ConfigurationException, which will bomb the application, OR you have default
values, in which case you don't know when things are not working, OR you have
even more code for handling the ConfigurationExceptions.
In FT, we would have a fairly sweet component;
public class MyClass
{
private String m_DbUrl;
private String m_DbUser;
private String m_DbPassword;
public interface Context
{
Connection getActiveConnection();
}
public MyClass( Context ctx )
{
Connection conn = ctx.getActiveConnection();
m_DbUrl = conn.getDatabaseUrl();
m_DbUser = conn.getDatabaseUser();
m_DbPassword = conn.getDatabasePassword();
initializeDatabase();
}
:
}
As you can see, no exceptions, nothing that can go wrong, and barely any code.
NOW, the problem is of course, how do I set up the Connection instance?
And that is where you will see you have a lot of options, but it will always
be handled by a controller/manager and not the component itself.
* Unittests will create Context/Connection instances on-the-fly for
testability, and doesn't have to worry about files, or hard to generate
in-memory Configuration instances.
* FT utilities will allow the configuration from various sources, probably
including XML, and perhaps others, such as Jmx, databases, Java
preferences and more.
* Non-Metro applications that wants to use the component can also generate
the Context instance on the fly, in a way that fits with its strategy
for configuration.
Exactly how it is done at a macro level is not totally worked out yet, AFAICT.
Perhaps Steve has more thoughts about this by now.
Cheers
Niclas
More information about the dev-dpml
mailing list