4 import java.nio.file.Path;
5 import java.nio.file.StandardWatchEventKinds;
6 import java.nio.file.WatchEvent;
7 import java.nio.file.WatchEvent.Kind;
8 import java.nio.file.WatchKey;
9 import java.nio.file.WatchService;
10 import java.security.NoSuchAlgorithmException;
11 import java.sql.SQLException;
12 import java.util.ArrayList;
14 // Simple class to watch directory events.
15 class DirectoryWatcher implements Runnable {
19 public DirectoryWatcher(Path path) {
23 //rimuove tutti gli elementi non validi
24 private void pulisciCartella() {
25 File dir = new File(global.locationEsercizi);
26 String[] lista=dir.list();
27 for(String name : lista)
29 if (!EsNameParser.check(name))
31 File file = new File(global.locationEsercizi+name);
33 System.out.println("eliminato: "+name);
35 System.out.println("problema di eliminazione file");
39 xmlOperation es=new xmlOperation();
40 File car=new File(global.listaEsercizi);
42 dbConnect db=new dbConnect();
44 ArrayList<String> esercizi=suString.stringToArrayList(db.dbQuery("4"));
46 for(String esercizio : esercizi)
48 if(esercizio.equals(name))
53 } catch (SQLException e) {
55 } catch (ClassNotFoundException e) {
57 } catch (IllegalAccessException e) {
59 } catch (InstantiationException e) {
61 } catch (IOException e) {
63 } catch (NoSuchAlgorithmException e) {
71 // print the events and the affected file
72 private void printEvent(WatchEvent<?> event) throws IOException {
73 Kind<?> kind = event.kind();
74 if (kind.equals(StandardWatchEventKinds.ENTRY_CREATE))
76 Path pathCreated = (Path) event.context();
77 System.out.println("Entry created:" + pathCreated);
78 boolean check = EsNameParser.check(pathCreated.getFileName().toString());
81 File file = new File(global.locationEsercizi+pathCreated.getFileName().toString());
83 System.out.println("eliminato");
85 System.out.println("problema di eliminazione file");
89 xmlOperation es=new xmlOperation();
90 File dir=new File(global.listaEsercizi);
92 es.add(pathCreated.getFileName().toString());
95 else if (kind.equals(StandardWatchEventKinds.ENTRY_DELETE))
97 Path pathDeleted = (Path) event.context();
98 System.out.println("Entry deleted:" + pathDeleted);
99 xmlOperation es=new xmlOperation();
100 File dir=new File(global.listaEsercizi);
102 es.remove(pathDeleted.getFileName().toString());
104 else if (kind.equals(StandardWatchEventKinds.ENTRY_MODIFY))
106 Path pathModified = (Path) event.context();
107 System.out.println("Entry modified:" + pathModified);
115 WatchService watchService = path.getFileSystem().newWatchService();
116 path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
117 StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
119 // loop forever to watch directory
122 watchKey = watchService.take(); // this call is blocking until events are present
124 // poll for file system events on the WatchKey
125 for (final WatchEvent<?> event : watchKey.pollEvents()) {
129 // if the watched directed gets deleted, get out of run method
130 if (!watchKey.reset()) {
131 System.out.println("No longer valid");
133 watchService.close();
138 } catch (InterruptedException ex) {
139 System.out.println("interrupted. Goodbye");
141 } catch (IOException ex) {
142 ex.printStackTrace(); // don't do this in production code. Use a loggin framework