001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.jcs3.jcache.extras.cdi;
020
021import javax.cache.CacheManager;
022import javax.enterprise.context.ApplicationScoped;
023import javax.enterprise.context.spi.CreationalContext;
024import javax.enterprise.inject.spi.Bean;
025import javax.enterprise.inject.spi.InjectionPoint;
026import javax.enterprise.inject.spi.PassivationCapable;
027import java.lang.annotation.Annotation;
028import java.lang.reflect.Type;
029import java.util.HashSet;
030import java.util.Set;
031
032import static java.util.Collections.emptySet;
033
034public class CacheManagerBean implements Bean<CacheManager>, PassivationCapable
035{
036    private final Set<Type> types;
037    private final Set<Annotation> qualifiers;
038    private final CacheManager manager;
039    private final String id;
040
041    public CacheManagerBean(final CacheManager cacheManager)
042    {
043        manager = cacheManager;
044        id = getClass().getName() + "-" + hashCode();
045
046        types = new HashSet<>();
047        types.add(CacheManager.class);
048        types.add(Object.class);
049
050        qualifiers = new HashSet<>();
051        qualifiers.add(DefaultLiteral.INSTANCE);
052        qualifiers.add(AnyLiteral.INSTANCE);
053    }
054
055    @Override
056    public Set<Type> getTypes()
057    {
058        return types;
059    }
060
061    @Override
062    public Set<Annotation> getQualifiers()
063    {
064        return qualifiers;
065    }
066
067    @Override
068    public Class<? extends Annotation> getScope()
069    {
070        return ApplicationScoped.class;
071    }
072
073    @Override
074    public String getName()
075    {
076        return null;
077    }
078
079    @Override
080    public boolean isNullable()
081    {
082        return false;
083    }
084
085    @Override
086    public Set<InjectionPoint> getInjectionPoints()
087    {
088        return emptySet();
089    }
090
091    @Override
092    public Class<?> getBeanClass()
093    {
094        return CacheManager.class;
095    }
096
097    @Override
098    public Set<Class<? extends Annotation>> getStereotypes()
099    {
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}