You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Oliver Heger <Ol...@t-online.de> on 2003/10/07 11:07:31 UTC

[configuration][patch] Support for object creation

Hello,

I did not receive an answer on my last mail, but nevertheless created a
patch which implements my suggestions (including a unit test class and a
test configuration file; I only created new files and did not change
existing sources).
Maybe the code makes the intension clearer.
Please tell me your thoughts!

Regards
Oli

----- Original Message -----
From: "Oliver Heger" <Ol...@t-online.de>
To: "Jakarta Commons Developer List" <co...@jakarta.apache.org>
Sent: Wednesday, October 01, 2003 12:05 PM
Subject: [configuration] Support for object creation


> Hello configuration developers,
>
> I am quite new to open source and would like to contribute something to
this
> project. I was working on a proprietary configuration framework and maybe
I
> can share some of the ideas used there with the community.
>
> At first I'd like to suggest a simple but (as I hope) useful extension for
> creating objects based on configuration information. As a use case for
this
> consider an application that accesses some central components (e.g. a
> resource manager, a caching component or whatever) only through interfaces
> and at startup instanciates objects of implementation classes defined in
the
> configuration. This makes it possible to alter the application's behavior
by
> just changing an implementation class in the configuration; it is a kind
of
> application of the GoF strategy pattern.
>
> The idea is that information about a class is specified in a configuration
> file as shown in the following example (for the XML flavour):
>
>   <myclassproperty>
>     <class>mypackage.MyClass</class>
>     <args>
>       <property>Value</property>
>       <anotherproperty>Another value</anotherproperty>
>       ...
>     </args>
>   </myclassproperty>
>
> Then a new class of the configuration package named ObjectConfigData (or
> somebody comes up with a better name?) can be used to read this
> configuration and create an instance of the defined class:
>
>   Configuration config = ...
>   ObjectConfigData objData = new ObjectConfigData(config,
> "myclassproperty");
>   Object obj = objData.createInstance();
>
> ObjectConfigData would extract the name of the class to be instantiated
from
> the class property below the specified configuration key and also all keys
> in the content of the (optional) <args> element. It could use BeanUtils to
> initialize the newly created instance with the properties defined here. So
> the new instance can be parametrized.
>
> In addition to this initialization a Configurable (or similar) interface
can
> be defined with a single initialize method that takes a Configuration
object
> as argument. ObjectConfigData could check if the new instance implemented
> that interface and if so, pass the whole content of the <args> section to
> this method. This allows for a more sophisticated initialization.
>
> There are of course some details which need a further discussion (e.g.
> exception handling), but I hope you get the point. (And I hope this mail
is
> not too long.) Your comments are welcome!
>
> Greetings
> Oli
>

Re: [configuration][patch] Support for object creation

Posted by Oliver Heger <Ol...@t-online.de>.
Eric,

I thought again about your response and now I agree with you that my code
does something which theoretically Digester could do, too. But using a
Digester object in my implementation seems to be massive overhead.

The approach with a digester friendly configuration converter is a good
idea, this would be very generic and could support many use cases including
my object creation. Unfortunately it seems to be quite difficult to
implement such a converter.

A possibility of communicating with Digester would be to let a Configuration
object trigger SAX events. Because Digester implements the
org.xml.sax.ContentHandler interface it would be able to process them (I
suppose, I didn't look at the source) . This has also the advantage of
enabling other XMLish style processing for Configuration.

My problem is now that there must be a way of extracting all available keys
from a Configuration object in the correct order to feed them into Digester.
With the actual implementation of BaseConfiguration this seems to be
impossible because all properties are simply stored in a map. Though this is
a sequenced map their original structure is lost. This is especially fatal
for XML documents whose tree like structure is messed up (maybe it is not as
problematic for plain properties or other configuration sources). A simple
example to proof this would be a document that describes database tables as
in the following fragment:

<?xml version="1.0" encoding="UTF-8"?>
<config>
 <table>
   <name>Users</name>
   <fields>
     <field>
       <name>uid</name>
       <type>long</type>
     </field>
     <field>
       <name>lastName</name>
       <type>java.lang.String</type>
     </field>
     <field>
       <name>firstName</name>
       <type>java.lang.String</type>
     </field>
   </fields>
 </table>
 <table>
   <name>documents</name>
   <fields>
     <field>
       <name>docid</name>
       <type>long</type>
     </field>
     <field>
       <name>docName</name>
       <type>java.lang.String</type>
     </field>
     <field>
       <name>authorID</name>
       <type>long</type>
     </field>
   </fields>
 </table>
</config>

After this document was parsed there is no way to find out which fields
belong to which table. Are there some plans to deal with this problem? I
think it could be worth thinking about it because XML as a configuration
format is quite important and its tree-like structure is one of its main
benefits.

If the tree structure could somehow be saved, it would also be possible to
extend the syntax of the property keys, e.g.
conf.getList("table(1).fields.field.name");
would return a vector with the names of all fields of the first table or
something like that.

Regards
Oli

----- Original Message -----
From: "Eric Pugh" <ep...@upstate.com>
To: "'Jakarta Commons Developers List'" <co...@jakarta.apache.org>
Sent: Tuesday, October 07, 2003 11:30 AM
Subject: RE: [configuration][patch] Support for object creation


> Oliver..
>
> Sorry I missed your earlier post..  So, have you looked at the Digester
> commons project?  I think it does similar to what your code does..  It
takes
> in an XML file with various rules about constructores etc and then builds
> the objects from that.
>
> There is also in (I think :-) ) beanutils a simple version of Digester
that
> hides away a lot of the complexity of digester..
>
> Could you highlight how your code and digester are different? It seems
also
> that what you have done might be better applied to digester as a "simple"
> xml format..
>
> Lastly, would it make sense to write a digester friendly configuration
> converter?  In otherwords, something that would take a configuration
> objects, feed it to digester and get back objects?
>
> Eric


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


Re: [configuration][patch] Support for object creation

Posted by Oliver Heger <o....@qubix.de>.
Hello Eric,

thank you for your response.

Well, as far as I understand the Digester project, it is a tool that
transforms an XML file into an object hierarchy based on certain rules. So
one could argue that Digester could replace most of the stuff in the
Configuration project by processing (XML) configuration files and creating
specific objects an application could use to access its configuration.

But the approach of Configuration seems to be a bit different and - as I
think - for many applications that just whant to access configuration
properties much easier to use: different configuration sources are collected
and provided to an application as a single Configuration object. So an
application needs only to obtain a Configuration object and query it for
single properties instead of bothering with Digester rules.

What my code does can be simply seen as adding a new data type to the
properties already supported by Configuration: the type configurable object
instance. So like an application can ask a Configuration object for a string
or long property it can now obtain ready initialized objects.

I think this is just an extension to Configuration and does not overlap with
Digester. For an application that uses such configurable objects and that
already is a client of Configuration it would be more convenient to access
these objects directly through Configuration than to set up a set of
Digester rules and call this component, too. My code is also not quite
complicated; the initialization of the newly created object is internally
handled by BeanUtils. The whole thing was intended to be an additional and
easy to implement feature for Configuration.

What do you think?
Oli

----- Original Message -----
From: "Eric Pugh" <ep...@upstate.com>
To: "'Jakarta Commons Developers List'" <co...@jakarta.apache.org>
Sent: Tuesday, October 07, 2003 11:30 AM
Subject: RE: [configuration][patch] Support for object creation


> Oliver..
>
> Sorry I missed your earlier post..  So, have you looked at the Digester
> commons project?  I think it does similar to what your code does..  It
takes
> in an XML file with various rules about constructores etc and then builds
> the objects from that.
>
> There is also in (I think :-) ) beanutils a simple version of Digester
that
> hides away a lot of the complexity of digester..
>
> Could you highlight how your code and digester are different? It seems
also
> that what you have done might be better applied to digester as a "simple"
> xml format..
>
> Lastly, would it make sense to write a digester friendly configuration
> converter?  In otherwords, something that would take a configuration
> objects, feed it to digester and get back objects?
>
> Eric
>


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


RE: [configuration][patch] Support for object creation

Posted by Eric Pugh <ep...@upstate.com>.
Oliver..

Sorry I missed your earlier post..  So, have you looked at the Digester
commons project?  I think it does similar to what your code does..  It takes
in an XML file with various rules about constructores etc and then builds
the objects from that.

There is also in (I think :-) ) beanutils a simple version of Digester that
hides away a lot of the complexity of digester..

Could you highlight how your code and digester are different? It seems also
that what you have done might be better applied to digester as a "simple"
xml format..

Lastly, would it make sense to write a digester friendly configuration
converter?  In otherwords, something that would take a configuration
objects, feed it to digester and get back objects?

Eric

> -----Original Message-----
> From: Oliver Heger [mailto:Oliver.Heger@t-online.de]
> Sent: Tuesday, October 07, 2003 11:08 AM
> To: Jakarta Commons Developer List
> Subject: [configuration][patch] Support for object creation
>
>
> Hello,
>
> I did not receive an answer on my last mail, but nevertheless
> created a
> patch which implements my suggestions (including a unit test
> class and a
> test configuration file; I only created new files and did not change
> existing sources).
> Maybe the code makes the intension clearer.
> Please tell me your thoughts!
>
> Regards
> Oli
>
> ----- Original Message -----
> From: "Oliver Heger" <Ol...@t-online.de>
> To: "Jakarta Commons Developer List" <co...@jakarta.apache.org>
> Sent: Wednesday, October 01, 2003 12:05 PM
> Subject: [configuration] Support for object creation
>
>
> > Hello configuration developers,
> >
> > I am quite new to open source and would like to contribute
> something to
> this
> > project. I was working on a proprietary configuration
> framework and maybe
> I
> > can share some of the ideas used there with the community.
> >
> > At first I'd like to suggest a simple but (as I hope)
> useful extension for
> > creating objects based on configuration information. As a
> use case for
> this
> > consider an application that accesses some central
> components (e.g. a
> > resource manager, a caching component or whatever) only
> through interfaces
> > and at startup instanciates objects of implementation
> classes defined in
> the
> > configuration. This makes it possible to alter the
> application's behavior
> by
> > just changing an implementation class in the configuration;
> it is a kind
> of
> > application of the GoF strategy pattern.
> >
> > The idea is that information about a class is specified in
> a configuration
> > file as shown in the following example (for the XML flavour):
> >
> >   <myclassproperty>
> >     <class>mypackage.MyClass</class>
> >     <args>
> >       <property>Value</property>
> >       <anotherproperty>Another value</anotherproperty>
> >       ...
> >     </args>
> >   </myclassproperty>
> >
> > Then a new class of the configuration package named
> ObjectConfigData (or
> > somebody comes up with a better name?) can be used to read this
> > configuration and create an instance of the defined class:
> >
> >   Configuration config = ...
> >   ObjectConfigData objData = new ObjectConfigData(config,
> > "myclassproperty");
> >   Object obj = objData.createInstance();
> >
> > ObjectConfigData would extract the name of the class to be
> instantiated
> from
> > the class property below the specified configuration key
> and also all keys
> > in the content of the (optional) <args> element. It could
> use BeanUtils to
> > initialize the newly created instance with the properties
> defined here. So
> > the new instance can be parametrized.
> >
> > In addition to this initialization a Configurable (or
> similar) interface
> can
> > be defined with a single initialize method that takes a
> Configuration
> object
> > as argument. ObjectConfigData could check if the new
> instance implemented
> > that interface and if so, pass the whole content of the
> <args> section to
> > this method. This allows for a more sophisticated initialization.
> >
> > There are of course some details which need a further
> discussion (e.g.
> > exception handling), but I hope you get the point. (And I
> hope this mail
> is
> > not too long.) Your comments are welcome!
> >
> > Greetings
> > Oli
> >
>
>


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