On Sat, Mar 30, 2013 at 1:46 PM, Marco de Booij
<[email protected]>wrote:
> Hello,
>
> I have an application that works fine when I have JSF in control of my
> managed beans.
Your app can work with CDI as well. I'm sure TomEE committers may ask you
to try with latest TomEE+ 1.5.2 snapshot or 1.6.0 snapshot. They may also
ask if you have a WAR to share via github or a public repository where they
can download your WAR.
Now I try to replace this with CDI. I replaced (nearly) all the @EJB
> annotations with @Inject.
I migrated from Glassfish to TomEE/openwebbeans, because I was confident
that I could have success migrating from JSF managed beans to CDI managed
beans. So, congrats, glad to hear you are attempting to do the same.
FYI, it is 'not' necessary to replace all @EJB annotations with @Inject. I
kept @EJB annotations in my app, and 'only' added @Inject when referencing
CDI managed bean within (an)other CDI managed bean.
> For the managed beans I added @Named and @SessionScoped (or another scope).
+1 good
> The application works fine until I want to access the managed beans
> through:
> FacesContext.**getCurrentInstance().**getExternalContext().**getSessionMap().get(<bean
> name>)
>
> Seems logic to me since I do not want to use JSF for that anymore. I
> searched and found that you can access them through the BeanManager. I get
> the BeanManager like this:
> (BeanManager) initialContext.lookup("java:**comp/BeanManager")
>
I am using Apache MyFaces CODI bean manager to do this instead of
TomEE/openejb/openwebbeans classes to do this, but I'm sure you can find
success doing it your way as well. :)
>
> When I do a getBeans(<bean name>) then I get a bean with
> beans.iterator().next() and this is the bean I need. I do a
> bean.getBeanClass().getName() and the name is correct. When I cast the bean
> to its class then I get a java.lang.ClassCastException:
> org.apache.webbeans.component.**ManagedBean cannot be cast to
>
> What is my mistake?
>
> I use tomee 1.5.1. My application is packaged as WAR. I added an empty
> beans.xml into the WEB-INF and in the JAR files in the META-INF where I
> have the Managed Beans.
+1 good. still, you may want to try latest 1.5.2 or 1.6.0 SNAPSHOT instead
of 1.5.1, even though I found success with 1.5.1 release when I migrated
from JSF to CDI managed beans via TomEE 1.5.1.
> My faces-config.xml only has a phase-listener defined. I do not know if I
> still need the phase-listener.
>
> public static BeanManager getBeanManager() {
> BeanManager beanManager = null;
> try {
> final InitialContext initialContext = new InitialContext();
> beanManager =
> (BeanManager) initialContext.lookup("java:**comp/BeanManager");
> System.out.println("Gevonden."**);
> } catch(NamingException e) {
> LOGGER.error("BeanManager not found.", e);
> }
>
> return beanManager;
> }
>
> public static Bean<?> getBean(String beanname) {
> Set<Bean<?>> beans = getBeanManager().getBeans(**beanname);
> if (beans.isEmpty()) {
> LOGGER.debug("Bean " + beannaam + " not found.");
> return null;
> }
> Bean<?> bean = beans.iterator().next();
> // System.out.println("Bean: " + bean.getBeanClass().getName() + " (" +
> beans.size() + ")");
>
> return bean;
> }
>
>