|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| FilePersistStrategy.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 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 |
// instance variables
|
|
| 51 |
private File persistStorage;
|
|
| 52 |
|
|
| 53 |
private TaskFileFilter taskFileFilter;
|
|
| 54 |
|
|
| 55 | 0 |
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 | 0 |
public FilePersistStrategy(File directory) throws IOException { |
| 65 | 0 |
this.init(directory);
|
| 66 |
} |
|
| 67 |
|
|
| 68 | 0 |
protected void init(File directory) throws IOException { |
| 69 | 0 |
persistStorage = directory; |
| 70 | 0 |
if (!persistStorage.isDirectory())
|
| 71 | 0 |
throw new IOException("Expected a directory: " |
| 72 |
+ directory.getPath()); |
|
| 73 |
|
|
| 74 | 0 |
taskFileFilter = new TaskFileFilter();
|
| 75 |
} |
|
| 76 |
|
|
| 77 |
/**
|
|
| 78 |
* filters to .task files
|
|
| 79 |
*/
|
|
| 80 |
private class TaskFileFilter implements FilenameFilter { |
|
| 81 | 0 |
public boolean accept(File dir, String name) { |
| 82 | 0 |
if (name.endsWith(TASK_FILE_EXT))
|
| 83 | 0 |
return true; |
| 84 |
else
|
|
| 85 | 0 |
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 | 0 |
public Task[] readAll() throws IOException, ClassNotFoundException { |
| 96 | 0 |
synchronized (this) { |
| 97 | 0 |
ObjectInputStream inStream; |
| 98 | 0 |
FileInputStream file; |
| 99 |
|
|
| 100 | 0 |
File[] taskFiles = persistStorage.listFiles(taskFileFilter); |
| 101 | 0 |
Task[] tasks = new Task[taskFiles.length];
|
| 102 |
|
|
| 103 | 0 |
for (int i = 0; i < taskFiles.length; i++) { |
| 104 | 0 |
file = new FileInputStream(taskFiles[i]);
|
| 105 | 0 |
inStream = new ObjectInputStream(file);
|
| 106 | 0 |
tasks[i] = (Task) inStream.readObject(); |
| 107 | 0 |
inStream.close(); |
| 108 | 0 |
file.close(); |
| 109 |
} |
|
| 110 |
|
|
| 111 | 0 |
return tasks;
|
| 112 |
} |
|
| 113 |
} |
|
| 114 |
|
|
| 115 |
/**
|
|
| 116 |
* Write the task
|
|
| 117 |
*
|
|
| 118 |
* @param task The Task to write
|
|
| 119 |
*/
|
|
| 120 | 0 |
public void write(Task task) throws IOException { |
| 121 | 0 |
synchronized (this) { |
| 122 | 0 |
ObjectOutputStream outStream; |
| 123 | 0 |
FileOutputStream file; |
| 124 |
|
|
| 125 | 0 |
String fileName = new String(persistStorage.getPath()
|
| 126 |
+ File.separator + task.getID() + TASK_FILE_EXT); |
|
| 127 |
|
|
| 128 | 0 |
file = new FileOutputStream(fileName);
|
| 129 | 0 |
outStream = new ObjectOutputStream(file);
|
| 130 | 0 |
outStream.writeObject(task); |
| 131 | 0 |
outStream.close(); |
| 132 |
} |
|
| 133 |
} |
|
| 134 |
|
|
| 135 |
/**
|
|
| 136 |
* Delete the task
|
|
| 137 |
*/
|
|
| 138 | 0 |
public void delete(Task task) { |
| 139 | 0 |
synchronized (this) { |
| 140 | 0 |
String fileName = new String(persistStorage.getPath()
|
| 141 |
+ File.separator + task.getID() + TASK_FILE_EXT); |
|
| 142 |
|
|
| 143 | 0 |
File file = new File(fileName);
|
| 144 |
|
|
| 145 | 0 |
file.delete(); |
| 146 |
} |
|
| 147 |
} |
|
| 148 |
|
|
| 149 |
/**
|
|
| 150 |
* Clear all tasks
|
|
| 151 |
*/
|
|
| 152 | 0 |
public void deleteAll() { |
| 153 | 0 |
synchronized (this) { |
| 154 | 0 |
File[] taskFiles = persistStorage.listFiles(taskFileFilter); |
| 155 |
|
|
| 156 | 0 |
for (int i = 0; i < taskFiles.length; i++) { |
| 157 | 0 |
taskFiles[i].delete(); |
| 158 |
} |
|
| 159 |
} |
|
| 160 |
} |
|
| 161 |
|
|
| 162 |
} |
|
||||||||||