1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.tests.context;
18
19 import java.io.Serializable;
20 import java.util.HashMap;
21 import java.util.Iterator;
22 import java.util.LinkedList;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.springframework.beans.factory.ObjectFactory;
27 import org.springframework.beans.factory.config.Scope;
28
29
30
31
32 @SuppressWarnings("serial")
33 public class SimpleMapScope implements Scope, Serializable {
34
35 private final Map<String, Object> map = new HashMap<String, Object>();
36
37 private final List<Runnable> callbacks = new LinkedList<Runnable>();
38
39
40 public SimpleMapScope() {
41 }
42
43 public final Map<String, Object> getMap() {
44 return this.map;
45 }
46
47
48 @Override
49 public Object get(String name, ObjectFactory<?> objectFactory) {
50 synchronized (this.map) {
51 Object scopedObject = this.map.get(name);
52 if (scopedObject == null) {
53 scopedObject = objectFactory.getObject();
54 this.map.put(name, scopedObject);
55 }
56 return scopedObject;
57 }
58 }
59
60 @Override
61 public Object remove(String name) {
62 synchronized (this.map) {
63 return this.map.remove(name);
64 }
65 }
66
67 @Override
68 public void registerDestructionCallback(String name, Runnable callback) {
69 this.callbacks.add(callback);
70 }
71
72 @Override
73 public Object resolveContextualObject(String key) {
74 return null;
75 }
76
77 public void close() {
78 for (Iterator<Runnable> it = this.callbacks.iterator(); it.hasNext();) {
79 Runnable runnable = it.next();
80 runnable.run();
81 }
82 }
83
84 @Override
85 public String getConversationId() {
86 return null;
87 }
88
89 }