View Javadoc

1   /*$Id: InMemoryExecutionHistory.java,v 1.3 2004/07/26 12:07:38 nw Exp $
2    * Created on 26-May-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.Application;
14  import org.astrogrid.applications.beans.v1.cea.castor.ExecutionSummaryType;
15  import org.astrogrid.component.descriptor.ComponentDescriptor;
16  
17  import java.util.HashMap;
18  import java.util.Map;
19  
20  import junit.framework.Test;
21  
22  /*** In-memory implementation of an execution history component - keeps everything in memory, persists nothing
23   * <p />
24   * So not such a good one to use in a deployment environment. handy for testing however.
25   * <p />
26   * Implemented using two maps- one for current set, one for archive.
27   * @author Noel Winstanley nw@jb.man.ac.uk 26-May-2004
28   *
29   */
30  public class InMemoryExecutionHistory implements ExecutionHistory , ComponentDescriptor{
31      /*** Construct a new InMemoryExecutionHistory
32       * 
33       */
34      public InMemoryExecutionHistory() {
35          super();
36          currentSet = new HashMap();
37          archive= new SimpleHashMap();
38      }
39      protected  Map currentSet;
40      protected  SimpleMap archive;
41      /***
42       * @see org.astrogrid.applications.manager.persist.ExecutionHistory#isApplicationInCurrentSet(java.lang.String)
43       */
44      public boolean isApplicationInCurrentSet(String execID) {
45          return currentSet.containsKey(execID);
46      }
47      /***
48       * @see org.astrogrid.applications.manager.persist.ExecutionHistory#getApplicationFromCurrentSet(java.lang.String)
49       */
50      public Application getApplicationFromCurrentSet(String execID) throws ExecutionIDNotFoundException {
51          Application app = (Application)currentSet.get(execID);
52          if (app == null) {
53              throw new ExecutionIDNotFoundException(execID);
54          }
55          return app;
56      }
57      /***
58       * @see org.astrogrid.applications.manager.persist.ExecutionHistory#addApplicationToCurrentSet(org.astrogrid.applications.Application)
59       */
60      public void addApplicationToCurrentSet(Application app) {
61          currentSet.put(app.getID(),app);
62      }
63      /***
64       * @see org.astrogrid.applications.manager.persist.ExecutionHistory#moveApplicationFromCurrentSetToArchive(java.lang.String)
65       */
66      public void moveApplicationFromCurrentSetToArchive(String execID) throws ExecutionIDNotFoundException, PersistenceException{
67          Application app = getApplicationFromCurrentSet(execID);
68          ExecutionSummaryType summary = SummaryHelper.summarize(execID, app);
69          try {
70              archive.put(execID,summary);
71          } catch (Exception e) {
72              throw new PersistenceException("moveApplicationFromCurrentSetToArchive failed '" + execID + "'",e);
73          }
74          currentSet.remove(execID);
75        }
76        
77      /***
78       * @see org.astrogrid.applications.manager.persist.ExecutionHistory#getApplicationFromArchive(java.lang.String)
79       */
80      public ExecutionSummaryType getApplicationFromArchive(String execID) throws ExecutionIDNotFoundException, PersistenceException{
81          try {
82              ExecutionSummaryType summary = archive.get(execID);
83              if (summary == null) {
84                  throw new ExecutionIDNotFoundException(execID);
85              }
86              return summary;
87          } catch (ExecutionIDNotFoundException e) {
88              throw e;
89          } catch (Exception e) {
90              throw new PersistenceException("getApplicationFromArchive failed '" + execID + "'",e);
91          }
92      }
93      /***
94       * @see org.astrogrid.component.descriptor.ComponentDescriptor#getName()
95       */
96      public String getName() {
97          return "In-memory execution history";
98      }
99      /***
100      * @see org.astrogrid.component.descriptor.ComponentDescriptor#getDescription()
101      * @todo add dump of stored data.
102      */
103     public String getDescription() {
104         return "Simplest possible implementation - not for use in production environments";
105     }
106     /***
107      * @see org.astrogrid.component.descriptor.ComponentDescriptor#getInstallationTest()
108      */
109     public Test getInstallationTest() {
110         return null;
111     }
112     
113     /*** defined a limited map interface here, to make it simpler to provide different implementations - as full
114      * features of the {@link java.util.Map} interface not needed
115      * @author Noel Winstanley nw@jb.man.ac.uk 17-Jun-2004
116      *
117      */
118     public static interface SimpleMap /* restricts Map !! */ {
119         public void put(String key,ExecutionSummaryType value) throws Exception;
120         public ExecutionSummaryType get(String key) throws Exception;
121     }
122     
123     /*** implementation of the SimpleMap interface backed by a {@link java.util.HashMap}
124      * 
125      * @author Noel Winstanley nw@jb.man.ac.uk 17-Jun-2004
126      *
127      */
128     static class SimpleHashMap extends HashMap implements SimpleMap {
129 
130         /***
131          * @see org.astrogrid.applications.manager.persist.InMemoryExecutionHistory.SimpleMap#put(java.lang.String, org.astrogrid.applications.beans.v1.cea.castor.ExecutionSummaryType)
132          */
133         public void  put(String key, ExecutionSummaryType value) throws Exception {
134             super.put(key,value);
135         }
136 
137         /***
138          * @see org.astrogrid.applications.manager.persist.InMemoryExecutionHistory.SimpleMap#get(java.lang.String)
139          */
140         public ExecutionSummaryType get(String key) throws Exception {
141             return (ExecutionSummaryType)super.get(key);
142         }
143 
144     }
145 }
146 
147 
148 /* 
149 $Log: InMemoryExecutionHistory.java,v $
150 Revision 1.3  2004/07/26 12:07:38  nw
151 renamed indirect package to protocol,
152 renamed classes and methods within protocol package
153 javadocs
154 
155 Revision 1.2  2004/07/01 11:16:22  nw
156 merged in branch
157 nww-itn06-componentization
158 
159 Revision 1.1.2.3  2004/07/01 01:42:46  nw
160 final version, before merge
161 
162 Revision 1.1.2.2  2004/06/17 09:21:23  nw
163 finished all major functionality additions to core
164 
165 Revision 1.1.2.1  2004/06/14 08:56:58  nw
166 factored applications into sub-projects,
167 got packaging of wars to work again
168 
169 Revision 1.1.2.1  2004/05/28 10:23:10  nw
170 checked in early, broken version - but it builds and tests (fail)
171  
172 */