CacheProviderBean.java

  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.jcs3.jcache.extras.cdi;

  20. import javax.cache.spi.CachingProvider;
  21. import javax.enterprise.context.ApplicationScoped;
  22. import javax.enterprise.context.spi.CreationalContext;
  23. import javax.enterprise.inject.spi.Bean;
  24. import javax.enterprise.inject.spi.InjectionPoint;
  25. import javax.enterprise.inject.spi.PassivationCapable;
  26. import java.lang.annotation.Annotation;
  27. import java.lang.reflect.Type;
  28. import java.util.HashSet;
  29. import java.util.Set;

  30. import static java.util.Collections.emptySet;

  31. public class CacheProviderBean implements Bean<CachingProvider>, PassivationCapable
  32. {
  33.     private final Set<Type> types;
  34.     private final Set<Annotation> qualifiers;
  35.     private final CachingProvider provider;
  36.     private final String id;

  37.     public CacheProviderBean(final CachingProvider cacheManager)
  38.     {
  39.         provider = cacheManager;
  40.         id = getClass().getName() + "-" + hashCode();

  41.         types = new HashSet<>();
  42.         types.add(CachingProvider.class);
  43.         types.add(Object.class);

  44.         qualifiers = new HashSet<>();
  45.         qualifiers.add(DefaultLiteral.INSTANCE);
  46.         qualifiers.add(AnyLiteral.INSTANCE);
  47.     }

  48.     @Override
  49.     public Set<Type> getTypes()
  50.     {
  51.         return types;
  52.     }

  53.     @Override
  54.     public Set<Annotation> getQualifiers()
  55.     {
  56.         return qualifiers;
  57.     }

  58.     @Override
  59.     public Class<? extends Annotation> getScope()
  60.     {
  61.         return ApplicationScoped.class;
  62.     }

  63.     @Override
  64.     public String getName()
  65.     {
  66.         return null;
  67.     }

  68.     @Override
  69.     public boolean isNullable()
  70.     {
  71.         return false;
  72.     }

  73.     @Override
  74.     public Set<InjectionPoint> getInjectionPoints()
  75.     {
  76.         return emptySet();
  77.     }

  78.     @Override
  79.     public Class<?> getBeanClass()
  80.     {
  81.         return CachingProvider.class;
  82.     }

  83.     @Override
  84.     public Set<Class<? extends Annotation>> getStereotypes()
  85.     {
  86.         return emptySet();
  87.     }

  88.     @Override
  89.     public boolean isAlternative()
  90.     {
  91.         return false;
  92.     }

  93.     @Override
  94.     public CachingProvider create(final CreationalContext<CachingProvider> cacheManagerCreationalContext)
  95.     {
  96.         return provider;
  97.     }

  98.     @Override
  99.     public void destroy(final CachingProvider cacheProvider, final CreationalContext<CachingProvider> cacheManagerCreationalContext)
  100.     {
  101.         provider.close();
  102.     }

  103.     @Override
  104.     public String getId()
  105.     {
  106.         return id;
  107.     }
  108. }