Ryan Shaw wrote:
> Paulo wrote:
> 
> |||       /** Releases all not-yet-released components.  */
> |||       public void releaseAll();
> 
> actually, isn't this A Bad Thing?
> 
> if something like releaseAll() were public, any 
> Composable could release all the Components being
> managed.


That is why releaseAll should have a Token.  That Token scopes the components
that are released.

Granted, I would rather have the release mechanism on the Token object,
that way there is no chance of a BadTokenException to be thrown, or possible
resource leakage by managing several tokens.

Granted, the Token approach *can* be applied regardless of what the manager
interface is (as long as the manager has a release(Object) method).  Example:

public interface ServiceManager
{
     Object lookup(Object key); // can be Query or String based role
     boolean hasResource(Object key); // can be Query or String based role
     void release(Object resource);
}

public final class Token
{
     private final ServiceManager m_manager;
     private final Buffer         m_resources;

     public Token( ServiceManager manager )
     {
         m_manager = manager;
         m_resources = new VariableSizedBuffer();
     }

     public void register(Object resource)
     {
         m_resources.add( resource );
     }

     public void releaseAll()
     {
         int size = m_resources.size();

         for (int i = 0; i < size; i++)
         {
             m_manager.release( m_resources.remove() );
         }
     }
}


The usage would be something like this:

class Example extends Servicable
{
      ServiceManager m_manager;
      Token          m_token;

      void service( ServiceManager manager )
      {
          m_manager = manager;
          m_token = new Token( manager );
      }

      void criticalSection()
      {
          Parser parser = (Parser) m_manager.lookup(Parser.ROLE);
          m_token.register( parser );

          Transformer transformer = (Transformer) m_manager.lookup(Transformer.ROLE);
          m_token.register( transformer );

          // do the critical stuff

          m_token.releaseAll();
      }
}


As you can see, we can still have a Token approach--regardless of what the
interface gives us.  While I would rather have the Manager initialize the
Token with the ServiceManager and the associated services (only exposing a
public releaseAll() method)--it can be done nonetheless.

-- 

"They that give up essential liberty to obtain a little temporary safety
  deserve neither liberty nor safety."
                 - Benjamin Franklin


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to