示例#1
0
  private Exception convertFaultBean(Class<?> exClass, Object faultBean, Fault fault)
      throws Exception {
    Constructor<?> constructor = exClass.getConstructor(new Class[] {String.class});
    Exception e = (Exception) constructor.newInstance(new Object[] {fault.getMessage()});

    // Copy fault bean fields to exception
    for (Class<?> obj = exClass; !obj.equals(Object.class); obj = obj.getSuperclass()) {
      Field[] fields = obj.getDeclaredFields();
      for (Field f : fields) {
        try {
          Field beanField = faultBean.getClass().getDeclaredField(f.getName());
          ReflectionUtil.setAccessible(beanField);
          ReflectionUtil.setAccessible(f);
          f.set(e, beanField.get(faultBean));
        } catch (NoSuchFieldException e1) {
          // do nothing
        }
      }
    }
    // also use/try public getter/setter methods
    Method meth[] = faultBean.getClass().getMethods();
    for (Method m : meth) {
      if (m.getParameterTypes().length == 0
          && (m.getName().startsWith("get") || m.getName().startsWith("is"))) {
        try {
          String name;
          if (m.getName().startsWith("get")) {
            name = "set" + m.getName().substring(3);
          } else {
            name = "set" + m.getName().substring(2);
          }
          Method m2 = exClass.getMethod(name, m.getReturnType());
          m2.invoke(e, m.invoke(faultBean));
        } catch (Exception e1) {
          // ignore
        }
      }
    }

    return e;
  }
示例#2
0
  protected void processFaultDetail(Fault fault, Message msg) {
    Element exDetail = (Element) DOMUtils.getChild(fault.getDetail(), Node.ELEMENT_NODE);
    if (exDetail == null) {
      return;
    }
    QName qname = new QName(exDetail.getNamespaceURI(), exDetail.getLocalName());
    FaultInfo faultWanted = null;
    MessagePartInfo part = null;
    BindingOperationInfo boi = msg.getExchange().get(BindingOperationInfo.class);
    if (boi == null) {
      return;
    }
    if (boi.isUnwrapped()) {
      boi = boi.getWrappedOperation();
    }
    for (FaultInfo faultInfo : boi.getOperationInfo().getFaults()) {
      for (MessagePartInfo mpi : faultInfo.getMessageParts()) {
        if (qname.equals(mpi.getConcreteName())) {
          faultWanted = faultInfo;
          part = mpi;
          break;
        }
      }
      if (faultWanted != null) {
        break;
      }
    }
    if (faultWanted == null) {
      // did not find it using the proper qualified names, we'll try again with just the localpart
      for (FaultInfo faultInfo : boi.getOperationInfo().getFaults()) {
        for (MessagePartInfo mpi : faultInfo.getMessageParts()) {
          if (qname.getLocalPart().equals(mpi.getConcreteName().getLocalPart())) {
            faultWanted = faultInfo;
            part = mpi;
            break;
          }
        }
        if (faultWanted != null) {
          break;
        }
      }
    }
    if (faultWanted == null) {
      return;
    }
    Service s = msg.getExchange().get(Service.class);
    DataBinding dataBinding = s.getDataBinding();

    Object e = null;
    if (isDOMSupported(dataBinding)) {
      DataReader<Node> reader = this.getNodeDataReader(msg);
      reader.setProperty(DataReader.FAULT, fault);
      e = reader.read(part, exDetail);
    } else {
      DataReader<XMLStreamReader> reader = this.getDataReader(msg);
      XMLStreamReader xsr = new W3CDOMStreamReader(exDetail);
      try {
        xsr.nextTag();
      } catch (XMLStreamException e1) {
        throw new Fault(e1);
      }
      reader.setProperty(DataReader.FAULT, fault);
      e = reader.read(part, xsr);
    }

    if (!(e instanceof Exception)) {

      try {
        Class<?> exClass = faultWanted.getProperty(Class.class.getName(), Class.class);
        if (exClass == null) {
          return;
        }
        if (e == null) {
          Constructor<?> constructor = exClass.getConstructor(new Class[] {String.class});
          e = constructor.newInstance(new Object[] {fault.getMessage()});
        } else {

          try {
            Constructor<?> constructor = getConstructor(exClass, e);
            e = constructor.newInstance(new Object[] {fault.getMessage(), e});
          } catch (NoSuchMethodException e1) {
            // Use reflection to convert fault bean to exception
            e = convertFaultBean(exClass, e, fault);
          }
        }
        msg.setContent(Exception.class, e);
      } catch (Exception e1) {
        LogUtils.log(LOG, Level.INFO, "EXCEPTION_WHILE_CREATING_EXCEPTION", e1, e1.getMessage());
      }
    } else {
      if (fault.getMessage() != null) {
        Field f;
        try {
          f = Throwable.class.getDeclaredField("detailMessage");
          ReflectionUtil.setAccessible(f);
          f.set(e, fault.getMessage());
        } catch (Exception e1) {
          // ignore
        }
      }
      msg.setContent(Exception.class, e);
    }
  }
示例#3
0
  public WrapperHelper createWrapperHelper(
      Class<?> wrapperType,
      QName wrapperName,
      List<String> partNames,
      List<String> elTypeNames,
      List<Class<?>> partClasses) {
    List<Method> getMethods = new ArrayList<Method>(partNames.size());
    List<Method> setMethods = new ArrayList<Method>(partNames.size());
    List<Method> jaxbMethods = new ArrayList<Method>(partNames.size());
    List<Field> fields = new ArrayList<Field>(partNames.size());

    Method allMethods[] = wrapperType.getMethods();
    String packageName = PackageUtils.getPackageName(wrapperType);

    // if wrappertype class is generated by ASM,getPackage() always return null
    if (wrapperType.getPackage() != null) {
      packageName = wrapperType.getPackage().getName();
    }

    String objectFactoryClassName = packageName + ".ObjectFactory";

    Object objectFactory = null;
    try {
      objectFactory = wrapperType.getClassLoader().loadClass(objectFactoryClassName).newInstance();
    } catch (Exception e) {
      // ignore, probably won't need it
    }
    Method allOFMethods[];
    if (objectFactory != null) {
      allOFMethods = objectFactory.getClass().getMethods();
    } else {
      allOFMethods = new Method[0];
    }

    for (int x = 0; x < partNames.size(); x++) {
      String partName = partNames.get(x);
      if (partName == null) {
        getMethods.add(null);
        setMethods.add(null);
        fields.add(null);
        jaxbMethods.add(null);
        continue;
      }

      String elementType = elTypeNames.get(x);

      String getAccessor = JAXBUtils.nameToIdentifier(partName, JAXBUtils.IdentifierType.GETTER);
      String setAccessor = JAXBUtils.nameToIdentifier(partName, JAXBUtils.IdentifierType.SETTER);
      Method getMethod = null;
      Method setMethod = null;
      Class<?> valueClass = wrapperType;

      try {
        getMethod = valueClass.getMethod(getAccessor, AbstractWrapperHelper.NO_CLASSES);
      } catch (NoSuchMethodException ex) {
        // ignore for now
      }

      Field elField = getElField(partName, valueClass);
      if (getMethod == null
          && elementType != null
          && "boolean".equals(elementType.toLowerCase())
          && (elField == null
              || (!Collection.class.isAssignableFrom(elField.getType())
                  && !elField.getType().isArray()))) {

        try {
          String newAcc = getAccessor.replaceFirst("get", "is");
          getMethod = wrapperType.getMethod(newAcc, AbstractWrapperHelper.NO_CLASSES);
        } catch (NoSuchMethodException ex) {
          // ignore for now
        }
      }
      if (getMethod == null && "return".equals(partName)) {
        // RI generated code uses this
        try {
          getMethod = valueClass.getMethod("get_return", AbstractWrapperHelper.NO_CLASSES);
        } catch (NoSuchMethodException ex) {
          try {
            getMethod = valueClass.getMethod("is_return", new Class[0]);
          } catch (NoSuchMethodException ex2) {
            // ignore for now
          }
        }
      }
      if (getMethod == null && elField != null) {
        getAccessor =
            JAXBUtils.nameToIdentifier(elField.getName(), JAXBUtils.IdentifierType.GETTER);
        setAccessor =
            JAXBUtils.nameToIdentifier(elField.getName(), JAXBUtils.IdentifierType.SETTER);
        try {
          getMethod = valueClass.getMethod(getAccessor, AbstractWrapperHelper.NO_CLASSES);
        } catch (NoSuchMethodException ex) {
          // ignore for now
        }
      }
      String setAccessor2 = setAccessor;
      if ("return".equals(partName)) {
        // some versions of jaxb map "return" to "set_return" instead of "setReturn"
        setAccessor2 = "set_return";
      }

      for (Method method : allMethods) {
        if (method.getParameterTypes() != null
            && method.getParameterTypes().length == 1
            && (setAccessor.equals(method.getName()) || setAccessor2.equals(method.getName()))) {
          setMethod = method;
          break;
        }
      }

      getMethods.add(getMethod);
      setMethods.add(setMethod);
      if (setMethod != null
          && JAXBElement.class.isAssignableFrom(setMethod.getParameterTypes()[0])) {

        Type t = setMethod.getGenericParameterTypes()[0];
        Class<?> pcls = null;
        if (t instanceof ParameterizedType) {
          t = ((ParameterizedType) t).getActualTypeArguments()[0];
        }
        if (t instanceof Class) {
          pcls = (Class<?>) t;
        }

        String methodName =
            "create" + wrapperType.getSimpleName() + setMethod.getName().substring(3);

        for (Method m : allOFMethods) {
          if (m.getName().equals(methodName)
              && m.getParameterTypes().length == 1
              && (pcls == null || pcls.equals(m.getParameterTypes()[0]))) {
            jaxbMethods.add(m);
          }
        }
      } else {
        jaxbMethods.add(null);
      }

      if (elField != null) {
        // JAXB Type get XmlElement Annotation
        XmlElement el = elField.getAnnotation(XmlElement.class);
        if (el != null && (partName.equals(el.name()) || "##default".equals(el.name()))) {
          ReflectionUtil.setAccessible(elField);
          fields.add(elField);
        } else {
          if (getMethod == null && setMethod == null) {
            if (el != null) {
              LOG.warning(
                  "Could not create accessor for property "
                      + partName
                      + " of type "
                      + wrapperType.getName()
                      + " as the @XmlElement "
                      + "defines the name as "
                      + el.name());
            } else {
              LOG.warning(
                  "Could not create accessor for property "
                      + partName
                      + " of type "
                      + wrapperType.getName());
            }
          }
          fields.add(null);
        }
      } else {
        fields.add(null);
      }
    }

    return createWrapperHelper(
        wrapperType,
        setMethods.toArray(new Method[setMethods.size()]),
        getMethods.toArray(new Method[getMethods.size()]),
        jaxbMethods.toArray(new Method[jaxbMethods.size()]),
        fields.toArray(new Field[fields.size()]),
        objectFactory);
  }