Coverage Report - org.codehaus.plexus.container.initialization.InitializeComponentRegistryPhase
 
Classes in this File Line Coverage Branch Coverage Complexity
InitializeComponentRegistryPhase
83 %
20/24
100 %
2/2
2,667
 
 1  
 package org.codehaus.plexus.container.initialization;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2006 Codehaus Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *      http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import org.codehaus.plexus.component.repository.ComponentRepository;
 20  
 import org.codehaus.plexus.component.repository.ComponentDescriptor;
 21  
 import org.codehaus.plexus.component.repository.io.PlexusTools;
 22  
 import org.codehaus.plexus.component.repository.exception.ComponentRepositoryException;
 23  
 import org.codehaus.plexus.component.composition.CycleDetectedInComponentGraphException;
 24  
 import org.codehaus.plexus.component.manager.PerLookupComponentManagerFactory;
 25  
 import org.codehaus.plexus.component.manager.SingletonComponentManagerFactory;
 26  
 import org.codehaus.plexus.configuration.PlexusConfiguration;
 27  
 import org.codehaus.plexus.configuration.PlexusConfigurationException;
 28  
 import org.codehaus.plexus.DefaultComponentRegistry;
 29  
 import org.codehaus.plexus.ComponentRegistry;
 30  
 import org.codehaus.plexus.lifecycle.LifecycleHandlerManager;
 31  
 
 32  
 /**
 33  
  * @author Jason van Zyl
 34  
  */
 35  61
 public class InitializeComponentRegistryPhase 
 36  
     implements ContainerInitializationPhase
 37  
 {
 38  
     public void execute( ContainerInitializationContext context )
 39  
         throws ContainerInitializationException
 40  
     {
 41  61
         ComponentRepository repository = getComponentRepository( context );
 42  
 
 43  61
         LifecycleHandlerManager lifecycleHandlerManager = getLifecycleHandlerManager( context );
 44  
 
 45  61
         ComponentRegistry componentRegistry = new DefaultComponentRegistry( context.getContainer(),
 46  
             repository,
 47  
             lifecycleHandlerManager );
 48  
 
 49  61
         componentRegistry.registerComponentManagerFactory( new PerLookupComponentManagerFactory() );
 50  
 
 51  61
         componentRegistry.registerComponentManagerFactory( new SingletonComponentManagerFactory() );
 52  
 
 53  61
         context.getContainer().setComponentRegistry( componentRegistry );
 54  61
     }
 55  
 
 56  
     private ComponentRepository getComponentRepository( ContainerInitializationContext context )
 57  
         throws ContainerInitializationException
 58  
     {
 59  61
         ComponentRepository repository = context.getContainerConfiguration().getComponentRepository();
 60  
 
 61  
         // Add the components defined in the container xml configuration
 62  
         try
 63  
         {
 64  61
             PlexusConfiguration configuration = context.getContainerXmlConfiguration();
 65  
 
 66  61
             PlexusConfiguration[] componentConfigurations = configuration.getChild( "components" ).getChildren( "component" );
 67  1314
             for ( PlexusConfiguration componentConfiguration : componentConfigurations )
 68  
             {
 69  1253
                 ComponentDescriptor<?> componentDescriptor = PlexusTools.buildComponentDescriptor( componentConfiguration, context.getContainer().getContainerRealm() );
 70  1253
                 componentDescriptor.setRealm( context.getContainer().getContainerRealm() );
 71  1253
                 repository.addComponentDescriptor( componentDescriptor );
 72  
             }
 73  
         }
 74  0
         catch ( PlexusConfigurationException e )
 75  
         {
 76  0
             throw new ContainerInitializationException( "Error initializing component repository: " + "Cannot unmarshall component descriptor: ", e );
 77  
         }
 78  0
         catch ( CycleDetectedInComponentGraphException e )
 79  
         {
 80  0
             throw new ContainerInitializationException( "A cycle has been detected in the components of the system: ", e );
 81  61
         }
 82  
         
 83  61
         return repository;
 84  
     }
 85  
 
 86  
     private LifecycleHandlerManager getLifecycleHandlerManager( ContainerInitializationContext context )
 87  
     {
 88  61
         LifecycleHandlerManager lifecycleHandlerManager = context.getContainerConfiguration().getLifecycleHandlerManager();
 89  61
         lifecycleHandlerManager.initialize();
 90  61
         return lifecycleHandlerManager;
 91  
     }
 92  
 }