Home » synapse-1.2-src » org.apache.synapse.startup.quartz » [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.startup.quartz;
   21   
   22   import java.util.ArrayList;
   23   import java.util.Iterator;
   24   import java.util.List;
   25   import java.util.StringTokenizer;
   26   
   27   import javax.xml.namespace.QName;
   28   
   29   import org.apache.axiom.om.OMAttribute;
   30   import org.apache.axiom.om.OMElement;
   31   import org.apache.commons.logging.Log;
   32   import org.apache.commons.logging.LogFactory;
   33   import org.apache.synapse.config.xml.XMLConfigConstants;
   34   import org.apache.synapse.config.xml.PropertyHelper;
   35   import org.apache.synapse.config.xml.StartupFactory;
   36   import org.apache.synapse.Startup;
   37   import org.apache.synapse.SynapseException;
   38   
   39   /**
   40    * <task class="org.my.synapse.Task" name="string">
   41    *  <property name="stringProp" value="String"/>
   42    *  <property name="xmlProp">
   43    *   <somexml>config</somexml>
   44    *  </property>
   45    *  <trigger ([[count="10"]? interval="1000"] | [cron="0 * 1 * * ?"] | [once=(true | false)])/>
   46    * </task>
   47    */
   48   public class SimpleQuartzFactory implements StartupFactory {
   49   
   50       public final static QName TASK
   51           = new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "task");
   52   
   53       private final static QName TRIGGER
   54           = new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "trigger");
   55   
   56       private final static QName PROPERTY
   57           = new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "property");
   58   
   59       private final static Log log = LogFactory.getLog(SimpleQuartzFactory.class);
   60   
   61       public Startup createStartup(OMElement el) {
   62           
   63           if (log.isDebugEnabled()) {
   64               log.debug("Creating SimpleQuartz Task");
   65           }
   66           
   67           if (el.getQName().equals(TASK)) {
   68               
   69               SimpleQuartz q = new SimpleQuartz();
   70   
   71               String name = el.getAttributeValue(
   72                   new QName(XMLConfigConstants.NULL_NAMESPACE, "name"));
   73               if (name != null) {
   74                   q.setName(name);
   75               } else {
   76                   handleException("Name for a task is required, missing name in the task");
   77               }
   78   
   79               // set the task class
   80               OMAttribute classAttr = el.getAttribute(new QName("class"));
   81               if (classAttr != null && classAttr.getAttributeValue() != null) {
   82                   String classname = classAttr.getAttributeValue();
   83                   try {
   84                       Class.forName(classname).newInstance();
   85                   } catch (Exception e) {
   86                       handleException("Failed to load task class " + classname, e);
   87                   }
   88                   q.setJobClass(classname);
   89               } else {
   90                   handleException("Syntax error in the Task : no task class specified");
   91               }
   92               
   93               // set pinned server list
   94               OMAttribute pinnedServers = el.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "pinnedServers"));
   95               if (pinnedServers != null) {
   96                   String pinnedServersValue = pinnedServers.getAttributeValue();
   97                   if (pinnedServersValue == null) {
   98                       // default to all servers
   99                   } else {
  100                       StringTokenizer st = new StringTokenizer(pinnedServersValue, " ,");
  101                       List pinnedServersList = new ArrayList();
  102                       while (st.hasMoreTokens()) {
  103                           String token = st.nextToken();
  104                           if (token.length() != 0) {
  105                             pinnedServersList.add(token);
  106                           }
  107                       }
  108                       q.setPinnedServers(pinnedServersList);
  109                   }
  110               }
  111   
  112               // next sort out the property children
  113               Iterator it = el.getChildrenWithName(PROPERTY);
  114               while (it.hasNext()) {
  115                   OMElement prop = (OMElement) it.next();
  116                   if (PropertyHelper.isStaticProperty(prop)) {
  117                       q.addProperty(prop);
  118                   } else {
  119                       handleException("Tasks does not support dynamic properties");
  120                   }
  121               }
  122   
  123               // setting the trigger to the task
  124               OMElement trigger = el.getFirstChildWithName(TRIGGER);
  125               if (trigger != null) {
  126   
  127                   OMAttribute count = trigger.getAttribute(new QName("count"));
  128                   if (count != null) {
  129                       try {
  130                           q.setCount(Integer.parseInt(count.getAttributeValue()));
  131                       } catch (Exception e) {
  132                           handleException("Failed to parse trigger count as an integer", e);
  133                       }
  134                   }
  135   
  136                   OMAttribute once = trigger.getAttribute(new QName("once"));
  137                   if (once != null && Boolean.TRUE.toString().equals(once.getAttributeValue())) {
  138                       q.setCount(1);
  139                       q.setInterval(1);
  140                   }
  141   
  142                   OMAttribute repeatInterval = trigger.getAttribute(new QName("interval"));
  143                   if (repeatInterval == null && q.getCount() > 1) {
  144                       handleException("Trigger seems to be " +
  145                           "a simple trigger, but no interval specified");
  146                   } else if (repeatInterval != null && repeatInterval.getAttributeValue() != null) {
  147                       try {
  148                           long repeatIntervalInSeconds = Long.parseLong(repeatInterval.getAttributeValue());
  149                           long repeatIntervalInMillis = repeatIntervalInSeconds * 1000;
  150                           q.setInterval(repeatIntervalInMillis);
  151                       } catch (Exception e) {
  152                           handleException("Failed to parse trigger interval as a long value", e);
  153                       }
  154                   }
  155   
  156                   OMAttribute expr = trigger.getAttribute(new QName("cron"));
  157                   if (expr == null && q.getInterval() == 0) {
  158                       q.setCount(1);
  159                       q.setInterval(1);
  160                   } else if (expr != null && q.getInterval() > 0) {
  161                       handleException("Trigger syntax error : " +
  162                           "both cron and simple trigger attributes are present");
  163                   } else if (expr != null && expr.getAttributeValue() != null) {
  164                       q.setCron(expr.getAttributeValue());
  165                   }
  166   
  167               } else {
  168                   q.setCount(1);
  169                   q.setInterval(1);
  170               }
  171   
  172               return q;
  173           } else {
  174               handleException("Syntax error in the task : wrong QName for the task");
  175               return null;
  176           }
  177       }
  178   
  179       public Class getSerializerClass() {
  180           return SimpleQuartzSerializer.class;
  181       }
  182   
  183       public QName getTagQName() {
  184           return TASK;
  185       }
  186   
  187       private void handleException(String message, Exception e) {
  188           log.error(message);
  189           throw new SynapseException(message, e);
  190       }
  191   
  192       private void handleException(String message) {
  193           log.error(message);
  194           throw new SynapseException(message);
  195       }
  196   
  197   }

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