blob: 4a5b60f2e947c39f968c43467c67ad5da49083dd [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.geronimo.config.cdi;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.PassivationCapable;
import javax.enterprise.util.AnnotationLiteral;
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
/**
* @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
*/
public abstract class ConfigInjectionBean<T> implements Bean<T>, PassivationCapable {
private final static Set<Annotation> QUALIFIERS = new HashSet<>();
static {
QUALIFIERS.add(new ConfigPropertyLiteral());
}
private final Class rawType;
private final Set<Type> types;
private final String id;
private final boolean alternative;
ConfigInjectionBean(Type type) {
this(type,false);
}
ConfigInjectionBean(Type type, boolean alternative) {
this.types = new HashSet<>();
this.types.add(type);
this.rawType = getRawType(type);
this.id = "ConfigInjectionBean_" + type.toString();
this.alternative = alternative;
}
private Class getRawType(Type type) {
if (type instanceof Class) {
return (Class) type;
}
else if (type instanceof ParameterizedType) {
ParameterizedType paramType = (ParameterizedType) type;
return (Class) paramType.getRawType();
}
throw new UnsupportedOperationException("No idea how to handle " + type);
}
@Override
public Set<InjectionPoint> getInjectionPoints() {
return Collections.emptySet();
}
@Override
public Class<?> getBeanClass() {
return rawType;
}
@Override
public boolean isNullable() {
return false;
}
@Override
public void destroy(T instance, CreationalContext<T> context) {
// no-op
}
@Override
public Set<Type> getTypes() {
return types;
}
@Override
public Set<Annotation> getQualifiers() {
return QUALIFIERS;
}
@Override
public Class<? extends Annotation> getScope() {
return Dependent.class;
}
@Override
public String getName() {
return null;
}
@Override
public Set<Class<? extends Annotation>> getStereotypes() {
return Collections.emptySet();
}
@Override
public boolean isAlternative() {
return alternative;
}
@Override
public String getId() {
return id;
}
private static class ConfigPropertyLiteral extends AnnotationLiteral<ConfigProperty> implements ConfigProperty {
@Override
public String name() {
return "";
}
@Override
public String defaultValue() {
return "";
}
}
}