View Javadoc

1   package org.apache.maven.continuum.management.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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       * Logger used by this class.
48       */
49      protected Logger logger = LoggerFactory.getLogger( getClass() );
50  
51      private PlexusLifecycleBeanPostProcessor lifecycleBeanPostProcessor;
52  
53      /**
54       * @see org.springframework.context.support.AbstractXmlApplicationContext#loadBeanDefinitions(org.springframework.beans.factory.xml.XmlBeanDefinitionReader)
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       * @see org.springframework.context.support.AbstractApplicationContext#postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)
65       */
66      protected void postProcessBeanFactory( ConfigurableListableBeanFactory beanFactory, ApplicationContext context )
67      {
68          // Register a PlexusContainerAdapter bean to allow context lookups using plexus API
69          PlexusContainerAdapter plexus = new PlexusContainerAdapter();
70          plexus.setApplicationContext( context );
71          beanFactory.registerSingleton( "plexusContainer", plexus );
72  
73          // Register a beanPostProcessor to handle plexus interface-based lifecycle management
74          lifecycleBeanPostProcessor = new PlexusLifecycleBeanPostProcessor();
75          lifecycleBeanPostProcessor.setBeanFactory( context );
76          beanFactory.addBeanPostProcessor( lifecycleBeanPostProcessor );
77  
78          // Register a PropertyEditor to support plexus XML <configuration> set as CDATA in
79          // a spring context XML file.
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       * @see org.springframework.context.support.AbstractApplicationContext#doClose()
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 }