01: package com.mockrunner.example.struts;
02:
03: import java.util.HashMap;
04:
05: import javax.servlet.ServletContext;
06:
07: /**
08: * A simple data repository stored in the <ServletContext>.
09: * The implementation is not thread safe. Used by
10: * {@link StoreDataActionTest}.
11: */
12: public class MemoryBasedRepository {
13: private HashMap dataStore;
14:
15: private MemoryBasedRepository() {
16: dataStore = new HashMap();
17: }
18:
19: public static MemoryBasedRepository instance(ServletContext context) {
20: MemoryBasedRepository instance = (MemoryBasedRepository) context
21: .getAttribute(MemoryBasedRepository.class.getName());
22: if (null != instance)
23: return instance;
24: instance = new MemoryBasedRepository();
25: context.setAttribute(MemoryBasedRepository.class.getName(),
26: instance);
27: return instance;
28: }
29:
30: public void set(String id, Object data) {
31: dataStore.put(id, data);
32: }
33:
34: public Object get(String id) {
35: return dataStore.get(id);
36: }
37: }
|