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