avalon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Leo Simons <leosim...@apache.org>
Subject [not-RT] how to embed pico components inside an avalon container...
Date Tue, 05 Aug 2003 19:09:11 GMT
...in 5 minutes, without needing pico. It is almost a shame I wrote 
this, because it means we might not be using pico as the avalon core 
after all, but it's useful in the meantime.

Putting my code where my mouth is
Leo Simons wrote:
> I must tell you, I strongly 
> feel like moving away from the avalon lifecycle at this point. I also 
> have an idea how to do that in a non-radical way.

Huh-hum. It took longer than expected (an hour and a bit). I just wrote 
a single class (though I haven't tested it yet, I'm guessing it will 
work) that uses JDK 1.3+ dynamic proxies to support the deployment of 
any pico-style component into an existing avalon container. See attachment.

I estimate the effort needed to fully support deployment of any 
pico-compatible component into any avalon-container, *fully* 
transparently, is about half an hour per existing avalon container, 
maybe less.

 From the javadoc:

// write your pico component
public class MyComponentImpl implements MyComponent
      Logger m_logger;
      Configuration m_config;
      MyOtherComponent m_moc;

      public MyComponentImpl( Logger logger, Configuration config,
              MyOtherComponent moc )
          m_logger = logger;
          m_configuration = config;
          m_moc = moc;
      public void doStuff() {

// and then somewhere....
MyComponent comp = (MyComponent)
          AvalonInvocationHandler.getProxy( MyComponentImpl.class );

// the container will set up your logger, configuration and
// all other dependencies for you
myAvalonContainer.add( comp );

// you can only use methods specified by interfaces here, but
// that's already true for all avalon components

How it works
The getProxy() method creates a dynamic proxy around the provided class 
that intercepts calls to enableLogging(), configure(), etc etc. Once 
initialize() is called, it actually creates the component instance, 
calls any avalon-framework lifecycle methods that the component _does_ 
implement, and from then on just routes all method calls to the created 
instance. Bam. Slam. Dunk. That's it.

Of course, a setup like this is hideously twisted and completely the 
'wrong' way around, but I am confident in predicting it'll be immensely 
popular if we implement it ;)

The near future
Based on the code in the com.leosimons.jicarilla.container.factories 
package, it should be relatively straightforward to not only support 
pico-style components, but also Xwork and any number of other IoC-style 
lifecycle setups.

Who was it that said he never learned anything around here? ;)


- Leo

PS: yes, I'll contribute the class under ASF license if someone else 
volunteers to write unit tests and fix bugs :D

View raw message