public <S> S getBusinessObject(Object ejbRef, java.lang.Class<S> businessInterface) {

    EJBLocalObjectImpl localObjectImpl = getEJBLocalObject(ejbRef);

    if (localObjectImpl == null) {
      throw new IllegalStateException("Invalid ejb ref");
    }

    Container container = localObjectImpl.getContainer();
    EjbDescriptor ejbDesc = container.getEjbDescriptor();

    S businessObject = null;

    if (businessInterface != null) {
      String intfName = businessInterface.getName();
      if (ejbDesc.getLocalBusinessClassNames().contains(intfName)) {

        // Get proxy corresponding to this business interface.
        businessObject = (S) localObjectImpl.getClientObject(intfName);

      } else if (ejbDesc.isLocalBean()) {
        // If this is a no-interface view session bean, the bean
        // can be accessed through interfaces in its superclass as well
        boolean isValidBusinessInterface =
            ejbDesc.getNoInterfaceLocalBeanClasses().contains(intfName);
        if ((intfName.equals(ejbDesc.getEjbClassName())) || isValidBusinessInterface) {
          businessObject = (S) localObjectImpl.getClientObject(ejbDesc.getEjbClassName());
        }
      }
    }

    if (businessObject == null) {
      throw new IllegalStateException(
          "Unable to convert ejbRef for ejb "
              + ejbDesc.getName()
              + " to a business object of type "
              + businessInterface);
    }

    return businessObject;
  }