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.axiom.om.OMElement;
   23   import org.apache.axis2.clustering.ClusterManager;
   24   import org.apache.axis2.context.ConfigurationContext;
   25   import org.apache.commons.logging.Log;
   26   import org.apache.commons.logging.LogFactory;
   27   import org.apache.synapse.FaultHandler;
   28   import org.apache.synapse.MessageContext;
   29   import org.apache.synapse.SynapseConstants;
   30   import org.apache.synapse.core.axis2.Axis2MessageContext;
   31   import org.apache.synapse.endpoints.utils.EndpointDefinition;
   32   import org.apache.synapse.statistics.impl.EndPointStatisticsStack;
   33   
   34   import java.util.Stack;
   35   
   36   /**
   37    * WSDLEndpoint represents the endpoints built using a wsdl document. It stores the details about
   38    * the endpoint in a EndpointDefinition object. Once the WSDLEndpoint object is constructed, it
   39    * should not access the wsdl document at runtime to obtain endpoint information. If it is necessary
   40    * to create an endpoint using a dynamic wsdl, store the endpoint configuration in the registry and
   41    * create a dynamic wsdl endpoint using that registry key.
   42    * <p/>
   43    * TODO: This should allow various policies to be applied on fine grained level (e.g. operations).
   44    */
   45   public class WSDLEndpoint extends FaultHandler implements Endpoint {
   46   
   47       private static final Log log = LogFactory.getLog(WSDLEndpoint.class);
   48       private static final Log trace = LogFactory.getLog(SynapseConstants.TRACE_LOGGER);
   49   
   50       private String name = null;
   51       private String wsdlURI;
   52       private OMElement wsdlDoc;
   53       private String serviceName;
   54       private String portName;
   55   
   56       private Endpoint parentEndpoint = null;
   57       private EndpointDefinition endpoint = null;
   58   
   59       /**
   60        * The endpoint context , place holder for keep any runtime states related to the endpoint
   61        */
   62       private final EndpointContext endpointContext = new EndpointContext();
   63   
   64       /**
   65        * Sends the message through this endpoint. This method just handles statistics related
   66        * functions and gives the message to the Synapse environment to send. It does not add any
   67        * endpoint specific details to the message context. These details are added only to the cloned
   68        * message context by the Axis2FlexibleMepClient. So that we can reuse the original message
   69        * context for resending through different endpoints.
   70        *
   71        * @param synCtx MessageContext sent by client to Synapse
   72        */
   73       public void send(MessageContext synCtx) {
   74   
   75           boolean traceOn = isTraceOn(synCtx);
   76           boolean traceOrDebugOn = isTraceOrDebugOn(traceOn);
   77   
   78           if (traceOrDebugOn) {
   79               traceOrDebug(traceOn, "Start : WSDL Endpoint");
   80   
   81               if (traceOn && trace.isTraceEnabled()) {
   82                   trace.trace("Message : " + synCtx.getEnvelope());
   83               }
   84           }
   85   
   86           if (endpoint.getAddress() != null) {
   87   
   88               String eprAddress = endpoint.getAddress();
   89               boolean isClusteringEnable = false;
   90               // get Axis2 MessageContext and ConfigurationContext
   91               org.apache.axis2.context.MessageContext axisMC =
   92                       ((Axis2MessageContext) synCtx).getAxis2MessageContext();
   93               ConfigurationContext cc = axisMC.getConfigurationContext();
   94   
   95               //The check for clustering environment
   96               ClusterManager clusterManager = cc.getAxisConfiguration().getClusterManager();
   97               if (clusterManager != null &&
   98                       clusterManager.getContextManager() != null) {
   99                   isClusteringEnable = true;
  100               }
  101   
  102               String endPointName = this.getName();
  103               if (endPointName == null) {
  104   
  105                   if (traceOrDebugOn && isClusteringEnable) {
  106                       log.warn("In a clustering environment , the endpoint  name should be " +
  107                               "specified even for anonymous endpoints. Otherwise, the clustering " +
  108                               "would not be functioned correctly if there are more than one " +
  109                               "anonymous endpoints. ");
  110                   }
  111                   endPointName = SynapseConstants.ANONYMOUS_ENDPOINT;
  112               }
  113   
  114               if (isClusteringEnable) {
  115                   // if this is a cluster environment , then set configuration context
  116                   // to endpoint context
  117                   if (endpointContext.getConfigurationContext() == null) {
  118                       endpointContext.setConfigurationContext(cc);
  119                       endpointContext.setContextID(endPointName);
  120                   }
  121               }
  122   
  123               // Setting Required property to collect the End Point statistics
  124               boolean statisticsEnable =
  125                       (SynapseConstants.STATISTICS_ON == endpoint.getStatisticsState());
  126   
  127               if (statisticsEnable) {
  128                   EndPointStatisticsStack endPointStatisticsStack = null;
  129                   Object statisticsStackObj =
  130                           synCtx.getProperty(org.apache.synapse.SynapseConstants.ENDPOINT_STATS);
  131                   if (statisticsStackObj == null) {
  132                       endPointStatisticsStack = new EndPointStatisticsStack();
  133                       synCtx.setProperty(org.apache.synapse.SynapseConstants.ENDPOINT_STATS,
  134                               endPointStatisticsStack);
  135                   } else if (statisticsStackObj instanceof EndPointStatisticsStack) {
  136                       endPointStatisticsStack = (EndPointStatisticsStack) statisticsStackObj;
  137                   }
  138                   if (endPointStatisticsStack != null) {
  139                       boolean isFault = synCtx.getEnvelope().getBody().hasFault();
  140                       endPointStatisticsStack.put(endPointName, System.currentTimeMillis(),
  141                               !synCtx.isResponse(), statisticsEnable, isFault);
  142                   }
  143               }
  144               if (traceOrDebugOn) {
  145                   traceOrDebug(traceOn, "Sending message to WSDL endpoint : " +
  146                           endPointName + " resolves to address = " + eprAddress);
  147                   traceOrDebug(traceOn, "SOAPAction: " + (synCtx.getSoapAction() != null ?
  148                           synCtx.getSoapAction() : "null"));
  149                   traceOrDebug(traceOn, "WSA-Action: " + (synCtx.getWSAAction() != null ?
  150                           synCtx.getWSAAction() : "null"));
  151   
  152                   if (traceOn && trace.isTraceEnabled()) {
  153                       trace.trace("Envelope : \n" + synCtx.getEnvelope());
  154                   }
  155               }
  156   
  157               // register this as the immediate fault handler for this message.
  158               synCtx.pushFaultHandler(this);
  159   
  160               // add this as the last endpoint to process this message. it is used by statistics code.
  161               synCtx.setProperty(SynapseConstants.PROCESSED_ENDPOINT, this);
  162   
  163               synCtx.getEnvironment().send(endpoint, synCtx);
  164           }
  165       }
  166   
  167       public void onFault(MessageContext synCtx) {
  168           // perform retries here
  169   
  170           // if this endpoint has actually failed, inform the parent.
  171           setActive(false, synCtx);
  172   
  173           if (parentEndpoint != null) {
  174               parentEndpoint.onChildEndpointFail(this, synCtx);
  175           } else {
  176               Stack faultStack = synCtx.getFaultStack();
  177               if (!faultStack.isEmpty()) {
  178                   ((FaultHandler) faultStack.pop()).handleFault(synCtx);
  179               }
  180           }
  181       }
  182   
  183       public void onChildEndpointFail(Endpoint endpoint, MessageContext synMessageContext) {
  184           // WSDLEndpoint does not contain any child endpoints. So this method will never be called.
  185       }
  186   
  187       public String getName() {
  188           return name;
  189       }
  190   
  191       public void setName(String name) {
  192           this.name = name.trim();
  193       }
  194   
  195       public String getWsdlURI() {
  196           return wsdlURI;
  197       }
  198   
  199       public void setWsdlURI(String wsdlURI) {
  200           this.wsdlURI = wsdlURI;
  201       }
  202   
  203       public OMElement getWsdlDoc() {
  204           return wsdlDoc;
  205       }
  206   
  207       public void setWsdlDoc(OMElement wsdlDoc) {
  208           this.wsdlDoc = wsdlDoc;
  209       }
  210   
  211       public String getServiceName() {
  212           return serviceName;
  213       }
  214   
  215       public void setServiceName(String serviceName) {
  216           this.serviceName = serviceName;
  217       }
  218   
  219       public String getPortName() {
  220           return portName;
  221       }
  222   
  223       public void setPortName(String portName) {
  224           this.portName = portName;
  225       }
  226   
  227       /**
  228        * Checks if the endpoint is active (failed or not). If endpoint is in failed state and
  229        * suspendOnFailDuration has elapsed, it will be set to active.
  230        *
  231        * @param synMessageContext MessageContext of the current message. This is not used here.
  232        * @return true if endpoint is active. false otherwise.
  233        */
  234       public boolean isActive(MessageContext synMessageContext) {
  235           boolean active = endpointContext.isActive();
  236           if (!active) {
  237               long recoverOn = endpointContext.getRecoverOn();
  238               if (System.currentTimeMillis() > recoverOn) {
  239                   active = true;
  240                   endpointContext.setActive(true);
  241               }
  242           }
  243   
  244           if (log.isDebugEnabled()) {
  245               log.debug("WSDLEndpoint with name '" + name + "' is in "
  246                       + (active ? "active" : "inactive") + " state");
  247           }
  248   
  249           return active;
  250       }
  251   
  252       /**
  253        * Sets if endpoint active or not. if endpoint is set as failed (active = false), the recover on
  254        * time is calculated so that it will be activated after the recover on time.
  255        *
  256        * @param active            true if active. false otherwise.
  257        * @param synMessageContext MessageContext of the current message. This is not used here.
  258        */
  259       public void setActive(boolean active, MessageContext synMessageContext) {
  260   
  261           if (!active) {
  262               if (endpoint.getSuspendOnFailDuration() != -1) {
  263                   // Calculating a new value by adding suspendOnFailDuration to current time.
  264                   // as the endpoint is set as failed
  265                   endpointContext.setRecoverOn(
  266                           System.currentTimeMillis() + endpoint.getSuspendOnFailDuration());
  267               } else {
  268                   endpointContext.setRecoverOn(Long.MAX_VALUE);
  269               }
  270           }
  271   
  272           endpointContext.setActive(true);
  273       }
  274   
  275       public void setParentEndpoint(Endpoint parentEndpoint) {
  276           this.parentEndpoint = parentEndpoint;
  277       }
  278   
  279       public EndpointDefinition getEndpoint() {
  280           return endpoint;
  281       }
  282   
  283       public void setEndpoint(EndpointDefinition endpoint) {
  284           this.endpoint = endpoint;
  285       }
  286   
  287       /**
  288        * Should this mediator perform tracing? True if its explicitly asked to
  289        * trace, or its parent has been asked to trace and it does not reject it
  290        *
  291        * @param msgCtx the current message
  292        * @return true if tracing should be performed
  293        */
  294       protected boolean isTraceOn(MessageContext msgCtx) {
  295           return
  296                   (endpoint.getTraceState() == SynapseConstants.TRACING_ON) ||
  297                           (endpoint.getTraceState() == SynapseConstants.TRACING_UNSET &&
  298                                   msgCtx.getTracingState() == SynapseConstants.TRACING_ON);
  299       }
  300   
  301       /**
  302        * Is tracing or debug logging on?
  303        *
  304        * @param isTraceOn is tracing known to be on?
  305        * @return true, if either tracing or debug logging is on
  306        */
  307       protected boolean isTraceOrDebugOn(boolean isTraceOn) {
  308           return isTraceOn || log.isDebugEnabled();
  309       }
  310   
  311       /**
  312        * Perform Trace and Debug logging of a message @INFO (trace) and DEBUG (log)
  313        *
  314        * @param traceOn is runtime trace on for this message?
  315        * @param msg     the message to log/trace
  316        */
  317       protected void traceOrDebug(boolean traceOn, String msg) {
  318           if (traceOn) {
  319               trace.info(msg);
  320           }
  321           if (log.isDebugEnabled()) {
  322               log.debug(msg);
  323           }
  324       }
  325   }

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