Package org.apache.synapse

Examples of org.apache.synapse.SynapseLog


    private Input input = null;

    private Output output = null;

    public boolean mediate(MessageContext synCtx) {
        SynapseLog synLog = getLog(synCtx);

        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("Start : Smooks mediator");

            if (synLog.isTraceTraceEnabled()) {
                synLog.traceTrace("Message : " + synCtx.getEnvelope());
            }
        }

        // check weather we need to create the smooks configuration
        lock.lock();
        try {
            if (isCreationOrRecreationRequired(synCtx.getConfiguration())) {
                smooks = createSmooksConfig(synCtx);
            }
        } finally {
            lock.unlock();
        }

        // get the input as an stream
        ByteArrayInputStream byteArrayInputStream = input.process(synCtx, synLog);

        // create the execution context for smooks. This is required for every message
        ExecutionContext executionContext = smooks.createExecutionContext();

        // create a output stream for store the result
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        StreamResult streamResult = new StreamResult(outputStream);

        // filter the message through smooks
        smooks.filterSource(executionContext, new StreamSource(byteArrayInputStream), streamResult);

        // add result
        output.process(outputStream, synCtx, synLog);

        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("End : Smooks mediator");

            if (synLog.isTraceTraceEnabled()) {
                synLog.traceTrace("Message : " + synCtx.getEnvelope());
            }
        }

        return true;
    }
View Full Code Here


     * stored as a local entry or can be stored in the registry.
     * @param synCtx synapse context
     * @return Smooks configuration
     */
    private Smooks createSmooksConfig(MessageContext synCtx) {
        SynapseLog log = getLog(synCtx);
        Object o = synCtx.getEntry(configKey);
        if (o == null) {
            handleException("Cannot find the object for smooks config key: "
                    + configKey, synCtx);
        }

        InputStream in = SynapseConfigUtils.getInputStream(o);
        if (in == null) {
            handleException("Cannot get the input stream from the config key: " +
                    configKey, synCtx);
        }

        try {
            Smooks smooks = new Smooks(in);
            if (log.isTraceOrDebugEnabled()) {
                log.traceOrDebug("Smooks configuration is created from the config key: "
                        + configKey);               
            }
            return smooks;
        } catch (IOException e) {
            handleException("I/O error occurred while creating the Smooks " +
View Full Code Here

    private Value topic = null;

    private SynapseXPath expression = null;

    public boolean mediate(MessageContext synCtx) {
        SynapseLog synLog = getLog(synCtx);

        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("Start : Event mediator");

            if (synLog.isTraceTraceEnabled()) {
                synLog.traceTrace("Message : " + synCtx.getEnvelope());
            }
        }

        String topicValue = null;
        if (topic != null) {
            topicValue = topic.evaluateValue(synCtx);
        } else {
            org.apache.axis2.context.MessageContext mc =
                    ((Axis2MessageContext)synCtx).getAxis2MessageContext();
            try {
                topicValue = EventBrokerUtils.extractTopicFromMessage(mc);
            } catch (WSEventException e) {
                handleException("Error extracting the topic from the message", e, synCtx);
            }
        }

        if (topicValue == null) {
            handleException("Topic for the event cannot be found", synCtx);
        } else {
            org.apache.axis2.context.MessageContext mc =
                    ((Axis2MessageContext)synCtx).getAxis2MessageContext();
            EventBroker broker = (EventBroker) mc.getConfigurationContext().
                    getProperty("mediation.event.broker");
            if (broker == null) {
                handleException("EventBroker cannot be found", synCtx);
            } else {
                Message message = new Message();
                if (expression == null) {
                    message.setMessage(synCtx.getEnvelope().getBody().getFirstElement());
                } else {
                    try {
                        Object o = expression.selectSingleNode(synCtx);
                        if (o instanceof OMElement)  {
                            message.setMessage((OMElement)o);
                        } else {
                            handleException("The result of the expression:" +
                                    expression + " should be an OMElement", synCtx);
                        }
                    } catch (JaxenException e) {
                        handleException("Error evaluating the expression: " +
                                expression, synCtx);
                    }
                }

                int tenantID = SuperTenantCarbonContext.getCurrentContext(
                        mc.getConfigurationContext()).getTenantId();
                try {
                    SuperTenantCarbonContext.startTenantFlow();
                    SuperTenantCarbonContext.getCurrentContext().setTenantId(tenantID);
                    SuperTenantCarbonContext.getCurrentContext().getTenantDomain(true);
                    broker.publish(message, topicValue);
                } catch (EventBrokerException e) {
                    handleException("Error publishing the event to the broker", e, synCtx);
                } catch (Exception e) {
                    log.error("Error in setting tenant information", e);
                } finally {
                    SuperTenantCarbonContext.endTenantFlow();
                }
            }
        }

        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("End : Event mediator");

            if (synLog.isTraceTraceEnabled()) {
                synLog.traceTrace("Message : " + synCtx.getEnvelope());
            }
        }

        return true;
    }
View Full Code Here

     * @param synCtx - MessageContext which is subjected to the cloning
     * @return boolean true if this needs to be further mediated (continueParent=true)
     */
    public boolean mediate(MessageContext synCtx) {

        SynapseLog synLog = getLog(synCtx);

        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("Start : Clone mediator");

            if (synLog.isTraceTraceEnabled()) {
                synLog.traceTrace("Message : " + synCtx.getEnvelope());
            }
        }

        // get the targets list, clone the message for the number of targets and then
        // mediate the cloned messages using the targets
        Iterator<Target> iter = targets.iterator();
        int i = 0;
        while (iter.hasNext()) {
            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug("Submitting " + (i+1) + " of " + targets.size() +
                    " messages for processing in parallel");
            }

            iter.next().mediate(getClonedMessageContext(synCtx, i++, targets.size()));
        }

        // if the continuation of the parent message is stopped from here set the RESPONSE_WRITTEN
        // property to SKIP to skip the blank http response
        OperationContext opCtx
            = ((Axis2MessageContext) synCtx).getAxis2MessageContext().getOperationContext();
        if (!continueParent && opCtx != null) {
            opCtx.setProperty(Constants.RESPONSE_WRITTEN, "SKIP");
        }

        // finalize tracing and debugging
        synLog.traceOrDebug("End : Clone mediator");

        // if continue parent is true mediators after the clone will be called for the further
        // mediation of the message which is subjected for clonning (parent message)
        return continueParent;
    }
View Full Code Here

     * @param synCtx the current message where the transformation will apply
     * @return true always
     */
    public boolean mediate(MessageContext synCtx) {

        SynapseLog synLog = getLog(synCtx);

        synLog.traceOrDebug("Start : XSLT mediator");
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Message : " + synCtx.getEnvelope());
        }

        try {
            performXSLT(synCtx, synLog);

        } catch (Exception e) {
            handleException("Unable to perform XSLT transformation using : " + xsltKey +
                " against source XPath : " + source, e, synCtx);

        }

        synLog.traceOrDebug("End : XSLT mediator");

        return true;
    }
View Full Code Here

        }
    }

    public boolean mediate(MessageContext synCtx) {

        SynapseLog synLog = getLog(synCtx);

        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("Start : Cache mediator");

            if (synLog.isTraceTraceEnabled()) {
                synLog.traceTrace("Message : " + synCtx.getEnvelope());
            }
        }

        // if maxMessageSize is specified check for the message size before processing
        if (maxMessageSize > 0) {
            FixedByteArrayOutputStream fbaos = new FixedByteArrayOutputStream(maxMessageSize);
            try {
                MessageHelper.cloneSOAPEnvelope(synCtx.getEnvelope()).serialize(fbaos);
            } catch (XMLStreamException e) {
                handleException("Error in checking the message size", e, synCtx);
            } catch (SynapseException syne) {
                synLog.traceOrDebug("Message size exceeds the upper bound for caching, " +
                            "request will not be cached");
                return true;
            }
        }

        ConfigurationContext cfgCtx =
            ((Axis2MessageContext) synCtx).getAxis2MessageContext().getConfigurationContext();
        if (cfgCtx == null) {
            handleException("Unable to perform caching, "
                + " ConfigurationContext cannot be found", synCtx);
            return false; // never executes.. but keeps IDE happy
        }

        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("Looking up cache at scope : " + scope + " with ID : "
                    + cacheKey);
        }

        // look up cache
        Object prop = cfgCtx.getPropertyNonReplicable(CachingConstants.CACHE_MANAGER);
        CacheManager cacheManager;
        if (prop != null && prop instanceof CacheManager) {
            cacheManager = (CacheManager) prop;
        } else {
            synchronized (cfgCtx) {
                // check again after taking the lock to make sure no one else did it before us
                prop = cfgCtx.getPropertyNonReplicable(CachingConstants.CACHE_MANAGER);
                if (prop != null && prop instanceof CacheManager) {
                    cacheManager = (CacheManager) prop;

                } else {
                    synLog.traceOrDebug("Creating/recreating the cache object");
                    cacheManager = new CacheManager();
                    cfgCtx.setProperty(CachingConstants.CACHE_MANAGER, cacheManager);
                }
            }
        }

        boolean result = true;
        try {

            if (synCtx.isResponse()) {
                processResponseMessage(synCtx, cfgCtx, synLog, cacheManager);

            } else {
                result = processRequestMessage(synCtx, synLog, cacheManager);
            }

        } catch (ClusteringFault clusteringFault) {
            synLog.traceOrDebug("Unable to replicate Cache mediator state among the cluster");
        }

        synLog.traceOrDebug("End : Cache mediator");

        return result;
    }
View Full Code Here

     * @return true, always
     */
    public boolean mediate(MessageContext synCtx) {

        String name = (this instanceof DBLookupMediator ? "DBLookup" : "DBReport");
        SynapseLog synLog = getLog(synCtx);

        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("Start : " + name + " mediator");

            if (synLog.isTraceTraceEnabled()) {
                synLog.traceTrace("Message : " + synCtx.getEnvelope());
            }
        }

        for (Statement aStatement : statementList) {
            if (aStatement != null) {
                processStatement(aStatement, synCtx);
            }
        }

        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("End : " + name + " mediator");
        }
        return true;
    }
View Full Code Here

     * @throws SQLException on error
     */
    protected PreparedStatement getPreparedStatement(Statement stmnt,
                                                     MessageContext msgCtx) throws SQLException {

        SynapseLog synLog = getLog(msgCtx);

        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("Getting a connection from DataSource " + getDSName() +
                " and preparing statement : " + stmnt.getRawStatement());
        }

        Connection con = getDataSource().getConnection();
        if (con == null) {
            String msg = "Connection from DataSource " + getDSName() + " is null.";
            log.error(msg);
            throw new SynapseException(msg);
        }

        if (dataSource instanceof BasicDataSource) {

            BasicDataSource basicDataSource = (BasicDataSource) dataSource;
            int numActive = basicDataSource.getNumActive();
            int numIdle = basicDataSource.getNumIdle();
            String connectionId = Integer.toHexString(con.hashCode());

            DBPoolView dbPoolView = getDbPoolView();
            if (dbPoolView != null) {
                dbPoolView.setNumActive(numActive);
                dbPoolView.setNumIdle(numIdle);
                dbPoolView.updateConnectionUsage(connectionId);
            }

            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug("[ DB Connection : " + con + " ]");
                synLog.traceOrDebug("[ DB Connection instance identifier : " +
                        connectionId + " ]");
                synLog.traceOrDebug("[ Number of Active Connection : " + numActive + " ]");
                synLog.traceOrDebug("[ Number of Idle Connection : " + numIdle + " ]");
            }
        }

        PreparedStatement ps = con.prepareStatement(stmnt.getRawStatement());

        // set parameters if any
        List<Statement.Parameter> params = stmnt.getParameters();
        int column = 1;

        for (Statement.Parameter param : params) {
            if (param == null) {
                continue;
            }
            String value = (param.getPropertyName() != null ?
                    param.getPropertyName() : param.getXpath().stringValueOf(msgCtx));

            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug("Setting as parameter : " + column + " value : " + value +
                        " as JDBC Type : " + param.getType() + "(see java.sql.Types for valid " +
                        "types)");
            }

            switch (param.getType()) {
                // according to J2SE 1.5 /docs/guide/jdbc/getstart/mapping.html
                case Types.CHAR:
                case Types.VARCHAR:
                case Types.LONGVARCHAR: {
                    if (value != null && value.length() != 0) {
                        ps.setString(column++, value);
                    } else {
                        ps.setString(column++, null);
                    }
                    break;
                }
                case Types.NUMERIC:
                case Types.DECIMAL: {
                    if (value != null && value.length() != 0) {
                        ps.setBigDecimal(column++, new BigDecimal(value));
                    } else {
                        ps.setBigDecimal(column++, null);
                    }
                    break;
                }
                case Types.BIT: {
                    if (value != null && value.length() != 0) {
                        ps.setBoolean(column++, Boolean.parseBoolean(value));
                    } else {
                        ps.setNull(column++, Types.BIT);
                    }
                    break;
                }
                case Types.TINYINT: {
                    if (value != null && value.length() != 0) {
                        ps.setByte(column++, Byte.parseByte(value));
                    } else {
                        ps.setNull(column++, Types.TINYINT);
                    }
                    break;
                }
                case Types.SMALLINT: {
                    if (value != null && value.length() != 0) {
                        ps.setShort(column++, Short.parseShort(value));
                    } else {
                        ps.setNull(column++, Types.SMALLINT);
                    }
                    break;
                }
                case Types.INTEGER: {
                    if (value != null && value.length() != 0) {
                        ps.setInt(column++, Integer.parseInt(value));
                    } else {
                        ps.setNull(column++, Types.INTEGER);
                    }
                    break;
                }
                case Types.BIGINT: {
                    if (value != null && value.length() != 0) {
                        ps.setLong(column++, Long.parseLong(value));
                    } else {
                        ps.setNull(column++, Types.BIGINT);
                    }
                    break;
                }
                case Types.REAL: {
                    if (value != null && value.length() != 0) {
                        ps.setFloat(column++, Float.parseFloat(value));
                    } else {
                        ps.setNull(column++, Types.REAL);
                    }
                    break;
                }
                case Types.FLOAT: {
                    if (value != null && value.length() != 0) {
                        ps.setDouble(column++, Double.parseDouble(value));
                    } else {
                        ps.setNull(column++, Types.FLOAT);
                    }
                    break;
                }
                case Types.DOUBLE: {
                    if (value != null && value.length() != 0) {
                        ps.setDouble(column++, Double.parseDouble(value));
                    } else {
                        ps.setNull(column++, Types.DOUBLE);
                    }
                    break;
                }
                // skip BINARY, VARBINARY and LONGVARBINARY
                case Types.DATE: {
                    if (value != null && value.length() != 0) {
                        ps.setDate(column++, Date.valueOf(value));
                    } else {
                        ps.setNull(column++, Types.DATE);
                    }
                    break;
                }
                case Types.TIME: {
                    if (value != null && value.length() != 0) {
                        ps.setTime(column++, Time.valueOf(value));
                    } else {
                        ps.setNull(column++, Types.TIME);
                    }
                    break;
                }
                case Types.TIMESTAMP: {
                    if (value != null && value.length() != 0) {
                        ps.setTimestamp(column++, Timestamp.valueOf(value));
                    } else {
                        ps.setNull(column++, Types.TIMESTAMP);
                    }
                    break;
                }
                // skip CLOB, BLOB, ARRAY, DISTINCT, STRUCT, REF, JAVA_OBJECT
                default: {
                    String msg = "Trying to set an un-supported JDBC Type : " + param.getType() +
                            " against column : " + column + " and statement : " +
                            stmnt.getRawStatement() +
                            " used by a DB mediator against DataSource : " + getDSName() +
                            " (see java.sql.Types for valid type values)";
                    handleException(msg, msgCtx);
                }
            }
        }

        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("Successfully prepared statement : " + stmnt.getRawStatement() +
                " against DataSource : " + getDSName());
        }
        return ps;
    }
View Full Code Here

public class BuilderMediator extends AbstractMediator {
    private MessageBuilder messageBuilder = new MessageBuilder();

    public boolean mediate(MessageContext msgCtx) {
        SynapseLog synLog = getLog(msgCtx);

        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("Build mediator : start");
        }

        SOAPEnvelope envelope = msgCtx.getEnvelope();

        org.apache.axis2.context.MessageContext messageContext =
                ((Axis2MessageContext) msgCtx).getAxis2MessageContext();

        OMElement contentEle = envelope.getBody().getFirstChildWithName(
                RelayConstants.BINARY_CONTENT_QNAME);

        if (contentEle != null) {

            OMNode node = contentEle.getFirstOMChild();

            if (node != null && (node instanceof OMText)) {
                OMText binaryDataNode = (OMText) node;
                DataHandler dh = (DataHandler) binaryDataNode.getDataHandler();

                if (dh == null) {
                    if (synLog.isTraceOrDebugEnabled()) {
                        synLog.auditWarn("Message has the Binary content element. " +
                                        "But doesn't have binary content embedded within it");
                    }
                    return true;
                }

                DataSource dataSource = dh.getDataSource();

                //Ask the data source to stream, if it has not alredy cached the request
                if (dataSource instanceof StreamingOnRequestDataSource) {
                    ((StreamingOnRequestDataSource) dataSource).setLastUse(true);
                }

                InputStream in = null;
                try {
                    in = dh.getInputStream();
                } catch (IOException e) {
                    handleException("Error retrieving InputStream from data handler", e, msgCtx);
                }

                String contentType = (String) messageContext.getProperty(
                        Constants.Configuration.CONTENT_TYPE);

                OMElement element = null;
                if (synLog.isTraceOrDebugEnabled()) {
                    synLog.traceOrDebug("Trying to build a message with content type :" +
                            contentType);
                }
                try {
                    element = messageBuilder.getDocument(contentType, messageContext, in);
                } catch (Exception e) {
                    synLog.auditWarn("Error buidling message with content type :" + contentType);
                }

                if (element != null) {
                    try {
                        messageContext.setEnvelope(TransportUtils.createSOAPEnvelope(element));

                        // set the formatters map to the message context
                        messageContext.setProperty(MessageBuilder.RELAY_FORMATTERS_MAP,
                                messageBuilder.getFormatters());
                    } catch (AxisFault axisFault) {
                        handleException("Failed to set the built SOAP " +
                                "Envelope to the message context", axisFault, msgCtx);
                    }
                } else {
                    synLog.auditWarn("Error occurred while trying to build the message, " +
                            "trying to send the message throgh");
                }

                //now we have undone thing done  by Relay
                if (synLog.isTraceOrDebugEnabled()) {
                    synLog.traceOrDebug("Build mediator : end");
                }
            } else {
                if (synLog.isTraceOrDebugEnabled()) {
                    //if is not wrapped binary content, there is nothing to be done
                    synLog.traceOrDebug("not wrapped binary content, there is nothing to be done");
                }
            }
        }
        return true;
    }
View Full Code Here

     * @param synCtx the Synapse message context
     * @return the boolean result from the script invocation
     */
    public boolean mediate(MessageContext synCtx) {

        SynapseLog synLog = getLog(synCtx);

        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("Start : Script mediator");

            if (synLog.isTraceTraceEnabled()) {
                synLog.traceTrace("Message : " + synCtx.getEnvelope());
            }
        }

        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("Scripting language : " + language + " source " +
                    (key == null ? ": specified inline " : " loaded with key : " + key) +
                    (function != null ? " function : " + function : ""));
        }

        boolean returnValue;
        if (multiThreadedEngine) {
            returnValue = invokeScript(synCtx);
        } else {
            // TODO: change to use a pool of script engines (requires an update to BSF)
            synchronized (scriptEngine.getClass()) {
                returnValue = invokeScript(synCtx);
            }
        }

        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Result message after execution of script : " + synCtx.getEnvelope());
        }

        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("End : Script mediator return value : " + returnValue);
        }

        return returnValue;
    }
View Full Code Here

TOP

Related Classes of org.apache.synapse.SynapseLog

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.