I posted this problem on StackOverflow, but after doing more research, I'm not sure if this is a Weld, JBoss/WildFly or undertow issue.  Or whether its a framework issue, or how the framework is being used.  If this is not the correct list, can you please let me know which list/group I should be directing the issue to?

I'm running into a strange problem and error message that I do not understand. I'm running my application on JBoss 7 EAP.

Caused by: java.lang.ClassCastException: Cannot cast ejbs.LoginManager$417755913$Proxy$_$$_Weld$EnterpriseProxy$ to ejbs.LoginManager

I'm trying to programatically retrieve an EJB from the CDI context by using its interface and am getting this error message. I've created a sample proof-of-concept that is trying to retrieve EJB beans from the CDI context

public <T> T getSesionBean(Class<T> clazz, String name) {
    Properties properties = new Properties();

    try {
        InitialContext initContext = new InitialContext();

        return clazz.cast(CDI.current().select(clazz).get());
    } catch (Exception e) {
        // TODO Auto-generated catch block
        throw new RuntimeException(e);
    }
}

It is being called as:

LoginManager login = getSesionBean(LoginManager.class);

with my EJB defined as:

@Stateless(name="LoginManager")
@Local(LoginManager.class)
@LocalBinding(jndiBinding="LoginManager")
public class LoginManagerBean implements LoginManager {
...
...
...
}

I don't understand it. I'm expecting the bean being retrieved from the context to be a proxy. Shouldn't it be castable to my interface? Otherwise, how am I supposed to retrieve it?

More interestingly, this does not seem to be a consistent error; sometimes the code works fine. Sometimes it fails. I haven't been able to determine what makes it work or throw an exception.

I found a related issue here, but not sure if this is the same problem and/or which version of Weld is used in JB7EAP. I'm including the following dependency in my application:

    <dependency>
        <groupId>org.wildfly</groupId>
        <artifactId>wildfly-ejb-client-bom</artifactId>
        <version>10.1.0.Final</version>
        <type>pom</type>
    </dependency>

Am I doing something completely wrong? If this is a Weld bug, can I use a different proxy system than Weld?  Is this even configurable in undertow?  


Thanks,


Eric