1 package org.codehaus.plexus.spring;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
28 import java.util.StringTokenizer;
30 import org.apache.commons.lang.ClassUtils;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.codehaus.plexus.PlexusConstants;
34 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Disposable;
35 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
36 import org.springframework.beans.BeansException;
37 import org.springframework.beans.factory.ListableBeanFactory;
38 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
39 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
40 import org.springframework.context.ApplicationContext;
43 * Utility method to convert plexus descriptors to spring bean context.
45 * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
47 public class PlexusApplicationContextDelegate
49 /** Logger used by this class. */
50 protected final Log logger = LogFactory.getLog(getClass());
52 private PlexusLifecycleBeanPostProcessor lifecycleBeanPostProcessor;
55 * @see org.springframework.context.support.AbstractXmlApplicationContext#loadBeanDefinitions(org.springframework.beans.factory.xml.XmlBeanDefinitionReader)
57 protected void loadBeanDefinitions( XmlBeanDefinitionReader reader )
58 throws BeansException, IOException
60 logger.info( "Registering plexus to spring XML translation" );
61 reader.setDocumentReaderClass( PlexusBeanDefinitionDocumentReader.class );
62 reader.setValidationMode( XmlBeanDefinitionReader.VALIDATION_NONE );
66 * @see org.springframework.context.support.AbstractApplicationContext#postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)
68 protected void postProcessBeanFactory( ConfigurableListableBeanFactory beanFactory, ApplicationContext context )
70 // Register a PlexusContainerAdapter bean to allow context lookups using plexus API
71 PlexusContainerAdapter plexus = new PlexusContainerAdapter();
72 plexus.setApplicationContext( context );
73 beanFactory.registerSingleton( "plexusContainer", plexus );
75 // Register a beanPostProcessor to handle plexus interface-based lifecycle management
76 lifecycleBeanPostProcessor = new PlexusLifecycleBeanPostProcessor();
77 lifecycleBeanPostProcessor.setBeanFactory( context );
78 beanFactory.addBeanPostProcessor( lifecycleBeanPostProcessor );
80 // Register a PorpertyEditor to support plexus XML <configuration> set as CDATA in
81 // a spring context XML file.
82 beanFactory.addPropertyEditorRegistrar( new PlexusConfigurationPropertyEditor() );
83 beanFactory.addPropertyEditorRegistrar( new PropertiesPropertyEditor() );
87 * @see org.springframework.context.support.AbstractApplicationContext#doClose()
89 protected void doClose()
93 lifecycleBeanPostProcessor.destroy();
95 catch ( Throwable ex )
97 logger.error( "Exception thrown from PlexusLifecycleBeanPostProcessor handling ContextClosedEvent", ex );