KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > util > FileRepository


1 /*
2  * Copyright (C) 2006 ScalAgent Distributed Technologies
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA.
18  *
19  * Initial developer(s): ScalAgent Distributed Technologies
20  * Contributor(s):
21  */

22 package fr.dyade.aaa.util;
23
24 import java.io.*;
25
26 final class FileRepository implements Repository {
27   File dir = null;
28
29   private int nbsaved = 0;
30
31   /**
32    * Returns the number of save operation to repository.
33    *
34    * @return The number of save operation to repository.
35    */

36   public int getNbSavedObjects() {
37     return nbsaved;
38   }
39
40   private int nbdeleted = 0;
41
42   /**
43    * Returns the number of delete operation on repository.
44    *
45    * @return The number of delete operation on repository.
46    */

47   public int getNbDeletedObjects() {
48     return nbdeleted;
49   }
50
51   private int baddeleted = 0;
52
53   /**
54    * Returns the number of useless delete operation on repository.
55    *
56    * @return The number of useless delete operation on repository.
57    */

58   public int getNbBadDeletedObjects() {
59     return baddeleted;
60   }
61
62   private int nbloaded = 0;
63
64   /**
65    * Returns the number of load operation from repository.
66    *
67    * @return The number of load operation from repository.
68    */

69   public int getNbLoadedObjects() {
70     return nbloaded;
71   }
72
73   FileRepository() {
74   }
75
76   /**
77    * Initializes the repository.
78    * Nothing to do.
79    */

80   public void init(File dir) throws IOException {
81     this.dir = dir;
82   }
83
84   /**
85    * Gets a list of persistent objects that name corresponds to prefix.
86    *
87    * @return The list of corresponding names.
88    */

89   public String JavaDoc[] list(String JavaDoc prefix) throws IOException {
90     return dir.list(new StartWithFilter(prefix));
91   }
92
93   /**
94    * Save the corresponding bytes array.
95    */

96   public void save(String JavaDoc dirName, String JavaDoc name, byte[] content) throws IOException {
97     File file;
98     if (dirName == null) {
99       file = new File(dir, name);
100     } else {
101       File parentDir = new File(dir, dirName);
102       if (!parentDir.exists()) {
103         parentDir.mkdirs();
104       }
105       file = new File(parentDir, name);
106     }
107
108     FileOutputStream fos = new FileOutputStream(file);
109     fos.write(content);
110     fos.getFD().sync();
111     fos.close();
112
113     nbsaved += 1;
114   }
115
116   /**
117    * Loads the object.
118    *
119    * @return The loaded object or null if it does not exist.
120    */

121   public Object JavaDoc loadobj(String JavaDoc dirName, String JavaDoc name) throws IOException, ClassNotFoundException JavaDoc {
122     File file;
123     Object JavaDoc obj;
124     if (dirName == null) {
125       file = new File(dir, name);
126     } else {
127       File parentDir = new File(dir, dirName);
128       file = new File(parentDir, name);
129     }
130
131     FileInputStream fis = new FileInputStream(file);
132     ObjectInputStream ois = new ObjectInputStream(fis);
133     try {
134       obj = ois.readObject();
135     } finally {
136       ois.close();
137       fis.close();
138     }
139
140     nbloaded += 1;
141     return obj;
142   }
143
144   /**
145    * Loads the byte array.
146    *
147    * @return The loaded bytes array.
148    */

149   public byte[] load(String JavaDoc dirName, String JavaDoc name) throws IOException {
150       // Gets it from disk.
151
File file;
152       if (dirName == null) {
153         file = new File(dir, name);
154       } else {
155         File parentDir = new File(dir, dirName);
156         file = new File(parentDir, name);
157       }
158       FileInputStream fis = new FileInputStream(file);
159       byte[] buf = new byte[(int) file.length()];
160       for (int nb=0; nb<buf.length; ) {
161         int ret = fis.read(buf, nb, buf.length-nb);
162         if (ret == -1) throw new EOFException();
163         nb += ret;
164       }
165       fis.close();
166
167       nbloaded += 1;
168       return buf;
169   }
170
171   /**
172    * Deletes the corresponding objects in repository.
173    */

174   public void delete(String JavaDoc dirName, String JavaDoc name) throws IOException {
175     File file;
176     if (dirName == null) {
177       if (! new File(dir, name).delete()) baddeleted += 1;
178     } else {
179       File parentDir = new File(dir, dirName);
180       file = new File(parentDir, name);
181       if (! new File(parentDir, name).delete()) baddeleted += 1;
182       deleteDir(parentDir);
183     }
184     nbdeleted += 1;
185   }
186
187   /**
188    * Delete the specified directory if it is empty.
189    * Also recursively delete the parent directories if they are empty.
190    */

191   private final void deleteDir(File dir) {
192     // Check the disk state. It may be false according to the transaction
193
// log but it doesn't matter because directories are lazily created.
194
String JavaDoc[] children = dir.list();
195     // children may be null if dir doesn't exist any more.
196
if (children != null && children.length == 0) {
197       dir.delete();
198       if (dir.getAbsolutePath().length() >
199           this.dir.getAbsolutePath().length()) {
200         deleteDir(dir.getParentFile());
201       }
202     }
203   }
204
205   /**
206    * Commits all changes to the repository.
207    */

208   public void commit() throws IOException {}
209
210   /**
211    * Closes the repository.
212    */

213   public void close() throws IOException {}
214 }
215
Popular Tags