All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.neba.core.util.OsgiBeanSource Maven / Gradle / Ivy

Go to download

Contains the entire NEBA core implementation, i.e. the framework that interprets the NEBA API annotations and provides implementations for the service and lifecycle callback interfaces provided in the NEBA API. This package must not export anything as its implementation details are entirely private.

There is a newer version: 5.2.3
Show newest version
/**
 * Copyright 2013 the original author or authors.
 * 
 * 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 io.neba.core.util;

import org.apache.commons.lang.builder.HashCodeBuilder;
import org.osgi.framework.Bundle;
import org.springframework.beans.factory.BeanFactory;

import static org.osgi.framework.Bundle.ACTIVE;

/**
 * A source for beans in an {@link org.springframework.context.ApplicationContext} obtained
 * from a {@link org.osgi.framework.Bundle}.
 * @param  The bean's type.
 * 
 * @author Olaf Otto
 */
public class OsgiBeanSource {
	private final String beanName;
	private final BeanFactory factory;
	private final long bundleId;
    private final int hashCode;
    private final Class beanType;
    private final Bundle bundle;

    /**
     * @param beanName must not be null.
     * @param factory must not be null.
     */
    public OsgiBeanSource(String beanName, BeanFactory factory, Bundle bundle) {
        if (beanName == null) {
            throw new IllegalArgumentException("Method argument beanName must not be null.");
        }
        if (factory == null) {
            throw new IllegalArgumentException("Method argument factory must not be null.");
        }
        if (bundle == null) {
            throw new IllegalArgumentException("Method argument bundle must not be null.");
        }

        this.beanName = beanName;
		this.factory = factory;
		this.bundleId = bundle.getBundleId();
        this.bundle = bundle;
        // Referencing the bean type is safe: It either stems from the source bundle, or a bundle the source bundle depends on
        // via an import-package relationship. Thus, if the type changes, the source bundle is re-loaded as well thus
        // causing this bean source to be re-created.
        this.beanType = factory.getType(beanName);
		this.hashCode = new HashCodeBuilder().append(beanName).append(bundleId).toHashCode();
	}
	
	@SuppressWarnings("unchecked")
    public T getBean() {
		return (T) this.factory.getBean(this.getBeanName());
	}
	
	public Class getBeanType() {
		return this.beanType;
	}

	public long getBundleId() {
		return this.bundleId;
	}

	public BeanFactory getFactory() {
		return this.factory;
	}

    public String getBeanName() {
        return beanName;
    }

    /**
     * @return whether this reference points to an active bundle.
     */
    public boolean isValid() {
        return this.bundle.getState() == ACTIVE;
    }

	@Override
	public String toString() {
        return "Bean " + '"' + this.getBeanName() + '"' + " from bundle with id " + this.bundleId;
	}
	
	@Override
	public int hashCode() {
	    return this.hashCode;
	}
	
	@Override
	public boolean equals(Object obj) {
	    if (obj == this) {
	        return true;
	    }
	    if (obj == null || obj.getClass() != getClass()) {
	        return false;
	    }
	    
        OsgiBeanSource other = (OsgiBeanSource) obj;

        // Why are bundleId and beanName compared, but not simply the bean type?
        // Theoretically, it is possible to register the same type in two different OSGi bundles
        return this.bundleId == other.bundleId && 
               this.beanName.equals(other.beanName);
	}

    /**
     * @return never null.
     */
    public Bundle getBundle() {
        return this.bundle;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy