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.SynapseException;
   28   import org.apache.synapse.endpoints.utils.EndpointDefinition;
   29   
   30   /**
   31    * This class represents the endpoints referred by keys. It does not store the actual referred
   32    * endpoint as a private variable as it could expire. Therefore, it only stores the key and gets the
   33    * actual endpoint from the synapse configuration.
   34    * <p/>
   35    * As this is also an instance of endpoint, this can be used any place, where a normal endpoint is
   36    * used.
   37    */
   38   public class IndirectEndpoint extends FaultHandler implements Endpoint {
   39   
   40       private static final Log trace = LogFactory.getLog(SynapseConstants.TRACE_LOGGER);
   41       private static final Log log = LogFactory.getLog(IndirectEndpoint.class);
   42   
   43       private String name = null;
   44       private String key = null;
   45       private Endpoint parentEndpoint = null;
   46   
   47       public void send(MessageContext synMessageContext) {
   48   
   49           // get the actual endpoint and send
   50           Endpoint endpoint = synMessageContext.getEndpoint(key);
   51           if (endpoint == null) {
   52               handleException("Reference to non-existent endpoint for key : " + key);
   53           }
   54   
   55           assert endpoint != null;
   56           if (endpoint.isActive(synMessageContext)) {
   57                // register this as the immediate fault handler for this message.
   58               synMessageContext.pushFaultHandler(this);
   59               endpoint.send(synMessageContext);
   60           } else {
   61               // if this is a child of some other endpoint, inform parent about the failure.
   62               // if not, inform to the next fault handler.
   63               if (parentEndpoint != null) {
   64                   auditWarn("Endpoint : " + endpoint.getName() + " is currently inactive" +
   65                           " - invoking parent endpoint", synMessageContext);
   66                   parentEndpoint.onChildEndpointFail(this, synMessageContext);
   67   
   68               } else {
   69                   auditWarn("Endpoint : " + endpoint.getName() + " is currently inactive" +
   70                           " - invoking fault handler / assuming failure", synMessageContext);
   71   
   72                   Object o = synMessageContext.getFaultStack().pop();
   73                   if (o != null) {
   74                       ((FaultHandler) o).handleFault(synMessageContext);
   75                   }
   76               }
   77           }
   78       }
   79   
   80       public String getName() {
   81           return name;
   82       }
   83   
   84       public void setName(String name) {
   85           this.name = name.trim();
   86       }
   87   
   88       public String getKey() {
   89           return key;
   90       }
   91   
   92       public void setKey(String key) {
   93           this.key = key;
   94       }
   95   
   96       /**
   97        * IndirectEndpoints are active if its referref endpoint is active and vise versa. Therefore,
   98        * this returns if its referred endpoint is active or not.
   99        *
  100        * @param synMessageContext MessageContext of the current message.
  101        * @return true if the referred endpoint is active. false otherwise.
  102        */
  103       public boolean isActive(MessageContext synMessageContext) {
  104           Endpoint endpoint = synMessageContext.getEndpoint(key);
  105           if (endpoint == null) {
  106               handleException("Reference to non-existent endpoint for key : " + key);
  107           }
  108   
  109           assert endpoint != null;
  110           return endpoint.isActive(synMessageContext);
  111       }
  112   
  113       /**
  114        * Activating or deactivating an IndirectEndpoint is the activating or deactivating its
  115        * referref endpoint. Therefore, this sets the active state of its referred endpoint.
  116        *
  117        * @param active            true if active. false otherwise.
  118        * @param synMessageContext MessageContext of the current message.
  119        */
  120       public void setActive(boolean active, MessageContext synMessageContext) {
  121           Endpoint endpoint = synMessageContext.getEndpoint(key);
  122           if (endpoint == null) {
  123               handleException("Reference to non-existent endpoint for key : " + key);
  124           }
  125   
  126           assert endpoint != null;
  127           endpoint.setActive(active, synMessageContext);
  128       }
  129   
  130       public void setParentEndpoint(Endpoint parentEndpoint) {
  131           this.parentEndpoint = parentEndpoint;
  132       }
  133   
  134       public void onChildEndpointFail(Endpoint endpoint, MessageContext synMessageContext) {
  135   
  136           // if this is a child of some other endpoint, inform parent about the failure.
  137           // if not, inform to the next fault handler.
  138           if (parentEndpoint != null) {
  139               parentEndpoint.onChildEndpointFail(this, synMessageContext);
  140           } else {
  141               Object o = synMessageContext.getFaultStack().pop();
  142               if (o != null) {
  143                   ((FaultHandler) o).handleFault(synMessageContext);
  144               }
  145           }
  146       }
  147   
  148       private void handleException(String msg) {
  149           log.error(msg);
  150           throw new SynapseException(msg);
  151       }
  152   
  153       protected void auditWarn(String msg, MessageContext msgContext) {
  154           log.warn(msg);
  155           if (msgContext.getServiceLog() != null) {
  156               msgContext.getServiceLog().warn(msg);
  157           }
  158           if (shouldTrace(msgContext)) {
  159               trace.warn(msg);
  160           }
  161       }
  162   
  163       public boolean shouldTrace(MessageContext synCtx) {
  164           Endpoint endpoint = synCtx.getEndpoint(key);
  165           EndpointDefinition endptDefn = null;
  166           if (endpoint instanceof AddressEndpoint) {
  167               AddressEndpoint addEndpt = (AddressEndpoint) endpoint;
  168               endptDefn = addEndpt.getEndpoint();
  169           } else if (endpoint instanceof WSDLEndpoint) {
  170               WSDLEndpoint wsdlEndpt = (WSDLEndpoint) endpoint;
  171               endptDefn = wsdlEndpt.getEndpoint();
  172           }
  173   
  174           return endptDefn != null && ((endptDefn.getTraceState() == SynapseConstants.TRACING_ON) ||
  175                   (endptDefn.getTraceState() == SynapseConstants.TRACING_UNSET &&
  176                           synCtx.getTracingState() == SynapseConstants.TRACING_ON));
  177       }
  178   
  179       public void onFault(MessageContext synMessageContext) {
  180           // At this point,child endpoint is in inactive state
  181           // if this is a child of some other endpoint, inform parent about the failure.
  182           // if not, inform to the next fault handler.
  183           if (parentEndpoint != null) {
  184               parentEndpoint.onChildEndpointFail(this, synMessageContext);
  185           } else {
  186               Object o = synMessageContext.getFaultStack().pop();
  187               if (o != null) {
  188                   ((FaultHandler) o).handleFault(synMessageContext);
  189               }
  190           }
  191       }
  192   }

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