You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by Jochen Wiedmann <jo...@gmail.com> on 2009/08/10 22:32:46 UTC

Configuring a dynamically selected component

Hi,

in a plugin, I'd like to select a component dynamically. Basically,
I'd like to have something along these lines:

    <configuration>
      <myComponent hint="foo">
         ...
      </myComponent>
    </configuration>

The idea is to use the hint for looking up the "myComponent" bean and
use the XML fragment between <myComponent> and <myComponent> to
configure the bean.

Is that possible? Any other ideas how to achieve my goal?

Thanks,

Jochen

-- 
Base64 decoding, 300% faster than sun.misc.BASE64Decoder:
http://archive.netbsd.se/?ml=commons-dev&a=2008-05&t=7522166

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


Re: Configuring a dynamically selected component

Posted by Jochen Wiedmann <jo...@gmail.com>.
On Wed, Aug 12, 2009 at 2:59 AM, Jason van Zyl<jv...@sonatype.com> wrote:

> For Maven 2.x you'll be interested in this:
>
> http://svn.codehaus.org/plexus/plexus-containers/branches/PLX-343-plexus-container-default-1.0-alpha-9-stable-1/src/main/java/org/codehaus/plexus/component/configurator/

Thanks, I'll take a look at it.

Jochen


-- 
Base64 decoding, 300% faster than sun.misc.BASE64Decoder:
http://archive.netbsd.se/?ml=commons-dev&a=2008-05&t=7522166

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


Re: Configuring a dynamically selected component

Posted by Jason van Zyl <jv...@sonatype.com>.
On 11-Aug-09, at 7:37 AM, Jochen Wiedmann wrote:

> On Tue, Aug 11, 2009 at 12:13 PM, Jason van  
> Zyl<jv...@sonatype.com> wrote:
>
>> Brian semi-abused the configuration mechanism to achieve what he  
>> wanted. But
>> he did get it to work :-) We'll devise something that uses the  
>> components
>> for 3.x but what's happening is that configuration elements  
>> translate into
>> classes in the same package as the plugin itself. Plexus uses an
>> XStream-like mechanism to instantiate the objects and populate  
>> their values.
>> May not work internally exactly as you imagined but it does work  
>> and is
>> effective.
>
> Okay, understood. Thanks, that will be sufficient for me to go on.
>
> Just out of curiosity: Can you point me to the location in the
> configurator sources, which does that? Perhaps, I'd like to spend some
> work in that area.
>

For Maven 2.x you'll be interested in this:

http://svn.codehaus.org/plexus/plexus-containers/branches/PLX-343-plexus-container-default-1.0-alpha-9-stable-1/src/main/java/org/codehaus/plexus/component/configurator/

>
> Jochen
>
>
> -- 
> Base64 decoding, 300% faster than sun.misc.BASE64Decoder:
> http://archive.netbsd.se/?ml=commons-dev&a=2008-05&t=7522166
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org
>

Thanks,

Jason

----------------------------------------------------------
Jason van Zyl
Founder,  Apache Maven
http://twitter.com/jvanzyl
http://twitter.com/SonatypeNexus
http://twitter.com/SonatypeM2E
----------------------------------------------------------

We all have problems. How we deal with them is a measure of our worth.

  -- Unknown


Re: Configuring a dynamically selected component

Posted by Jochen Wiedmann <jo...@gmail.com>.
On Tue, Aug 11, 2009 at 12:13 PM, Jason van Zyl<jv...@sonatype.com> wrote:

> Brian semi-abused the configuration mechanism to achieve what he wanted. But
> he did get it to work :-) We'll devise something that uses the components
> for 3.x but what's happening is that configuration elements translate into
> classes in the same package as the plugin itself. Plexus uses an
> XStream-like mechanism to instantiate the objects and populate their values.
> May not work internally exactly as you imagined but it does work and is
> effective.

Okay, understood. Thanks, that will be sufficient for me to go on.

Just out of curiosity: Can you point me to the location in the
configurator sources, which does that? Perhaps, I'd like to spend some
work in that area.


Jochen


-- 
Base64 decoding, 300% faster than sun.misc.BASE64Decoder:
http://archive.netbsd.se/?ml=commons-dev&a=2008-05&t=7522166

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


Re: Configuring a dynamically selected component

Posted by Jason van Zyl <jv...@sonatype.com>.
On 11-Aug-09, at 1:27 AM, Jochen Wiedmann wrote:

> On Tue, Aug 11, 2009 at 6:01 AM, Jason van Zyl<jv...@sonatype.com>  
> wrote:
>> Look at the enforcer plugin. As each rule is a component that gets
>> configured.
>
> Thanks once more. I have checked the sources of the enforcer and it
> sounds *exactly* like what I am looking for. However, I haven't got
> the slightest idea how it works.
>

Brian semi-abused the configuration mechanism to achieve what he  
wanted. But he did get it to work :-) We'll devise something that uses  
the components for 3.x but what's happening is that configuration  
elements translate into classes in the same package as the plugin  
itself. Plexus uses an XStream-like mechanism to instantiate the  
objects and populate their values. May not work internally exactly as  
you imagined but it does work and is effective.

> I can see that the enforcer-rules module is using the
> plexus-maven-plugin, but that doesn't detect any components. (And
> rightly so, because there aren't any annotations I am aware of.)
>
> What magic translates
>
>            <configuration>
>              <rules>
>                <requireMavenVersion>
>
> to "create me an instance of RequireMavenVersion"?
>
>
> Thanks,
>
> Jochen
>
>
> -- 
> Base64 decoding, 300% faster than sun.misc.BASE64Decoder:
> http://archive.netbsd.se/?ml=commons-dev&a=2008-05&t=7522166
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org
>

Thanks,

Jason

----------------------------------------------------------
Jason van Zyl
Founder,  Apache Maven
http://twitter.com/jvanzyl
http://twitter.com/SonatypeNexus
http://twitter.com/SonatypeM2E
----------------------------------------------------------

To do two things at once is to do neither.

  -—Publilius Syrus, Roman slave, first century B.C.


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


Re: Configuring a dynamically selected component

Posted by Jochen Wiedmann <jo...@gmail.com>.
On Tue, Aug 11, 2009 at 6:01 AM, Jason van Zyl<jv...@sonatype.com> wrote:
> Look at the enforcer plugin. As each rule is a component that gets
> configured.

Thanks once more. I have checked the sources of the enforcer and it
sounds *exactly* like what I am looking for. However, I haven't got
the slightest idea how it works.

I can see that the enforcer-rules module is using the
plexus-maven-plugin, but that doesn't detect any components. (And
rightly so, because there aren't any annotations I am aware of.)

What magic translates

            <configuration>
              <rules>
                <requireMavenVersion>

to "create me an instance of RequireMavenVersion"?


Thanks,

Jochen


-- 
Base64 decoding, 300% faster than sun.misc.BASE64Decoder:
http://archive.netbsd.se/?ml=commons-dev&a=2008-05&t=7522166

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


Re: Configuring a dynamically selected component

Posted by Jason van Zyl <jv...@sonatype.com>.
Look at the enforcer plugin. As each rule is a component that gets  
configured.

On 10-Aug-09, at 8:54 PM, Jochen Wiedmann wrote:

> Hi, Jason,
>
> thanks for your reply. That will at least help me to get a working
> solution. However, I am not quite sure whether I expressed myself
> right. Sorry, if not.
>
> My ideal solution would be to have something like
>
>    <plugins>
>        <plugin>
>             <artifactId>myPlugin</artifactId>
>             <configuration>
>                 <myComponent hint="foo">
>                     ..          <- XML snippet
>                 </myComponent>
>             </configuration>
>        </plugin>
>    </plugins>
>
> Not quite sure, but I think that's a bit more than you wrote, because
> I've got to catch Maven's automatisms at some point.
>
> Any idea for this variant?
>
>
> Thanks,
>
> Jochen
>
>
>
> On Tue, Aug 11, 2009 at 5:15 AM, Jason van Zyl<jv...@sonatype.com>  
> wrote:
>>
>> On 10-Aug-09, at 1:32 PM, Jochen Wiedmann wrote:
>>
>>> Hi,
>>>
>>> in a plugin, I'd like to select a component dynamically. Basically,
>>> I'd like to have something along these lines:
>>>
>>>   <configuration>
>>>     <myComponent hint="foo">
>>>        ...
>>>     </myComponent>
>>>   </configuration>
>>>
>>> The idea is to use the hint for looking up the "myComponent" bean  
>>> and
>>> use the XML fragment between <myComponent> and <myComponent> to
>>> configure the bean.
>>>
>>> Is that possible? Any other ideas how to achieve my goal?
>>>
>>
>> // Lookup you unconfigured component
>> //
>> Foo component = container.lookup( Foo.class, "myComponent"  );
>>
>> // Create a PlexusConfiguration to use against your component and  
>> populate
>> as necessary
>> // by adding elements and attributes. Use autocomplete in your IDE  
>> to find
>> out what's available.
>> PlexusConfiguration configuration = new XmlPlexusConfiguration(
>> "configuration" );
>>
>> // Look up the default component configurator which is used  
>> internally to
>> configure component.
>> //
>> ComponentConfigurator configurator =  
>> container.lookup( Configurator.class );
>>
>> // I assume you're in a plugin so you'll need the classrealm of the  
>> plugin
>> itself
>> // which you can access through the mojoExecution using the $ 
>> {mojoExecution}
>> expression
>> ClassRealm pluginRealm =
>> mojoExecution 
>> .getMojoDescriptor().getPluginDescriptor().getClassRealm();
>>
>> // Use the configurator to configure the component
>> configurator.configureComponent( component, configuration,  
>> pluginRealm );
>>
>> You might want to make your own implementation of the  
>> PlexusConfiguration if
>> you're going to use another source other then XML. Internally in  
>> Plexus the
>> configuration can come from any source but in Maven it's primarily  
>> XML but
>> you're not restricted to XML.
>>
>>> Thanks,
>>>
>>> Jochen
>>>
>>> --
>>> Base64 decoding, 300% faster than sun.misc.BASE64Decoder:
>>> http://archive.netbsd.se/?ml=commons-dev&a=2008-05&t=7522166
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
>>> For additional commands, e-mail: dev-help@maven.apache.org
>>>
>>
>> Thanks,
>>
>> Jason
>>
>> ----------------------------------------------------------
>> Jason van Zyl
>> Founder,  Apache Maven
>> http://twitter.com/jvanzyl
>> http://twitter.com/SonatypeNexus
>> http://twitter.com/SonatypeM2E
>> ----------------------------------------------------------
>>
>> People develop abstractions by generalizing from concrete examples.
>> Every attempt to determine the correct abstraction on paper without
>> actually developing a running system is doomed to failure. No one
>> is that smart. A framework is a resuable design, so you develop it by
>> looking at the things it is supposed to be a design of. The more  
>> examples
>> you look at, the more general your framework will be.
>>
>>  -- Ralph Johnson & Don Roberts, Patterns for Evolving Frameworks
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
>> For additional commands, e-mail: dev-help@maven.apache.org
>>
>>
>
>
>
> -- 
> Base64 decoding, 300% faster than sun.misc.BASE64Decoder:
> http://archive.netbsd.se/?ml=commons-dev&a=2008-05&t=7522166
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org
>

Thanks,

Jason

----------------------------------------------------------
Jason van Zyl
Founder,  Apache Maven
http://twitter.com/jvanzyl
http://twitter.com/SonatypeNexus
http://twitter.com/SonatypeM2E
----------------------------------------------------------

Three may keep a secret if two of them are dead.

  -- Benjamin Franklin


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


Re: Configuring a dynamically selected component

Posted by Jochen Wiedmann <jo...@gmail.com>.
Hi, Jason,

thanks for your reply. That will at least help me to get a working
solution. However, I am not quite sure whether I expressed myself
right. Sorry, if not.

My ideal solution would be to have something like

    <plugins>
        <plugin>
             <artifactId>myPlugin</artifactId>
             <configuration>
                 <myComponent hint="foo">
                     ..          <- XML snippet
                 </myComponent>
             </configuration>
        </plugin>
    </plugins>

Not quite sure, but I think that's a bit more than you wrote, because
I've got to catch Maven's automatisms at some point.

Any idea for this variant?


Thanks,

Jochen



On Tue, Aug 11, 2009 at 5:15 AM, Jason van Zyl<jv...@sonatype.com> wrote:
>
> On 10-Aug-09, at 1:32 PM, Jochen Wiedmann wrote:
>
>> Hi,
>>
>> in a plugin, I'd like to select a component dynamically. Basically,
>> I'd like to have something along these lines:
>>
>>   <configuration>
>>     <myComponent hint="foo">
>>        ...
>>     </myComponent>
>>   </configuration>
>>
>> The idea is to use the hint for looking up the "myComponent" bean and
>> use the XML fragment between <myComponent> and <myComponent> to
>> configure the bean.
>>
>> Is that possible? Any other ideas how to achieve my goal?
>>
>
> // Lookup you unconfigured component
> //
> Foo component = container.lookup( Foo.class, "myComponent"  );
>
> // Create a PlexusConfiguration to use against your component and populate
> as necessary
> // by adding elements and attributes. Use autocomplete in your IDE to find
> out what's available.
> PlexusConfiguration configuration = new XmlPlexusConfiguration(
> "configuration" );
>
> // Look up the default component configurator which is used internally to
> configure component.
> //
> ComponentConfigurator configurator = container.lookup( Configurator.class );
>
> // I assume you're in a plugin so you'll need the classrealm of the plugin
> itself
> // which you can access through the mojoExecution using the ${mojoExecution}
> expression
> ClassRealm pluginRealm =
> mojoExecution.getMojoDescriptor().getPluginDescriptor().getClassRealm();
>
> // Use the configurator to configure the component
> configurator.configureComponent( component, configuration, pluginRealm );
>
> You might want to make your own implementation of the PlexusConfiguration if
> you're going to use another source other then XML. Internally in Plexus the
> configuration can come from any source but in Maven it's primarily XML but
> you're not restricted to XML.
>
>> Thanks,
>>
>> Jochen
>>
>> --
>> Base64 decoding, 300% faster than sun.misc.BASE64Decoder:
>> http://archive.netbsd.se/?ml=commons-dev&a=2008-05&t=7522166
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
>> For additional commands, e-mail: dev-help@maven.apache.org
>>
>
> Thanks,
>
> Jason
>
> ----------------------------------------------------------
> Jason van Zyl
> Founder,  Apache Maven
> http://twitter.com/jvanzyl
> http://twitter.com/SonatypeNexus
> http://twitter.com/SonatypeM2E
> ----------------------------------------------------------
>
> People develop abstractions by generalizing from concrete examples.
> Every attempt to determine the correct abstraction on paper without
> actually developing a running system is doomed to failure. No one
> is that smart. A framework is a resuable design, so you develop it by
> looking at the things it is supposed to be a design of. The more examples
> you look at, the more general your framework will be.
>
>  -- Ralph Johnson & Don Roberts, Patterns for Evolving Frameworks
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org
>
>



-- 
Base64 decoding, 300% faster than sun.misc.BASE64Decoder:
http://archive.netbsd.se/?ml=commons-dev&a=2008-05&t=7522166

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


Re: Configuring a dynamically selected component

Posted by Jason van Zyl <jv...@sonatype.com>.
On 10-Aug-09, at 1:32 PM, Jochen Wiedmann wrote:

> Hi,
>
> in a plugin, I'd like to select a component dynamically. Basically,
> I'd like to have something along these lines:
>
>    <configuration>
>      <myComponent hint="foo">
>         ...
>      </myComponent>
>    </configuration>
>
> The idea is to use the hint for looking up the "myComponent" bean and
> use the XML fragment between <myComponent> and <myComponent> to
> configure the bean.
>
> Is that possible? Any other ideas how to achieve my goal?
>

// Lookup you unconfigured component
//
Foo component = container.lookup( Foo.class, "myComponent"  );

// Create a PlexusConfiguration to use against your component and  
populate as necessary
// by adding elements and attributes. Use autocomplete in your IDE to  
find out what's available.
PlexusConfiguration configuration = new  
XmlPlexusConfiguration( "configuration" );

// Look up the default component configurator which is used internally  
to configure component.
//
ComponentConfigurator configurator =  
container.lookup( Configurator.class );

// I assume you're in a plugin so you'll need the classrealm of the  
plugin itself
// which you can access through the mojoExecution using the $ 
{mojoExecution} expression
ClassRealm pluginRealm =  
mojoExecution.getMojoDescriptor().getPluginDescriptor().getClassRealm();

// Use the configurator to configure the component
configurator.configureComponent( component, configuration,  
pluginRealm );

You might want to make your own implementation of the  
PlexusConfiguration if you're going to use another source other then  
XML. Internally in Plexus the configuration can come from any source  
but in Maven it's primarily XML but you're not restricted to XML.

> Thanks,
>
> Jochen
>
> -- 
> Base64 decoding, 300% faster than sun.misc.BASE64Decoder:
> http://archive.netbsd.se/?ml=commons-dev&a=2008-05&t=7522166
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org
>

Thanks,

Jason

----------------------------------------------------------
Jason van Zyl
Founder,  Apache Maven
http://twitter.com/jvanzyl
http://twitter.com/SonatypeNexus
http://twitter.com/SonatypeM2E
----------------------------------------------------------

People develop abstractions by generalizing from concrete examples.
Every attempt to determine the correct abstraction on paper without
actually developing a running system is doomed to failure. No one
is that smart. A framework is a resuable design, so you develop it by
looking at the things it is supposed to be a design of. The more  
examples
you look at, the more general your framework will be.

   -- Ralph Johnson & Don Roberts, Patterns for Evolving Frameworks


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