cziegeler    2004/04/05 05:05:38

  Modified:    fortress/container-impl/src/java/org/apache/avalon/fortress/util
                        ContextManager.java
               fortress/container-impl/src/java/org/apache/avalon/fortress/impl
                        DefaultECMContainer.java
  Added:       
fortress/container-impl/src/java/org/apache/avalon/fortress/impl/role
                        ECMMetaInfoManager.java
  Log:
  First implementation for selectors
  
  Revision  Changes    Path
  1.55      +6 -2      
avalon-excalibur/fortress/container-impl/src/java/org/apache/avalon/fortress/util/ContextManager.java
  
  Index: ContextManager.java
  ===================================================================
  RCS file: 
/home/cvs/avalon-excalibur/fortress/container-impl/src/java/org/apache/avalon/fortress/util/ContextManager.java,v
  retrieving revision 1.54
  retrieving revision 1.55
  diff -u -r1.54 -r1.55
  --- ContextManager.java       5 Apr 2004 11:07:08 -0000       1.54
  +++ ContextManager.java       5 Apr 2004 12:05:38 -0000       1.55
  @@ -22,6 +22,7 @@
   import org.apache.avalon.fortress.MetaInfoManager;
   import org.apache.avalon.fortress.RoleManager;
   import org.apache.avalon.fortress.impl.role.ConfigurableRoleManager;
  +import org.apache.avalon.fortress.impl.role.ECMMetaInfoManager;
   import org.apache.avalon.fortress.impl.role.ECMRoleManager;
   import org.apache.avalon.fortress.impl.role.FortressRoleManager;
   import org.apache.avalon.fortress.impl.role.Role2MetaInfoManager;
  @@ -670,7 +671,10 @@
               // if we use the ecm role manager then we don't wrap it inside a 
service meta manager!
               if ( roleManager instanceof ECMRoleManager )
               {
  -                mim = new Role2MetaInfoManager( roleManager );               
 
  +                final ECMMetaInfoManager metaManager =
  +                    new ECMMetaInfoManager( new Role2MetaInfoManager( 
roleManager ), classLoader );
  +                metaManager.enableLogging( 
m_loggerManager.getLoggerForCategory( "system.meta" ) );
  +                mim = metaManager;                
               }
               else
               {
  
  
  
  1.5       +128 -60   
avalon-excalibur/fortress/container-impl/src/java/org/apache/avalon/fortress/impl/DefaultECMContainer.java
  
  Index: DefaultECMContainer.java
  ===================================================================
  RCS file: 
/home/cvs/avalon-excalibur/fortress/container-impl/src/java/org/apache/avalon/fortress/impl/DefaultECMContainer.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- DefaultECMContainer.java  5 Apr 2004 11:07:09 -0000       1.4
  +++ DefaultECMContainer.java  5 Apr 2004 12:05:38 -0000       1.5
  @@ -25,6 +25,7 @@
   import org.apache.avalon.fortress.impl.handler.ComponentHandler;
   import org.apache.avalon.fortress.impl.handler.LEAwareComponentHandler;
   import org.apache.avalon.fortress.impl.lookup.FortressServiceSelector;
  +import org.apache.avalon.fortress.impl.role.ECMMetaInfoManager;
   import org.apache.avalon.framework.configuration.Configuration;
   import org.apache.avalon.framework.configuration.ConfigurationException;
   import org.apache.avalon.framework.container.ContainerUtil;
  @@ -133,74 +134,42 @@
           for ( int i = 0; i < elements.length; i++ )
           {
               final Configuration element = elements[i];
  -            String hint = element.getAttribute( "id", null );
  -            if ( null == hint ) {
  -                // Fortress requires a hint, so we just give it one :)
  -                hint = element.getLocation();
  -            }
  -            final String className = getClassname( element );
  -            
  -            final int activation = 
ComponentHandlerMetaData.ACTIVATION_BACKGROUND;
  -            final ComponentHandlerMetaData metaData =
  -                new ComponentHandlerMetaData( hint, className, element, 
activation );
   
               // figure out Role
               String role = getRole( element );
  -            final int pos = role.indexOf('/');
  -            if ( pos != -1 ) {
  -                hint = role.substring(pos+1);
  -                role = role.substring(0, pos);
  +            if ( role.endsWith("Selector") ) 
  +            {
  +                processSelector(role.substring(0, role.length()-8), element 
);
               }
  -            try 
  +            else
               {
  -                final MetaInfoEntry metaEntry = 
m_metaManager.getMetaInfoForClassname( className );
  -                if ( null == metaEntry )
  +            
  +                // get the implementation
  +                final String className = getClassname( element );
  +    
  +                final int pos = role.indexOf('/');
  +                final String hint;
  +                if ( pos != -1 ) 
                   {
  -                    final String message = "No role defined for " + 
className;
  -                    throw new IllegalArgumentException( message );
  -                }
  -
  -                if ( DEFAULT_ENTRY.equals( metaData.getName() ) ||
  -                        SELECTOR_ENTRY.equals( metaData.getName() ) )
  +                    hint = role.substring(pos+1);
  +                    role = role.substring(0, pos);
  +                } 
  +                else
                   {
  -                    throw new IllegalArgumentException( "Using a reserved id 
name" + metaData.getName() );
  +                    hint = null;
                   }
  -
  -                // create a handler for the combo of Role+MetaData
  -                final ComponentHandler handler =
  -                        getComponentHandler( className, 
  -                                             getComponentHandlerClassName( 
className, element), 
  -                                             metaData );
  -
  -                // put the role into our role mapper. If the role doesn't 
exist
  -                // yet, just stuff it in as DEFAULT_ENTRY. If it does, we 
create a
  -                // ServiceSelector and put that in as SELECTOR_ENTRY.
  -                Map hintMap = (Map) m_mapper.get( role );
  -
  -                // Initialize the hintMap if it doesn't exist yet.
  -                if ( null == hintMap )
  +                
  +                final String shortName;
  +                if ( "component".equals( element.getName() )) 
                   {
  -                    hintMap = createHintMap();
  -                    hintMap.put( DEFAULT_ENTRY, handler );
  -                    hintMap.put( SELECTOR_ENTRY,
  -                            new FortressServiceSelector( this, role ) );
  -                    m_mapper.put( role, hintMap );
  +                    shortName = null;
                   }
  -
  -                hintMap.put( hint, handler );
  -
  -                if ( element.getAttributeAsBoolean( "default", false ) )
  +                else
                   {
  -                    hintMap.put( DEFAULT_ENTRY, handler );
  +                    shortName = element.getName();
                   }
  -            } 
  -            catch ( ConfigurationException ce )
  -            {
  -                throw ce;
  -            } 
  -            catch ( Exception e ) 
  -            {
  -                throw new ConfigurationException( "Could not add component", 
e );
  +                
  +                this.addComponent(role, hint, shortName, className, element 
);
               }
               
               if ( getLogger().isDebugEnabled() ) 
  @@ -293,10 +262,10 @@
           return handler;
       }
   
  -    protected Class getComponentHandlerClassName(final String 
defaultClassName, Configuration config ) 
  +    protected Class getComponentHandlerClass(final String defaultClassName, 
final String shortName ) 
           throws Exception
       {
  -        if ( "component".equals( config.getName() ) ) 
  +        if ( shortName == null ) 
           {
               String handlerClassName = null;
               
  @@ -334,12 +303,12 @@
           } 
           else 
           {
  -            final MetaInfoEntry roleEntry = 
m_metaManager.getMetaInfoForShortName( config.getName() );
  +            final MetaInfoEntry roleEntry = 
m_metaManager.getMetaInfoForShortName( shortName );
               if ( null == roleEntry )
               {
                     
                   final String message = "No class found matching 
configuration name " +
  -                        "[name: " + config.getName() + ", location: " + 
config.getLocation() + "]";
  +                        "[name: " + shortName + "]";
                   throw new ConfigurationException( message );
               }
               
  @@ -347,4 +316,103 @@
           }
       }
       
  +    protected void processSelector(String role, Configuration config)
  +        throws ConfigurationException
  +    {
  +        final Configuration[] children = config.getChildren();
  +        if ( children != null ) 
  +        {
  +            for(int i=0; i<children.length; i++)
  +            {
  +                final Configuration element = children[i];       
  +                final String hint = element.getAttribute("name");
  +                final String className = element.getAttribute("class");
  +                
  +                if ( m_metaManager instanceof ECMMetaInfoManager )
  +                {
  +                    try
  +                    {
  +                        
((ECMMetaInfoManager)m_metaManager).addSelectorComponent(role, hint, className, 
getComponentHandlerClass(className, null).getName());
  +                    } 
  +                    catch (ConfigurationException ce )
  +                    {
  +                        throw ce;
  +                    }
  +                    catch (Exception e)
  +                    {
  +                        throw new ConfigurationException("Unable to add 
selector component.", e);
  +                    }
  +                }
  +                addComponent(role, hint, null, className, element );
  +            }
  +        }
  +    }
  +    
  +    protected void addComponent(final String role,
  +                                String hint,
  +                                String shortName,
  +                                final String className,
  +                                final Configuration element)
  +        throws ConfigurationException
  +    {
  +        final int activation = 
ComponentHandlerMetaData.ACTIVATION_BACKGROUND;
  +
  +        // Fortress requires a hint, so we just give it one :) (if missing)
  +        final String metaDataHint = element.getAttribute( "id", 
element.getLocation() );
  +        
  +        if ( hint == null )
  +        {
  +            hint = metaDataHint;
  +        }
  +        
  +        final ComponentHandlerMetaData metaData =
  +            new ComponentHandlerMetaData( metaDataHint, className, element, 
activation );
  +
  +        try 
  +        {
  +
  +            if ( DEFAULT_ENTRY.equals( metaData.getName() ) ||
  +                    SELECTOR_ENTRY.equals( metaData.getName() ) )
  +            {
  +                throw new IllegalArgumentException( "Using a reserved id 
name" + metaData.getName() );
  +            }
  +
  +            // create a handler for the combo of Role+MetaData
  +            final ComponentHandler handler =
  +                    getComponentHandler( className, 
  +                                         getComponentHandlerClass( 
className, shortName), 
  +                                         metaData );
  +
  +            // put the role into our role mapper. If the role doesn't exist
  +            // yet, just stuff it in as DEFAULT_ENTRY. If it does, we create 
a
  +            // ServiceSelector and put that in as SELECTOR_ENTRY.
  +            Map hintMap = (Map) m_mapper.get( role );
  +
  +            // Initialize the hintMap if it doesn't exist yet.
  +            if ( null == hintMap )
  +            {
  +                hintMap = createHintMap();
  +                hintMap.put( DEFAULT_ENTRY, handler );
  +                hintMap.put( SELECTOR_ENTRY,
  +                        new FortressServiceSelector( this, role ) );
  +                m_mapper.put( role, hintMap );
  +            }
  +
  +            hintMap.put( hint, handler );
  +
  +            if ( element.getAttributeAsBoolean( "default", false ) )
  +            {
  +                hintMap.put( DEFAULT_ENTRY, handler );
  +            }
  +        } 
  +        catch ( ConfigurationException ce )
  +        {
  +            throw ce;
  +        } 
  +        catch ( Exception e ) 
  +        {
  +            throw new ConfigurationException( "Could not add component", e );
  +        }
  +    }
  +        
   }
  
  
  
  1.1                  
avalon-excalibur/fortress/container-impl/src/java/org/apache/avalon/fortress/impl/role/ECMMetaInfoManager.java
  
  Index: ECMMetaInfoManager.java
  ===================================================================
  /* 
   * Copyright 2003-2004 The Apache Software Foundation
   * Licensed  under the  Apache License,  Version 2.0  (the "License");
   * you may not use  this file  except in  compliance with the License.
   * You may obtain a copy of the License at 
   * 
   *   http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed  under the  License is distributed on an "AS IS" BASIS,
   * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
   * implied.
   * 
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.avalon.fortress.impl.role;
  
  import java.util.ArrayList;
  import java.util.Properties;
  
  import org.apache.avalon.fortress.MetaInfoEntry;
  import org.apache.avalon.fortress.MetaInfoManager;
  
  /**
   * ECMMetaInfoManager
   *
   * @author <a href="mailto:dev@avalon.apache.org";>Avalon Development Team</a>
   * @version CVS Revision: 1.1 $
   */
  public final class ECMMetaInfoManager extends AbstractMetaInfoManager
  {
  
      /**
       * Create a ECMMetaInfoManager.
       */
      public ECMMetaInfoManager()
      {
          super( (MetaInfoManager) null );
      }
  
      /**
       * Create a ServiceMetaManager with a parent.
       *
       * @param parent
       */
      public ECMMetaInfoManager( final MetaInfoManager parent )
      {
          super( parent );
      }
  
      /**
       * Create a ECMMetaInfoManager with the supplied classloader and
       * parent Rr.
       *
       * @param parent
       * @param loader
       */
      public ECMMetaInfoManager( final MetaInfoManager parent, final 
ClassLoader loader )
      {
          super( parent, loader );
      }
  
      /**
       * Add a component defined inside a selector
       */
      public final void addSelectorComponent( final String role,
                                              final String hint,
                                              final String className,
                                              final String handlerClassName )
      {
          Properties props = new Properties();
          final String lifestyle;
          if (MetaInfoEntry.THREADSAFE_HANDLER.equals(handlerClassName) )
          {
              lifestyle = "singleton";
          }
          else if ( MetaInfoEntry.PER_THREAD_HANDLER.equals(handlerClassName) )
          {
              lifestyle = "thread";
          }   
          else if ( MetaInfoEntry.POOLABLE_HANDLER.equals(handlerClassName) )
          {
              lifestyle = "pooled";
          }   
          else
          {
              lifestyle = "transient";
          }
          
          props.setProperty("x-avalon.lifestyle", lifestyle); 
          props.setProperty("x-avalon.name", role + '/' + hint);
          super.addComponent(role, className, props, new ArrayList());
          
      }
  }
  
  
  

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

Reply via email to