View Javadoc

1   /*$Id: FileStoreExecutionHistory.java,v 1.7 2006/03/17 17:50:58 clq2 Exp $
2    * Created on 16-Jun-2004
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
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     }// end inner class
107     
108     
109     // componentDescritpin interface 
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 $Log: FileStoreExecutionHistory.java,v $
159 Revision 1.7  2006/03/17 17:50:58  clq2
160 gtr_1489_cea correted version
161 
162 Revision 1.5  2006/03/07 21:45:26  clq2
163 gtr_1489_cea
164 
165 Revision 1.4.148.1  2006/01/25 17:04:32  gtr
166 Refactored: the configuration is now a fixed structure based at the configurable location cea.base.dir.
167 
168 Revision 1.4  2004/09/17 10:17:09  nw
169 made sure streams are closed
170 
171 Revision 1.3  2004/07/26 12:07:38  nw
172 renamed indirect package to protocol,
173 renamed classes and methods within protocol package
174 javadocs
175 
176 Revision 1.2  2004/07/01 11:16:22  nw
177 merged in branch
178 nww-itn06-componentization
179 
180 Revision 1.1.2.2  2004/07/01 01:42:46  nw
181 final version, before merge
182 
183 Revision 1.1.2.1  2004/06/17 09:21:23  nw
184 finished all major functionality additions to core
185  
186 */