be.redlab.context.spring.integration.SpringContextAwareApplicationContext.java Source code

Java tutorial

Introduction

Here is the source code for be.redlab.context.spring.integration.SpringContextAwareApplicationContext.java

Source

/*
 * Copyright 2012 Balder Van Camp
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package be.redlab.context.spring.integration;

import java.util.Collection;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import be.redlab.context.BeanApplicationContext;

/**
 * This application context extends from the normal {@link BeanApplicationContext} and can be made aware of a Spring
 * {@link ApplicationContext}. The find and get methods will first look in the BeanApplicationContext where this one
 * extends from and if nothing found it will look for the requested thing in the Spring ApplicationContext.
 *
 * @author redlab
 *
 */
public class SpringContextAwareApplicationContext extends BeanApplicationContext
        implements ApplicationContextAware {
    private ApplicationContext springCtx;

    /*
     * (non-Javadoc)
     *
     * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.
     * ApplicationContext)
     */
    @Override
    public void setApplicationContext(final ApplicationContext springCtx) throws BeansException {
        this.springCtx = springCtx;
    }

    /*
     * (non-Javadoc)
     *
     * @see be.redlab.context.BeanApplicationContext#find(java.lang.Class)
     */
    @Override
    public <T> T find(final Class<T> klass) {
        T find = super.find(klass);
        if (null == find) {
            find = springCtx.getBean(klass);
        }
        return find;
    }

    /*
     * (non-Javadoc)
     *
     * @see be.redlab.context.BeanApplicationContext#findAll(java.lang.Class)
     */
    @Override
    public <T> Collection<T> findAll(final Class<T> klass) {
        Collection<T> find = super.findAll(klass);
        if (null == find) {
            Map<String, T> beansOfType = springCtx.getBeansOfType(klass);
            if (!beansOfType.isEmpty()) {
            } else {
                find = beansOfType.values();
            }
        }
        return find;
    }

    /*
     * (non-Javadoc)
     *
     * @see be.redlab.context.BeanApplicationContext#get(java.lang.String)
     */
    @Override
    public <T> T get(final String name) {
        T get = super.get(name);
        if (null == get) {
            Object bean = springCtx.getBean(name);
            try {
                get = (T) bean;
            } catch (ClassCastException e) {
            }
        }
        return get;
    }

    /*
     * (non-Javadoc)
     *
     * @see be.redlab.context.BeanApplicationContext#hasBean(java.lang.String)
     */
    @Override
    public boolean hasBean(final String name) {
        boolean has = super.hasBean(name);
        if (!has) {
            has = springCtx.containsBean(name);
        }
        return has;
    }

}