001    /*******************************************************************************
002     * Portions created by Sebastian Thomschke are copyright (c) 2005-2013 Sebastian
003     * Thomschke.
004     *
005     * All Rights Reserved. This program and the accompanying materials
006     * are made available under the terms of the Eclipse Public License v1.0
007     * which accompanies this distribution, and is available at
008     * http://www.eclipse.org/legal/epl-v10.html
009     *
010     * Contributors:
011     *     Sebastian Thomschke - initial implementation.
012     *******************************************************************************/
013    package net.sf.oval.integration.spring;
014    
015    import net.sf.oval.internal.Log;
016    
017    import org.springframework.beans.factory.annotation.Autowired;
018    import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
019    import org.springframework.stereotype.Component;
020    import org.springframework.util.Assert;
021    
022    /**
023     * Injects spring beans into unmanaged Java objects having {@link org.springframework.beans.factory.annotation.Autowired},
024     * {@link org.springframework.beans.factory.annotation.Value} and {@link javax.inject.Inject} annotations.
025     *
026     * <pre>
027     * &lt;bean class="net.sf.oval.integration.spring.SpringInjector" /&gt;
028     * </pre>
029     *
030     * or
031     *
032     * <pre>
033     * &lt;context:component-scan base-package="net.sf.oval.integration.spring" /&gt;
034     * </pre>
035     *
036     * @author Sebastian Thomschke
037     */
038    @Component
039    public class SpringInjector
040    {
041            private static final Log LOG = Log.getLog(SpringInjector.class);
042    
043            private static SpringInjector INSTANCE;
044    
045            public static SpringInjector get()
046            {
047                    Assert.notNull(INSTANCE, "No SpringInjector instance created yet. Add  <bean class=\"" + SpringInjector.class.getName()
048                                    + "\" /> to your spring configuration!");
049    
050                    return INSTANCE;
051            }
052    
053            @Autowired
054            private AutowiredAnnotationBeanPostProcessor processor;
055    
056            private SpringInjector()
057            {
058                    LOG.info("Instantiated.");
059    
060                    INSTANCE = this;
061            }
062    
063            public void inject(final Object unmanagedBean)
064            {
065                    processor.processInjection(unmanagedBean);
066            }
067    }