1 package ch.busyboxes.agoo.service.impl;
2
3 import java.util.Calendar;
4 import java.util.List;
5
6 import org.apache.log4j.Logger;
7
8 import ch.busyboxes.agoo.controller.model.WebWatchedFile;
9 import ch.busyboxes.agoo.dao.FileSystemDAO;
10 import ch.busyboxes.agoo.dao.WatchedFileDAO;
11 import ch.busyboxes.agoo.model.WatchedFile;
12 import ch.busyboxes.agoo.model.WatchedFolder;
13 import ch.busyboxes.agoo.model.enumeration.WatchedFileState;
14 import ch.busyboxes.agoo.service.WatchedFileService;
15 import ch.busyboxes.agoo.service.impl.adapter.WatchedFileAdapter;
16
17
18
19
20
21
22 public class WatchedFileServiceImpl implements WatchedFileService {
23
24
25 private Logger logger = Logger.getLogger(WatchedFileServiceImpl.class);
26
27
28 private WatchedFileDAO watchedFileDAO;
29
30
31 private FileSystemDAO fileSystemDAO;
32
33
34
35
36
37
38 public long getNumberOfFilesWatched() {
39 return watchedFileDAO.getTotalFilesWatched();
40 }
41
42
43
44
45
46
47 public void setWatchedFileDAO(WatchedFileDAO watchedFileDAO) {
48 this.watchedFileDAO = watchedFileDAO;
49 }
50
51
52
53
54 @Override
55 public void checkWatchedFile(long fileId) {
56
57 WatchedFile watchedFile = watchedFileDAO.getWatchedFileById(fileId);
58 WatchedFolder containingFolder = watchedFile.getWatchedFolder();
59
60 if (logger.isDebugEnabled()) {
61 logger.debug("Checking watched file: " + watchedFile.getFilename());
62 }
63
64 String fullPath = containingFolder.getFolderPath() + watchedFile.getFilename();
65
66 String oldMd5Hash = watchedFile.getMd5Hash();
67 String newMd5Hash = fileSystemDAO.computeMd5Hash(fullPath);
68
69 if (oldMd5Hash == null) {
70
71 watchedFile.setMd5Hash(newMd5Hash);
72 watchedFile.setWatchedFileState(WatchedFileState.OK);
73 watchedFile.setLastCheck(Calendar.getInstance().getTime());
74 watchedFileDAO.saveWatchedFile(watchedFile);
75 } else {
76 if (oldMd5Hash.equals(newMd5Hash)) {
77 watchedFile.setLastCheck(Calendar.getInstance().getTime());
78 watchedFile.setWatchedFileState(WatchedFileState.OK);
79 watchedFileDAO.saveWatchedFile(watchedFile);
80 logger.debug("The md5 hashes are matching, file integrity is OK");
81 } else {
82 watchedFile.setLastCheck(Calendar.getInstance().getTime());
83 watchedFile.setWatchedFileState(WatchedFileState.CORRUPT);
84 watchedFileDAO.saveWatchedFile(watchedFile);
85 throw new RuntimeException("The md5 hashes differ, old: " + oldMd5Hash +
86 ", new: " + newMd5Hash + ", file integrity is compromised");
87 }
88 }
89
90 }
91
92
93
94
95
96
97 public void setFileSystemDAO(FileSystemDAO fileSystemDAO) {
98 this.fileSystemDAO = fileSystemDAO;
99 }
100
101
102
103
104 @Override
105 public List<WebWatchedFile> getWatchedFilesByFolderPaged(Long folderId,
106 int filesPerPage, int page) {
107
108 List<WatchedFile> watchedFiles = watchedFileDAO.getWatchedFilesByFolderPaged(folderId,
109 filesPerPage, page);
110
111 return WatchedFileAdapter.adaptWatchedFiles(watchedFiles);
112 }
113
114
115
116
117 @Override
118 public WebWatchedFile getWatchedFileById(Long fileId) {
119
120 WatchedFile watchedFile = watchedFileDAO.getWatchedFileById(fileId);
121
122 return WatchedFileAdapter.adaptWatchedFile(watchedFile);
123 }
124 }