Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package jpaoletti.jpm.core.monitor;
  
  import java.io.FileReader;
  import java.util.List;

A monitor source that takes information from a file

Author(s):
jpaoletti
 
 public class FileMonitorSource extends MonitorSource {
 
     private String filename;

    
Get the file lines since the actual until the last.

Parameters:
actual Actual line identification
Returns:
The list of lines
Throws:
java.lang.Exception
 
     @Override
     public List<MonitorLinegetLinesFrom(Object actualthrows Exception {
         //TODO Enhance line retrieve to get last lines directly
         String line;
         Integer currentLineNo = 0;
         final List<MonitorLineresult = new ArrayList<MonitorLine>();
         BufferedReader in = null;
         try {
             in = new BufferedReader(new FileReader(getFilename()));
 
             Integer startLine = (actual == null) ? 0 : (Integeractual;
             //read to startLine
             while (currentLineNo < startLine + 1) {
                 if (in.readLine() == null) {
                     throw new IOException("File too small");
                 }
                 currentLineNo++;
             }
             //read until endLine
             line = in.readLine();
             while (line != null) {
                 MonitorLine l = new MonitorLine();
                 l.setId(currentLineNo);
                 l.setValue(line);
                 result.add(l);
                 currentLineNo++;
                 line = in.readLine();
             }
         } finally {
             try {
                 if (in != null) {
                     in.close();
                 }
             } catch (IOException ignore) {
             }
         }
         return result;
     }

    
Return the last file line

Returns:
The line
Throws:
java.lang.Exception
 
     @Override
     public MonitorLine getLastLine() throws Exception {
         String line = null;
         MonitorLine result = new MonitorLine();
         BufferedReader in = null;
         try {
             in = new BufferedReader(new FileReader(getFilename()));
             int i = 0;
             line = in.readLine();
             while (line != null) {
                 result.setId(i);
                 result.setValue(line);
                 i++;
                 line = in.readLine();
             }
         } finally {
             try {
                 if (in != null) {
                     in.close();
                 }
             } catch (IOException ignore) {
             }
         }
         return result;
     }

    
Retrieve the filename
 
    @Override
    public void init() {
        setFilename(getConfig("filename"));
    }

    

Parameters:
filename
    public void setFilename(String filename) {
        this. = filename;
    }

    

Returns:
the filename
    public String getFilename() {
        return ;
    }
New to GrepCode? Check out our FAQ X