]> matita.cs.unibo.it Git - logicplayer.git/blob - server/com/company/DirectoryWhatcer.java
Bug fixed: made robust against lost of connection with db.
[logicplayer.git] / server / com / company / DirectoryWhatcer.java
1 package com.company;
2
3 import java.io.*;
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;
13
14 // Simple class to watch directory events.
15 class DirectoryWatcher implements Runnable {
16
17     private Path path;
18
19     public DirectoryWatcher(Path path) {
20         this.path = path;
21     }
22
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)
28         {
29             if (!EsNameParser.check(name))
30             {
31                 File file = new File(global.locationEsercizi+name);
32                 if (file.delete())
33                     System.out.println("eliminato: "+name);
34                 else
35                     System.out.println("problema di eliminazione file");
36             }
37             else
38             {
39                 xmlOperation es=new xmlOperation();
40                 File car=new File(global.listaEsercizi);
41                 if (car.exists()) {
42                     try {
43                         ArrayList<String> esercizi=suString.stringToArrayList(global.request.dbQuery("4"));
44                         boolean add=true;
45                         for(String esercizio : esercizi)
46                         {
47                             if(esercizio.equals(name))
48                                 add=false;
49                         }
50                         if(add)
51                             es.add(name);
52                     } catch (SQLException e) {
53                         e.printStackTrace();
54                     } catch (ClassNotFoundException e) {
55                         e.printStackTrace();
56                     } catch (IllegalAccessException e) {
57                         e.printStackTrace();
58                     } catch (InstantiationException e) {
59                         e.printStackTrace();
60                     } catch (IOException e) {
61                         e.printStackTrace();
62                     } catch (NoSuchAlgorithmException e) {
63                         e.printStackTrace();
64                     }
65                 }
66             }
67         }
68         try {
69             String[] esercizi=suString.stringToVectorString(global.request.dbQuery("4"));
70             boolean delete=true;
71             for(String esercizio : esercizi)
72             {
73                 for(String elemento : lista)
74                 {
75                     if(esercizio.contentEquals(elemento))
76                         delete=false;
77                 }
78                 if(delete) {
79                     xmlOperation es = new xmlOperation();
80                     es.remove(esercizio);
81                 }
82
83             }
84         } catch (SQLException e) {
85             e.printStackTrace();
86         } catch (ClassNotFoundException e) {
87             e.printStackTrace();
88         } catch (IllegalAccessException e) {
89             e.printStackTrace();
90         } catch (InstantiationException e) {
91             e.printStackTrace();
92         } catch (IOException e) {
93             e.printStackTrace();
94         } catch (NoSuchAlgorithmException e) {
95             e.printStackTrace();
96         }
97     }
98
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))
103         {
104             Path pathCreated = (Path) event.context();
105             System.out.println("Entry created:" + pathCreated);
106             boolean check = EsNameParser.check(pathCreated.getFileName().toString());
107             if(!check)
108             {
109                 File file = new File("/var/www/html/esercizi/"+pathCreated.getFileName().toString());
110                 if(file.delete())
111                     System.out.println("eliminato");
112                 else
113                     System.out.println("problema di eliminazione file");
114             }
115             else
116             {
117                 xmlOperation es=new xmlOperation();
118                 File dir=new File(global.listaEsercizi);
119                 if (dir.exists())
120                     es.add(pathCreated.getFileName().toString());
121             }
122         }
123         else if (kind.equals(StandardWatchEventKinds.ENTRY_DELETE))
124         {
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);
129             if (dir.exists())
130                 es.remove(pathDeleted.getFileName().toString());
131         }
132         else if (kind.equals(StandardWatchEventKinds.ENTRY_MODIFY))
133         {
134             Path pathModified = (Path) event.context();
135             System.out.println("Entry modified:" + pathModified);
136         }
137     }
138
139     @Override
140     public void run() {
141         try {
142             pulisciCartella();
143             WatchService watchService = path.getFileSystem().newWatchService();
144             path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
145                     StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
146
147             // loop forever to watch directory
148             while (true) {
149                 WatchKey watchKey;
150                 watchKey = watchService.take(); // this call is blocking until events are present
151
152                 // poll for file system events on the WatchKey
153                 for (final WatchEvent<?> event : watchKey.pollEvents()) {
154                     printEvent(event);
155                 }
156
157                 // if the watched directed gets deleted, get out of run method
158                 if (!watchKey.reset()) {
159                     System.out.println("No longer valid");
160                     watchKey.cancel();
161                     watchService.close();
162                     break;
163                 }
164             }
165
166         } catch (InterruptedException ex) {
167             System.out.println("interrupted. Goodbye");
168             return;
169         } catch (IOException ex) {
170             ex.printStackTrace();  // don't do this in production code. Use a loggin framework
171             return;
172         }
173     }
174
175 }