Hi again,
An IBM Injection Provider exists already in com.ibm.ws.webcontainer.jar (WebSphere 7.0.0.9): com.sun.faces.vendor.WebSphereInjectionProvider.
In order to use it as injection provider with a custom implementation, you must declare it like this in your web.xml:
<context-param>
<description>In order to make the JSF dependency injection work in WebSphere</description>
<param-name>com.sun.faces.injectionProvider</param-name>
<param-value>com.sun.faces.vendor.WebSphereInjectionProvider</param-value>
</context-param>
For my needs, I made a class based on the IBM one with reflexion to avoid the need to provide all the IBM WAS 7 runtime to compile the class (we are using maven in our projects). I used slf4j as log facade but this can easily be changed.
It works well with the last JSF 1.2 version. I didn't tried it with JSF 2.0 but it should be similar.
Hope it's useful for somebody.
Regards,
Florian Minjat
Here is the class (sorry for the formatting, I didn't know where to put it):
package com.sun.faces.vendor;
import java.lang.reflect.Method;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sun.faces.spi.DiscoverableInjectionProvider;
import com.sun.faces.spi.InjectionProviderException;
import com.sun.faces.util.Util;
/**
* This class provides Dependency Injection and Pre/Post Construct on JSF ManagedBeans (tested with JSF 1.2_14)
* It's based on IBM com.sun.faces.vendor.WebSphereInjectionProvider class from com.ibm.ws.webcontainer.jar (WebSphere 7.0.0.9)
*
* @author fmt
*/
public class WebSphereInjectionProvider extends DiscoverableInjectionProvider {
private static final Logger LOG = LoggerFactory.getLogger(WebSphereInjectionProvider.class);
private static final String WEBSPHERE_ANNOTATION_HELPER_MANAGER_CLASS = "com.ibm.wsspi.webcontainer.annotation.AnnotationHelperManager";
private Object runtimeAnnotationHelper;
/**
* invoke com.ibm.wsspi.wetainer.annotation.AnnotationHelperManager.getInstance(ServletContext context);
* then com.ibm.wsspi.wetainer.annotation.AnnotationHelperManager.getAnnotationHelper()
*/
public WebSphereInjectionProvider() {
try {
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
Class<?> clazz = Util.loadClass(WEBSPHERE_ANNOTATION_HELPER_MANAGER_CLASS, WebSphereInjectionProvider.class);
Method mGetInstance = clazz.getMethod("getInstance", new Class[] {ServletContext.class});
Method mGetAnnotationHelper = clazz.getMethod("getAnnotationHelper", new Class[] {});
Object annotationHelperManager = mGetInstance.invoke(null, (ServletContext) externalContext.getContext());
this.runtimeAnnotationHelper = mGetAnnotationHelper.invoke(annotationHelperManager, new Object[] {});
if (LOG.isInfoEnabled()) { LOG.info("WebSphere injection provider successfully loaded.");
} catch (Exception e) {
LOG.error("WebSphere injection provider initialisation failed: " + e, e);
}
}
/**
* invoke com.ibm.wsspi.wetainer.annotation.AnnotationHelper.inject(Object managedBean)
*/
public void inject(Object managedBean) throws InjectionProviderException {
try {
Method mInject = runtimeAnnotationHelper.getClass().getMethod("inject", new Class[] {Object.class});
mInject.invoke(runtimeAnnotationHelper, managedBean);
if (LOG.isDebugEnabled()) { LOG.debug("inject(managedBean='"+managedBean+"') OK !"); }
} catch (Exception e) {
LOG.error("inject(managedBean='"+managedBean+"') KO: " + e, e);
throw new InjectionProviderException(e);
}
}
/**
* invoke com.ibm.wsspi.wetainer.annotation.AnnotationHelper.doPreDestroy(Object managedBean)
*/
public void invokePreDestroy(Object managedBean) throws InjectionProviderException {
try {
Method mInject = runtimeAnnotationHelper.getClass().getMethod("doPreDestroy", new Class[] {Object.class});
mInject.invoke(runtimeAnnotationHelper, managedBean);
if (LOG.isDebugEnabled()) { LOG.debug("invokePreDestroy(managedBean='"+managedBean+"') OK !"); }
} catch (Exception e) {
LOG.error("invokePreDestroy(managedBean='"+managedBean+"') KO: " + e, e);
throw new InjectionProviderException(e);
}
}
/**
* invoke com.ibm.wsspi.wetainer.annotation.AnnotationHelper.doPostConstruct(Object managedBean)
*/
public void invokePostConstruct(Object managedBean) throws InjectionProviderException {
try {
Method mInject = runtimeAnnotationHelper.getClass().getMethod("doPostConstruct", new Class[] {Object.class});
mInject.invoke(runtimeAnnotationHelper, managedBean);
if (LOG.isDebugEnabled()) { LOG.debug("invokePostConstruct(managedBean='"+managedBean+"') OK !"); }
} catch (Exception e) {
LOG.error("invokePostConstruct(managedBean='"+managedBean+"') KO: " + e, e);
throw new InjectionProviderException(e);
}
}
public static boolean isInjectionFeatureAvailable(String delegateClass) {
try {
Util.loadClass(delegateClass, null);
return true;
} catch (Exception e) {
LOG.error("isInjectionFeatureAvailable(delegateClass='"+delegateClass+"') KO: "+e, e);
}
return false;
}
}
[Message sent by forum member 'fmt']
http://forums.java.net/jive/thread.jspa?messageID=470905