Thank to fmt for pointing to sample implementation of InjectionProvider.
I'm not tested it for JSF 1.2, but it is not enought for JSF 2.0.
I've tried JSF RI 2.0.4 with WebSphere 7, jsf2 jars are deployed as isolated
shared library — the way that IBM told us to do
http://publib.boulder.ibm.com/infocenter/wasinfo/v7r0/index.jsp?topic=/com.ibm.websphere.nd.multiplatform.doc/info/ae/ae/tweb_jsf.html
After configuring custom jsf implementation we need to add listener to our
web.xml:
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
Injection of EJBs in JSF's Managed Beans are worked only if Managed Beans are
specified in the faces-config.xml. For Managed Beans defined by @ManagedBean
annotation EJB injection simple does not work.
I've made some research and found that WebSphere's AnnotationHelper have a
method getClassesToScan() returning List<Class>. After i've manually added
some classes to this list, EJB injection started to work for these added
classes. So we need to add list of classes, annotated by @ManagedBean and
recognized by JSF to this list.
But this only the half of the problem — lifecycle methods (annotated with
@PostConstruct and @PreDestroy) are not called, after I configured JSF to use
my Was7InjectionProvider. And i took a simplest solution — created a
instance of base WebContainerInjectionProvider and delegated methods
invokePostConstruct and invokePreDestory to it.
import java.util.HashSet; import java.util.List; import java.util.Set; import
java.util.logging.Level; import java.util.logging.Logger; import
javax.faces.bean.ManagedBean; import javax.faces.context.FacesContext; import
javax.servlet.ServletContext; import
com.ibm.wsspi.webcontainer.annotation.AnnotationHelper; import
com.ibm.wsspi.webcontainer.annotation.AnnotationHelperManager; import
com.sun.faces.config.ConfigManager; import
com.sun.faces.spi.DiscoverableInjectionProvider; import
com.sun.faces.spi.InjectionProviderException; import
com.sun.faces.vendor.WebContainerInjectionProvider; /** * InjectionProvider
for JSF2 RI for WebSphere Application Server v7 */ public class
Was7InjectionProvider extends DiscoverableInjectionProvider { public static
final String CLASS_NAME = Was7InjectionProvider.class.getName(); private
static final Logger LOGGER =
Logger.getLogger(Was7InjectionProvider.CLASS_NAME); private AnnotationHelper
annotationHelper; private AnnotationHelperManager annotationHelperManager;
private WebContainerInjectionProvider lifecycleHelper; public
Was7InjectionProvider() { FacesContext currentInstance =
FacesContext.getCurrentInstance(); ServletContext servletContext =
(ServletContext) currentInstance.getExternalContext().getContext();
annotationHelperManager =
AnnotationHelperManager.getInstance(servletContext); annotationHelper =
annotationHelperManager.getAnnotationHelper(); lifecycleHelper = new
WebContainerInjectionProvider(); addAnnotatedManagedBeans(currentInstance); }
/** * {_at_inheritDoc} */ @Override public void inject(Object managedBean)
throws InjectionProviderException { try {
annotationHelper.inject(managedBean); } catch (Exception ie) { throw new
InjectionProviderException(ie); } } /** * {_at_inheritDoc} */ @Override public
void invokePostConstruct(Object managedBean) throws
InjectionProviderException { try {
lifecycleHelper.invokePostConstruct(managedBean); } catch (Exception ie) {
throw new InjectionProviderException(ie); } } /** * {_at_inheritDoc} */
@Override public void invokePreDestroy(Object managedBean) throws
InjectionProviderException { try {
lifecycleHelper.invokePreDestroy(managedBean); } catch (Exception ie) { throw
new InjectionProviderException(ie); } } private void
addAnnotatedManagedBeans(FacesContext currentInstance) { // Target list of
classes List<Class<?>> classesToScan = annotationHelper.getClassesToScan();
// Getting list of classes annotaded with @ManagedBean Set<Class<?>>
managedBeanClasses =
ConfigManager.getAnnotatedClasses(currentInstance).get(ManagedBean.class); //
Make a copy Set<Class<?>> classesToAdd = new
HashSet<Class<?>>(managedBeanClasses); // Remove classes that already exist
in list classesToAdd.removeAll(classesToScan); if (classesToAdd.isEmpty()) {
LOGGER.log(Level.INFO, "No @ManagedBean classes to add"); } else {
LOGGER.log(Level.INFO, "Adding the following @ManagedBean classes: {0}",
classesToAdd); // Addeding result to target list of classes
classesToScan.addAll(classesToAdd); } } }
We also used maven for building artifacts, and I put
com.ibm.ws.webcontainer.jar, that needed to build presented class to local
maven repository managed running nexus.
So there is only one strange thing remainging, on the startup of application,
WebSphere say that is initializing JSF 1.2:
[4/6/11 15:58:22:438 MSD] 00000049 config I Initializing Sun's JavaServer
Faces implementation (1.2_07-b03-FCS) for context '/front.web'
But this is not true, for the specified context JSF2-only features are
working in this context!
--
[Message sent by forum member 'ragesteel']
View Post: http://forums.java.net/node/701374