Rev 45 | Rev 57 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 45 | Rev 51 | ||
---|---|---|---|
Line 19... | Line 19... | ||
19 | * @author sergio.oliveira.jr@gmail.com
|
19 | * @author sergio.oliveira.jr@gmail.com
|
20 | */
|
20 | */
|
21 | public class MentaContainer implements Container { |
21 | public class MentaContainer implements Container { |
22 | 22 | ||
23 | private Map<String, Component> beans = new HashMap<String, Component>(); |
23 | private Map<String, Component> beans = new HashMap<String, Component>(); |
24 | - | ||
25 | private Map<String, Object> singletons = new HashMap<String, Object>(); |
- | |
- | 24 | ||
- | 25 | private Set<String> singletons = new HashSet<String>(); |
|
- | 26 | ||
- | 27 | private Map<String, Object> singletonsCache = new HashMap<String, Object>(); |
|
26 | 28 | ||
27 | private Set<Dependency> dependencies = new HashSet<Dependency>(); |
29 | private Set<Dependency> dependencies = new HashSet<Dependency>(); |
28 | 30 | ||
29 | public <T> T get(String key) { |
31 | public <T> T get(String key) { |
30 | 32 | ||
31 | if (!beans.containsKey(key)) return null; |
33 | if (!beans.containsKey(key)) return null; |
32 | 34 | ||
33 | Component c = beans.get(key); |
35 | Component c = beans.get(key); |
- | 36 | ||
- | 37 | boolean isSingleton = singletons.contains(key); |
|
34 | 38 | ||
35 | Object target = null; |
39 | Object target = null; |
36 | 40 | ||
37 | try { |
41 | try { |
38 | 42 | ||
39 | if (c.isSingleton()) { |
- | |
- | 43 | if (isSingleton) { |
|
40 | 44 | ||
41 | if (singletons.containsKey(key)) { |
- | |
- | 45 | if (singletonsCache.containsKey(key)) { |
|
42 | 46 | ||
43 | target = singletons.get(key); |
- | |
- | 47 | target = singletonsCache.get(key); |
|
44 | 48 | ||
45 | return (T) target; |
49 | return (T) target; |
46 | 50 | ||
47 | } else { |
51 | } else { |
48 | 52 | ||
49 | target = c.getInstance(); |
53 | target = c.getInstance(); |
50 | 54 | ||
51 | singletons.put(key, target); |
- | |
- | 55 | singletonsCache.put(key, target); |
|
52 | }
|
56 | }
|
53 | 57 | ||
54 | } else { |
58 | } else { |
55 | 59 | ||
56 | target = c.getInstance(); |
60 | target = c.getInstance(); |
Line 107... | Line 111... | ||
107 | 111 | ||
108 | throw new RuntimeException(e); |
112 | throw new RuntimeException(e); |
109 | }
|
113 | }
|
110 | }
|
114 | }
|
111 | 115 | ||
112 | public Component ioc(String key, Component component) { |
- | |
- | 116 | public Component ioc(String key, Component component, boolean isSingleton) { |
|
113 | 117 | ||
114 | beans.put(key, component); |
118 | beans.put(key, component); |
115 | 119 | ||
116 | // clear the singleton cache in case of adding again...
|
- | |
117 | singletons.remove(key); |
- | |
- | 120 | singletonsCache.remove(key); // just in case we are overriding a previous singleton bean... |
|
- | 121 | ||
- | 122 | if (isSingleton) { |
|
- | 123 | ||
- | 124 | singletons.add(key); |
|
- | 125 | ||
- | 126 | } else { |
|
- | 127 | ||
- | 128 | singletons.remove(key); // just in case... |
|
- | 129 | }
|
|
118 | 130 | ||
119 | return component; |
131 | return component; |
- | 132 | }
|
|
- | 133 | ||
- | 134 | public Component ioc(String key, Component component) { |
|
- | 135 | ||
- | 136 | return ioc(key, component, false); |
|
120 | }
|
137 | }
|
121 | 138 | ||
122 | public ConfigurableComponent ioc(String key, Class<? extends Object> klass) { |
139 | public ConfigurableComponent ioc(String key, Class<? extends Object> klass) { |
123 | 140 | ||
124 | ConfigurableComponent cc = new MentaComponent(klass); |
141 | ConfigurableComponent cc = new MentaComponent(klass); |
Line 128... | Line 145... | ||
128 | return cc; |
145 | return cc; |
129 | }
|
146 | }
|
130 | 147 | ||
131 | public ConfigurableComponent ioc(String key, Class<? extends Object> klass, boolean singleton) { |
148 | public ConfigurableComponent ioc(String key, Class<? extends Object> klass, boolean singleton) { |
132 | 149 | ||
133 | ConfigurableComponent cc = new MentaComponent(klass, singleton); |
- | |
- | 150 | ConfigurableComponent cc = new MentaComponent(klass); |
|
134 | 151 | ||
135 | ioc(key, cc); |
- | |
- | 152 | ioc(key, cc, singleton); |
|
136 | 153 | ||
137 | return cc; |
154 | return cc; |
138 | }
|
155 | }
|
139 | 156 | ||
140 | public Dependency autowire(Dependency d) { |
157 | public Dependency autowire(Dependency d) { |