]> matita.cs.unibo.it Git - logicplayer.git/blob - server/com/company/DirectoryWhatcer.java
8b376a1a3557e5cdec54122e390834a1ac8cb760
[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     }
70
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))
75         {
76             Path pathCreated = (Path) event.context();
77             System.out.println("Entry created:" + pathCreated);
78             boolean check = EsNameParser.check(pathCreated.getFileName().toString());
79             if(!check)
80             {
81                 File file = new File(global.locationEsercizi+pathCreated.getFileName().toString());
82                 if(file.delete())
83                     System.out.println("eliminato");
84                 else
85                     System.out.println("problema di eliminazione file");
86             }
87             else
88             {
89                 xmlOperation es=new xmlOperation();
90                 File dir=new File(global.listaEsercizi);
91                 if (dir.exists())
92                     es.add(pathCreated.getFileName().toString());
93             }
94         }
95         else if (kind.equals(StandardWatchEventKinds.ENTRY_DELETE))
96         {
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);
101             if (dir.exists())
102                 es.remove(pathDeleted.getFileName().toString());
103         }
104         else if (kind.equals(StandardWatchEventKinds.ENTRY_MODIFY))
105         {
106             Path pathModified = (Path) event.context();
107             System.out.println("Entry modified:" + pathModified);
108         }
109     }
110
111     @Override
112     public void run() {
113         try {
114             pulisciCartella();
115             WatchService watchService = path.getFileSystem().newWatchService();
116             path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
117                     StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
118
119             // loop forever to watch directory
120             while (true) {
121                 WatchKey watchKey;
122                 watchKey = watchService.take(); // this call is blocking until events are present
123
124                 // poll for file system events on the WatchKey
125                 for (final WatchEvent<?> event : watchKey.pollEvents()) {
126                     printEvent(event);
127                 }
128
129                 // if the watched directed gets deleted, get out of run method
130                 if (!watchKey.reset()) {
131                     System.out.println("No longer valid");
132                     watchKey.cancel();
133                     watchService.close();
134                     break;
135                 }
136             }
137
138         } catch (InterruptedException ex) {
139             System.out.println("interrupted. Goodbye");
140             return;
141         } catch (IOException ex) {
142             ex.printStackTrace();  // don't do this in production code. Use a loggin framework
143             return;
144         }
145     }
146
147 }