[Logo] Enterprise Client Community
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
Using CDI (JBoss WELD implementation)  XML
Forum Index -> Deployment
Author Message
mreich

Power User
[Avatar]

Joined: 30/01/2009 08:34:23
Messages: 744
Offline

Hi community,

is there anybody using CDI for DI in CaptainCasa?
I'm trying but got the problem that the Dispatcher extends a HashMap which has a non final method. Resulting the error:
org.jboss.weld.exceptions.UnproxyableResolutionException: WELD-001437 Normal scoped bean class java.util.HashMap is not proxyable because the type is final or it contains a final method final java.util.HashMap$Entry java.util.HashMap.getEntry(java.lang.Object).

I also tried to change the scope of the dispatcher to change from @SessionScoped to @Dependent as mentioned in
http://docs.jboss.org/weld/reference/1.1.0.Final/en-US/html_single/#d0e1429

this makes the error disappear, but it still doesn't work

regards
Markus
[WWW]
mreich

Power User
[Avatar]

Joined: 30/01/2009 08:34:23
Messages: 744
Offline

The only solution I found is to use JNDI to get a reference for the BeanManager in the constructor of the Dispatcher bean I use the Manager to instantiate my beans!

e.g.
Code:
 public Dispatcher() {
     	try {
 			Context ctx = new InitialContext();
 			BeanManager beanManager = (BeanManager)ctx.lookup("java:comp/env/BeanManager");
 			Bean credentialsBean = (Bean)beanManager.getBeans(services.Credentials.class).iterator().next();
 	        CreationalContext credentialsCc = beanManager.createCreationalContext(credentialsBean);
 	        credentials = (Credentials)beanManager.getReference(credentialsBean, services.Credentials.class, credentialsCc);	 
 		    ctx.close();
 		} catch (NamingException ex) {
 			ex.printStackTrace();
 		}
     }
 


So I use still the faces-config.xml to define the Dispatcher Managed Bean

regards
Markus
[WWW]
mreich

Power User
[Avatar]

Joined: 30/01/2009 08:34:23
Messages: 744
Offline

Hi,

I found a solution by switching my Dispatcher from inherting WorkplaceDispatcher to delegating pattern!

But the code smells

This is my dispatcher.java

Code:
 
 package managedbeans;
 
 import java.io.Serializable;
 import java.util.Collection;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
 
 import javax.enterprise.context.SessionScoped;
 import javax.faces.event.ActionEvent;
 import javax.inject.Inject;
 import javax.inject.Named;
 
 import org.eclnt.jsfserver.defaultscreens.ModalPopup;
 import org.eclnt.jsfserver.defaultscreens.ModelessPopup;
 import org.eclnt.jsfserver.defaultscreens.Statusbar;
 import org.eclnt.jsfserver.managedbean.DefaultDispatcher.DispatcherInfo;
 import org.eclnt.jsfserver.managedbean.IDispatcher;
 import org.eclnt.workplace.IWorkpage;
 import org.eclnt.workplace.IWorkpageContainer;
 import org.eclnt.workplace.IWorkpageDispatcher;
 import org.eclnt.workplace.WorkpageDispatcher;
 
 import services.Dictionary;
 
 /*
  * The dispatcher is referenced in faces-config.xml. When changing the package
  * of the dispatcher, then also update the faces-config.xml link!
  */
 @SessionScoped
 @Named("d")
 public class Dispatcher implements IWorkpageDispatcher, Statusbar.IStatusBarDrillDownInfoProvider, Serializable
 { 
 	MyWorkpageDispatcher workpageDispatcher;
 	
 	
 	public boolean checkIfToDrillDownToGlobalStatusbar() {
 		return workpageDispatcher.checkIfToDrillDownToGlobalStatusbar();
 	}
 
 	
 	public ModalPopup createModalPopup() {
 		return workpageDispatcher.createModalPopup();
 	}
 
 	
 	public ModelessPopup createModelessPopup() {
 		return workpageDispatcher.createModelessPopup();
 	}
 
 	
 	public IDispatcher createSubDispatcherInstance(Class subDispatcherClass) {		
 		return workpageDispatcher.createSubDispatcherInstance(subDispatcherClass);
 	}
 
 	
 	protected IWorkpageContainer createWorkpageContainer() {
 		return workpageDispatcher.createWorkpageContainer();
 	}
 
 	
 	public void destroy() {
 		workpageDispatcher.destroy();
 	}
 
 	
 	public boolean getIsMaximized() {
 		return workpageDispatcher.getIsMaximized();
 	}
 
 	
 	public IWorkpageDispatcher getOwner() {
 		return workpageDispatcher.getOwner();
 	}
 
 	
 	protected Class getPreferredConstructorArguentClass() {
 		return workpageDispatcher.getPreferredConstructorArguentClass();
 	}
 
 	
 	public Statusbar getStatusbar() {
 		return workpageDispatcher.getStatusbar();
 	}
 
 	
 	public IWorkpageDispatcher getTopOwner() {
 		return workpageDispatcher.getTopOwner();
 	}
 
 	
 	public IWorkpage getWorkpage() {
 		return workpageDispatcher.getWorkpage();
 	}
 
 	
 	public IWorkpageContainer getWorkpageContainer() {
 		return workpageDispatcher.getWorkpageContainer();
 	}
 
 	
 	public boolean isOpenedAsPopup() {
 		
 		return workpageDispatcher.isOpenedAsPopup();
 	}
 
 	
 	public void onMaximize(ActionEvent event) {
 		
 		workpageDispatcher.onMaximize(event);
 	}
 
 	
 	public void onMoveWorkpageIntoContentArea(ActionEvent event) {
 		
 		workpageDispatcher.onMoveWorkpageIntoContentArea(event);
 	}
 
 	
 	protected void prepareObject(Object o) {
 		
 		workpageDispatcher.prepareObject(o);
 	}
 
 	
 	public void setOwner(IDispatcher owner) {
 		
 		workpageDispatcher.setOwner(owner);
 	}
 
 	
 	public void setWorkpage(IWorkpage workpage) {
 		
 		workpageDispatcher.setWorkpage(workpage);
 	}
 
 	
 	public IDispatcher createSubDispatcherInstance() {
 		
 		return workpageDispatcher.createSubDispatcherInstance();
 	}
 
 	
 	public Object get(Object key) {
 		
 		return workpageDispatcher.get(key);
 	}
 
 	
 	public String getContentReplace() {
 		
 		return workpageDispatcher.getContentReplace();
 	}
 
 	
 	public Object getDispatchedBean(Class dispachtedBeanClass) {
 		
 		return workpageDispatcher.getDispatchedBean(dispachtedBeanClass);
 	}
 
 	
 	public Object getDispatchedBean(String dispatchedBeanName) {
 		
 		return workpageDispatcher.getDispatchedBean(dispatchedBeanName);
 	}
 
 	
 	public String getExpressionBase() {
 		
 		return workpageDispatcher.getExpressionBase();
 	}
 
 	
 	public String getMyExpression(Class cl) {
 		
 		return workpageDispatcher.getMyExpression(cl);
 	}
 
 	
 	public String getMyExpression(Object o) {
 		
 		return workpageDispatcher.getMyExpression(o);
 	}
 
 	
 	protected void initDispatcher() {
 		
 		workpageDispatcher.initDispatcher();
 	}
 
 	
 	protected void outputLogInfoWhenNoClassWasResolved(String key) {
 		
 		workpageDispatcher.outputLogInfoWhenNoClassWasResolved(key);
 	}
 
 	
 	protected Object readObject(String key) throws Exception {
 		
 		return workpageDispatcher.readObject(key);
 	}
 
 	
 	protected void registerSubDispatcherInstance(IDispatcher subDispatcher) {
 		
 		workpageDispatcher.registerSubDispatcherInstance(subDispatcher);
 	}
 
 	
 	public void removeDispatchedBean(Class dispatchedBeanClass) {
 		
 		workpageDispatcher.removeDispatchedBean(dispatchedBeanClass);
 	}
 
 	
 	public void removeDispatchedBean(String dispatchedBeanName) {
 		
 		workpageDispatcher.removeDispatchedBean(dispatchedBeanName);
 	}
 
 	
 	public Class resolveClass(String key) {
 		
 		return workpageDispatcher.resolveClass(key);
 	}
 
 	
 	public String toString() {
 		
 		return workpageDispatcher.toString();
 	}
 
 	
 	public void unregisterSubDispatcherInstance(IDispatcher subDispatcher) {
 		
 		workpageDispatcher.unregisterSubDispatcherInstance(subDispatcher);
 	}
 
 	
 	public String updateExpression(String expression) {
 		
 		return workpageDispatcher.updateExpression(expression);
 	}
 
 	
 	public int size() {
 		
 		return workpageDispatcher.size();
 	}
 
 	
 	public boolean isEmpty() {
 		
 		return workpageDispatcher.isEmpty();
 	}
 
 	
 	public boolean containsKey(Object key) {
 		
 		return workpageDispatcher.containsKey(key);
 	}
 
 	
 	public Object put(String key, Object value) {
 		
 		return workpageDispatcher.put(key, value);
 	}
 
 	
 	public void putAll(Map<? extends String, ? extends Object> m) {
 		
 		workpageDispatcher.putAll(m);
 	}
 
 	
 	public Object remove(Object key) {
 		
 		return workpageDispatcher.remove(key);
 	}
 
 	
 	public void clear() {
 		
 		workpageDispatcher.clear();
 	}
 
 	
 	public boolean containsValue(Object value) {
 		
 		return workpageDispatcher.containsValue(value);
 	}
 
 	
 	public Object clone() {
 		
 		return workpageDispatcher.clone();
 	}
 
 	
 	public Set<String> keySet() {
 		
 		return workpageDispatcher.keySet();
 	}
 
 	
 	public Collection<Object> values() {
 		
 		return workpageDispatcher.values();
 	}
 
 	
 	public Set<Entry<String, Object>> entrySet() {
 		
 		return workpageDispatcher.entrySet();
 	}
 
 	@Inject
 	Dictionary dictionary;
 
 	/**
      * This method needs to be implemented if you want to extend the page bean browser tool.
      */
     public static DispatcherInfo getStaticDispatcherInfo() { return new DispatcherInfo(Dispatcher.class); }
     
     /**
      * Returns the expression under which the dispatcher can be reached.
      */
     protected String getRootExpression() { return "#{d}"; }
     
     // ------------------------------------------------------------------------
     // constructors
     // ------------------------------------------------------------------------
     
     /**
      * Dispatcher that is used for the root object, e.g. "#{d}".
      */
     public Dispatcher()
     {
     	workpageDispatcher = new MyWorkpageDispatcher();
     	System.out.println("Create Bean");
         // add any implementation...
     }
 
     /**
      * Dispatcher that is used for the sub dispatcher objects, e.g. "#{d.d_1}".
      */
     public Dispatcher(IWorkpageContainer workpageContainer)
     {
     	workpageDispatcher = new MyWorkpageDispatcher(workpageContainer);
     	//super(workpageContainer);
         // add any implementation...
     }
 
 	
 	
 	public class MyWorkpageDispatcher extends WorkpageDispatcher {
 
 		public Dictionary getDictionary() {
 			return Dispatcher.this.dictionary;
 		}
 		
 		public MyWorkpageDispatcher() {
 			super();
 			// TODO Auto-generated constructor stub
 		}
 
 		public MyWorkpageDispatcher(IWorkpageContainer workpageContainer) {
 			super(workpageContainer);
 			// TODO Auto-generated constructor stub
 		}
 
 		@Override
 		protected IWorkpageContainer createWorkpageContainer() {
 			
 			return super.createWorkpageContainer();
 		}
 
 		@Override
 		protected Class getPreferredConstructorArguentClass() {
 			
 			return super.getPreferredConstructorArguentClass();
 		}
 
 		@Override
 		protected String getRootExpression() {
 			
 			return super.getRootExpression();
 		}
 
 		@Override
 		protected void prepareObject(Object o) {
 			
 			super.prepareObject(o);
 		}
 
 		@Override
 		protected void initDispatcher() {
 			
 			super.initDispatcher();
 		}
 
 		@Override
 		protected void outputLogInfoWhenNoClassWasResolved(String key) {
 			
 			super.outputLogInfoWhenNoClassWasResolved(key);
 		}
 
 		@Override
 		protected Object readObject(String key) throws Exception {
 			
 			return super.readObject(key);
 		}
 
 		@Override
 		protected void registerSubDispatcherInstance(IDispatcher subDispatcher) {
 			
 			super.registerSubDispatcherInstance(subDispatcher);
 		}
 		
 	}
 
     
 }
 


Accessing the Dictionary Bean by
Code:
 Dispatcher.MyWorkpageDispatcher dispatcher = (Dispatcher.MyWorkpageDispatcher)getWorkpage().getDispatcher();
 Dictionary dict = dispatcher.getDictionary();
 
[WWW]
afrank



Joined: 25/10/2011 20:31:05
Messages: 2
Offline

Hi,

we are using CDI in our project as well, but instead of managing the dispatcher with CDI we leave this part to JSF.

Within the dispatcher we have overridden the prepareObject method to inject dependend objects into the PageBeans after they are created.

This solution provides the possibility to use the @Inject annotation for DI without caring about the additional work the dispatcher does.

kind regards
Alexander



mreich

Power User
[Avatar]

Joined: 30/01/2009 08:34:23
Messages: 744
Offline

Hmm, ok seems to be the best solution, but how can you use the annotation inject? The only way I found was to go over the BeanManager.
Maybe you can give me an additional hint

Markus
[WWW]
afrank



Joined: 25/10/2011 20:31:05
Messages: 2
Offline

That's correct, for using the @Inject annotation you have to use the BeanManager.

Use JNDI to lookup the BeanManager, as you did on your second post and create an InjectionTarget with it. The InjectionTarget is able to inject the dependencies to an existing object.

Code:
 public static <T> void inject(T anObject){
   BeanManager beanManager = getBeanManager();
   Class<T> objClass = (Class<T>)anObject.getClass();
   AnnotatedType<T> annotatedType = beanManager.createAnnotatedType(objClass);
   InjectionTarget<T> injectionTarget = beanManager.createInjectionTarget(annotatedType);
   CreationalContext<T> context = new IgnorantCreationalContext<T>();
   injectionTarget.inject(anObject, context);
 }
 


The inject method should be called within the prepareObject method of the Dispatcher.

edit: The IgnorantCreationalContext is an empty implementation of the CreationalContext interface.

Alexander
mreich

Power User
[Avatar]

Joined: 30/01/2009 08:34:23
Messages: 744
Offline

Hi Alexander,

thx for your helpful hints. It works perfect!!!!

regards
Markus
[WWW]
mametz



Joined: 14/09/2020 18:27:21
Messages: 3
Offline

Hi,
I came across this topic because I am also thinking about using WELD with CaptainCasa. Some years have passed since the last post and both WELD and CaptainCasa have developed further. Were you able to gain more experience using WELD and CaptainCasa?

Does anyone have any experience with the combination: CaptainCasa, SpringBoot and WELD?

regards
matthias
 
Forum Index -> Deployment
Go to:   
Powered by JForum 2.1.6 © JForum Team