1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.sf.jdiskcatalog.analysis;
24
25 import java.io.BufferedInputStream;
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Map;
32
33 import net.sf.jdiskcatalog.api.FileAnalyser;
34 import net.sf.jdiskcatalog.api.FileSystemScanner;
35 import net.sf.jdiskcatalog.api.MutableNodeInfo;
36 import net.sf.jdiskcatalog.api.Node;
37 import net.sf.jdiskcatalog.api.NodeInfo;
38 import net.sf.jdiskcatalog.api.StreamAnalyser;
39 import net.sf.jdiskcatalog.api.StreamScanner;
40 import net.sf.jdiskcatalog.config.Configurable;
41 import net.sf.jdiskcatalog.config.Configuration;
42 import net.sf.jdiskcatalog.scanner.FsStreamScanner;
43
44
45
46
47
48
49
50 public class AnalysisManager implements StreamScanner, FileSystemScanner
51 {
52 private List<StreamAnalyser> streamAnalysers = new ArrayList<StreamAnalyser>();
53 private List<FileAnalyser> fileAnalysers = new ArrayList<FileAnalyser>();
54 private List<StreamScanner> streamScanners = new ArrayList<StreamScanner>();
55 private List<FileSystemScanner> fileSystemScanners = new ArrayList<FileSystemScanner>();
56
57 private Configuration config;
58
59 private static final int BUFFER_SIZE = 8192;
60
61 public AnalysisManager(Configuration config)
62 {
63 this.config = config;
64 }
65
66 public void add(StreamAnalyser analyser)
67 {
68 if (analyser instanceof Configurable)
69 config.configure((Configurable)analyser);
70 streamAnalysers.add(analyser);
71 fileAnalysers.add(new FileStreamAnalyser(analyser));
72 }
73
74 public void add(FileAnalyser analyser)
75 {
76 if (analyser instanceof Configurable)
77 config.configure((Configurable)analyser);
78 fileAnalysers.add(analyser);
79 }
80
81 public void add(StreamScanner scanner)
82 {
83 if (scanner instanceof Configurable)
84 config.configure((Configurable)scanner);
85 streamScanners.add(scanner);
86 scanner.setScanner(this);
87 fileSystemScanners.add(new FsStreamScanner(scanner));
88 }
89
90 public void add(FileSystemScanner scanner)
91 {
92 if (scanner instanceof Configurable)
93 config.configure((Configurable)scanner);
94 scanner.setScanner(this);
95 fileSystemScanners.add(scanner);
96 }
97
98 private void analyse(InputStream stream, NodeInfo nodeInfo)
99 {
100 MutableNodeInfo info = (MutableNodeInfo)nodeInfo;
101 for (StreamAnalyser analyser : streamAnalysers)
102 {
103 try
104 {
105 stream.reset();
106 }
107 catch (IOException e)
108 {
109 break;
110 }
111 try
112 {
113 Map<String, Object> result = analyser.analyse(stream, nodeInfo);
114 add(info, result);
115 }
116 catch (IOException e)
117 {
118
119 System.out.println("Exception while analysing " + nodeInfo.getName() + ": " + e.getMessage());
120
121 }
122 }
123 }
124
125 private void analyse(File file, NodeInfo nodeInfo)
126 {
127 MutableNodeInfo info = (MutableNodeInfo)nodeInfo;
128 for (FileAnalyser analyser : fileAnalysers)
129 {
130 try
131 {
132 Map<String, Object> result = analyser.analyse(file, nodeInfo);
133 add(info, result);
134 }
135 catch (IOException e)
136 {
137
138 System.out.println(e.getMessage());
139
140 }
141 }
142 }
143
144 public boolean scan(InputStream stream, Node root) throws IOException
145 {
146 stream = new BufferedInputStream(stream, BUFFER_SIZE);
147 stream.mark(BUFFER_SIZE);
148 for (StreamScanner scanner : streamScanners)
149 if (scanner.scan(stream, root))
150 break;
151 analyse(stream, root.getNodeInfo());
152 return true;
153 }
154
155 public void scan(File file, Node root) throws IOException
156 {
157 for (FileSystemScanner scanner : fileSystemScanners)
158 scanner.scan(file, root);
159 analyse(file, root.getNodeInfo());
160 }
161
162 private void add(MutableNodeInfo info, Map<String, Object> values)
163 {
164 if (values == null)
165 return;
166 for (Map.Entry<String, Object> entry : values.entrySet())
167 info.setProperty(entry.getKey(), entry.getValue());
168 }
169
170 public void setScanner(StreamScanner scanner)
171 {
172 }
173
174 public void setScanner(FileSystemScanner scanner)
175 {
176 }
177 }