1   package org.apache.maven.continuum.management.util;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  import org.codehaus.plexus.spring.PlexusBeanDefinitionDocumentReader;
23  import org.codehaus.plexus.spring.PlexusConfigurationPropertyEditor;
24  import org.codehaus.plexus.spring.PlexusContainerAdapter;
25  import org.codehaus.plexus.spring.PlexusLifecycleBeanPostProcessor;
26  import org.codehaus.plexus.spring.editors.CollectionPropertyEditor;
27  import org.codehaus.plexus.spring.editors.MapPropertyEditor;
28  import org.codehaus.plexus.spring.editors.PropertiesPropertyEditor;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  import org.springframework.beans.BeansException;
32  import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
33  import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
34  import org.springframework.context.ApplicationContext;
35  
36  import java.io.IOException;
37  import java.util.ArrayList;
38  import java.util.HashMap;
39  import java.util.HashSet;
40  import java.util.List;
41  import java.util.Map;
42  import java.util.Set;
43  
44  public class PlexusApplicationContextDelegate
45  {
46      
47  
48  
49      protected Logger logger = LoggerFactory.getLogger( getClass() );
50  
51      private PlexusLifecycleBeanPostProcessor lifecycleBeanPostProcessor;
52  
53      
54  
55  
56      protected void loadBeanDefinitions( XmlBeanDefinitionReader reader )
57          throws BeansException, IOException
58      {
59          logger.info( "Registering Plexus to Spring XML translation" );
60          reader.setDocumentReaderClass( PlexusBeanDefinitionDocumentReader.class );
61      }
62  
63      
64  
65  
66      protected void postProcessBeanFactory( ConfigurableListableBeanFactory beanFactory, ApplicationContext context )
67      {
68          
69          PlexusContainerAdapter plexus = new PlexusContainerAdapter();
70          plexus.setApplicationContext( context );
71          beanFactory.registerSingleton( "plexusContainer", plexus );
72  
73          
74          lifecycleBeanPostProcessor = new PlexusLifecycleBeanPostProcessor();
75          lifecycleBeanPostProcessor.setBeanFactory( context );
76          beanFactory.addBeanPostProcessor( lifecycleBeanPostProcessor );
77  
78          
79          
80          beanFactory.addPropertyEditorRegistrar( new PlexusConfigurationPropertyEditor() );
81          beanFactory.addPropertyEditorRegistrar( new PropertiesPropertyEditor() );
82          beanFactory.addPropertyEditorRegistrar( new CollectionPropertyEditor( List.class, ArrayList.class ) );
83          beanFactory.addPropertyEditorRegistrar( new CollectionPropertyEditor( Set.class, HashSet.class ) );
84          beanFactory.addPropertyEditorRegistrar( new MapPropertyEditor( Map.class, HashMap.class ) );
85      }
86  
87      
88  
89  
90      protected void doClose()
91      {
92          try
93          {
94              lifecycleBeanPostProcessor.destroy();
95          }
96          catch ( Throwable ex )
97          {
98              logger.error( "Exception thrown from PlexusLifecycleBeanPostProcessor handling ContextClosedEvent", ex );
99          }
100     }
101 }