1
2
3
4
5
6
7
8
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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172