|
| 1 | +/* |
| 2 | + * Copyright 2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.redis.repository.cdi; |
| 17 | + |
| 18 | +import java.lang.annotation.Annotation; |
| 19 | +import java.lang.reflect.Type; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.HashSet; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Set; |
| 26 | + |
| 27 | +import javax.enterprise.context.ApplicationScoped; |
| 28 | +import javax.enterprise.context.spi.CreationalContext; |
| 29 | +import javax.enterprise.inject.Alternative; |
| 30 | +import javax.enterprise.inject.Default; |
| 31 | +import javax.enterprise.inject.Stereotype; |
| 32 | +import javax.enterprise.inject.spi.Bean; |
| 33 | +import javax.enterprise.inject.spi.BeanManager; |
| 34 | +import javax.enterprise.inject.spi.InjectionPoint; |
| 35 | +import javax.enterprise.inject.spi.PassivationCapable; |
| 36 | + |
| 37 | +import org.slf4j.Logger; |
| 38 | +import org.slf4j.LoggerFactory; |
| 39 | +import org.springframework.util.Assert; |
| 40 | +import org.springframework.util.StringUtils; |
| 41 | + |
| 42 | +/** |
| 43 | + * Base class for {@link Bean} wrappers. |
| 44 | + * |
| 45 | + * @author Mark Paluch |
| 46 | + */ |
| 47 | +public abstract class CdiBean<T> implements Bean<T>, PassivationCapable { |
| 48 | + |
| 49 | + private static final Logger LOGGER = LoggerFactory.getLogger(CdiBean.class); |
| 50 | + |
| 51 | + protected final BeanManager beanManager; |
| 52 | + |
| 53 | + private final Set<Annotation> qualifiers; |
| 54 | + private final Set<Type> types; |
| 55 | + private final Class<T> beanClass; |
| 56 | + private final String passivationId; |
| 57 | + |
| 58 | + /** |
| 59 | + * Creates a new {@link CdiBean}. |
| 60 | + * |
| 61 | + * @param qualifiers must not be {@literal null}. |
| 62 | + * @param beanClass has to be an interface must not be {@literal null}. |
| 63 | + * @param beanManager the CDI {@link BeanManager}, must not be {@literal null}. |
| 64 | + */ |
| 65 | + public CdiBean(Set<Annotation> qualifiers, Class<T> beanClass, BeanManager beanManager) { |
| 66 | + this(qualifiers, Collections.<Type> emptySet(), beanClass, beanManager); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Creates a new {@link CdiBean}. |
| 71 | + * |
| 72 | + * @param qualifiers must not be {@literal null}. |
| 73 | + * @param types additional bean types, must not be {@literal null}. |
| 74 | + * @param beanClass must not be {@literal null}. |
| 75 | + * @param beanManager the CDI {@link BeanManager}, must not be {@literal null}. |
| 76 | + */ |
| 77 | + public CdiBean(Set<Annotation> qualifiers, Set<Type> types, Class<T> beanClass, BeanManager beanManager) { |
| 78 | + |
| 79 | + Assert.notNull(qualifiers); |
| 80 | + Assert.notNull(beanManager); |
| 81 | + Assert.notNull(types); |
| 82 | + Assert.notNull(beanClass); |
| 83 | + |
| 84 | + this.qualifiers = qualifiers; |
| 85 | + this.types = types; |
| 86 | + this.beanClass = beanClass; |
| 87 | + this.beanManager = beanManager; |
| 88 | + this.passivationId = createPassivationId(qualifiers, beanClass); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Creates a unique identifier for the given repository type and the given annotations. |
| 93 | + * |
| 94 | + * @param qualifiers must not be {@literal null} or contain {@literal null} values. |
| 95 | + * @param repositoryType must not be {@literal null}. |
| 96 | + * @return |
| 97 | + */ |
| 98 | + private final String createPassivationId(Set<Annotation> qualifiers, Class<?> repositoryType) { |
| 99 | + |
| 100 | + List<String> qualifierNames = new ArrayList<String>(qualifiers.size()); |
| 101 | + |
| 102 | + for (Annotation qualifier : qualifiers) { |
| 103 | + qualifierNames.add(qualifier.annotationType().getName()); |
| 104 | + } |
| 105 | + |
| 106 | + Collections.sort(qualifierNames); |
| 107 | + |
| 108 | + StringBuilder builder = new StringBuilder(StringUtils.collectionToDelimitedString(qualifierNames, ":")); |
| 109 | + builder.append(":").append(repositoryType.getName()); |
| 110 | + |
| 111 | + return builder.toString(); |
| 112 | + } |
| 113 | + |
| 114 | + /* |
| 115 | + * (non-Javadoc) |
| 116 | + * @see javax.enterprise.inject.spi.Bean#getTypes() |
| 117 | + */ |
| 118 | + public Set<Type> getTypes() { |
| 119 | + |
| 120 | + Set<Type> types = new HashSet<Type>(); |
| 121 | + types.add(beanClass); |
| 122 | + types.addAll(Arrays.asList(beanClass.getInterfaces())); |
| 123 | + types.addAll(this.types); |
| 124 | + |
| 125 | + return types; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Returns an instance of the given {@link Bean} from the container. |
| 130 | + * |
| 131 | + * @param <S> the actual class type of the {@link Bean}. |
| 132 | + * @param bean the {@link Bean} defining the instance to create. |
| 133 | + * @param type the expected component type of the instance created from the {@link Bean}. |
| 134 | + * @return an instance of the given {@link Bean}. |
| 135 | + * @see javax.enterprise.inject.spi.BeanManager#getReference(Bean, Type, CreationalContext) |
| 136 | + * @see javax.enterprise.inject.spi.Bean |
| 137 | + * @see java.lang.reflect.Type |
| 138 | + */ |
| 139 | + @SuppressWarnings("unchecked") |
| 140 | + protected <S> S getDependencyInstance(Bean<S> bean, Type type) { |
| 141 | + return (S) beanManager.getReference(bean, type, beanManager.createCreationalContext(bean)); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Forces the initialization of bean target. |
| 146 | + */ |
| 147 | + public final void initialize() { |
| 148 | + create(beanManager.createCreationalContext(this)); |
| 149 | + } |
| 150 | + |
| 151 | + /* |
| 152 | + * (non-Javadoc) |
| 153 | + * @see javax.enterprise.context.spi.Contextual#destroy(java.lang.Object, javax.enterprise.context.spi.CreationalContext) |
| 154 | + */ |
| 155 | + public void destroy(T instance, CreationalContext<T> creationalContext) { |
| 156 | + |
| 157 | + if (LOGGER.isDebugEnabled()) { |
| 158 | + LOGGER.debug(String.format("Destroying bean instance %s for repository type '%s'.", instance.toString(), |
| 159 | + beanClass.getName())); |
| 160 | + } |
| 161 | + |
| 162 | + creationalContext.release(); |
| 163 | + } |
| 164 | + |
| 165 | + /* |
| 166 | + * (non-Javadoc) |
| 167 | + * @see javax.enterprise.inject.spi.Bean#getQualifiers() |
| 168 | + */ |
| 169 | + public Set<Annotation> getQualifiers() { |
| 170 | + return qualifiers; |
| 171 | + } |
| 172 | + |
| 173 | + /* |
| 174 | + * (non-Javadoc) |
| 175 | + * @see javax.enterprise.inject.spi.Bean#getName() |
| 176 | + */ |
| 177 | + public String getName() { |
| 178 | + |
| 179 | + return getQualifiers().contains(Default.class) ? beanClass.getName() |
| 180 | + : beanClass.getName() + "-" + getQualifiers().toString(); |
| 181 | + } |
| 182 | + |
| 183 | + /* |
| 184 | + * (non-Javadoc) |
| 185 | + * @see javax.enterprise.inject.spi.Bean#getStereotypes() |
| 186 | + */ |
| 187 | + public Set<Class<? extends Annotation>> getStereotypes() { |
| 188 | + |
| 189 | + Set<Class<? extends Annotation>> stereotypes = new HashSet<Class<? extends Annotation>>(); |
| 190 | + |
| 191 | + for (Annotation annotation : beanClass.getAnnotations()) { |
| 192 | + Class<? extends Annotation> annotationType = annotation.annotationType(); |
| 193 | + if (annotationType.isAnnotationPresent(Stereotype.class)) { |
| 194 | + stereotypes.add(annotationType); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + return stereotypes; |
| 199 | + } |
| 200 | + |
| 201 | + /* |
| 202 | + * (non-Javadoc) |
| 203 | + * @see javax.enterprise.inject.spi.Bean#getBeanClass() |
| 204 | + */ |
| 205 | + public Class<?> getBeanClass() { |
| 206 | + return beanClass; |
| 207 | + } |
| 208 | + |
| 209 | + /* |
| 210 | + * (non-Javadoc) |
| 211 | + * @see javax.enterprise.inject.spi.Bean#isAlternative() |
| 212 | + */ |
| 213 | + public boolean isAlternative() { |
| 214 | + return beanClass.isAnnotationPresent(Alternative.class); |
| 215 | + } |
| 216 | + |
| 217 | + /* |
| 218 | + * (non-Javadoc) |
| 219 | + * @see javax.enterprise.inject.spi.Bean#isNullable() |
| 220 | + */ |
| 221 | + public boolean isNullable() { |
| 222 | + return false; |
| 223 | + } |
| 224 | + |
| 225 | + /* |
| 226 | + * (non-Javadoc) |
| 227 | + * @see javax.enterprise.inject.spi.Bean#getInjectionPoints() |
| 228 | + */ |
| 229 | + public Set<InjectionPoint> getInjectionPoints() { |
| 230 | + return Collections.emptySet(); |
| 231 | + } |
| 232 | + |
| 233 | + /* |
| 234 | + * (non-Javadoc) |
| 235 | + * @see javax.enterprise.inject.spi.Bean#getScope() |
| 236 | + */ |
| 237 | + public Class<? extends Annotation> getScope() { |
| 238 | + return ApplicationScoped.class; |
| 239 | + } |
| 240 | + |
| 241 | + /* |
| 242 | + * (non-Javadoc) |
| 243 | + * @see javax.enterprise.inject.spi.PassivationCapable#getId() |
| 244 | + */ |
| 245 | + public String getId() { |
| 246 | + return passivationId; |
| 247 | + } |
| 248 | + |
| 249 | + /* |
| 250 | + * (non-Javadoc) |
| 251 | + * @see java.lang.Object#toString() |
| 252 | + */ |
| 253 | + @Override |
| 254 | + public String toString() { |
| 255 | + return String.format("CdiBean: type='%s', qualifiers=%s", beanClass.getName(), qualifiers.toString()); |
| 256 | + } |
| 257 | + |
| 258 | +} |
0 commit comments