SHARE
TWEET

LogReader.java

olegtikhonov Apr 1st, 2013 122 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *  Licensed under the Apache License, Version 2.0 (the "License");
  3.  *  you may not use this file except in compliance with the License.
  4.  *  You may obtain a copy of the License at
  5.  *
  6.  *       http://www.apache.org/licenses/LICENSE-2.0
  7.  *
  8.  *  Unless required by applicable law or agreed to in writing, software
  9.  *  distributed under the License is distributed on an "AS IS" BASIS,
  10.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11.  *  See the License for the specific language governing permissions and
  12.  *  limitations under the License.
  13.  */
  14. package org.example.tcpserver;
  15.  
  16. import java.io.BufferedReader;
  17. import java.io.DataInputStream;
  18. import java.io.FileInputStream;
  19. import java.io.IOException;
  20. import java.io.InputStreamReader;
  21. import java.text.SimpleDateFormat;
  22. import java.util.Calendar;
  23. import java.util.concurrent.TimeUnit;
  24. import java.util.concurrent.atomic.AtomicInteger;
  25. import org.apache.log4j.Logger;
  26. import org.springframework.integration.Message;
  27. import org.springframework.integration.channel.DirectChannel;
  28. import org.springframework.integration.support.MessageBuilder;
  29.  
  30. /**
  31.  * Reads Log file & sends each command by specific time.
  32.  *
  33.  */
  34. public class LogReader implements Runnable{
  35.     /* Local logger. */
  36.     private static final Logger LOG = Logger.getLogger(LogReader.class);
  37.     private final SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss.S");
  38.     private long timeToWait = 500 ;
  39.     private String command;
  40.     private String path2File;
  41.     DirectChannel channel;
  42.     @SuppressWarnings("rawtypes")
  43.         private final Message origMsg;
  44.     private AtomicInteger counter = new AtomicInteger();
  45.  
  46.     public LogReader(DirectChannel channel, @SuppressWarnings("rawtypes") Message origMsg) {
  47.         this.channel = channel;
  48.         this.origMsg = origMsg;
  49.     }
  50.  
  51.    
  52.     /**
  53.      * Reads & sends commands to the client.
  54.      */
  55.  
  56.     private void read(){
  57.                 FileInputStream fstream = null;
  58.                 String line;
  59.                
  60.                 try {
  61.                         /* Creates an input stream to be read. */
  62.                         fstream = new FileInputStream(getPath2File());
  63.                         /* Wraps an input stream in order to be able reading of a whole line */
  64.                         DataInputStream in = new DataInputStream(fstream);
  65.                         @SuppressWarnings("resource")
  66.                         BufferedReader br = new BufferedReader(new InputStreamReader(in));
  67.  
  68.                         while ((line = br.readLine()) != null) {
  69.                                 command = line;
  70.                                 sendAndLog(timeToWait);
  71.                         }
  72.                 } catch (Exception e) {
  73.                         LOG.error("Error: " + e.getMessage());
  74.                 } finally {
  75.             LOG.info("Transmission finished.");
  76.                         /* Closed the stream */
  77.                         if(fstream != null) {
  78.                                 try {
  79.                                         fstream.close();
  80.                                 } catch (IOException e) {
  81.                     LOG.error(e);
  82.                                 }
  83.                         }
  84.                 }
  85.         }
  86.  
  87.     /**
  88.      *
  89.      * @param value
  90.      */
  91.     private void sendAndLog(long value){
  92.         sleep(value);
  93.         LOG.info("t:" + command + " at " + dateFormat.format(Calendar.getInstance().getTime()) + "\n");
  94.         channel.send(MessageBuilder.withPayload(command.getBytes()).copyHeaders(origMsg.getHeaders()).build());
  95.     }
  96.  
  97.         /**
  98.          * Waits for awhile as provided parameter.
  99.          *
  100.          * @param timeInMillisec to be waited.
  101.          */
  102.         private static void sleep(long timeInMillisec){
  103.                 try {
  104.                         TimeUnit.MILLISECONDS.sleep(timeInMillisec);
  105.                 } catch (InterruptedException e) {
  106.             LOG.error(e);
  107.                 }
  108.         }
  109.  
  110.         //=======================================
  111.         //                      Getters/Setters
  112.         //=======================================
  113.         /**
  114.          * Gets a {@link java.text.SimpleDateFormat}.
  115.          *
  116.          * @return a date formatter.
  117.          */
  118.         public SimpleDateFormat getDateFormat() {
  119.                 return dateFormat;
  120.         }
  121.  
  122.  
  123.         /**
  124.          * Gets a command.
  125.          *
  126.          * @return a command.
  127.          */
  128.         public String getCommand() {
  129.                 return command;
  130.         }
  131.  
  132.         /**
  133.          * Sets a command.
  134.          *
  135.          * @param command to be set.
  136.          */
  137.         public void setCommand(String command) {
  138.                 this.command = command;
  139.         }
  140.  
  141.     public String getPath2File() {
  142.         return path2File;
  143.     }
  144.  
  145.     public void setPath2File(String path2File) {
  146.         this.path2File = path2File;
  147.     }
  148.  
  149.         public void run() {
  150.                 LOG.info("\r***************************************\rGoing to read file ...["
  151.                                 + counter.incrementAndGet()
  152.                                 + "]\r***************************************\n");
  153.                 read();
  154.         }
  155. }
RAW Paste Data
Top