DirectoryChecker.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.nio.file.FileSystems;
  8. import java.nio.file.Path;
  9. import java.nio.file.Paths;
  10. import java.nio.file.WatchEvent;
  11. import java.nio.file.WatchKey;
  12. import java.nio.file.WatchService;
  13. import java.text.DateFormat;
  14. import java.text.SimpleDateFormat;
  15. import java.util.Date;
  16. import java.util.Enumeration;
  17. import java.util.Properties;
  18. import java.util.logging.FileHandler;
  19. import java.util.logging.Logger;
  20. import java.util.logging.SimpleFormatter;
  21. /**
  22. *
  23. * @author Clindo
  24. */
  25. /***
  26. *
  27. * This DirectoryChecker uses the Watch Service API for checking the addition of
  28. * files in specified path This is developed as a part of TK Praktikum at TU
  29. * Darmstadt, Germany.
  30. *
  31. */
  32. public class DirectoryChecker {
  33. public static Logger logger = Logger.getLogger("MyLog");
  34. public static FileHandler fh;
  35. public static DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
  36. public static Date dateobj = new Date();
  37. public static void main(String[] args) {
  38. logger.setUseParentHandlers(false);
  39. /*
  40. * Logger logger = Logger.getLogger(DirectoryChecker.class);
  41. *
  42. * Appender Notconsole = new ConsoleAppender(); Logger root =
  43. * Logger.getRootLogger(); root.addAppender(Notconsole);
  44. *
  45. * BasicConfigurator.configure();
  46. * logger.info("This is my first log4j's statement");
  47. */
  48. String DirPath;
  49. try {
  50. fh = new FileHandler("LogFile.log");
  51. logger.addHandler(fh);
  52. SimpleFormatter formatter = new SimpleFormatter();
  53. fh.setFormatter(formatter);
  54. logger.info("---THE LOG FOR DIRECTORY CHECKER STARTS HERE--- "+df.format(dateobj));
  55. File file = new File("config.xml");
  56. FileInputStream fileInput = new FileInputStream(file);
  57. Properties properties = new Properties();
  58. properties.loadFromXML(fileInput);
  59. fileInput.close();
  60. Enumeration enuKeys = properties.keys();
  61. String propName = (String) enuKeys.nextElement();
  62. String value = properties.getProperty(propName);
  63. // System.out.println(propName + ": " + value);
  64. DirPath = value;
  65. /*
  66. * while (enuKeys.hasMoreElements()) { String key = (String)
  67. * enuKeys.nextElement(); String value =
  68. * properties.getProperty(key); System.out.println(key + ": " +
  69. * value); }
  70. */
  71. WatchService watcher = FileSystems.getDefault().newWatchService();
  72. Path dir = Paths.get(DirPath);
  73. dir.register(watcher, ENTRY_CREATE); // , ENTRY_DELETE, ENTRY_MODIFY
  74. logger.info("Watch Service registered for dir: "
  75. + dir.getFileName()+df.format(dateobj));
  76. while (true) {
  77. WatchKey key;
  78. try {
  79. key = watcher.take();
  80. } catch (InterruptedException ex) {
  81. return;
  82. }
  83. for (WatchEvent<?> event : key.pollEvents()) {
  84. WatchEvent.Kind<?> kind = event.kind();
  85. @SuppressWarnings("unchecked")
  86. WatchEvent<Path> ev = (WatchEvent<Path>) event;
  87. Path fileName = ev.context();
  88. // System.out.println(kind.name() + ": " + fileName);
  89. /*
  90. * if (kind == ENTRY_MODIFY && fileName.toString().equals(
  91. * "DirectoryChecker.java"))
  92. */
  93. if (kind == ENTRY_CREATE) {
  94. logger.info("---New Folder is created by Camtasia--- "+df.format(dateobj));
  95. String htmlName=fileName.toString()+".html";
  96. System.out.println(htmlName);
  97. String htmlPath=DirPath+"/"+htmlName;
  98. appendXml(htmlPath);
  99. }
  100. }
  101. boolean valid = key.reset();
  102. if (!valid) {
  103. break;
  104. }
  105. }
  106. } catch (IOException ex) {
  107. System.err.println(ex);
  108. }
  109. }
  110. public static void appendXml(String htmlPath) {
  111. System.out.println("Append xml here");
  112. BufferedWriter bw = null;
  113. try {
  114. // APPEND MODE SET HERE
  115. bw = new BufferedWriter(new FileWriter(
  116. "D:/Master Studies/WS16-17/TKPraktikum/root.conf", true));
  117. String toAppend= "Alias /TK/WeekNumber ";
  118. logger.info("---Alias is appended to Apache Root--- "+df.format(dateobj));
  119. bw.write(toAppend+htmlPath);
  120. bw.newLine();
  121. bw.flush();
  122. } catch (IOException ioe) {
  123. ioe.printStackTrace();
  124. } finally { // always close the file
  125. if (bw != null)
  126. try {
  127. bw.close();
  128. } catch (IOException ioe2) {
  129. // just ignore it
  130. }
  131. } // end try/catch/finally
  132. } // end test()
  133. }