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);
43 ArrayList<String> esercizi=suString.stringToArrayList(global.request.dbQuery("4"));
45 for(String esercizio : esercizi)
47 if(esercizio.equals(name))
52 } catch (SQLException e) {
54 } catch (ClassNotFoundException e) {
56 } catch (IllegalAccessException e) {
58 } catch (InstantiationException e) {
60 } catch (IOException e) {
62 } catch (NoSuchAlgorithmException e) {
69 String[] esercizi=suString.stringToVectorString(global.request.dbQuery("4"));
71 for(String esercizio : esercizi)
73 for(String elemento : lista)
75 if(esercizio.contentEquals(elemento))
79 xmlOperation es = new xmlOperation();
84 } catch (SQLException e) {
86 } catch (ClassNotFoundException e) {
88 } catch (IllegalAccessException e) {
90 } catch (InstantiationException e) {
92 } catch (IOException e) {
94 } catch (NoSuchAlgorithmException e) {
99 // print the events and the affected file
100 private void printEvent(WatchEvent<?> event) throws IOException {
101 Kind<?> kind = event.kind();
102 if (kind.equals(StandardWatchEventKinds.ENTRY_CREATE))
104 Path pathCreated = (Path) event.context();
105 System.out.println("Entry created:" + pathCreated);
106 boolean check = EsNameParser.check(pathCreated.getFileName().toString());
109 File file = new File("/var/www/html/esercizi/"+pathCreated.getFileName().toString());
111 System.out.println("eliminato");
113 System.out.println("problema di eliminazione file");
117 xmlOperation es=new xmlOperation();
118 File dir=new File(global.listaEsercizi);
120 es.add(pathCreated.getFileName().toString());
123 else if (kind.equals(StandardWatchEventKinds.ENTRY_DELETE))
125 Path pathDeleted = (Path) event.context();
126 System.out.println("Entry deleted:" + pathDeleted);
127 xmlOperation es=new xmlOperation();
128 File dir=new File(global.listaEsercizi);
130 es.remove(pathDeleted.getFileName().toString());
132 else if (kind.equals(StandardWatchEventKinds.ENTRY_MODIFY))
134 Path pathModified = (Path) event.context();
135 System.out.println("Entry modified:" + pathModified);
143 WatchService watchService = path.getFileSystem().newWatchService();
144 path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
145 StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
147 // loop forever to watch directory
150 watchKey = watchService.take(); // this call is blocking until events are present
152 // poll for file system events on the WatchKey
153 for (final WatchEvent<?> event : watchKey.pollEvents()) {
157 // if the watched directed gets deleted, get out of run method
158 if (!watchKey.reset()) {
159 System.out.println("No longer valid");
161 watchService.close();
166 } catch (InterruptedException ex) {
167 System.out.println("interrupted. Goodbye");
169 } catch (IOException ex) {
170 ex.printStackTrace(); // don't do this in production code. Use a loggin framework