public void process()

in engines/servicemix-scripting/src/main/java/org/apache/servicemix/scripting/ScriptingEndpoint.java [192:350]


    public void process(MessageExchange exchange) throws Exception {
        if (exchange == null) {
            return;
        }

        // The component acts as a consumer, this means this exchange is
        // received because
        // we sent it to another component. As it is active, this is either an
        // out or a fault
        // If this component does not create / send exchanges, you may just
        // throw an
        // UnsupportedOperationException
        if (exchange.getRole() == Role.CONSUMER) { 
            return; 
        } else if (exchange.getRole() == MessageExchange.Role.PROVIDER) {
            // The component acts as a provider, this means that another component
            // has requested our
            // service
            // As this exchange is active, this is either an in or a fault (out are
            // send by this
            // component)

            // Exchange is finished
            if (exchange.getStatus() == ExchangeStatus.DONE) {
                return;
            } else if (exchange.getStatus() == ExchangeStatus.ERROR) {
                // Exchange has been aborted with an exception
                return;
            } else if (exchange.getFault() != null) {
                // Fault message
                done(exchange);
            } else {
                InOnly outExchange = null;
                NormalizedMessage outMsg = null;
                NormalizedMessage inMsg = exchange.getMessage("in");

                Bindings scriptBindings = engine.createBindings();
                scriptBindings.put(KEY_CONTEXT, getContext());
                scriptBindings.put(KEY_IN_EXCHANGE, exchange);
                scriptBindings.put(KEY_IN_MSG, inMsg);
                scriptBindings.put(KEY_ENDPOINT, this);
                scriptBindings.put(KEY_CHANNEL, getChannel());
                scriptBindings.put(KEY_ENDPOINTNAME, getEndpoint());
                scriptBindings.put(KEY_SERVICENAME, getService());
                scriptBindings.put(KEY_INTERFACENAME, getInterfaceName());
                scriptBindings.put(KEY_LOGGER, getScriptLogger());

                if (exchange instanceof InOnly || exchange instanceof RobustInOnly) {
                    outExchange = getExchangeFactory().createInOnlyExchange();
                    String processCorrelationId = (String)exchange.getProperty(JbiConstants.CORRELATION_ID);
                    if (processCorrelationId != null) {
                        outExchange.setProperty(JbiConstants.CORRELATION_ID, processCorrelationId);
                    }                    
                    
                    outMsg = outExchange.createMessage();
                    outExchange.setMessage(outMsg, "in");
                    
                    scriptBindings.put(KEY_OUT_EXCHANGE, outExchange);
                    scriptBindings.put(KEY_OUT_MSG, outMsg);                    
                } else {
                    outMsg = exchange.createMessage();
                    exchange.setMessage(outMsg, "out");
                    scriptBindings.put(KEY_OUT_EXCHANGE, exchange);
                    scriptBindings.put(KEY_OUT_MSG, outMsg);
                }
                
                try {
                    scriptBindings.put(KEY_SCRIPT, getScript().getFile().getAbsolutePath());
                } catch (IOException ioex) {
                    scriptBindings.put(KEY_SCRIPT, getScript());
                }

                scriptBindings.put(KEY_USER_BINDINGS, bindings);
                scriptBindings.put(KEY_COMPONENT_NAMESPACE, scriptBindings);

                // call back method for custom marshaler to inject it's own beans
                this.marshaler.registerUserBeans(this, exchange, scriptBindings);

                // get the input stream to the script code from the marshaler
                InputStream is = null;
                try {
                    is = this.marshaler.getScriptCode(this, exchange);
                } catch (IOException ioex) {
                    logger.error("Unable to load script in marshaler: {}", this.marshaler.getClass().getName(), ioex);
                }
                // if the marshaler does not return a valid input stream, use the script property to load it
                if (is != null) {
                    try {
                        // execute the script
                        this.engine.eval(new InputStreamReader(is), scriptBindings);
                    } catch (ScriptException ex) {
                        logger.error("Error executing the script: " + ex.getFileName() + " at line: "
                                     + ex.getLineNumber() + " and column: " + ex.getColumnNumber(), ex);
                        throw ex;
                    }
                } else {
                    try {
                        // use the compiled script interfaces if possible
                        if (compiledScript == null && engine instanceof Compilable) {
                            compiledScript = ((Compilable) engine).compile(new InputStreamReader(this.script.getInputStream()));
                        }
                        if (compiledScript != null) {
                            // execute the script
                            compiledScript.eval(scriptBindings);
                        } else {
                            // execute the script
                            engine.eval(new InputStreamReader(this.script.getInputStream()), scriptBindings);
                        }
                    } catch (IOException ioex) {
                        logger.error("Unable to load the script {}", script.getFilename(), ioex);
                        throw new MessagingException("Unable to load the script " + script.getFilename());
                    } catch (ScriptException ex) {
                        logger.error("Error executing the script: " + ex.getFileName() + " at line: "
                                     + ex.getLineNumber() + " and column: " + ex.getColumnNumber(), ex);
                        throw ex;
                    }
                }

                if (!isDisableOutput()) {
                    boolean txSync = exchange.isTransacted() && Boolean.TRUE.equals(exchange.getProperty(JbiConstants.SEND_SYNC));
                    
                    // copy headers
                    if (isCopyProperties()) {
                        copyProperties(inMsg, outMsg);
                    }
                    
                    // copy attachments
                    if (isCopyAttachments()) {
                        copyAttachments(inMsg, outMsg);
                    }                                        
                    
                    // on InOut exchanges we always do answer
                    if (exchange instanceof InOut || exchange instanceof InOptionalOut) {
                        if (txSync) {
                            getChannel().sendSync(exchange);
                        } else {
                            getChannel().send(exchange);
                        }
                    } else {
                        configureTarget(outExchange);
                        
                        if (txSync) {
                            getChannel().sendSync(outExchange);
                        } else {
                            getChannel().send(outExchange);
                        }
                        // if InOnly exchange then we only answer if user wants to
                        done(exchange);
                    }
                } else {
                    // if InOnly exchange then we only answer if user wants to
                    done(exchange);
                }    
            }
        } else { 
            // Unknown role
            throw new MessagingException("ScriptingEndpoint.process(): Unknown role: " + exchange.getRole());
        }
    }