1 /*
2 Bloof - visualize the evolution of your software project
3 Copyright ( C ) 2003 Lukasz Pekacki <lukasz@pekacki.de>
4 http://bloof.sf.net/
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along with
15 this program; if not, write to the Free Software Foundation, Inc.,
16 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18 $RCSfile: Bloof.java,v $
19 Created on $Date: 2003/09/13 12:22:45 $
20 */
21 package net.sf.bloof;
22
23 import java.io.File;
24 import java.io.FileWriter;
25 import java.io.IOException;
26 import java.sql.SQLException;
27 import java.util.Date;
28 import java.util.HashSet;
29 import java.util.Iterator;
30 import java.util.logging.LogManager;
31 import java.util.logging.Logger;
32
33 import net.n3.nanoxml.XMLWriter;
34 import net.sf.bloof.db.Database;
35 import net.sf.bloof.db.DbAccess;
36 import net.sf.bloof.db.DefaultDbAccess;
37 import net.sf.bloof.events.BloofEvent;
38 import net.sf.bloof.events.BloofEventListener;
39 import net.sf.bloof.metrics.Metric;
40 import net.sf.bloof.metrics.MetricRunner;
41 import net.sf.bloof.metrics.TimeInterval;
42 import net.sf.bloof.scm.ScmAccess;
43 import net.sf.bloof.scm.ScmAccessException;
44 import net.sf.bloof.util.ProgressEvent;
45 import net.sf.bloof.util.ProgressListener;
46 import net.sf.bloof.util.logging.LogFormatter;
47
48 /***
49 * Central controlling class for the Bloof system.
50 * Any client that want to use the Bloof API mainly uses the static functions
51 * provided by this class.
52 * @author Lukasz Pekacki <pekacki@users.sourceforge.net>
53 * @version $Id: Bloof.java,v 1.25 2003/09/13 12:22:45 pekacki Exp $
54 */
55 public class Bloof {
56
57 /***
58 * Adds a Listener to the Bloof event dispatching list
59 * @param aListener Listener to add
60 */
61 public static void addBloofEventListener(BloofEventListener aListener) {
62 Bloof.sBloofEventListeners.add(aListener);
63 }
64
65 /***
66 * Registers a new ExceptionListener to Bloof.
67 * @param aListener receives progress events
68 */
69 public static void addBloofExceptionListener(BloofExceptionListener aListener) {
70 Bloof.sBloofExceptionListeners.add(aListener);
71 }
72
73 /***
74 * Registers a new ProgressListener to Bloof.
75 * @param aProgressListener receives progress events
76 */
77 public static void addProgressListener(ProgressListener aProgressListener) {
78 Bloof.sProgressListener.add(aProgressListener);
79 }
80
81 /***
82 * Closes the current project
83 * @throws SQLException if the database could not be found or started
84 * @throws IOException if the database could not be found or started
85 */
86 public static void closeProject() throws SQLException, IOException {
87 collectGarbage();
88 if (sDatabase != null) {
89 sDatabase.close();
90 sDatabase = null;
91 }
92 propagateBloofEvent(BloofEvent.CLOSED);
93 }
94
95 /***
96 * Runs the garbage collector and finalizers in order to
97 * free memory.
98 */
99 public static void collectGarbage() {
100 sLogger.fine("Collecting garbage");
101 try {
102 System.gc();
103 Thread.sleep(100);
104 System.runFinalization();
105 Thread.sleep(100);
106 } catch (InterruptedException ex) {
107 ex.printStackTrace();
108 }
109 }
110 /***
111 * This method is called on exit of Bloof
112 */
113 public static void exit() {
114
115 }
116
117 /***
118 * Exports the result of a metric to a XML file
119 * @param aMetric the result of which is to be exported
120 * @param aFileName name of the XML file
121 */
122 public static void exportResult(Metric aMetric, String aFileName) {
123 if (aFileName == null) {
124 XMLWriter xw = new XMLWriter(System.out);
125 System.out.print(Metric.XML_HEAD);
126 try {
127 xw.write(aMetric.getResultXml(), true, 4);
128 } catch (IOException e) {
129 fail("Could not export result to STD OUT." + " Reason:" + e.toString());
130 }
131 return;
132 }
133 File f = new File(aFileName);
134 if (!f.exists()) {
135 try {
136 FileWriter fw = new FileWriter(f);
137 XMLWriter xw = new XMLWriter(fw);
138 fw.write(Metric.XML_HEAD);
139 xw.write(aMetric.getResultXml(), true, 4);
140 fw.flush();
141 fw.close();
142 } catch (IOException e) {
143 fail("Could not export file." + f.getAbsolutePath() + " Reason:" + e.toString());
144 }
145 } else {
146 fail("File does already exist." + f.getAbsolutePath());
147 }
148 propagateBloofEvent(BloofEvent.EXPORT_FINISHED);
149
150 }
151
152 /***
153 * This method is called if a serious error has occured during processing. The client that
154 * @param aFailReason reason for exit.
155 */
156 public static void fail(String aFailReason) {
157 System.out.print(aFailReason);
158 System.out.print("\n");
159 propagateBloofException(new BloofException(aFailReason));
160 }
161
162 /***
163 * Returns the current database
164 * @return current database
165 */
166 public static Database getDatabase() {
167 return sDatabase;
168 }
169
170 /***
171 * Fills a new database with the data stored in the scm accessd by scmAccess
172 * @param aScmAccess access object
173 * @param aDbAccess access object for the database to be used
174 * @throws SQLException on database error
175 * @throws IOException on database script error
176 * @throws ScmAccessException on scm access error
177 */
178 public static void importProject(ScmAccess aScmAccess, DbAccess aDbAccess)
179 throws SQLException, ScmAccessException, IOException {
180 Bloof.propagateProgressEvent(new ProgressEvent("Importing new project", 5));
181 ImportProject newProject = new ImportProject(aDbAccess, aScmAccess);
182 Thread newProjectThread = new Thread(newProject);
183 newProjectThread.start();
184 }
185
186 /***
187 * Opens the project in the specified database connection
188 * @param aDbAccess access object for the database to be used
189 * @throws SQLException if the database could not be found or started
190 */
191 public static void openProject(DbAccess aDbAccess) throws SQLException {
192 Bloof.propagateProgressEvent(new ProgressEvent("Opening project", 5));
193 OpenProject newProject = new OpenProject(aDbAccess);
194 Thread newProjectThread = new Thread(newProject);
195 newProjectThread.start();
196 }
197
198 /***
199 * Notifies the registered ProgessEventListeners about a new Bloof Event
200 * @param aPe BloofEvent that had happend
201 */
202 public static void propagateBloofEvent(BloofEvent aPe) {
203 for (Iterator iter = Bloof.sProgressListener.iterator(); iter.hasNext();) {
204 BloofEventListener elem = (BloofEventListener) iter.next();
205 elem.bloofEventOccured(aPe);
206 }
207 }
208
209 /***
210 * Notifies the registered BloofExecptionListeners about a new Exception
211 * @param aBloofException Exception that has happend
212 */
213 public static void propagateBloofException(BloofException aBloofException) {
214 for (Iterator iter = Bloof.sBloofExceptionListeners.iterator(); iter.hasNext();) {
215 BloofExceptionListener element = (BloofExceptionListener) iter.next();
216 element.bloofExceptionHappend(aBloofException);
217 }
218 }
219
220 /***
221 * Notifies the registered ProgessEventListeners about a new Progress
222 * @param aPe Progress that had happend
223 */
224 public static void propagateProgressEvent(ProgressEvent aPe) {
225 for (Iterator iter = Bloof.sProgressListener.iterator(); iter.hasNext();) {
226 ProgressListener element = (ProgressListener) iter.next();
227 element.progressHappend(aPe);
228 }
229 }
230
231 /***
232 * Runs a metric
233 * @param aMetric metric to run
234 */
235 public static void runMetric(Metric aMetric) {
236 Thread runMetricThread = new Thread(new MetricRunner(aMetric, sDatabase));
237 runMetricThread.setName("MetricRunner");
238 runMetricThread.start();
239 }
240
241 /***
242 * @param aDatabase new database
243 */
244 public static void setDatabase(Database aDatabase) {
245 sDatabase = aDatabase;
246 }
247
248 /***
249 * Fills a new database with the data stored in the scm accessd by scmAccess
250 * @param aScmAccess access object
251 * @param aDbAccess access object for the database to be used
252 * @throws SQLException on database error
253 * @throws IOException on database script error
254 * @throws ScmAccessException on scm access error
255 */
256 public static void updateProject(ScmAccess aScmAccess, DbAccess aDbAccess)
257 throws SQLException, ScmAccessException, IOException {
258 if (sDatabase == null) {
259 Bloof.openProject(aDbAccess);
260 }
261 Date maxDate = null;
262 TimeInterval wholeTime = sDatabase.getTimespanBoundaries();
263 maxDate = wholeTime.getTo();
264 UpdateProject updateProject = new UpdateProject(sDatabase, aScmAccess, maxDate);
265 Thread updateThread = new Thread(updateProject);
266 updateThread.start();
267 }
268
269 /***
270 * Bloof local dir
271 */
272 public static final String LOCAL_BLOOF_DIR = System.getProperty("user.home")+"/bloof";
273 /***
274 * CvsAccess to internal McKoi Database
275 */
276 public static final DbAccess INTERNAL_DATABASE =
277 new DefaultDbAccess("Default internal database", "bloof", "bloof", LOCAL_BLOOF_DIR+"/bloofdb");
278 private static Database sDatabase;
279 private static Logger sLogger = Logger.getLogger(Bloof.class.getName());
280 private static HashSet sProgressListener = new HashSet(),
281 sBloofExceptionListeners = new HashSet(),
282 sBloofEventListeners = new HashSet();
283
284 /***
285 * Method initLogManager
286 * initializes the logging API
287 */
288 static {
289 try {
290 LogManager sLm = LogManager.getLogManager();
291 if (sLm.getProperty("inited") == null) {
292 sLm.readConfiguration(
293 LogFormatter.class.getResourceAsStream("logging-silent.properties"));
294 }
295 } catch (IOException e) {
296 System.err.println("ERROR: Logging could not be initialized!");
297 }
298 File bloofLocalDir = new File(LOCAL_BLOOF_DIR);
299 if (!bloofLocalDir.exists()) {
300 bloofLocalDir.mkdir();
301 }
302 }
303
304 }
This page was automatically generated by Maven