1 /***
2 * @(#)FilePersistStrategy.java
3 *
4 * JFoxSOAF, Service-Oriented Application Framework
5 *
6 * Copyright(c) JFoxSOAF Team
7 *
8 * Licensed under the GNU LGPL, Version 2.1 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.gnu.org/copyleft/lesser.html
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 * For more information, please visit:
21 * http://www.jfox.cn/confluence/display/JFoxSOAF/Home
22 * http://www.huihoo.org/jfox/jfoxsoaf
23 */
24
25 package org.huihoo.jfox.soaf.services.timer;
26
27 import java.io.File;
28 import java.io.FilenameFilter;
29 import java.io.IOException;
30 import java.io.ObjectInputStream;
31 import java.io.FileInputStream;
32 import java.io.ObjectOutputStream;
33 import java.io.FileOutputStream;
34
35 /***
36 * <p>
37 * Implementation of TaskPersistStrategy to a file system..
38 * </p>
39 *
40 * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
41 * @author <a href="mailto:berkovichnyc@hotmail.com">Efraim Berkovich </a>
42 * @version $Revision: 1.2 $ $Date: 2005/05/22 06:52:07 $
43 * @version Revision: 1.0
44 */
45
46 public class FilePersistStrategy implements TaskPersistStrategy {
47
48 protected String TASK_FILE_EXT = ".task";
49
50
51 private File persistStorage;
52
53 private TaskFileFilter taskFileFilter;
54
55 protected FilePersistStrategy() {
56 }
57
58 /***
59 * Create the strategy. Set the directory which is our persistent storage.
60 *
61 * @param directory The directory to which to write the files.
62 * @exception IOException if the passed File is not a directory
63 */
64 public FilePersistStrategy(File directory) throws IOException {
65 this.init(directory);
66 }
67
68 protected void init(File directory) throws IOException {
69 persistStorage = directory;
70 if (!persistStorage.isDirectory())
71 throw new IOException("Expected a directory: "
72 + directory.getPath());
73
74 taskFileFilter = new TaskFileFilter();
75 }
76
77 /***
78 * filters to .task files
79 */
80 private class TaskFileFilter implements FilenameFilter {
81 public boolean accept(File dir, String name) {
82 if (name.endsWith(TASK_FILE_EXT))
83 return true;
84 else
85 return false;
86 }
87 }
88
89 /***
90 * Read all the tasks in persisted storage
91 *
92 * @exception IOException on file issues
93 * @exception ClassNotFoundException on bad file
94 */
95 public Task[] readAll() throws IOException, ClassNotFoundException {
96 synchronized (this) {
97 ObjectInputStream inStream;
98 FileInputStream file;
99
100 File[] taskFiles = persistStorage.listFiles(taskFileFilter);
101 Task[] tasks = new Task[taskFiles.length];
102
103 for (int i = 0; i < taskFiles.length; i++) {
104 file = new FileInputStream(taskFiles[i]);
105 inStream = new ObjectInputStream(file);
106 tasks[i] = (Task) inStream.readObject();
107 inStream.close();
108 file.close();
109 }
110
111 return tasks;
112 }
113 }
114
115 /***
116 * Write the task
117 *
118 * @param task The Task to write
119 */
120 public void write(Task task) throws IOException {
121 synchronized (this) {
122 ObjectOutputStream outStream;
123 FileOutputStream file;
124
125 String fileName = new String(persistStorage.getPath()
126 + File.separator + task.getID() + TASK_FILE_EXT);
127
128 file = new FileOutputStream(fileName);
129 outStream = new ObjectOutputStream(file);
130 outStream.writeObject(task);
131 outStream.close();
132 }
133 }
134
135 /***
136 * Delete the task
137 */
138 public void delete(Task task) {
139 synchronized (this) {
140 String fileName = new String(persistStorage.getPath()
141 + File.separator + task.getID() + TASK_FILE_EXT);
142
143 File file = new File(fileName);
144
145 file.delete();
146 }
147 }
148
149 /***
150 * Clear all tasks
151 */
152 public void deleteAll() {
153 synchronized (this) {
154 File[] taskFiles = persistStorage.listFiles(taskFileFilter);
155
156 for (int i = 0; i < taskFiles.length; i++) {
157 taskFiles[i].delete();
158 }
159 }
160 }
161
162 }