1 package org.csc.phynixx.loggersystem.logger.channellogger;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 import org.csc.phynixx.common.logger.IPhynixxLogger;
25 import org.csc.phynixx.common.logger.PhynixxLogManager;
26 import org.csc.phynixx.loggersystem.logger.IDataLogger;
27 import org.csc.phynixx.loggersystem.logger.IDataLoggerFactory;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.text.MessageFormat;
32 import java.util.HashSet;
33 import java.util.Set;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public class FileChannelDataLoggerFactory implements IDataLoggerFactory {
58
59 private static final String LOGGER_SYSTEM_FORMAT_PATTERN = "({0})_([a-z,A-Z,0-9]*[^_])_([0-9]*[^\\.])\\.[\\w]*";
60
61 private static final String LOGGER_FORMAT_PATTERN = "({0})_([0-9]*[^\\.])\\.[\\w]*";
62
63 private static final IPhynixxLogger LOGGER = PhynixxLogManager.getLogger(FileChannelDataLoggerFactory.class);
64
65 private File directory = null;
66 private String loggerSystemName = null;
67
68
69
70
71
72
73 public FileChannelDataLoggerFactory(String loggerSystemName, String directoryName) {
74 super();
75 this.loggerSystemName = loggerSystemName;
76 this.directory = new File(directoryName);
77 if (this.directory.exists() && !this.directory.isDirectory()) {
78 throw new IllegalArgumentException("Directory " + directory.getAbsolutePath() + " doesn't exist or is not a directory");
79 }
80 if (!this.directory.canExecute() && !this.directory.canWrite()) {
81 throw new IllegalArgumentException("Directory " + directory.getAbsolutePath() + " could not be written");
82 }
83 }
84
85
86
87
88
89 public FileChannelDataLoggerFactory(String loggerSystemName, File directory) {
90 super();
91 this.loggerSystemName = loggerSystemName;
92 this.directory = directory;
93
94 if (directory == null) {
95 throw new IllegalArgumentException("Log directory must be specified");
96 }
97
98
99 if (!directory.exists()) {
100 throw new IllegalArgumentException("Log directory must exists");
101 }
102
103 if (this.directory.exists() && !this.directory.isDirectory()) {
104 throw new IllegalArgumentException("Argument 'directory' has to be an existing directory");
105 }
106 }
107
108
109
110
111 public String getLoggerSystemName() {
112 return loggerSystemName;
113 }
114
115
116
117
118
119 public File getLoggingDirectory() {
120 return directory;
121 }
122
123
124 private String createQualifiedLoggerName(String loggerName, int qualifier) {
125 return new StringBuilder(this.loggerSystemName).append("_").append(loggerName).append("_").append(qualifier).toString();
126 }
127
128
129
130
131
132
133
134 public synchronized IDataLogger instanciateLogger(String loggerName) throws IOException {
135 return this.instanciateLogger(loggerName, AccessMode.APPEND);
136 }
137
138
139
140
141
142
143 public synchronized IDataLogger instanciateLogger(String loggerName, AccessMode accessMode) throws IOException {
144
145 File logFile = this.provideFile(createQualifiedLoggerName(loggerName, 1), this.directory);
146 return new FileChannelDataLogger(logFile, accessMode);
147 }
148
149
150
151
152 @Override
153 public synchronized void cleanup() {
154 String pattern = MessageFormat.format(LOGGER_SYSTEM_FORMAT_PATTERN, this.loggerSystemName);
155 LogFilenameMatcher matcher = new LogFilenameMatcher(pattern);
156
157 LogFileTraverser.ICollectorCallback cb = new LogFileTraverser.ICollectorCallback() {
158 public void match(File file,
159 LogFilenameMatcher.LogFilenameParts parts) {
160 boolean success = file.delete();
161 if (LOGGER.isDebugEnabled()) {
162 LOGGER.debug("Deleting " + file + " success=" + success);
163 }
164 }
165 };
166 new LogFileTraverser(matcher,
167 FileChannelDataLoggerFactory.this.getLoggingDirectory(), cb);
168 }
169
170 @Override
171
172
173
174 public synchronized void destroyLogger(String loggerName) {
175 String pattern = MessageFormat.format(LOGGER_FORMAT_PATTERN, loggerName);
176 LogFilenameMatcher matcher = new LogFilenameMatcher(pattern);
177
178 LogFileTraverser.ICollectorCallback cb = new LogFileTraverser.ICollectorCallback() {
179 public void match(File file,
180 LogFilenameMatcher.LogFilenameParts parts) {
181 boolean success = file.delete();
182 if (LOGGER.isDebugEnabled()) {
183 LOGGER.debug("Deleting " + file + " success=" + success);
184 }
185 }
186 };
187 new LogFileTraverser(matcher,
188 FileChannelDataLoggerFactory.this.getLoggingDirectory(), cb);
189 }
190
191 @Override
192
193
194
195 public synchronized Set<String> findLoggerNames() throws IOException {
196
197 String pattern = MessageFormat.format(LOGGER_SYSTEM_FORMAT_PATTERN, this.loggerSystemName);
198 LogFilenameMatcher matcher = new LogFilenameMatcher(pattern);
199
200 final Set<String> loggerNames = new HashSet<String>();
201 LogFileTraverser.ICollectorCallback cb = new LogFileTraverser.ICollectorCallback() {
202 public void match(File file, LogFilenameMatcher.LogFilenameParts parts) {
203 loggerNames.add(parts.getLoggerName());
204 }
205 };
206 new LogFileTraverser(matcher, this.getLoggingDirectory(), cb);
207
208 return loggerNames;
209 }
210
211 private File provideFile(String fileName, File directory) throws IOException {
212
213
214 File file = new File(directory, fileName + ".log");
215
216
217 if (file.exists()) {
218 return file;
219 }
220
221 if (!directory.exists()) {
222 final boolean mkdirs = directory.mkdirs();
223 if(!mkdirs) {
224 throw new IOException("Failed to create directory " + directory + " or one of its children");
225 }
226 }
227
228 if(!file.createNewFile()) {
229
230 throw new IOException("Failed to create file "+file);
231 }
232
233 return file;
234
235 }
236
237
238 }