1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.jcs3.jcache.extras.cdi;
20
21 import javax.cache.CacheManager;
22 import javax.enterprise.context.ApplicationScoped;
23 import javax.enterprise.context.spi.CreationalContext;
24 import javax.enterprise.inject.spi.Bean;
25 import javax.enterprise.inject.spi.InjectionPoint;
26 import javax.enterprise.inject.spi.PassivationCapable;
27 import java.lang.annotation.Annotation;
28 import java.lang.reflect.Type;
29 import java.util.HashSet;
30 import java.util.Set;
31
32 import static java.util.Collections.emptySet;
33
34 public class CacheManagerBean implements Bean<CacheManager>, PassivationCapable
35 {
36 private final Set<Type> types;
37 private final Set<Annotation> qualifiers;
38 private final CacheManager manager;
39 private final String id;
40
41 public CacheManagerBean(final CacheManager cacheManager)
42 {
43 manager = cacheManager;
44 id = getClass().getName() + "-" + hashCode();
45
46 types = new HashSet<>();
47 types.add(CacheManager.class);
48 types.add(Object.class);
49
50 qualifiers = new HashSet<>();
51 qualifiers.add(DefaultLiteral.INSTANCE);
52 qualifiers.add(AnyLiteral.INSTANCE);
53 }
54
55 @Override
56 public Set<Type> getTypes()
57 {
58 return types;
59 }
60
61 @Override
62 public Set<Annotation> getQualifiers()
63 {
64 return qualifiers;
65 }
66
67 @Override
68 public Class<? extends Annotation> getScope()
69 {
70 return ApplicationScoped.class;
71 }
72
73 @Override
74 public String getName()
75 {
76 return null;
77 }
78
79 @Override
80 public boolean isNullable()
81 {
82 return false;
83 }
84
85 @Override
86 public Set<InjectionPoint> getInjectionPoints()
87 {
88 return emptySet();
89 }
90
91 @Override
92 public Class<?> getBeanClass()
93 {
94 return CacheManager.class;
95 }
96
97 @Override
98 public Set<Class<? extends Annotation>> getStereotypes()
99 {
100 return emptySet();
101 }
102
103 @Override
104 public boolean isAlternative()
105 {
106 return false;
107 }
108
109 @Override
110 public CacheManager create(final CreationalContext<CacheManager> cacheManagerCreationalContext)
111 {
112 return manager;
113 }
114
115 @Override
116 public void destroy(final CacheManager cacheManager, final CreationalContext<CacheManager> cacheManagerCreationalContext)
117 {
118 manager.close();
119 }
120
121 @Override
122 public String getId()
123 {
124 return id;
125 }
126 }