1
2
3
4
5
6
7
8
9
10
11 package org.astrogrid.applications.manager.persist;
12
13 import org.astrogrid.applications.beans.v1.cea.castor.ExecutionSummaryType;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 import org.exolab.castor.xml.MarshalException;
18 import org.exolab.castor.xml.ValidationException;
19
20 import java.io.File;
21 import java.io.FileNotFoundException;
22 import java.io.FileReader;
23 import java.io.FileWriter;
24 import java.io.IOException;
25 import java.net.URLEncoder;
26
27 import junit.framework.Test;
28 import junit.framework.TestCase;
29
30 /*** Execution history that persists archives as xml documents to disk.
31 * Current Set of executing applications still only kept in memory.
32 * @author Noel Winstanley nw@jb.man.ac.uk 16-Jun-2004
33 *
34 */
35 public class FileStoreExecutionHistory extends InMemoryExecutionHistory {
36 /***
37 * Commons Logger for this class
38 */
39 private static final Log logger = LogFactory.getLog(FileStoreExecutionHistory.class);
40
41 /*** configuration interface for this component */
42 public interface StoreDir {
43 /*** base directory to store records in */
44 File getDir();
45 }
46
47 /*** implementation for testing of stoe dir that creates a temporary scratch space - deleted on exit */
48 public static class TemporaryStoreDir implements StoreDir {
49 public TemporaryStoreDir() throws IOException {
50 dir = File.createTempFile("temporary-file-store-execution-history",null);
51 dir.delete();
52 dir.deleteOnExit();
53 }
54 protected final File dir;
55 public File getDir() {
56 return dir;
57 }
58 }
59 /*** Construct a new FileStoreExecutionHistory
60 *
61 */
62 public FileStoreExecutionHistory(final StoreDir dir) {
63 super();
64 this.baseDir = dir.getDir();
65 this.baseDir.mkdirs();
66 archive = new XMLFileMap();
67 }
68 private final File baseDir;
69
70 /*** implementation of the map interface over a directory of xml documents
71 *
72 * @author Noel Winstanley nw@jb.man.ac.uk 16-Jun-2004
73 *
74 */
75 class XMLFileMap implements SimpleMap {
76 /***
77 * Commons Logger for this class
78 */
79
80 public XMLFileMap() {
81 logger.info("BaseDir set to " + baseDir.getAbsolutePath());
82 }
83 /***
84 * @see org.astrogrid.applications.manager.persist.InMemoryExecutionHistory.SimpleMap#put(java.lang.Object, java.lang.Object)
85 */
86 public void put(String key,ExecutionSummaryType value) throws MarshalException, ValidationException, IOException{
87 File f = mkFile(key);
88 FileWriter fw = new FileWriter(f);
89 try {
90 value.marshal(fw);
91 } finally {
92 try {
93 fw.close();
94 } catch (IOException e) {
95 logger.debug("failed to close file" );
96 }
97 }
98 }
99
100 /*** compute appropriate filename from key */
101 protected File mkFile(Object key) {
102 return new File(baseDir,URLEncoder.encode(key.toString()));
103 }
104 /***
105 * @see org.astrogrid.applications.manager.persist.InMemoryExecutionHistory.SimpleMap#get(java.lang.Object)
106 */
107 public ExecutionSummaryType get(String key) throws MarshalException, ValidationException, FileNotFoundException {
108 File f = mkFile(key);
109 if (!f.exists()) {
110 return null;
111 }
112 FileReader fr = new FileReader(f);
113 try {
114 return ExecutionSummaryType.unmarshalExecutionSummaryType(fr);
115 } finally {
116 try {
117 fr.close();
118 } catch (IOException e){
119 logger.debug("failed to close file" );
120 }
121 }
122 }
123
124 }
125
126
127
128 /***
129 * @see org.astrogrid.component.descriptor.ComponentDescriptor#getDescription()
130 */
131 public String getDescription() {
132 return "Execution History Store that persists records as xmldocuments on disk"
133 + "\n store directory " + baseDir.getAbsolutePath()
134 + "\n # records " + baseDir.list().length
135 ;
136 }
137
138 /***
139 * @see org.astrogrid.component.descriptor.ComponentDescriptor#getInstallationTest()
140 */
141 public Test getInstallationTest() {
142 return new InstallationTest("testBaseDir");
143 }
144
145 /***
146 * Installation Test for the FileStore
147 * checks that the basedir exists and is writable
148 * @author Noel Winstanley nw@jb.man.ac.uk 26-Jul-2004
149 *
150 */
151 public class InstallationTest extends TestCase {
152
153 public InstallationTest(String arg0) {
154 super(arg0);
155 }
156
157 public void testBaseDir() throws Exception {
158 assertTrue("Base Directory does not exist",baseDir.exists());
159 assertTrue("Base Directory isn't a directory",baseDir.isDirectory());
160 assertTrue("Base Directory can't be read",baseDir.canRead());
161 assertTrue("Base Directory can't be written to",baseDir.canWrite());
162 }
163 }
164
165 /***
166 * @see org.astrogrid.component.descriptor.ComponentDescriptor#getName()
167 */
168 public String getName() {
169 return "FileStoreExecutionHistory";
170 }
171
172 }
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195