avalon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Leo Sutic" <leo.su...@inspireinfrastructure.com>
Subject RE: Son of ComponentManager
Date Sat, 16 Feb 2002 14:01:43 GMT
Right - since the object you will lookup do not:

  - Implement any Avalon lifestyle interfaces
  - Are all threadsafe
  - Will probably be added to the CM via addComponent calls

Here - patchy, but here *today* and does *not* break compatibility:

package packagename;

import org.apache.avalon.framework.component.Component;
import org.apache.avalon.framework.component.ComponentException;

public interface ObjectManager implements Component {
  public static void ROLE = "packagename.ObjectManager";
  public Object lookup (String role) throws ComponentException;
}

----------------------------

public class DefaultObjectManager implements ObjectManager, Composable,
ThreadSafe {

  private ComponentManager manager = null;

  public void compose (ComponentManager manager) {
    this.manager = manager;
  }

  public Object lookup (String role) throws ComponentException {
    Component maybeWrapper = manager.lookup (role);
    if (maybeWrapper instanceof ComponentWrapper) {
      return ((ComponentWrapper) maybeWrapper).getObject ();
    } else {
      return maybeWrapper;
    }
  }
}

----------------------------

public class ComponentWrapper implements Component, Configurable, ThreadSafe
{

   private void Object object;

   /**
    * When using ComponentManager.addComponent
    */
   public ComponentWrapper (Object object) {
     this.object = object;
   }

   /**
    * When using dynamic config. Useful?
    */
   public void configure (Configuration config) throws
ConfigurationException {
     try {
       String wrappedClassName = config.getAttribute ("wrapped-class");
       // ... make this.object an instance of wrappedClassName
     } catch (Exception e) {
       throw new ConfigurationException ("Could not instantiate wrapped
class.");
     }
   }

   public Object getObject () {
     return object;
   }
}

----------------------------------------

Use the above like this:

public class MyComponent {
  ObjectManager om = null;

  public void compose (ComponentManager cm) throws ComponentException {
    om = (ObjectManager) cm.lookup (ObjectManager.ROLE);
  }
  ...
}

And then you can use om.lookup for objects and components.

Does this solve your immediate needs? That is, getting something working
NOW,
with not too much crap for future cleanups? Getting the next version may not
be possible for a while, maybe by summer.

(Summer of CM, of course.)

/LS


--
To unsubscribe, e-mail:   <mailto:avalon-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-dev-help@jakarta.apache.org>


Mime
View raw message