public Object createEjbProviderObject(Object providerObject) {
        Map<Class<?>, Object> ejbProviderMap = new HashMap<Class<?>, Object>();
        //if ejbName == null, it means there is no EJBEndpoint for this providerObject. Thus, it's not EJB, just return the original instance.
        if (this.ejbName == null)
            return providerObject;
        //session bean doesn't implement any interface or have interface but annotated with @LocalBean
        if (this.ejbLocalInterfaces.size() == 0) {
            Object ejbObject = lookUpProviderEjb(null, providerObject);
            for (Class inter : providerObject.getClass().getInterfaces()) {
                ejbProviderMap.put(inter, ejbObject);
            }

            return Proxy.newProxyInstance(providerObject.getClass().getClassLoader(),
                                          ejbProviderMap.keySet().toArray(new Class<?>[ejbProviderMap.size()]),
                                          new EjbProxyInvocationHandler(this.hasEJBExceptionMapper, providerObject, ejbProviderMap));
//            return ejbObject;
        }
        for (Class<?> providerInterface : PROVIDER_INTERFACES) {

            for (String ejbLocalInterface : this.ejbLocalInterfaces) {
                //session bean implements provider directly, just use provider interface for JNDI lookup
                if (ejbLocalInterface.equals(providerInterface.getName())) {
                    Object ejbObject = lookUpProviderEjb(providerInterface, providerObject);
                    if (ejbObject != providerObject) {
                        ejbProviderMap.put(providerInterface, ejbObject);
                    }
                }
                //session bean implements a customized interface, which extend the provider service. The customized interface is an Local interface.
                else {
                    try {
                        Class ejbLocalInterfaceClazz = providerObject.getClass().getClassLoader().loadClass(ejbLocalInterface);
//                        Class providerInterfaceClazz = providerObject.getClass().getClassLoader().loadClass(providerInterface.getName());
//                        Class ejbLocalInterfaceClazz = Thread.currentThread().getContextClassLoader().loadClass(ejbLocalInterface);
                        if (providerInterface.isAssignableFrom(ejbLocalInterfaceClazz)) {
                            @SuppressWarnings("unchecked")
                            Object ejbObject = lookUpProviderEjb(ejbLocalInterfaceClazz, providerObject);
                            if (ejbObject != providerObject) {
                                ejbProviderMap.put(ejbLocalInterfaceClazz, ejbObject);
                            }
                        }

                    } catch (ClassNotFoundException e) {

                    }

                }
            }

        }

        switch (ejbProviderMap.size()) {
            case 0:
                return providerObject;
//            case 1:
//                return ejbProviderMap.values().iterator().next();
            default: {
                return Proxy.newProxyInstance(providerObject.getClass().getClassLoader(),
                                              ejbProviderMap.keySet().toArray(new Class<?>[ejbProviderMap.size()]),
                                              new EjbProxyInvocationHandler(this.hasEJBExceptionMapper, providerObject, ejbProviderMap));
            }
        }
    }
public Object createEjbProviderObject ( Object providerObject ) { Map < Class < ? > , Object > ejbProviderMap = new HashMap < Class < ? > , Object > ( ) ; if ( this . ejbName == null ) return providerObject ; if ( this . ejbLocalInterfaces . size ( ) == 0 ) { Object ejbObject = lookUpProviderEjb ( null , providerObject ) ; for ( Class inter : providerObject . getClass ( ) . getInterfaces ( ) ) { ejbProviderMap . put ( inter , ejbObject ) ; } return Proxy . newProxyInstance ( providerObject . getClass ( ) . getClassLoader ( ) , ejbProviderMap . keySet ( ) . toArray ( new Class < ? > [ ejbProviderMap . size ( ) ] ) , new EjbProxyInvocationHandler ( this . hasEJBExceptionMapper , providerObject , ejbProviderMap ) ) ; } for ( Class < ? > providerInterface : PROVIDER_INTERFACES ) { for ( String ejbLocalInterface : this . ejbLocalInterfaces ) { if ( ejbLocalInterface . equals ( providerInterface . getName ( ) ) ) { Object ejbObject = lookUpProviderEjb ( providerInterface , providerObject ) ; if ( ejbObject != providerObject ) { ejbProviderMap . put ( providerInterface , ejbObject ) ; } } else { try { Class ejbLocalInterfaceClazz = providerObject . getClass ( ) . getClassLoader ( ) . loadClass ( ejbLocalInterface ) ; if ( providerInterface . isAssignableFrom ( ejbLocalInterfaceClazz ) ) { @ SuppressWarnings ( "unchecked" ) Object ejbObject = lookUpProviderEjb ( ejbLocalInterfaceClazz , providerObject ) ; if ( ejbObject != providerObject ) { ejbProviderMap . put ( ejbLocalInterfaceClazz , ejbObject ) ; } } } catch ( ClassNotFoundException e ) { } } } } switch ( ejbProviderMap . size ( ) ) { case 0 : return providerObject ; default : { return Proxy . newProxyInstance ( providerObject . getClass ( ) . getClassLoader ( ) , ejbProviderMap . keySet ( ) . toArray ( new Class < ? > [ ejbProviderMap . size ( ) ] ) , new EjbProxyInvocationHandler ( this . hasEJBExceptionMapper , providerObject , ejbProviderMap ) ) ; } } }
@param providerObject
@param ejbObjects
@return