1 | /* |
2 | * Copyright 2005-2006 The RbUtils Project |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | * |
16 | */ |
17 | |
18 | // $Id: AbstractMainProcessor.java,v 1.7 2008/07/03 08:15:41 moishi Exp $ |
19 | |
20 | package org.ktc.rbutils.api.audit; |
21 | |
22 | import java.io.File; |
23 | import java.util.ArrayList; |
24 | import java.util.Collection; |
25 | import java.util.Collections; |
26 | import java.util.Iterator; |
27 | import java.util.List; |
28 | import java.util.Locale; |
29 | |
30 | import org.apache.commons.io.FileUtils; |
31 | import org.ktc.rbutils.api.audit.event.ProcessEndEvent; |
32 | import org.ktc.rbutils.api.audit.event.ProcessFileEndEvent; |
33 | import org.ktc.rbutils.api.audit.event.ProcessFileStartEvent; |
34 | import org.ktc.rbutils.api.audit.event.ProcessStartEvent; |
35 | import org.ktc.rbutils.api.i18n.LocaleUtils; |
36 | import org.ktc.rbutils.api.i18n.Message; |
37 | |
38 | /** |
39 | * Abstract base class for Main Processing. Adds the root directory fonctionnality. |
40 | * @since RbUtils 0.9.3.3 |
41 | * @version $Revision: 1.7 $ |
42 | * @author moishi |
43 | */ |
44 | public abstract class AbstractMainProcessor extends AbstractProcessor implements MainProcessor { |
45 | /** Root directory of the files processed by this Processor. */ |
46 | protected File root; |
47 | |
48 | /** Default value of the recursivity on subdirectories. */ |
49 | protected static final boolean DEFAULT_RECURSIVE_CHECK = true; |
50 | /** |
51 | * <code>true</code> if the check must be performed on all subdirectories of the root directory. |
52 | * <code>false</code> otherwise. |
53 | */ |
54 | protected boolean recursiveCheck = DEFAULT_RECURSIVE_CHECK; |
55 | /** List of properties files to be checked. */ |
56 | protected final List filesList = new ArrayList(); |
57 | |
58 | /** |
59 | * Indicates that the files list is already initialized. No need to look for files in the root |
60 | * directory. |
61 | */ |
62 | protected boolean filesAlreadyAdded; |
63 | |
64 | /** Default extension of the properties file used as source when processing files. */ |
65 | public static final String DEFAULT_PROPERTIES_EXTENSION = "properties"; |
66 | |
67 | /** Extension of the properties file used as source for the check. */ |
68 | protected String srcRBextension; |
69 | |
70 | /** Logger that counts the number of errors during the process. */ |
71 | private final ErrorCounter errorsCounter = new ErrorCounter(); |
72 | |
73 | /** Logger that counts the number of used files during the process. */ |
74 | private final FileCounter filesCounter = new FileCounter(); |
75 | |
76 | /** |
77 | * {@inheritDoc} |
78 | */ |
79 | public void fireProcessStarted() { |
80 | final ProcessStartEvent event = new ProcessStartEvent(this, root); |
81 | for (final Iterator it = loggers.iterator(); it.hasNext();) { |
82 | final Logger logger = (Logger) it.next(); |
83 | logger.processStarted(event); |
84 | } |
85 | |
86 | } |
87 | |
88 | /** |
89 | * {@inheritDoc} |
90 | */ |
91 | public void fireProcessEnded() { |
92 | final ProcessEndEvent event = new ProcessEndEvent(this, root); |
93 | for (final Iterator it = loggers.iterator(); it.hasNext();) { |
94 | final Logger logger = (Logger) it.next(); |
95 | logger.processEnded(event); |
96 | } |
97 | } |
98 | |
99 | /** |
100 | * {@inheritDoc} |
101 | */ |
102 | public boolean addFiles(final Collection files) { |
103 | // TODO should initialized filesAlreadyAdded if addAll returns true and/or the collection is |
104 | // not empty |
105 | return filesList.addAll(files); |
106 | } |
107 | |
108 | /** |
109 | * {@inheritDoc} |
110 | */ |
111 | public void setFilesAlreadyAdded(final boolean filesAlreadyAdded) { |
112 | //TODO Code-should be removed |
113 | this.filesAlreadyAdded = filesAlreadyAdded; |
114 | } |
115 | |
116 | /** |
117 | * Finalizes the class initialization. |
118 | * <p> |
119 | * Details: |
120 | * <ul> |
121 | * <li>if no logger has been registered, registers the ConsoleLogger</li> |
122 | * <li>if no files have already been added to the list to be processed, looks for files |
123 | * (recursively or not using the default or providing properties file extension) in the |
124 | * <code>root</code> directory.</li> |
125 | * </ul> |
126 | */ |
127 | protected void finalizeInitialization() { |
128 | // If no listeners registered, add the ConsoleLogger |
129 | if (loggers.size() == 0) { |
130 | addConsoleLogger(); |
131 | } |
132 | // Add counters |
133 | addLogger(errorsCounter); |
134 | addLogger(filesCounter); |
135 | |
136 | // If the list of properties files has not been previously set, look in the root directory |
137 | // for files with the instance extension attribute. |
138 | // If the extension has not been already set, use the default one. |
139 | // if (filesList.isEmpty()) { |
140 | if (!filesAlreadyAdded) { |
141 | if (srcRBextension == null) { |
142 | srcRBextension = DEFAULT_PROPERTIES_EXTENSION; |
143 | } |
144 | final String[] extensions = {srcRBextension}; |
145 | final Collection defaultList = FileUtils.listFiles(root, extensions, recursiveCheck); |
146 | filesList.addAll(defaultList); |
147 | } |
148 | |
149 | // Sort the files list to be inspected |
150 | Collections.sort(filesList); |
151 | } |
152 | |
153 | /** |
154 | * {@inheritDoc} |
155 | */ |
156 | public void setExtension(final String extension) { |
157 | srcRBextension = extension; |
158 | } |
159 | |
160 | /** |
161 | * {@inheritDoc} |
162 | */ |
163 | public String getExtension() { |
164 | return srcRBextension; |
165 | } |
166 | |
167 | /** |
168 | * {@inheritDoc} |
169 | */ |
170 | public void setMessagesLocale(final Locale locale) { |
171 | // TODO Code - this should be static and maybe in the abstract class (use for FileProcessor) |
172 | if (locale != null) { |
173 | Message.setLocale(locale); |
174 | } |
175 | } |
176 | |
177 | /** |
178 | * {@inheritDoc} |
179 | */ |
180 | public void fireException(final Exception exception) { |
181 | // We overidde the method to be sure that the Exception is used by XmlLogger |
182 | // With this, we are also sure that the FileCounter counts the file |
183 | final String msg = "Unknown Error"; |
184 | final String type = "Exception"; |
185 | final Locale locale = LocaleUtils.getEmptyLocale(); |
186 | final ProcessFileStartEvent startEvent = new ProcessFileStartEvent(this, msg, |
187 | locale, |
188 | type); |
189 | final ProcessFileEndEvent endEvent = new ProcessFileEndEvent(this, msg, |
190 | locale, |
191 | type); |
192 | for (final Iterator it = loggers.iterator(); it.hasNext();) { |
193 | final Logger logger = (Logger) it.next(); |
194 | logger.processFileStarted(startEvent); |
195 | logger.addException(exception); |
196 | logger.processFileEnded(endEvent); |
197 | } |
198 | } |
199 | |
200 | /** |
201 | * Returns the number of errors that occur during the process. |
202 | * @return the number of errors. |
203 | */ |
204 | public int getErrorsCount() { |
205 | return errorsCounter.getCount(); |
206 | } |
207 | |
208 | /** |
209 | * {@inheritDoc} |
210 | */ |
211 | public int getProcessedFilesCount() { |
212 | return filesCounter.getCount(); |
213 | } |
214 | |
215 | } |