-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WFLY-412 Add support for @AroundConstruct
- Loading branch information
Showing
6 changed files
with
105 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
weld/src/main/java/org/jboss/as/weld/injection/WeldConstructionStartInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.jboss.as.weld.injection; | ||
|
||
import java.util.Map; | ||
|
||
import javax.enterprise.context.spi.CreationalContext; | ||
import javax.enterprise.inject.spi.AnnotatedConstructor; | ||
|
||
import org.jboss.invocation.Interceptor; | ||
import org.jboss.invocation.InterceptorContext; | ||
import org.jboss.weld.construction.api.AroundConstructCallback; | ||
import org.jboss.weld.construction.api.ConstructionHandle; | ||
import org.jboss.weld.context.CreationalContextImpl; | ||
|
||
/** | ||
* Initiates component construction. This interceptor delegates to Weld to start component construction. When Weld resolves the values of constructor | ||
* parameters, it invokes a callback which allows WF to perform AroundConstruct interception. The callback is registered within | ||
* {@link #setupAroundConstructCallback(CreationalContext, InterceptorContext)} | ||
* | ||
* @author Jozef Hartinger | ||
* | ||
*/ | ||
public class WeldConstructionStartInterceptor implements Interceptor { | ||
|
||
public static final WeldConstructionStartInterceptor INSTANCE = new WeldConstructionStartInterceptor(); | ||
|
||
private WeldConstructionStartInterceptor() { | ||
} | ||
|
||
@Override | ||
public Object processInvocation(InterceptorContext context) throws Exception { | ||
WeldInjectionContext injectionCtx = context.getPrivateData(WeldInjectionContext.class); | ||
setupAroundConstructCallback(injectionCtx.getContext(), context); | ||
/* | ||
* Here we delegate to Weld to start component construction. WF will be called back on the callback registered within #setupAroundConstructCallback. | ||
* The return value of the following call is not important as we get the component reference from ConstructionContext in WeldManagedReferenceFactory. | ||
*/ | ||
injectionCtx.produce(); | ||
return context.getTarget(); | ||
} | ||
|
||
private <T> void setupAroundConstructCallback(CreationalContext<T> ctx, final InterceptorContext context) { | ||
CreationalContextImpl<T> ctxImpl = (CreationalContextImpl<T>) ctx; | ||
ctxImpl.setConstructorInterceptionSuppressed(true); // Weld will not try to invoke around construct interceptors on this instance | ||
|
||
ctxImpl.registerAroundConstructCallback(new AroundConstructCallback<T>() { | ||
@SuppressWarnings("unchecked") | ||
@Override | ||
public T aroundConstruct(ConstructionHandle<T> ctx, AnnotatedConstructor<T> constructor, Object[] parameters, Map<String, Object> data) throws Exception { | ||
context.putPrivateData(ConstructionHandle.class, ctx); | ||
context.setParameters(parameters); | ||
context.setContextData(data); | ||
context.setConstructor(constructor.getJavaMember()); | ||
context.proceed(); // proceed with the WF interceptor chain | ||
return (T) context.getTarget(); | ||
} | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters