001package com.dpillay.tools.tail4j.launcher; 002 003import java.io.File; 004import java.util.ArrayList; 005import java.util.List; 006 007import com.dpillay.tools.tail4j.characters.StringTailedFileReader; 008import com.dpillay.tools.tail4j.configuration.TailConfiguration; 009import com.dpillay.tools.tail4j.core.PrintWriterTailPrinter; 010import com.dpillay.tools.tail4j.core.TailExecutor; 011import com.dpillay.tools.tail4j.core.TailListener; 012import com.dpillay.tools.tail4j.core.TailPrinter; 013import com.dpillay.tools.tail4j.core.TailedReader; 014 015/** 016 * Main class that launches the tailer. 017 * 018 * @author dpillay 019 * 020 */ 021public class TailLauncher { 022 public static void main(String[] args) { 023 // check if the arguments are sane 024 try { 025 argumentSanityChecker(args); 026 } catch (RuntimeException re) { 027 if (re.getMessage() != null) { 028 System.out.println("Error: " + re.getMessage()); 029 System.out.println(); 030 } 031 usage(); 032 return; 033 } 034 035 // if help is requested, show the usage. 036 if (isUsage(args)) { 037 usage(); 038 return; 039 } 040 041 // continue with tail 042 TailConfiguration tc = TailLauncher.build(args); 043 if (tc == null) 044 return; 045 List<TailedReader<String, File>> tailedFiles = new ArrayList<TailedReader<String, File>>(); 046 TailListener<String> tailListener = new TailListener<String>(); 047 for (String filePath : tc.getFiles()) { 048 File file = new File(filePath); 049 TailedReader<String, File> tailedFile = new StringTailedFileReader( 050 tc, file, tailListener); 051 tailedFiles.add(tailedFile); 052 } 053 TailPrinter<String> printer = new PrintWriterTailPrinter<String>( 054 System.out, tailListener); 055 TailExecutor executor = new TailExecutor(); 056 executor.execute(tailedFiles, printer); 057 } 058 059 private static boolean isUsage(String[] args) { 060 for (int i = 0; i < args.length; ++i) { 061 String arg = args[i]; 062 CommandLineOption option = CommandLineOption 063 .getCommandLineOption(arg); 064 if (option.equals(CommandLineOption.HELP_OPTION) 065 || option.equals(CommandLineOption.HELP_DESC_OPTION)) { 066 return true; 067 } 068 } 069 return false; 070 } 071 072 private static void usage() { 073 System.out.println("tail4j - (c) Dinesh Pillay"); 074 System.out.println("Usage: tail4j [-n <lines to show>] [-f] files.."); 075 System.out 076 .println("\t-n\t\tSpecifies the number of lines to be shown counting from the bottom."); 077 System.out 078 .println("\t-f\t\tSpecifies whether to keep reading even after encountering end of file."); 079 System.out.println("\tfiles\t\tList of files to be tailed."); 080 System.out.println("\t--help\t\tThis help section."); 081 } 082 083 private static void argumentSanityChecker(String[] args) { 084 if (args.length == 0) 085 throw new RuntimeException(); 086 087 for (int i = 0; i < args.length; ++i) { 088 String arg = args[i]; 089 CommandLineOption option = CommandLineOption 090 .getCommandLineOption(arg); 091 if (option.equals(CommandLineOption.INVALID_OPTION)) 092 throw new RuntimeException( 093 "Invalid option or File does not exist: [" + arg + "]"); 094 i += option.getSkipArgs(); 095 } 096 } 097 098 public static TailConfiguration build(String[] args) { 099 TailConfiguration tc = new TailConfiguration(); 100 for (int i = 0; i < args.length; ++i) { 101 String arg = args[i]; 102 CommandLineOption option = CommandLineOption 103 .getCommandLineOption(arg); 104 switch (option) { 105 case SHOW_LINES_OPTION: 106 long showLines = -1; 107 try { 108 showLines = Long.parseLong(args[++i]); 109 } catch (Throwable t) { 110 } 111 tc.setShowLines(showLines); 112 break; 113 case FORCE_OPTION: 114 tc.setForce(true); 115 break; 116 case FILE_ARGUMENT: 117 tc.getFiles().add(arg); 118 break; 119 } 120 } 121 return tc; 122 } 123}