Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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 org.jboss.weld.bean.builtin;
import static org.jboss.weld.util.reflection.Reflections.cast;
import java.io.ObjectInputStream;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Iterator;
import java.util.Set;
import javax.enterprise.context.spi.AlterableContext;
import javax.enterprise.context.spi.Context;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.util.TypeLiteral;
import org.jboss.weld.bean.proxy.ProxyMethodHandler;
import org.jboss.weld.bean.proxy.ProxyObject;
import org.jboss.weld.context.WeldCreationalContext;
import org.jboss.weld.exceptions.InvalidObjectException;
import org.jboss.weld.injection.CurrentInjectionPoint;
import org.jboss.weld.logging.BeanLogger;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.resolution.Resolvable;
import org.jboss.weld.resolution.ResolvableBuilder;
import org.jboss.weld.util.reflection.Formats;
import org.jboss.weld.util.reflection.Reflections;
import com.google.common.base.Preconditions;
/**
* Helper implementation for Instance for getting instances
*
* @param
* @author Gavin King
*/
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = { "SE_NO_SUITABLE_CONSTRUCTOR", "SE_BAD_FIELD" }, justification = "Uses SerializationProxy")
public class InstanceImpl extends AbstractFacade> implements Instance, Serializable {
private static final long serialVersionUID = -376721889693284887L;
public static Instance of(InjectionPoint injectionPoint, CreationalContext creationalContext,
BeanManagerImpl beanManager) {
return new InstanceImpl(injectionPoint, creationalContext, beanManager);
}
private InstanceImpl(InjectionPoint injectionPoint, CreationalContext super T> creationalContext,
BeanManagerImpl beanManager) {
super(injectionPoint, creationalContext, beanManager);
}
public T get() {
Resolvable resolvable = new ResolvableBuilder(getType(), getBeanManager()).addQualifiers(getQualifiers())
.setDeclaringBean(getInjectionPoint().getBean()).create();
Bean> bean = getBeanManager().getBean(resolvable);
return getBeanInstance(bean);
}
private T getBeanInstance(Bean> bean) {
// Generate a correct injection point for the bean, we do this by taking the original injection point and adjusting the
// qualifiers and type
InjectionPoint ip = new DynamicLookupInjectionPoint(getInjectionPoint(), getType(), getQualifiers());
CurrentInjectionPoint currentInjectionPoint = getBeanManager().getServices().get(CurrentInjectionPoint.class);
try {
currentInjectionPoint.push(ip);
return Reflections. cast(getBeanManager().getReference(bean, getType(), getCreationalContext()));
} finally {
currentInjectionPoint.pop();
}
}
/**
* Gets a string representation
*
* @return A string representation
*/
@Override
public String toString() {
return Formats.formatAnnotations(getQualifiers()) + " Instance<" + Formats.formatType(getType()) + ">";
}
private Set> getBeans() {
Set> beans = getBeanManager().getBeans(getType(), getQualifiers());
return getBeanManager().getBeanResolver().resolve(beans);
}
public Iterator iterator() {
return new InstanceImplIterator(getBeans());
}
public boolean isAmbiguous() {
return getBeans().size() > 1;
}
public boolean isUnsatisfied() {
return getBeans().size() == 0;
}
public Instance select(Annotation... qualifiers) {
return selectInstance(this.getType(), qualifiers);
}
public Instance select(Class subtype, Annotation... qualifiers) {
return selectInstance(subtype, qualifiers);
}
public Instance select(TypeLiteral subtype, Annotation... qualifiers) {
return selectInstance(subtype.getType(), qualifiers);
}
private Instance selectInstance(Type subtype, Annotation[] newQualifiers) {
InjectionPoint modifiedInjectionPoint = new FacadeInjectionPoint(getBeanManager(), getInjectionPoint(), subtype, getQualifiers(),
newQualifiers);
return new InstanceImpl(modifiedInjectionPoint, getCreationalContext(), getBeanManager());
}
@Override
public void destroy(T instance) {
Preconditions.checkNotNull(instance);
// check if this is a proxy of a normal-scoped bean
if (instance instanceof ProxyObject) {
ProxyObject proxy = (ProxyObject) instance;
if (proxy.getHandler() instanceof ProxyMethodHandler) {
ProxyMethodHandler handler = (ProxyMethodHandler) proxy.getHandler();
Bean> bean = handler.getBean();
Context context = getBeanManager().getContext(bean.getScope());
if (context instanceof AlterableContext) {
AlterableContext alterableContext = (AlterableContext) context;
alterableContext.destroy(bean);
return;
} else {
throw BeanLogger.LOG.destroyUnsupported(context);
}
}
}
// check if this is a dependent instance
CreationalContext super T> ctx = getCreationalContext();
if (ctx instanceof WeldCreationalContext>) {
WeldCreationalContext super T> weldCtx = cast(ctx);
weldCtx.destroyDependentInstance(instance);
}
}
// Serialization
private Object writeReplace() throws ObjectStreamException {
return new SerializationProxy(this);
}
private void readObject(ObjectInputStream stream) throws InvalidObjectException {
throw BeanLogger.LOG.proxyRequired();
}
private static class SerializationProxy extends AbstractFacadeSerializationProxy> {
private static final long serialVersionUID = 9181171328831559650L;
public SerializationProxy(InstanceImpl instance) {
super(instance);
}
private Object readResolve() throws ObjectStreamException {
return InstanceImpl.of(getInjectionPoint(), getCreationalContext(), getBeanManager());
}
}
final class InstanceImplIterator implements Iterator {
private final Iterator> delegate;
private InstanceImplIterator(Set> beans) {
super();
this.delegate = beans.iterator();
}
@Override
public boolean hasNext() {
return delegate.hasNext();
}
@Override
public T next() {
return getBeanInstance(delegate.next());
}
@Override
public void remove() {
throw BeanLogger.LOG.instanceIteratorRemoveUnsupported();
}
}
}