View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.jcs.jcache.extras.cdi;
20  
21  import javax.cache.spi.CachingProvider;
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 CacheProviderBean implements Bean<CachingProvider>, PassivationCapable
35  {
36      private final Set<Type> types;
37      private final Set<Annotation> qualifiers;
38      private final CachingProvider provider;
39      private final String id;
40  
41      public CacheProviderBean(final CachingProvider cacheManager)
42      {
43          provider = cacheManager;
44          id = getClass().getName() + "-" + hashCode();
45  
46          types = new HashSet<Type>();
47          types.add(CachingProvider.class);
48          types.add(Object.class);
49  
50          qualifiers = new HashSet<Annotation>();
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 CachingProvider.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 CachingProvider create(final CreationalContext<CachingProvider> cacheManagerCreationalContext)
111     {
112         return provider;
113     }
114 
115     @Override
116     public void destroy(final CachingProvider cacheProvider, final CreationalContext<CachingProvider> cacheManagerCreationalContext)
117     {
118         provider.close();
119     }
120 
121     @Override
122     public String getId()
123     {
124         return id;
125     }
126 }