Skip to end of metadata
Go to start of metadata

PicoContainer provides a mechanism for developers to be informed of key events in the lifecytcle of components. If you pass in a ComponentMonitor DefaultPicoContainer as you instantiate it, you can be kept informed of these events:

ComponentMonitor implementations have six methods. From its interface:

As components are instantiated, or methods on them are invoked (with some limits), the methods on the CM implementation are invoked as appropriate.

Instantiation

If a component is instantiated, by one of the default ComponentAdapters, there be two calls to methods in CM. The first to 'instantiating' just before the instantiation of the
component, the second to either 'instantiated' or 'instantiationFailed' as appropriate after the attempted instantiation.

Invocation

Where the ComponentAdapter in use can intercept arbitrary method invocations for a component, calls to the CM implementation will happen before component method invocation ('invoking') and after ('inkoked' or 'invocationFailed').

There are some limitations to this:

One limitation is the ComponentAdapter, as mentioned, has to be able to intercept method invocations. ImplementationHidingComponentAdapter is one such impl, but it is only able to intercept methods declared on the public interfaces it is exposing via reflection.

The second limitation is that the methods invocations being monitored are only thos invoked from outside of the component from one of the other components depending on it. This may well be monitored:

Whereas, this may not:

ComponentMonitor Implementations.

There are a number of implementations of CM that are supplied by the PicoContainer team:

CommonsLoggingComponentMontor - Logs instantiations and invocations to a Log, implemented by Apache's Commons-Logging framework (in the PicoContainer gems jar)
Log4JComponentMontor - Logs instantiations and invocations to a Log, implemented by Apache's Log4J framework (also in the PicoContainer gems jar)
ConsoleLoggingComponentMonitor - Logs to the console. (Default is System.out)
NullComponentMonitor - Does nothing. As per NullObject pattern (also called Special Case), and the default for DefaultPicoContainer.
WriterComponentMonitor - Writes to a java.io.Writer object. (Also can chain with other monitors)
DotDependencyGraphComponentMonitor - Creates GraphViz Dot graph output for instantiated component dependencies

Chaining

Some CM implementations are chainable. That is, you can get one impl to invoke the same method in another, subject to filtering or modification.

Implementations yet to be written

RegexFilterComponentMonitor - one that filters instantiations and invocations according to one or more reular expression.
PasswordHidingComponentMonitor - one that for methods called 'login' obscures the second string parameter before passing on the invocation. This means that passwords do not appear in log files (amongst other things).

For More Information

There are several articles on the Internet that provide a good background to the concept of PicoContainer monitors:

Avalon No Logging  By Paul Hammant

Logging, Just Say No By Paul Hammant 

IoC Taken Too Far By Mike Mason 

Labels
  • None