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