Details
- 
        Type: Bug Bug
- 
        Status: Closed Closed
- 
            Priority: Major Major
- 
            Resolution: Won't Fix
- 
            Affects Version/s: 1.0-beta-5
- 
            Fix Version/s: 1.0-RC-1
- 
            Component/s: None
- 
            Labels:None
- 
                        Number of attachments :
Description
It is an extremely common pattern in Pico applications to register listeners in the constructor. It is therefor crucial that start() instantiates all components in the container. This pattern is even used in the demo we did at JavaPolis.
The following code should outline the pattern:
public interface SocketConnector
{
  void addSocketListener(SocketListener listener);
}
public interface SocketListener extends java.util.EventListener
{
  void socketRequest(Socket socket);
}
public class SocketServer implements SocketConnector, Lifecycle
{
  public void start() 
  // ...
}
public class HttpServer implements SocketListener
{
  public HttpServer(SocketConnector connector) 
public void socketRequest(Socket socket)
{ // implement http }}
MutablePicoContainer pico = new DefaultPicoContainer();
pico.registerComponentImplementation(SocketServer.class);
pico.registerComponentImplementation(HttpServer.class);
pico.start();
If not both the SocketServer and the HttpServer is instantiated in start() the server will simply not function. The server will listen to requests on the socket but noone will process them. A catastrophy!
Issue Links
- relates to
- 
             PICO-120
        Default lifecycle should be a little more intelligent PICO-120
        Default lifecycle should be a little more intelligent
-         
Activity
 Jon Tirsen
 made changes  -
 Jon Tirsen
 made changes  - 
        | Field | Original Value | New Value | 
|---|---|---|
| Fix Version/s | 1.0-beta-5 [ 10145 ] | 
 Aslak Hellesøy
 made changes  -
 Aslak Hellesøy
 made changes  - 
        | Fix Version/s | 1.0-beta-5 [ 10145 ] | |
| Fix Version/s | 1.0-RC-1 [ 10461 ] | |
| Affects Version/s | 1.0-beta-5 [ 10145 ] | 
 Aslak Hellesøy
 made changes  -
 Aslak Hellesøy
 made changes  - 
        | Status | Open [ 1 ] | Closed [ 6 ] | 
| Resolution | Won't Fix [ 2 ] | |
| Assignee | Aslak Hellesoy [ rinkrank ] | 
 Jörg Schaible
 made changes  -
 Jörg Schaible
 made changes  - 
        

Even if this is an "extremely common" pattern (which I doubt), it isn't necessarily a good pattern.
Making the classes that register themselves as a listener Startable solves this problem. (addListener(this) in start, removeListener(this) in stop()).
See http://lists.codehaus.org/pipermail/picocontainer-dev/2004-February/002738.html