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) {
69 dbConnect db=new dbConnect();
71 String[] esercizi=suString.stringToVectorString(db.dbQuery("4"));
73 for(String esercizio : esercizi)
75 for(String elemento : lista)
77 if(esercizio.contentEquals(elemento))
81 xmlOperation es = new xmlOperation();
86 } catch (SQLException e) {
88 } catch (ClassNotFoundException e) {
90 } catch (IllegalAccessException e) {
92 } catch (InstantiationException e) {
94 } catch (IOException e) {
96 } catch (NoSuchAlgorithmException e) {
101 // print the events and the affected file
102 private void printEvent(WatchEvent<?> event) throws IOException {
103 Kind<?> kind = event.kind();
104 if (kind.equals(StandardWatchEventKinds.ENTRY_CREATE))
106 Path pathCreated = (Path) event.context();
107 System.out.println("Entry created:" + pathCreated);
108 boolean check = EsNameParser.check(pathCreated.getFileName().toString());
111 File file = new File("/var/www/html/esercizi/"+pathCreated.getFileName().toString());
113 System.out.println("eliminato");
115 System.out.println("problema di eliminazione file");
119 xmlOperation es=new xmlOperation();
120 File dir=new File(global.listaEsercizi);
122 es.add(pathCreated.getFileName().toString());
125 else if (kind.equals(StandardWatchEventKinds.ENTRY_DELETE))
127 Path pathDeleted = (Path) event.context();
128 System.out.println("Entry deleted:" + pathDeleted);
129 xmlOperation es=new xmlOperation();
130 File dir=new File(global.listaEsercizi);
132 es.remove(pathDeleted.getFileName().toString());
134 else if (kind.equals(StandardWatchEventKinds.ENTRY_MODIFY))
136 Path pathModified = (Path) event.context();
137 System.out.println("Entry modified:" + pathModified);
145 WatchService watchService = path.getFileSystem().newWatchService();
146 path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
147 StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
149 // loop forever to watch directory
152 watchKey = watchService.take(); // this call is blocking until events are present
154 // poll for file system events on the WatchKey
155 for (final WatchEvent<?> event : watchKey.pollEvents()) {
159 // if the watched directed gets deleted, get out of run method
160 if (!watchKey.reset()) {
161 System.out.println("No longer valid");
163 watchService.close();
168 } catch (InterruptedException ex) {
169 System.out.println("interrupted. Goodbye");
171 } catch (IOException ex) {
172 ex.printStackTrace(); // don't do this in production code. Use a loggin framework