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