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 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      /** Logger used by this class. */
47      protected Logger logger = LoggerFactory.getLogger( getClass() );
48  
49      private PlexusLifecycleBeanPostProcessor lifecycleBeanPostProcessor;
50  
51      /**
52       * @see org.springframework.context.support.AbstractXmlApplicationContext#loadBeanDefinitions(org.springframework.beans.factory.xml.XmlBeanDefinitionReader)
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       * @see org.springframework.context.support.AbstractApplicationContext#postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)
63       */
64      protected void postProcessBeanFactory( ConfigurableListableBeanFactory beanFactory, ApplicationContext context )
65      {
66          // Register a PlexusContainerAdapter bean to allow context lookups using plexus API
67          PlexusContainerAdapter plexus = new PlexusContainerAdapter();
68          plexus.setApplicationContext( context );
69          beanFactory.registerSingleton( "plexusContainer", plexus );
70  
71          // Register a beanPostProcessor to handle plexus interface-based lifecycle management
72          lifecycleBeanPostProcessor = new PlexusLifecycleBeanPostProcessor();
73          lifecycleBeanPostProcessor.setBeanFactory( context );
74          beanFactory.addBeanPostProcessor( lifecycleBeanPostProcessor );
75  
76          // Register a PropertyEditor to support plexus XML <configuration> set as CDATA in
77          // a spring context XML file.
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       * @see org.springframework.context.support.AbstractApplicationContext#doClose()
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  }