Package org.jbpm.wire.binding

Source Code of org.jbpm.wire.binding.JobExecutorBinding

/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jbpm.wire.binding;

import org.jbpm.client.CommandService;
import org.jbpm.jobexecutor.JobExecutor;
import org.jbpm.wire.descriptor.AbstractDescriptor;
import org.jbpm.wire.descriptor.IntegerDescriptor;
import org.jbpm.wire.descriptor.ObjectDescriptor;
import org.jbpm.wire.descriptor.StringDescriptor;
import org.jbpm.wire.descriptor.TypeRefDescriptor;
import org.jbpm.wire.operation.FieldOperation;
import org.jbpm.wire.operation.InvokeOperation;
import org.jbpm.xml.Binding;
import org.jbpm.xml.Parse;
import org.jbpm.xml.Parser;
import org.jbpm.xml.XmlUtil;
import org.w3c.dom.Element;

/**
* @author Tom Baeyens
*/
public class JobExecutorBinding implements Binding {

  public Object parse(Element element, Parse parse, Parser parser) {
    // create a job executor object   
    ObjectDescriptor descriptor = new ObjectDescriptor();
    descriptor.setClassName(JobExecutor.class.getName());

    // by default invoke the start method, unless auto-start is disabled
    boolean autoStart = XmlUtil.booleanEquals(element, "auto-start", Boolean.TRUE);
    if (autoStart) {
      InvokeOperation invokeStartOperation = new InvokeOperation();
      invokeStartOperation.setMethodName("start");
      descriptor.addOperation(invokeStartOperation);
    }
   
    // by default eager initialization is enabled
//    descriptor.setInit(AbstractDescriptor.INIT_REQUIRED);

    // inject the command executor
    TypeRefDescriptor commandExecutorDescriptor = new TypeRefDescriptor();
    commandExecutorDescriptor.setContextClass(CommandService.class);

    FieldOperation commandExecutorInjection = new FieldOperation();
    commandExecutorInjection.setFieldName("commandService");
    commandExecutorInjection.setDescriptor(commandExecutorDescriptor);
   
    descriptor.addOperation(commandExecutorInjection);

    if (element.hasAttribute("name")) {
      StringDescriptor nameDescriptor = new StringDescriptor();
      nameDescriptor.setValue(element.getAttribute("name"));
      FieldOperation nameInjection = new FieldOperation();
      nameInjection.setFieldName("name");
      nameInjection.setDescriptor(nameDescriptor);
    }

    parseIntAttribute(element, "threads", descriptor, "nbrOfThreads", parse);
    parseIntAttribute(element, "idle", descriptor, "idleMillis", parse);
    parseIntAttribute(element, "idle-max", descriptor, "idleMillisMax", parse);
    parseIntAttribute(element, "history", descriptor, "historySize", parse);
    parseIntAttribute(element, "lock", descriptor, "lockMillis", parse);
    parseIntAttribute(element, "lock-buffer", descriptor, "lockMillisBuffer", parse);

    return descriptor;
  }

  private void parseIntAttribute(Element element, String attributeName, ObjectDescriptor descriptor, String fieldName, Parse parse) {
    if (element.hasAttribute(attributeName)) {
      String intText = element.getAttribute(attributeName);
      try {
        int intValue = Integer.parseInt(intText);
        IntegerDescriptor intValueDescriptor = new IntegerDescriptor();
        intValueDescriptor.setValue(intValue);
        FieldOperation intValueInjection = new FieldOperation();
        intValueInjection.setFieldName(fieldName);
        intValueInjection.setDescriptor(intValueDescriptor);
        descriptor.addOperation(intValueInjection);
      } catch (NumberFormatException e) {
        parse.addProblem("couldn't parse attribute "+attributeName+" as an integer: "+intText);
      }
    }
  }
}
TOP

Related Classes of org.jbpm.wire.binding.JobExecutorBinding

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.