--- /dev/null
+package com.company;
+
+import java.io.*;
+import java.nio.file.Path;
+import java.nio.file.StandardWatchEventKinds;
+import java.nio.file.WatchEvent;
+import java.nio.file.WatchEvent.Kind;
+import java.nio.file.WatchKey;
+import java.nio.file.WatchService;
+import java.security.NoSuchAlgorithmException;
+import java.sql.SQLException;
+import java.util.ArrayList;
+
+// Simple class to watch directory events.
+class DirectoryWatcher implements Runnable {
+
+ private Path path;
+
+ public DirectoryWatcher(Path path) {
+ this.path = path;
+ }
+
+ //rimuove tutti gli elementi non validi
+ private void pulisciCartella() {
+ File dir = new File(global.locationEsercizi);
+ String[] lista=dir.list();
+ for(String name : lista)
+ {
+ if (!EsNameParser.check(name))
+ {
+ File file = new File(global.locationEsercizi+name);
+ if (file.delete())
+ System.out.println("eliminato: "+name);
+ else
+ System.out.println("problema di eliminazione file");
+ }
+ else
+ {
+ xmlOperation es=new xmlOperation();
+ File car=new File(global.listaEsercizi);
+ if (car.exists()) {
+ dbConnect db=new dbConnect();
+ try {
+ ArrayList<String> esercizi=suString.stringToArrayList(db.dbQuery("4"));
+ boolean add=true;
+ for(String esercizio : esercizi)
+ {
+ if(esercizio.equals(name))
+ add=false;
+ }
+ if(add)
+ es.add(name);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ } catch (ClassNotFoundException e) {
+ e.printStackTrace();
+ } catch (IllegalAccessException e) {
+ e.printStackTrace();
+ } catch (InstantiationException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (NoSuchAlgorithmException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+ }
+
+ // print the events and the affected file
+ private void printEvent(WatchEvent<?> event) throws IOException {
+ Kind<?> kind = event.kind();
+ if (kind.equals(StandardWatchEventKinds.ENTRY_CREATE))
+ {
+ Path pathCreated = (Path) event.context();
+ System.out.println("Entry created:" + pathCreated);
+ boolean check = EsNameParser.check(pathCreated.getFileName().toString());
+ if(!check)
+ {
+ File file = new File(global.locationEsercizi+pathCreated.getFileName().toString());
+ if(file.delete())
+ System.out.println("eliminato");
+ else
+ System.out.println("problema di eliminazione file");
+ }
+ else
+ {
+ xmlOperation es=new xmlOperation();
+ File dir=new File(global.listaEsercizi);
+ if (dir.exists())
+ es.add(pathCreated.getFileName().toString());
+ }
+ }
+ else if (kind.equals(StandardWatchEventKinds.ENTRY_DELETE))
+ {
+ Path pathDeleted = (Path) event.context();
+ System.out.println("Entry deleted:" + pathDeleted);
+ xmlOperation es=new xmlOperation();
+ File dir=new File(global.listaEsercizi);
+ if (dir.exists())
+ es.remove(pathDeleted.getFileName().toString());
+ }
+ else if (kind.equals(StandardWatchEventKinds.ENTRY_MODIFY))
+ {
+ Path pathModified = (Path) event.context();
+ System.out.println("Entry modified:" + pathModified);
+ }
+ }
+
+ @Override
+ public void run() {
+ try {
+ pulisciCartella();
+ WatchService watchService = path.getFileSystem().newWatchService();
+ path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
+ StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
+
+ // loop forever to watch directory
+ while (true) {
+ WatchKey watchKey;
+ watchKey = watchService.take(); // this call is blocking until events are present
+
+ // poll for file system events on the WatchKey
+ for (final WatchEvent<?> event : watchKey.pollEvents()) {
+ printEvent(event);
+ }
+
+ // if the watched directed gets deleted, get out of run method
+ if (!watchKey.reset()) {
+ System.out.println("No longer valid");
+ watchKey.cancel();
+ watchService.close();
+ break;
+ }
+ }
+
+ } catch (InterruptedException ex) {
+ System.out.println("interrupted. Goodbye");
+ return;
+ } catch (IOException ex) {
+ ex.printStackTrace(); // don't do this in production code. Use a loggin framework
+ return;
+ }
+ }
+
+}