Home » synapse-1.2-src » org.apache.synapse.endpoints » [javadoc | source]

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
   10    *   http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    *  Unless required by applicable law or agreed to in writing,
   13    *  software distributed under the License is distributed on an
   14    *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    *  KIND, either express or implied.  See the License for the
   16    *  specific language governing permissions and limitations
   17    *  under the License.
   18    */
   19   
   20   package org.apache.synapse.endpoints;
   21   
   22   import org.apache.commons.logging.Log;
   23   import org.apache.commons.logging.LogFactory;
   24   import org.apache.synapse.FaultHandler;
   25   import org.apache.synapse.MessageContext;
   26   import org.apache.synapse.SynapseConstants;
   27   import org.apache.synapse.endpoints.utils.EndpointDefinition;
   28   import org.apache.synapse.statistics.impl.EndPointStatisticsStack;
   29   
   30   import java.util.Stack;
   31   
   32   /**
   33    * This class represents an endpoint with epr as addressing to header of the message. It is
   34    * responsible for sending the message to the epr specified in the message To header, performing
   35    * retries if a failure occurred and informing the parent endpoint if a failure couldn't be
   36    * recovered.
   37    */
   38   public class DefaultEndpoint extends FaultHandler implements Endpoint {
   39   
   40       protected Log log;
   41       
   42       protected static final Log trace = LogFactory.getLog(SynapseConstants.TRACE_LOGGER);
   43   
   44       /**
   45        * Name of the endpoint. Used for named endpoints which can be referred using the key attribute
   46        * of indirect endpoints.
   47        */
   48       private String name = null;
   49   
   50       /**
   51        * Stores the endpoint details for this endpoint. Details include EPR, WS-Addressing
   52        * information, WS-Security information, etc.
   53        */
   54       private EndpointDefinition endpoint = null;
   55   
   56       /**
   57        * Parent endpoint of this endpoint if this used inside another endpoint. Possible parents are
   58        * LoadbalanceEndpoint, SALoadbalanceEndpoint and FailoverEndpoint objects.
   59        */
   60       private Endpoint parentEndpoint = null;
   61   
   62       public DefaultEndpoint() {
   63           log = LogFactory.getLog(this.getClass());
   64       }
   65   
   66       public EndpointDefinition getEndpoint() {
   67           return endpoint;
   68       }
   69   
   70       public void setEndpoint(EndpointDefinition endpoint) {
   71           this.endpoint = endpoint;
   72       }
   73   
   74       public String getName() {
   75           return name;
   76       }
   77   
   78       public void setName(String name) {
   79           this.name = name.trim();
   80       }
   81   
   82       /**
   83        * This will always return true, because the endpoint epr is dependent on the message being
   84        * processed
   85        *
   86        * @param synMessageContext not being used
   87        * @return true
   88        */
   89       public boolean isActive(MessageContext synMessageContext) {
   90           return true;
   91       }
   92   
   93       /**
   94        * since this is a virtual representation of an endpoint and the epr changes from message
   95        * to message setting active state doesn't have a meaning
   96        *
   97        * @param active            not being used
   98        * @param synMessageContext not being used
   99        */
  100       public synchronized void setActive(boolean active, MessageContext synMessageContext) {
  101           // no implementation according to the behavior
  102       }
  103   
  104       /**
  105        * Sends the message through this endpoint. This method just handles statistics related
  106        * functions and gives the message to the Synapse environment to send. It does not add any
  107        * endpoint specific details to the message context. These details are added only to the cloned
  108        * message context by the Axis2FlexibleMepClient. So that we can reuse the original message
  109        * context for resending through different endpoints.
  110        *
  111        * @param synCtx MessageContext sent by client to Synapse
  112        */
  113       public void send(MessageContext synCtx) {
  114   
  115           boolean traceOn = isTraceOn(synCtx);
  116           boolean traceOrDebugOn = isTraceOrDebugOn(traceOn);
  117   
  118           if (traceOrDebugOn) {
  119               traceOrDebug(traceOn, "Start : Default Endpoint");
  120   
  121               if (traceOn && trace.isTraceEnabled()) {
  122                   trace.trace("Message : " + synCtx.getEnvelope());
  123               }
  124           }
  125   
  126           String endPointName = this.getName();
  127           if (endPointName == null) {
  128               endPointName = SynapseConstants.ANONYMOUS_ENDPOINT;
  129           }
  130   
  131           // Setting Required property to collect the End Point statistics
  132           boolean statisticsEnable
  133                   = (SynapseConstants.STATISTICS_ON == endpoint.getStatisticsState());
  134           if (statisticsEnable) {
  135               EndPointStatisticsStack endPointStatisticsStack = null;
  136               Object statisticsStackObj =
  137                       synCtx.getProperty(org.apache.synapse.SynapseConstants.ENDPOINT_STATS);
  138               if (statisticsStackObj == null) {
  139                   endPointStatisticsStack = new EndPointStatisticsStack();
  140                   synCtx.setProperty(org.apache.synapse.SynapseConstants.ENDPOINT_STATS,
  141                           endPointStatisticsStack);
  142               } else if (statisticsStackObj instanceof EndPointStatisticsStack) {
  143                   endPointStatisticsStack = (EndPointStatisticsStack) statisticsStackObj;
  144               }
  145               if (endPointStatisticsStack != null) {
  146                   boolean isFault = synCtx.getEnvelope().getBody().hasFault();
  147                   endPointStatisticsStack.put(endPointName, System.currentTimeMillis(),
  148                           !synCtx.isResponse(), statisticsEnable, isFault);
  149               }
  150           }
  151   
  152           if (synCtx.getTo() != null && synCtx.getTo().getAddress() != null) {
  153               if (traceOrDebugOn) {
  154                   traceOrDebug(traceOn, "Sending message to endpoint : " +
  155                           endPointName + " resolves to address = " + synCtx.getTo().getAddress());
  156                   traceOrDebug(traceOn, "SOAPAction: " + (synCtx.getSoapAction() != null ?
  157                           synCtx.getSoapAction() : "null"));
  158                   traceOrDebug(traceOn, "WSA-Action: " + (synCtx.getWSAAction() != null ?
  159                           synCtx.getWSAAction() : "null"));
  160   
  161                   if (traceOn && trace.isTraceEnabled()) {
  162                       trace.trace("Envelope : \n" + synCtx.getEnvelope());
  163                   }
  164               }
  165           }
  166   
  167           // register this as the immediate fault handler for this message.
  168           synCtx.pushFaultHandler(this);
  169   
  170           // add this as the last endpoint to process this message. it is used by statistics code.
  171           synCtx.setProperty(SynapseConstants.PROCESSED_ENDPOINT, this);
  172   
  173           synCtx.getEnvironment().send(endpoint, synCtx);
  174       }
  175   
  176       public void onChildEndpointFail(Endpoint endpoint, MessageContext synMessageContext) {
  177           // nothing to do as this is a leaf level endpoint
  178       }
  179   
  180       public void setParentEndpoint(Endpoint parentEndpoint) {
  181           this.parentEndpoint = parentEndpoint;
  182       }
  183   
  184       public void onFault(MessageContext synCtx) {
  185           // perform retries here
  186   
  187           if (parentEndpoint != null) {
  188               parentEndpoint.onChildEndpointFail(this, synCtx);
  189           } else {
  190               Stack faultStack = synCtx.getFaultStack();
  191               if (!faultStack.isEmpty()) {
  192                   ((FaultHandler) faultStack.pop()).handleFault(synCtx);
  193               }
  194           }
  195       }
  196   
  197       /**
  198        * Should this mediator perform tracing? True if its explicitly asked to
  199        * trace, or its parent has been asked to trace and it does not reject it
  200        *
  201        * @param msgCtx the current message
  202        * @return true if tracing should be performed
  203        */
  204       protected boolean isTraceOn(MessageContext msgCtx) {
  205           return (endpoint.getTraceState() == SynapseConstants.TRACING_ON) ||
  206                   (endpoint.getTraceState() == SynapseConstants.TRACING_UNSET &&
  207                           msgCtx.getTracingState() == SynapseConstants.TRACING_ON);
  208       }
  209   
  210       /**
  211        * Is tracing or debug logging on?
  212        *
  213        * @param isTraceOn is tracing known to be on?
  214        * @return true, if either tracing or debug logging is on
  215        */
  216       protected boolean isTraceOrDebugOn(boolean isTraceOn) {
  217           return isTraceOn || log.isDebugEnabled();
  218       }
  219   
  220       /**
  221        * Perform Trace and Debug logging of a message @INFO (trace) and DEBUG (log)
  222        *
  223        * @param traceOn is runtime trace on for this message?
  224        * @param msg     the message to log/trace
  225        */
  226       protected void traceOrDebug(boolean traceOn, String msg) {
  227           if (traceOn) {
  228               trace.info(msg);
  229           }
  230           if (log.isDebugEnabled()) {
  231               log.debug(msg);
  232           }
  233       }
  234   }

Home » synapse-1.2-src » org.apache.synapse.endpoints » [javadoc | source]