Skip to content

Commit

Permalink
WELD-1177 Built-in InjectionPoint bean not injected for stateless ses…
Browse files Browse the repository at this point in the history
…sion bean
  • Loading branch information
luksa authored and jharting committed Jan 25, 2013
1 parent 97afcec commit dd9247a
Show file tree
Hide file tree
Showing 15 changed files with 472 additions and 73 deletions.
Expand Up @@ -52,7 +52,7 @@ protected Class<?> computeInstanceType(Bean<?> bean) {
return computeInstanceType(bean.getTypes());
}

protected Class<?> computeInstanceType(Set<Type> types) {
protected static Class<?> computeInstanceType(Set<Type> types) {
TypeInfo typeInfo = TypeInfo.of(types);
Class<?> superClass = typeInfo.getSuperClass();
if (superClass.equals(Object.class)) {
Expand Down
Expand Up @@ -25,21 +25,22 @@

/**
* @author David Allen
* @author Marko Luksa
*/
public class EnterpriseTargetBeanInstance extends AbstractBeanInstance implements Serializable {
private static final long serialVersionUID = 2825052095047112162L;

private final Class<?> beanType;
private final MethodHandler methodHandler;


public EnterpriseTargetBeanInstance(Class<?> baseType, MethodHandler methodHandler) {
this.beanType = baseType;
this.methodHandler = methodHandler;
}

public EnterpriseTargetBeanInstance(Set<Type> types, MethodHandler methodHandler) {
this.beanType = computeInstanceType(types);
this.methodHandler = methodHandler;
this(computeInstanceType(types), methodHandler);
}

public Object getInstance() {
Expand All @@ -51,8 +52,8 @@ public Class<?> getInstanceType() {
}

public Object invoke(Object instance, Method method, Object... arguments) throws Throwable {
// Pass the invocation directly to the method handler
return methodHandler.invoke(null, method, method, arguments);
// Pass the invocation directly to the method handler
return methodHandler.invoke(null, method, method, arguments);
}

}
@@ -0,0 +1,73 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2013, 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.proxy;

import java.io.ObjectStreamException;
import java.lang.reflect.Method;

import javax.enterprise.inject.spi.InjectionPoint;

import org.jboss.weld.Container;
import org.jboss.weld.injection.CurrentInjectionPoint;
import org.jboss.weld.injection.EmptyInjectionPoint;
import org.jboss.weld.injection.SLSBInvocationInjectionPoint;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.serialization.InjectionPointHolder;

/**
* @author Marko Luksa
* @author Jozef Hartinger
*
*/
public class InjectionPointPropagatingEnterpriseTargetBeanInstance extends EnterpriseTargetBeanInstance {

private static final long serialVersionUID = 166825647603520280L;

private final InjectionPointHolder injectionPointHolder;
private transient SLSBInvocationInjectionPoint slsbInvocationInjectionPoint;

public InjectionPointPropagatingEnterpriseTargetBeanInstance(Class<?> baseType, MethodHandler methodHandler, BeanManagerImpl manager) {
super(baseType, methodHandler);
this.slsbInvocationInjectionPoint = manager.getServices().get(SLSBInvocationInjectionPoint.class);
InjectionPoint ip = manager.getServices().get(CurrentInjectionPoint.class).peek();
if (ip != null) {
this.injectionPointHolder = new InjectionPointHolder(ip);
} else {
this.injectionPointHolder = null;
}
}

@Override
public Object invoke(Object instance, Method method, Object... arguments) throws Throwable {
if (injectionPointHolder != null) {
slsbInvocationInjectionPoint.push(injectionPointHolder.get());
} else {
slsbInvocationInjectionPoint.push(EmptyInjectionPoint.INSTANCE);
}

try {
return super.invoke(instance, method, arguments);
} finally {
slsbInvocationInjectionPoint.pop();
}
}

private Object readResolve() throws ObjectStreamException {
this.slsbInvocationInjectionPoint = Container.instance().services().get(SLSBInvocationInjectionPoint.class);
return this;
}
}
Expand Up @@ -108,6 +108,7 @@
import org.jboss.weld.exceptions.IllegalStateException;
import org.jboss.weld.executor.ExecutorServicesFactory;
import org.jboss.weld.injection.CurrentInjectionPoint;
import org.jboss.weld.injection.SLSBInvocationInjectionPoint;
import org.jboss.weld.injection.producer.InjectionTargetService;
import org.jboss.weld.logging.messages.VersionMessage;
import org.jboss.weld.manager.BeanManagerImpl;
Expand Down Expand Up @@ -336,6 +337,7 @@ private void addImplementationServices(ServiceRegistry services) {
services.add(MetaAnnotationStore.class, new MetaAnnotationStore(classTransformer));
services.add(ContextualStore.class, new ContextualStoreImpl());
services.add(CurrentInjectionPoint.class, new CurrentInjectionPoint());
services.add(SLSBInvocationInjectionPoint.class, new SLSBInvocationInjectionPoint());
services.add(SpecializationAndEnablementRegistry.class, new SpecializationAndEnablementRegistry());

GlobalObserverNotifierService observerNotificationService = new GlobalObserverNotifierService(services);
Expand Down
Expand Up @@ -16,70 +16,6 @@
*/
package org.jboss.weld.injection;

import org.jboss.weld.bootstrap.api.Service;

import javax.enterprise.inject.spi.InjectionPoint;
import java.util.EmptyStackException;
import java.util.Stack;

public class CurrentInjectionPoint implements Service {

private final ThreadLocal<Stack<InjectionPoint>> currentInjectionPoint;

public CurrentInjectionPoint() {
this.currentInjectionPoint = new ThreadLocal<Stack<InjectionPoint>>();
}

/**
* Replaces (or adds) the current injection point. If a current injection
* point exists, it will be replaced. If no current injection point exists,
* one will be added.
*
* @param injectionPoint the injection point to use
* @return the injection point added, or null if previous existed did not exist
*/
public void push(InjectionPoint injectionPoint) {
Stack<InjectionPoint> stack = currentInjectionPoint.get();
if (stack == null) {
stack = new Stack<InjectionPoint>();
currentInjectionPoint.set(stack);
}
stack.push(injectionPoint);
}

public InjectionPoint pop() {
Stack<InjectionPoint> stack = currentInjectionPoint.get();
if (stack == null) {
throw new EmptyStackException();
}
try {
return stack.pop();
} finally {
if (stack.isEmpty()) {
currentInjectionPoint.remove();
}
}
}

/**
* The injection point being operated on for this thread
*
* @return the current injection point
*/
public InjectionPoint peek() {
Stack<InjectionPoint> stack = currentInjectionPoint.get();
if (stack == null) {
return null;
}
if (!stack.empty()) {
return stack.peek();
} else {
return null;
}
}

public void cleanup() {

}
public class CurrentInjectionPoint extends InjectionPointStack {

}
@@ -0,0 +1,54 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2013, 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.injection;

import java.io.ObjectStreamException;
import java.io.Serializable;

import javax.enterprise.inject.spi.InjectionPoint;

import org.jboss.weld.Container;
import org.jboss.weld.bootstrap.api.ServiceRegistry;

/**
* A proxy that forwards call to the current {@link SLSBInvocationInjectionPoint}.
*
* @author Marko Luksa
*
*/
public class DynamicInjectionPoint extends ForwardingInjectionPoint implements Serializable {

private static final long serialVersionUID = 0L;

private final transient SLSBInvocationInjectionPoint invocationInjectionPoint;

public DynamicInjectionPoint(ServiceRegistry services) {
this.invocationInjectionPoint = services.get(SLSBInvocationInjectionPoint.class);
}

private DynamicInjectionPoint(SLSBInvocationInjectionPoint invocationInjectionPoint) {
this.invocationInjectionPoint = invocationInjectionPoint;
}

protected InjectionPoint delegate() {
return invocationInjectionPoint.peek();
}

private Object readResolve() throws ObjectStreamException {
return new DynamicInjectionPoint(Container.instance().services().get(SLSBInvocationInjectionPoint.class));
}
}
@@ -0,0 +1,86 @@
/*
* 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.injection;

import java.util.EmptyStackException;
import java.util.Stack;

import javax.enterprise.inject.spi.InjectionPoint;

import org.jboss.weld.bootstrap.api.Service;

public class InjectionPointStack implements Service {

private final ThreadLocal<Stack<InjectionPoint>> injectionPointStack;

public InjectionPointStack() {
this.injectionPointStack = new ThreadLocal<Stack<InjectionPoint>>();
}

/**
* Replaces (or adds) the current injection point. If a current injection
* point exists, it will be replaced. If no current injection point exists,
* one will be added.
*
* @param injectionPoint the injection point to use
* @return the injection point added, or null if previous existed did not exist
*/
public void push(InjectionPoint injectionPoint) {
Stack<InjectionPoint> stack = injectionPointStack.get();
if (stack == null) {
stack = new Stack<InjectionPoint>();
injectionPointStack.set(stack);
}
stack.push(injectionPoint);
}

public InjectionPoint pop() {
Stack<InjectionPoint> stack = injectionPointStack.get();
if (stack == null) {
throw new EmptyStackException();
}
try {
return stack.pop();
} finally {
if (stack.isEmpty()) {
injectionPointStack.remove();
}
}
}

/**
* The injection point being operated on for this thread
*
* @return the current injection point
*/
public InjectionPoint peek() {
Stack<InjectionPoint> stack = injectionPointStack.get();
if (stack == null) {
return null;
}
if (!stack.empty()) {
return stack.peek();
} else {
return null;
}
}

public void cleanup() {

}

}
@@ -0,0 +1,21 @@
/*
* 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.injection;

public class SLSBInvocationInjectionPoint extends InjectionPointStack {

}

0 comments on commit dd9247a

Please sign in to comment.