Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2008, Red Hat, Inc., and individual contributors
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * http://www.apache.org/licenses/LICENSE-2.0
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.jboss.weld.bean.proxy;
 
 import java.util.Map;
 import java.util.Set;
 
 
Method handler for enterprise bean client proxies

Author(s):
Nicklas Karlsson
Pete Muir
Marko Luksa
 
 public class EnterpriseBeanProxyMethodHandler<T> implements MethodHandlerSerializable {
 
     private static final long serialVersionUID = 2107723373882153667L;
 
     private final BeanManagerImpl manager;
     private final BeanIdentifier beanId;
     private final SessionObjectReference reference;
 
     private final transient SessionBean<T> bean;
 
     private final transient Map<Class<?>, Class<?>> typeToBusinessInterfaceMap;

    
Constructor

Parameters:
bean the session bean
 
     public EnterpriseBeanProxyMethodHandler(SessionBean<T> bean) {
         this(beannull);
     }

    
Constructor used directly by readResolve().

Parameters:
bean the session bean
reference session object reference or null to create a new reference
 
     private EnterpriseBeanProxyMethodHandler(SessionBean<T> beanSessionObjectReference reference) {
         this. = bean;
         this. = bean.getBeanManager();
         this. = bean.getIdentifier();
 
         Map<Class<?>, Class<?>> typeToBusinessInterfaceMap = new HashMap<Class<?>, Class<?>>();
         discoverBusinessInterfaces(typeToBusinessInterfaceMapbean.getEjbDescriptor().getRemoteBusinessInterfacesAsClasses());
         discoverBusinessInterfaces(typeToBusinessInterfaceMapbean.getEjbDescriptor().getLocalBusinessInterfacesAsClasses());
         this. = ImmutableMap.copyOf(typeToBusinessInterfaceMap);
 
         if (reference == null) {
             this. = bean.createReference();
             ..createdSessionBeanProxy(bean);
         } else {
             this. = reference;
             ..activatedSessionBeanProxy(bean);
         }
     }

    
Looks up the EJB in the container and executes the method on it

Parameters:
self the proxy instance.
method the overridden method declared in the super class or interface.
proceed the forwarder method for invoking the overridden method. It is null if the overridden method is abstract or declared in the interface.
args an array of objects containing the values of the arguments passed in the method invocation on the proxy instance. If a parameter type is a primitive type, the type of the array element is a wrapper class.
Returns:
the resulting value of the method invocation.
Throws:
java.lang.Throwable if the method invocation fails.
    @Override
    public Object invoke(Object selfMethod methodMethod proceedObject[] argsthrows Throwable {
        if ("destroy".equals(method.getName()) && Marker.isMarker(0, methodargs)) {
            if (.getEjbDescriptor().isStateful()) {
                if (!.isRemoved()) {
                    .remove();
                }
            }
            return null;
        }
        if (!.isClientCanCallRemoveMethods() && isRemoveMethod(method)) {
            throw ..invalidRemoveMethodInvocation(method);
        }
        Class<?> businessInterface = getBusinessInterface(method);
        if (.isRemoved() && isToStringMethod(method)) {
            return businessInterface.getName() + " [REMOVED]";
        }
        Object proxiedInstance = .getBusinessObject(businessInterface);
        Object returnValue = Reflections.invokeAndUnwrap(proxiedInstancemethodargs);
        ..callProxiedMethod(methodproxiedInstanceargsreturnValue);
        return returnValue;
    }
    private boolean isRemoveMethod(Method method) {
        // TODO we can certainly optimize this search algorithm!
        MethodSignature methodSignature = new MethodSignatureImpl(method);
        return .getEjbDescriptor().getRemoveMethodSignatures().contains(methodSignature);
    }
    private boolean isToStringMethod(Method method) {
        return "toString".equals(method.getName()) && method.getParameterTypes().length == 0;
    }
    private Class<?> getBusinessInterface(Method method) {
        Class<?> declaringClass = method.getDeclaringClass();
        Class<?> businessInterface = null;
        if (declaringClass.equals(Object.class)) {
            businessInterface = .getEjbDescriptor().getObjectInterface();
        } else {
            // This will likely not work perfectly if there is a session bean with more than one views extending/implementing the declaringClass
            businessInterface = .get(declaringClass);
        }
        if (businessInterface == null) {
            throw new RuntimeException("Unable to locate a business interface declaring " + method);
        }
        return businessInterface;
    }
    @SuppressWarnings("unchecked")
    private Object readResolve() throws ObjectStreamException {
    }
    private void discoverBusinessInterfaces(Map<Class<?>, Class<?>> typeToBusinessInterfaceMapSet<Class<?>> businessInterfaces) {
        for (Class<?> businessInterfaceClass : businessInterfaces) {
            for (Class<?> type : HierarchyDiscovery.forNormalizedType(businessInterfaceClass).getTypeMap().keySet()) {
                typeToBusinessInterfaceMap.put(typebusinessInterfaceClass);
            }
        }
    }
New to GrepCode? Check out our FAQ X