1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.cdi.core;
20
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import javax.enterprise.context.spi.Contextual;
27
28 import org.eclipse.jetty.util.log.Log;
29 import org.eclipse.jetty.util.log.Logger;
30
31 public class SimpleBeanStore
32 {
33 private static final Logger LOG = Log.getLogger(SimpleBeanStore.class);
34
35 public Map<Contextual<?>, List<ScopedInstance<?>>> beans = new HashMap<>();
36
37 public void addBean(ScopedInstance<?> instance)
38 {
39 if (LOG.isDebugEnabled())
40 {
41 LOG.debug("addBean({})",instance);
42 }
43 List<ScopedInstance<?>> instances = getBeans(instance.bean);
44 if (instances == null)
45 {
46 instances = new ArrayList<>();
47 beans.put(instance.bean,instances);
48 }
49 instances.add(instance);
50 }
51
52 public void clear()
53 {
54 beans.clear();
55 }
56
57 public void destroy()
58 {
59 if (LOG.isDebugEnabled())
60 {
61 LOG.debug("destroy() - {} beans",beans.size());
62 }
63 for (List<ScopedInstance<?>> instances : beans.values())
64 {
65 if (LOG.isDebugEnabled())
66 {
67 LOG.debug("destroying - {} instance(s)",instances.size());
68 }
69 for (ScopedInstance<?> instance : instances)
70 {
71 if (LOG.isDebugEnabled())
72 {
73 LOG.debug("destroying instance {}",instance);
74 }
75 instance.destroy();
76 }
77 }
78 }
79
80 public List<ScopedInstance<?>> getBeans(Contextual<?> contextual)
81 {
82 if (LOG.isDebugEnabled())
83 {
84 LOG.debug("getBeans({})",contextual);
85 }
86 return beans.get(contextual);
87 }
88
89 @Override
90 public String toString()
91 {
92 return String.format("%s@%X[size=%d]",this.getClass().getSimpleName(),hashCode(),beans.size());
93 }
94 }