All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.anaptecs.jeaf.fwk.tools.scheduling.SchedulingImporter Maven / Gradle / Ivy

There is a newer version: 1.8.0
Show newest version
/*
 * anaptecs GmbH, Burgstr. 96, 72764 Reutlingen, Germany
 * 
 * Copyright 2004 - 2014 All rights reserved.
 */
package com.anaptecs.jeaf.fwk.tools.scheduling;

import static com.anaptecs.jeaf.fwk.tools.scheduling.SchedulingDTD.DTD_NAME;
import static com.anaptecs.jeaf.fwk.tools.scheduling.SchedulingDTD.SYSTEM_ID;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.security.auth.login.LoginException;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.anaptecs.jeaf.application.base.JEAFApplication;
import com.anaptecs.jeaf.core.annotations.JEAFService;
import com.anaptecs.jeaf.services.scheduling.OneTimeExecution;
import com.anaptecs.jeaf.services.scheduling.PeriodicExecution;
import com.anaptecs.jeaf.services.scheduling.ScheduledTask;
import com.anaptecs.jeaf.services.scheduling.SchedulingApplicationException;
import com.anaptecs.jeaf.services.scheduling.SchedulingService;
import com.anaptecs.jeaf.tools.api.Tools;
import com.anaptecs.jeaf.xfun.api.checks.Assert;

/**
 * The Class SchedulingImporter.
 */
public class SchedulingImporter extends JEAFApplication {
  /**
   * Reference to SchedulingService. The reference is never null.
   */
  @JEAFService
  private SchedulingService schedulingService;

  /**
   * Instantiates a new scheduling importer.
   * 
   * @throws LoginException the login exception
   */
  public SchedulingImporter( ) throws LoginException {
    super(true);
  }

  /**
   * Method returns if the concrete command line application has files as input or not. This influences how parameters
   * that are passed to the application will be handled.
   * 
   * @return boolean Method returns true if the application expects files as input and false in all other cases.
   */
  @Override
  protected boolean hasFileInput( ) {
    return true;
  }

  /**
   * Method returns if the concrete command line application writes files as output or not. This influences how
   * parameters that are passed to the application will be handled.
   * 
   * @return boolean Method returns true if the application expects files as input and false in all other cases.
   */
  @Override
  protected boolean hasFileOutput( ) {
    return false;
  }

  /*
   * (non-Javadoc)
   * 
   * @see com.anaptecs.jeaf.fwk.core.util.JEAFApplication#printUsage()
   */
  @Override
  protected void printUsage( ) {
  }

  /**
   * Method returns the extensions of all supported file types.
   * 
   * @return {@link List} List with the extensions of all supported file types. The method must not return null.
   */
  protected List getSupportedExtensions( ) {
    List lExtensions = new ArrayList();
    lExtensions.add(".xml");
    return lExtensions;
  }

  /**
   * Run application.
   * 
   * @param pInputFileNames List contains the names of all files that should be handled by the application. The
   * parameter is never null.
   * @param pOutputDirectoryName Absolute path of the output directory where results should be written to. The parameter
   * may be null.
   * @param pOptions Options that are passed to the application. The parameter is never null. If no options are defined
   * the list is empty.
   * @throws SchedulingApplicationException
   * @throws ClassNotFoundException
   */
  @Override
  protected void runApplication( List pInputFileNames, String pOutputDirectoryName,
      Map pOptions )
    throws ClassNotFoundException, SchedulingApplicationException {
    for (String lFileName : pInputFileNames) {
      this.processSchedulingDefinition(lFileName);
    }
  }

  /**
   * Process scheduling definition.
   * 
   * @param pInputFileName the input file name
   * @throws ClassNotFoundException
   * @throws SchedulingApplicationException
   */
  private void processSchedulingDefinition( String pInputFileName )
    throws ClassNotFoundException, SchedulingApplicationException {
    // Check parameters.
    Assert.assertNotNull(pInputFileName, "pInputFileName");

    // Parse definition of scheduling.
    Document lMessageResource = Tools.getXMLTools().parseFile(pInputFileName, true, SYSTEM_ID);
    final String lSystemID = lMessageResource.getDoctype().getSystemId();
    if (DTD_NAME.equals(lSystemID) == true) {
      final NodeList lScheduledTaskElements = lMessageResource.getElementsByTagName(SchedulingDTD.SCHEDULED_TASK);
      for (int i = 0; i < lScheduledTaskElements.getLength(); i++) {
        Node lScheduledTaskElement = lScheduledTaskElements.item(i);
        NamedNodeMap lScheduledTaskAttributes = lScheduledTaskElement.getAttributes();

        Node lServiceClassAttribute = lScheduledTaskAttributes.getNamedItem(SchedulingDTD.SERVICE_CLASS);
        String lServiceClassValue = lServiceClassAttribute.getNodeValue();
        Class lServiceClass = Class.forName(lServiceClassValue);

        Node lDescriptionAttribute = lScheduledTaskAttributes.getNamedItem(SchedulingDTD.DESCRIPTION);
        String lDescriptionValue = lDescriptionAttribute.getNodeValue();

        ScheduledTask lScheduledTask = ScheduledTask.Builder.newBuilder().build();
        lScheduledTask.setServiceClass(lServiceClass);
        lScheduledTask.setDescription(lDescriptionValue);

        NodeList lScheduledTaskChildNodes = lScheduledTaskElement.getChildNodes();
        for (int j = 0; j < lScheduledTaskChildNodes.getLength(); j++) {
          Node lNextScheduledTaskChildNode = lScheduledTaskChildNodes.item(j);
          NamedNodeMap lExecutionStrategyAttributes = lNextScheduledTaskChildNode.getAttributes();

          if (lExecutionStrategyAttributes != null) {
            // We either have a OneTimeExecution or a PeriodicExecution.
            Node lOnceExecutionTimeAttribute =
                lExecutionStrategyAttributes.getNamedItem(SchedulingDTD.ONCE_EXECUTION_TIME);
            if (lOnceExecutionTimeAttribute != null) {
              String lOnceExecutionTimeValue = lOnceExecutionTimeAttribute.getNodeValue();
              Date lOnceExecutionTimeDate = Tools.getDateTools().toDate(lOnceExecutionTimeValue);
              Calendar lOnceExecutionTimeCalendar = Tools.getDateTools().toCalendar(lOnceExecutionTimeDate);

              OneTimeExecution lOneTimeExecution = OneTimeExecution.Builder.newBuilder().build();
              lOneTimeExecution.setExecutionTime(lOnceExecutionTimeCalendar);

              lScheduledTask.setExecutionStrategy(lOneTimeExecution);
            }
            else {
              Node lPeriodInitialStartAttribute =
                  lExecutionStrategyAttributes.getNamedItem(SchedulingDTD.PERIOD_INITIAL_START);
              String lPeriodInitialStartValue = lPeriodInitialStartAttribute.getNodeValue();
              Date lPeriodInitialStartDate = Tools.getDateTools().toDate(lPeriodInitialStartValue);
              Calendar lPeriodInitialStartCalendar = Tools.getDateTools().toCalendar(lPeriodInitialStartDate);

              Node lPeriodAttribute = lExecutionStrategyAttributes.getNamedItem(SchedulingDTD.PERIOD);
              String lPeriodValue = lPeriodAttribute.getNodeValue();
              Integer lPeriod = Integer.valueOf(lPeriodValue);

              Node lPeriodEndTimeAttribute = lExecutionStrategyAttributes.getNamedItem(SchedulingDTD.PERIOD_END_TIME);
              String lPeriodEndTimeValue = lPeriodEndTimeAttribute.getNodeValue();
              Date lPeriodEndTimeDate = Tools.getDateTools().toDate(lPeriodEndTimeValue);
              Calendar lPeriodEndTimeCalendar = Tools.getDateTools().toCalendar(lPeriodEndTimeDate);

              PeriodicExecution lPeriodicExecution = PeriodicExecution.Builder.newBuilder().build();
              lPeriodicExecution.setInitialStart(lPeriodInitialStartCalendar);
              lPeriodicExecution.setPeriod(lPeriod);
              lPeriodicExecution.setEndTime(lPeriodEndTimeCalendar);

              lScheduledTask.setExecutionStrategy(lPeriodicExecution);
            }
          }
        }

        ScheduledTask lTask = schedulingService.scheduleTask(lScheduledTask);
        System.out.println("Created scheduled task: " + lTask.getDescription());
      }
    }
    else {
      throw new IllegalArgumentException("Input file " + pInputFileName + " does not contain scheduled tasks.");
    }
  }

  /**
   * The main method.
   * 
   * @param pArguments the arguments
   */
  public static void main( String[] pArguments ) {
    SchedulingImporter lSchedulingImporter;
    try {
      lSchedulingImporter = new SchedulingImporter();
      final int lExitCode = lSchedulingImporter.start(pArguments);
      System.exit(lExitCode);
    }
    catch (LoginException e) {
      e.printStackTrace();
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy