Package sos.scheduler.file

Source Code of sos.scheduler.file.JobSchedulerTextProcessor

/********************************************************* begin of preamble
**
** Copyright (C) 2003-2010 Software- und Organisations-Service GmbH.
** All rights reserved.
**
** This file may be used under the terms of either the
**
**   GNU General Public License version 2.0 (GPL)
**
**   as published by the Free Software Foundation
**   http://www.gnu.org/licenses/gpl-2.0.txt and appearing in the file
**   LICENSE.GPL included in the packaging of this file.
**
** or the
** 
**   Agreement for Purchase and Licensing
**
**   as offered by Software- und Organisations-Service GmbH
**   in the respective terms of supply that ship with this file.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
** IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
** THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
** BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
** POSSIBILITY OF SUCH DAMAGE.
********************************************************** end of preamble*/
package sos.scheduler.file;


import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;

import sos.spooler.Job_impl;
import sos.spooler.Variable_set;
import sos.util.SOSLogger;
import sos.util.SOSSchedulerLogger;
import sos.util.SOSStandardLogger;
class SOSSchedulerTextProcessor{
   private SOSLogger logger;
   private String command;
   private HashMap commands;
   private File file;
   private String  param="";
   public SOSSchedulerTextProcessor(SOSLogger logger_, File file_, String command_ ) throws Exception {
    logger = logger_;
    this.command = command_.trim().toLowerCase().replaceAll("\\s{2,}", " ");
    this.file = file_;
   
      param       = command.replaceFirst("^[^\\s]+\\s*(.*)$", "$1");
      command     = command.replaceFirst("^([^\\s]+)\\s*.*$", "$1");

      commands = new HashMap();
    commands.put("count", "1");
    commands.put("countCaseSensitive", "2");
    commands.put("add", "3");
      commands.put("read", "4");
      commands.put("insert", "5");

   }
  
  public String exexute() throws Exception {
     return go();
  }
  public String exexute(String command_) throws Exception {
    param       =  command_.replaceFirst("^[^\\s]+\\s*(.*)$", "$1");
    command     =  command_.replaceFirst("^([^\\s]+)\\s*.*$", "$1");
    return go();
  }
  public String exexute(String command_, String param_) throws Exception {
     this.command = command_;
     this.param = param_;
     return go();
  }
 
  private String count(boolean ignoreCase) throws IOException {
     BufferedReader reader = new BufferedReader(new FileReader(file));
     String rec = null;
     String s = param;
     if (ignoreCase) s = s.toLowerCase();
       int i = 0;
     while((rec = reader.readLine()) != null) {
      if (ignoreCase) rec = rec.toLowerCase();
       
        while (rec.indexOf(s) >= 0) {
        i++;
        rec = rec.replaceFirst(s,"");
        }
       
     }
     reader.close();
     return String.valueOf(i);
  }

  private String add() throws IOException {
    FileOutputStream f = new FileOutputStream(file, true);
    String s = "\n"+param;
    f.write(s.getBytes(),0,s.length());
      return param;
  }

  private String insert() throws Exception {
    
     String line  =  param.replaceFirst("^[^\\s]+\\s*(.*).*$", "$1");
    
    
     String c = line.replaceFirst("^.*\\{char:\\s*([0-9]+)\\s*\\}.*$", "$1");
     if (!c.equals(line)){
       int intVal=0;
      try {
          intVal = Integer.parseInt(c, 10);
      }catch (NumberFormatException e){
        logger.warn(c + " is not a valid number. 0 assumed");
        intVal = 0;
      }
        char charVal = (char) intVal;
        String s =  String.valueOf(charVal);
        line = line.replaceFirst("\\{char:\\s*" + c + "\\s*\\}",s);
     }
  
    
     line = line+ "\n";
     param        =  param.replaceFirst("^([^\\s]+)\\s*.*$", "$1");    
   
     if (param.equals("last") ) {
       add();
     }else{
    
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String rec = "";  
    
        int i =0;
        if (param.equals("first")) {
          i=1;
        }else {
             try {
                i = Integer.parseInt(param);
             }catch (NumberFormatException e) {
              logger.error(param + " is not a valid line number: 0 assumed");
              i = 0;
             }
         }
   
       
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        i--;
        while ((rec = reader.readLine()) != null && i > 0) {
         rec = rec + "\n";
           baos.write(rec.getBytes());
           i--;
        }
        baos.write(line.getBytes());
        if (rec != null){
          rec = rec + "\n";
          baos.write(rec.getBytes());
        }
        while ((rec = reader.readLine()) != null ) {
            rec = rec + "\n";
          baos.write(rec.getBytes());
        }
        reader.close();    
   
        FileOutputStream f = new FileOutputStream(file, false);
          f.write(baos.toByteArray());
          f.close();        
     }
      return param;
  }
   
  private String read() throws Exception{
     BufferedReader reader = new BufferedReader(new FileReader(file));
     String rec = "";     String erg = "";
    
     int i =0;
     if (param.equals("first")) {
      i=1;
     }else {
         if (!param.equals("last") ) {
             try {
                i = Integer.parseInt(param);
             }catch (NumberFormatException e) {
              logger.error(param + " is not a valid line number: 0 assumed");
              i = 0;
             }
          }
      }
    
     while((rec = reader.readLine()) != null && (param.equals("last") || i > 0)) {
        erg = rec;       
        i--;
     }
     if (!param.equals("last") && rec == null && i > 0)erg="(eof)";
    
     reader.close();
       return erg;
  }
 
  private String go() throws Exception {
     String erg = "";
     if (param.equals("")) {
       throw new Exception ("Param missing in: " + command);
     }

     int command_id = getCommandId();
     switch(command_id) {
           case 1: //count
                    return count(true);
           case 2: //countCaseSensitive
              return count(false);
           case 3: //add
              return add();
           case 4: //read
               return read();
           case 5: //insert
               return insert();
     }
     return erg;
           
     }

 
  private int getCommandId() throws Exception{
     if (commands.get(command) == null)throw new Exception ("Unknow command: (not in count, add, read) " + command);
     String s = commands.get(command).toString();
     int commandId = 0;
     if (s != null) {
      commandId = Integer.parseInt(s);
     }
     return commandId;
  }

   public String getCommand() {
      return command;
   }

   public String getParam() {
      return param;
   }
 
  
}
/**
* This job performs some action on textfiles
*
* @author Uwe Risse <uwe.risse@sos-berlin.com>
* @since  2009-05-25
*/
public class JobSchedulerTextProcessor extends Job_impl {
 
  private SOSLogger logger = null;
 
 
  /**
   * Implementierung f�r Spooler API.
   * Initialisierungmethode f�r den Scheduler.
   * @return boolean
   */
  public boolean spooler_init() {
   
    try {
     
      try {
        this.logger = new SOSSchedulerLogger(this.spooler_log);
      } catch (Exception e) {
        throw new Exception("error occurred instantiating logger: " + e.getMessage());
      }
     
      return true;
    } catch (Exception e) {
      try {
        if ( logger!=null ) logger.error("error occurred in spooler_init(): " + e.getMessage());
      } catch (Exception x) {}
     
      return false;
    }
  }
 

 
  private String getParam(Variable_set params, String name, boolean mandatory ) throws Exception {
  
    String erg= "";
    if ( params.var(name)!=null && params.var(name).length()>0 )       
      erg = params.var(name);
    else
      if (mandatory)throw new Exception("job parameter is missing: ["+name+"]");
   
    logger.info(".. job parameter ["+name+"]: " + erg);
    return erg;
    }
 
 
  /**
   * Implementierung f�r Spooler API. L�uft bis return = false
   * @return boolean
   */
  public boolean spooler_process() {
/*         �  Lesen der n-ten Zeile aus einer Datei
     �  Zeile an Datei anf�gen
     �  Z�hlen des Vorkommens einer bestimmten Zeichenfolge in einer Datei
*/
  
       
    try {
     
      // Job oder Order
      Variable_set params = spooler.create_variable_set();
      if (spooler_task.params() != null) params.merge(spooler_task.params());
      if (spooler_job.order_queue() != null && spooler_task.order().params() != null) {
        params.merge(spooler_task.order().params());
      }
     
      // mandatory parameters
      String fileName    = getParam(params,"filename",true);
      String command     = getParam(params,"command",true);  
      String param       = getParam(params,"param",false);
     
      spooler_log.debug3("command=" + command);
      spooler_log.debug3("param=" + param);
      spooler_log.debug3("filename=" + fileName);
     
     
      command = command + " " + param;
     
     
       String oldFile = fileName;
      //To make orderparams available for substitution in orderparam value
      while (fileName.matches("^.*%[^%]+%.*$")) {
         String p = fileName.replaceFirst("^.*%([^%]+)%.*$", "$1");
         String s = params.var(p);
         s = s.replace('\\','/');
         fileName = fileName.replaceAll("%"+p+"%", s);
        }
       
      if (!fileName.equals(oldFile))logger.info(".. job parameter after substitution [file]: " + fileName);

      spooler_task.order().params().set_var("scheduler_textprocessor_filename", fileName);
      SOSSchedulerTextProcessor textProcessor = new SOSSchedulerTextProcessor (logger, new File(fileName),command);
      String result = textProcessor.exexute();
      if ( spooler_job.order_queue() != null) {
        spooler_task.order().params().set_var("scheduler_textprocessor_result", result);
      }
     
      spooler_task.order().params().set_var("scheduler_textprocessor_command", textProcessor.getCommand());
      spooler_task.order().params().set_var("scheduler_textprocessor_param", textProcessor.getParam());
      
     
      return (spooler_job.order_queue() != null);
     
    } catch (Exception e) {
      try {
        logger.error("error occurred in JobSchedulerTextProcessor: " + e.getMessage());
      } catch(Exception x) {}
     
      return false;
    }
   
  }
 
   public static void main(String[] args) throws Exception {
     SOSStandardLogger logger = new SOSStandardLogger(9);

     String file        = "c:/1.txt";

     String command     = " count   test";  
    SOSSchedulerTextProcessor textProcessor = new SOSSchedulerTextProcessor (logger, new File(file),command);
   

        try {
           System.out.println(command + " ->" + textProcessor.exexute());
        }catch (Exception e) {System.out.println(e.getMessage());
        }
      try {
      System.out.println("count (Missing Argument)" + " ->" + textProcessor.exexute("count"));
        }catch (Exception e) {System.out.println(e.getMessage());
        }
        try {
      System.out.println("countxxx (Unknow command)" + " ->" + textProcessor.exexute("countxxx"));
        }catch (Exception e) {System.out.println(e.getMessage());
        }
        try {
      System.out.println("count a" + " ->" + textProcessor.exexute("count a"));
        }catch (Exception e) {System.out.println(e.getMessage());
        }
        try {
       System.out.println("count b" + " ->" + textProcessor.exexute("count","b"));
         }catch (Exception e) {System.out.println(e.getMessage());
         }
     
        try {
         System.out.println("count xtest" + " ->" + textProcessor.exexute("count","xtest"));
       }catch (Exception e) {System.out.println(e.getMessage());
       }
        try {
        System.out.println("insert  last letzte" + " ->" + textProcessor.exexute("insert  last letzte"));
      }catch (Exception e) {System.out.println(e.getMessage());
      }      
        try {
          System.out.println("insert  2 zweite" + " ->" + textProcessor.exexute("insert  2 zweite"));
        }catch (Exception e) {System.out.println(e.getMessage());
        }      
        try {
        System.out.println("insert  first erste" + " ->" + textProcessor.exexute("insert  first erste{char:27}test"));
      }catch (Exception e) {System.out.println(e.getMessage());
      }      

     
    /*    try {
          System.out.println("read  2" + " ->" + textProcessor.exexute("read","3"));
        }catch (Exception e) {System.out.println(e.getMessage());
        }
        try {
       System.out.println("read  2223" + " ->" + textProcessor.exexute("read","2223"));
        }catch (Exception e) {System.out.println(e.getMessage());
      }
      try {
        System.out.println("read  first" + " ->" + textProcessor.exexute("read first"));
       }catch (Exception e) {System.out.println(e.getMessage());
       }
  
       try {
         System.out.println("add xxtestxxx" + " ->" + textProcessor.exexute("add xxxtestxxx"));
           }catch (Exception e) {System.out.println(e.getMessage());
           }      
         try {
           System.out.println("insert 2 Zeile2 ist nun" + " ->" + textProcessor.exexute("insert 2 Zeile2 ist nun"));
        }catch (Exception e) {System.out.println(e.getMessage());
             }      

           try {
         System.out.println("read  last" + " ->" + textProcessor.exexute("read","last"));
      }catch (Exception e) {System.out.println(e.getMessage());
       }*/
   
     }
 
}
TOP

Related Classes of sos.scheduler.file.JobSchedulerTextProcessor

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.