01: /*
02: * ChainBuilder ESB
03: * Visual Enterprise Integration
04: *
05: * Copyright (C) 2008 Bostech Corporation
06: *
07: * This program is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU General Public License as published by the
09: * Free Software Foundation; either version 2 of the License, or (at your option)
10: * any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15: * for more details.
16: *
17: * You should have received a copy of the GNU General Public License along with
18: * this program; if not, write to the Free Software Foundation, Inc.,
19: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20: *
21: *
22: * $Id$
23: */
24: package com.bostechcorp.cbesb.runtime.file;
25:
26: import java.io.BufferedReader;
27: import java.io.BufferedWriter;
28: import java.io.File;
29: import java.io.FileReader;
30: import java.io.FileWriter;
31: import java.io.IOException;
32:
33: public class FileCounter {
34:
35: private File counterFile;
36:
37: public FileCounter(File counterFile) {
38: this .counterFile = counterFile;
39: }
40:
41: public synchronized int getNextValue() throws IOException {
42: int count = 0;
43:
44: if (counterFile.exists()) {
45: count = getCountFromFile();
46: }
47:
48: count++;
49:
50: saveCountToFile(count);
51:
52: return count;
53: }
54:
55: private int getCountFromFile() throws IOException {
56: int count = 0;
57: BufferedReader reader = new BufferedReader(new FileReader(
58: counterFile));
59: String value = reader.readLine();
60: reader.close();
61: if (value != null) {
62: try {
63: count = Integer.parseInt(value.trim());
64: } catch (NumberFormatException e) {
65: //Ignore
66: }
67: }
68: return count;
69: }
70:
71: private void saveCountToFile(int count) throws IOException {
72: BufferedWriter writer = new BufferedWriter(new FileWriter(
73: counterFile));
74: writer.write(Integer.toString(count) + "\n");
75: writer.close();
76: }
77:
78: }
|