public void process()

in engines/servicemix-wsn2005/src/main/java/org/apache/servicemix/wsn/component/WSNEndpoint.java [129:222]


    public void process(MessageExchange exchange) throws Exception {
        if (exchange.getStatus() == ExchangeStatus.DONE) {
            return;
        } else if (exchange.getStatus() == ExchangeStatus.ERROR) {
            return;
        }

        AtomicBoolean isJbiWrapped = new AtomicBoolean(false);
        Source source = exchange.getMessage("in").getContent();
        // Unwrap JBI message if needed
        source = JbiWrapperHelper.unwrap(source, isJbiWrapped);

        Object input = jaxbContext.createUnmarshaller().unmarshal(source);
        Method webMethod = null;
        Class inputClass = input.getClass();
        if (input instanceof JAXBElement) {
            inputClass = ((JAXBElement) input).getDeclaredType();
            input = ((JAXBElement) input).getValue(); 
        }
        for (Class clazz : endpointInterfaces) {
            for (Method mth : clazz.getMethods()) {
                Class[] params = mth.getParameterTypes();
                if (params.length == 1 && params[0].isAssignableFrom(inputClass)) {
                    if (webMethod == null) {
                        webMethod = mth;
                    } else if (!mth.getName().equals(webMethod.getName())) {
                        throw new IllegalStateException("Multiple methods matching parameters");
                    }
                }
            }
        }
        if (webMethod == null) {
            throw new IllegalStateException("Could not determine invoked web method");
        }
        boolean oneWay = webMethod.getAnnotation(Oneway.class) != null;
        Object output;
        try {
            output = webMethod.invoke(pojo, new Object[] {input });
        } catch (InvocationTargetException e) {
            if (e.getCause() instanceof Exception) {
                WebFault fa = (WebFault) e.getCause().getClass().getAnnotation(WebFault.class);
                if (!(exchange instanceof InOnly) && fa != null) {
                    BaseFaultType info = (BaseFaultType) e.getCause().getClass().getMethod("getFaultInfo").invoke(e.getCause());
                    // Set description if not already set
                    if (info.getDescription().size() == 0) {
                        BaseFaultType.Description desc = new BaseFaultType.Description();
                        desc.setValue(e.getCause().getMessage());
                        info.getDescription().add(desc);
                    }
                    // TODO: create originator field?
                    // Set timestamp if needed
                    if (info.getTimestamp() == null) {
                        info.setTimestamp(DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar()));
                    }
                    
                    // TODO: do we want to send the full stack trace here ?
                    //BaseFaultType.FaultCause cause = new BaseFaultType.FaultCause();
                    //cause.setAny(new XmlException(e.getCause()));
                    //info.setFaultCause(cause);
                    Fault fault = exchange.createFault();
                    exchange.setFault(fault);
                    Document doc = JbiWrapperHelper.createDocument();
                    JAXBElement el = new JAXBElement(new QName(fa.targetNamespace(), fa.name()), info.getClass(), null, info);
                    jaxbContext.createMarshaller().marshal(el, doc);
                    if (isJbiWrapped.get()) {
                        JbiWrapperHelper.wrap(doc);
                    }
                    fault.setContent(new DOMSource(doc));
                    send(exchange);
                    return;
                } else {
                    throw (Exception) e.getCause();
                }
            } else if (e.getCause() instanceof Error) {
                throw (Error) e.getCause();
            } else {
                throw new RuntimeException(e.getCause());
            }
        }
        if (oneWay) {
            exchange.setStatus(ExchangeStatus.DONE);
            send(exchange);
        } else {
            NormalizedMessage msg = exchange.createMessage();
            exchange.setMessage(msg, "out");
            Document doc = JbiWrapperHelper.createDocument();
            jaxbContext.createMarshaller().marshal(output, doc);
            if (isJbiWrapped.get()) {
                JbiWrapperHelper.wrap(doc);
            }
            msg.setContent(new DOMSource(doc));
            send(exchange);
        }
    }