methodmarshallerutils.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 1,273 行 · 第 1/4 页

JAVA
1,273
字号

            // Use "by java type" marshalling if necessary
            if (blockContext.getConstructionType() != 
                JAXBUtils.CONSTRUCTION_TYPE.BY_CONTEXT_PATH &&
                    isNotJAXBRootElement(faultBeanFormalClass, marshalDesc)) {
                blockContext.setProcessType(faultBeanFormalClass);
            }

            // Get the jaxb block and business object
            Block jaxbBlock = factory.createFrom(detailBlocks[0], blockContext);
            Object faultBeanObject = jaxbBlock.getBusinessObject(true);

            // At this point, faultBeanObject is an object that can be rendered as an
            // element.  We want the object that represents the type.
            if (faultBeanObject instanceof JAXBElement) {
                faultBeanObject = ((JAXBElement)faultBeanObject).getValue();
            }

            if (log.isErrorEnabled()) {
                log.debug("Unmarshalled the detail element into a JAXB object");
            }

            // Construct the JAX-WS generated exception that holds the faultBeanObject
            Class exceptionClass = loadClass(faultDesc.getExceptionClassName());
            if (log.isErrorEnabled()) {
                log.debug("Found FaultDescription.  The exception name is " +
                        exceptionClass.getName());
            }
            exception = createServiceException(xmlfault.getReason().getText(),
                                               exceptionClass,
                                               faultBeanObject,
                                               faultBeanFormalClass,
                                               marshalDesc,
                                               isLegacy);
        }
        return exception;
    }

    
    /**
     * @param pds
     * @return Number of inout or out parameters
     */
    static int numOutputBodyParams(ParameterDescription[] pds) {
        int count = 0;
        for (int i=0; i<pds.length; i++) {
            // TODO Need to change this to also detect not attachment
            if (!pds[i].isHeader()) {
                if (pds[i].getMode() == Mode.INOUT ||
                        pds[i].getMode() == Mode.OUT) {
                    count++;
                }
            }
        }
        return count;
    }
    

    /**
     * @param value
     * @return if async handler
     */
    static boolean isAsyncHandler(Object value) {
        return (value instanceof AsyncHandler);
    }

    /**
     * @param value
     * @return true if value is holder
     */
    static boolean isHolder(Object value) {
        return value != null && Holder.class.isAssignableFrom(value.getClass());
    }

    /**
     * Crate a Holder
     *
     * @param <T>
     * @param paramType
     * @param value
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws ClassNotFoundException
     */
    static <T> Holder<T> createHolder(Class paramType, T value)
            throws IllegalAccessException, InstantiationException, ClassNotFoundException {
        if (Holder.class.isAssignableFrom(paramType)) {
            Holder holder = (Holder) paramType.newInstance();
            holder.value = value;
            return holder;
        }
        return null;
    }

    /**
     * Load the class
     *
     * @param className
     * @return loaded class
     * @throws ClassNotFoundException
     */
    static Class loadClass(String className) throws ClassNotFoundException {
        // Don't make this public, its a security exposure
        Class cls = ClassUtils.getPrimitiveClass(className);
        if (cls == null) {
            cls = forName(className, true, getContextClassLoader());
        }
        return cls;
    }

    /**
     * Return the class for this name
     *
     * @return Class
     */
    private static Class forName(final String className, final boolean initialize,
                                 final ClassLoader classLoader) {
        // NOTE: This method must remain private because it uses AccessController
        Class cl = null;
        try {
            cl = (Class)AccessController.doPrivileged(
                    new PrivilegedExceptionAction() {
                        public Object run() throws ClassNotFoundException {
                            // Class.forName does not support primitives
                            Class cls = ClassUtils.getPrimitiveClass(className);
                            if (cls == null) {
                                cls = Class.forName(className, initialize, classLoader);
                            }
                            return cls;
                        }
                    }
            );
        } catch (PrivilegedActionException e) {
            if (log.isDebugEnabled()) {
                log.debug("Exception thrown from AccessController: " + e);
            }
            throw ExceptionFactory.makeWebServiceException(e.getException());
        }

        return cl;
    }

    /** @return ClassLoader */
    private static ClassLoader getContextClassLoader() {
        // NOTE: This method must remain private because it uses AccessController
        ClassLoader cl = null;
        try {
            cl = (ClassLoader)AccessController.doPrivileged(
                    new PrivilegedExceptionAction() {
                        public Object run() throws ClassNotFoundException {
                            return Thread.currentThread().getContextClassLoader();
                        }
                    }
            );
        } catch (PrivilegedActionException e) {
            if (log.isDebugEnabled()) {
                log.debug("Exception thrown from AccessController: " + e);
            }
            throw ExceptionFactory.makeWebServiceException(e.getException());
        }

        return cl;
    }


    /**
     * Create a JAX-WS Service Exception (Generated Exception)
     *
     * @param message
     * @param exceptionclass
     * @param bean
     * @param beanFormalType
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws NoSuchMethodException
     * @parma marshalDesc is used to get cached information about the exception class and bean
     */
    private static Exception createServiceException(String message,
                                                    Class exceptionclass,
                                                    Object bean,
                                                    Class beanFormalType,
                                                    MarshalServiceRuntimeDescription marshalDesc,
                                                    boolean isLegacyException) throws
            InvocationTargetException, IllegalAccessException, InstantiationException,
            NoSuchMethodException {

        if (log.isDebugEnabled()) {
            log.debug("Constructing JAX-WS Exception:" + exceptionclass);
        }
        Exception exception = null;
        if (isLegacyException) {
            // Legacy Exception
            exception = LegacyExceptionUtil.createFaultException(exceptionclass, 
                                                                 bean, 
                                                                 marshalDesc);
        } else {
            // Normal case, use the contstructor to create the exception
            Constructor constructor =
                    exceptionclass.getConstructor(new Class[] { String.class, beanFormalType });
            exception = (Exception)constructor.newInstance(new Object[] { message, bean });
        }

        return exception;

    }

    /**
     * Create a system exception
     *
     * @param message
     * @return
     */
    public static ProtocolException createSystemException(XMLFault xmlFault, Message message) {
        ProtocolException e = null;
        Protocol protocol = message.getProtocol();
        String text = xmlFault.getReason().getText();

        if (protocol == Protocol.soap11 || protocol == Protocol.soap12) {
            // Throw a SOAPFaultException
            if (log.isDebugEnabled()) {
                log.debug("Constructing SOAPFaultException for " + text);
            }
            String protocolNS = (protocol == Protocol.soap11) ?
                    SOAPConstants.URI_NS_SOAP_1_1_ENVELOPE :
                    SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE;
            try {
                // The following set of instructions is used to avoid 
                // some unimplemented methods in the Axis2 SAAJ implementation
                javax.xml.soap.MessageFactory mf = SAAJFactory.createMessageFactory(protocolNS);
                SOAPBody body = mf.createMessage().getSOAPBody();
                SOAPFault soapFault = XMLFaultUtils.createSAAJFault(xmlFault, body);
                e = new SOAPFaultException(soapFault);
            } catch (Exception ex) {
                // Exception occurred during exception processing.
                // TODO Probably should do something better here
                if (log.isDebugEnabled()) {
                    log.debug("Exception occurred during fault processing:", ex);
                }
                e = ExceptionFactory.makeProtocolException(text, null);
            }
        } else if (protocol == Protocol.rest) {
            if (log.isDebugEnabled()) {
                log.debug("Constructing ProtocolException for " + text);
            }
            // TODO Is there an explicit exception for REST
            e = ExceptionFactory.makeProtocolException(text, null);
        } else if (protocol == Protocol.unknown) {
            // REVIEW What should happen if there is no protocol
            if (log.isDebugEnabled()) {
                log.debug("Constructing ProtocolException for " + text);
            }
            e = ExceptionFactory.makeProtocolException(text, null);
        }
        return e;
    }

    /**
     * @param ed
     * @return
     */
    static MarshalServiceRuntimeDescription getMarshalDesc(EndpointDescription ed) {
        ServiceDescription sd = ed.getServiceDescription();
        return MarshalServiceRuntimeDescriptionFactory.get(sd);
    }

    /**
     * This probably should be available from the ParameterDescription
     *
     * @param cls
     * @param marshalDesc
     * @return true if primitive, wrapper, java.lang.String. Calendar (or GregorianCalendar),
     *         BigInteger etc or anything other java type that is mapped by the basic schema types
     */
    static boolean isNotJAXBRootElement(Class cls, MarshalServiceRuntimeDescription marshalDesc) {
        if (cls == String.class ||
                cls.isPrimitive() ||
                cls == Calendar.class ||
                cls == byte[].class ||
                cls == GregorianCalendar.class ||
                cls == Date.class ||
                cls == BigInteger.class ||
                cls == BigDecimal.class) {

            return true;
        }
        AnnotationDesc aDesc = marshalDesc.getAnnotationDesc(cls);
        if (aDesc != null) {
            // XmlRootElementName returns null if @XmlRootElement is not specified
            return (aDesc.getXmlRootElementName() == null);
        }
        return true;  
    }
    
    /**
     * Get a string containing the stack of the specified exception   
     * @param e   
     * @return    
     */   
    public static String stackToString(Throwable e) {       
        java.io.StringWriter sw= new java.io.StringWriter();        
        java.io.BufferedWriter bw = new java.io.BufferedWriter(sw);       
        java.io.PrintWriter pw= new java.io.PrintWriter(bw);       
        e.printStackTrace(pw);       
        pw.close();       
        return sw.getBuffer().toString();      
    }
    
    static boolean isSWAAttachment(ParameterDescription pd) {
        return pd.getAttachmentDescription() != null &&
            pd.getAttachmentDescription().getAttachmentType() == AttachmentType.SWA;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?