Home » synapse-1.2-src » org.apache.synapse.mediators.throttle » [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   package org.apache.synapse.mediators.throttle;
   20   
   21   import org.apache.axiom.om.OMElement;
   22   import org.apache.axiom.om.OMNamespace;
   23   import org.apache.axiom.om.OMNode;
   24   import org.apache.commons.logging.Log;
   25   import org.apache.commons.logging.LogFactory;
   26   import org.apache.synapse.Mediator;
   27   import org.apache.synapse.config.xml.AbstractMediatorSerializer;
   28   import org.apache.synapse.config.xml.SequenceMediatorSerializer;
   29   import org.apache.synapse.config.xml.XMLConfigConstants;
   30   import org.apache.synapse.mediators.base.SequenceMediator;
   31   
   32   /**
   33    * The Serializer for Throttle Mediator  saving throttle instance
   34    *
   35    * <pre>
   36    * &lt;throttle [onReject="string"] [onAccept="string"] id="string"&gt;
   37    *   (&lt;policy key="string"/&gt; | &lt;policy&gt;..&lt;/policy&gt;)
   38    *    &lt;onReject&gt;..&lt;/onReject&gt;?
   39    *    &lt;onAccept&gt;..&lt;/onAccept&gt;?
   40    * &lt;/throttle&gt;
   41    * </pre>
   42    */
   43   
   44   public class ThrottleMediatorSerializer extends AbstractMediatorSerializer {
   45   
   46       private static final Log log = LogFactory.getLog(ThrottleMediatorSerializer.class);
   47   
   48       private static final OMNamespace throttleNS
   49               = fac.createOMNamespace(XMLConfigConstants.SYNAPSE_NAMESPACE, "throttle");
   50   
   51       public OMElement serializeMediator(OMElement parent, Mediator m) {
   52           if (!(m instanceof ThrottleMediator)) {
   53               handleException("Invalid Mediator has passed to serializer");
   54           }
   55           ThrottleMediator throttleMediator = (ThrottleMediator) m;
   56           OMElement throttle = fac.createOMElement("throttle", synNS);
   57           OMElement policy = fac.createOMElement("policy", synNS);
   58           String key = throttleMediator.getPolicyKey();
   59           if (key != null) {
   60               policy.addAttribute(fac.createOMAttribute(
   61                       "key", nullNS, key));
   62               throttle.addChild(policy);
   63           } else {
   64               OMNode inlinePolicy = throttleMediator.getInLinePolicy();
   65               if (inlinePolicy != null) {
   66                   policy.addChild(inlinePolicy);
   67                   throttle.addChild(policy);
   68               }
   69           }
   70           saveTracingState(throttle, throttleMediator);
   71   
   72           String id = throttleMediator.getId();
   73           if(id != null){
   74               throttle.addAttribute(fac.createOMAttribute(
   75                       "id", nullNS, id));
   76           }
   77           
   78           String onReject = throttleMediator.getOnRejectSeqKey();
   79           if (onReject != null) {
   80               throttle.addAttribute(fac.createOMAttribute(XMLConfigConstants.ONREJECT, nullNS, onReject));
   81           } else {
   82               Mediator mediator = throttleMediator.getOnRejectMediator();
   83               SequenceMediatorSerializer serializer = new SequenceMediatorSerializer();
   84               if (mediator != null && mediator instanceof SequenceMediator) {
   85                   OMElement element = serializer.serializeAnonymousSequence(null,
   86                           (SequenceMediator) mediator);
   87                   element.setLocalName(XMLConfigConstants.ONREJECT);
   88                   throttle.addChild(element);
   89               }
   90           }
   91           String onAccept = throttleMediator.getOnAcceptSeqKey();
   92           if (onAccept != null) {
   93               throttle.addAttribute(fac.createOMAttribute(XMLConfigConstants.ONACCEPT, nullNS, onAccept));
   94           } else {
   95               Mediator mediator = throttleMediator.getOnAcceptMediator();
   96               SequenceMediatorSerializer serializer = new SequenceMediatorSerializer();
   97               if (mediator != null && mediator instanceof SequenceMediator) {
   98                   OMElement element = serializer.serializeAnonymousSequence(null,
   99                           (SequenceMediator) mediator);
  100                   element.setLocalName(XMLConfigConstants.ONACCEPT);
  101                   throttle.addChild(element);
  102               }
  103           }
  104           if (parent != null) {
  105               parent.addChild(throttle);
  106           }
  107           return throttle;
  108   
  109       }
  110   
  111       public String getMediatorClassName() {
  112           return ThrottleMediator.class.getName();
  113       }
  114   }

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