Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PAYARA-2545 upgrade microprofile config to 1.2.1 #2587

Merged
merged 4 commits into from
May 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
package fish.payara.microprofile.config.cdi;

import fish.payara.nucleus.microprofile.config.spi.PayaraConfig;
import java.lang.reflect.Array;
import java.lang.reflect.ParameterizedType;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.config.inject.ConfigProperty;
Expand All @@ -48,7 +50,9 @@
import javax.enterprise.inject.spi.*;
import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import javax.inject.Provider;

/**
* CDI extension that implements the Microprofile Config API ConfigProperty injection
Expand All @@ -68,9 +72,15 @@ public void validateInjectionPoint(@Observes ProcessInjectionPoint<?, ?> pip) {
if (property != null) {
// see if we can resolve the injection point in the future
try {
Type t = pip.getInjectionPoint().getType();
if (Class.class.isInstance(pip.getInjectionPoint().getType())) {
ConfigPropertyProducer.getGenericProperty(pip.getInjectionPoint());
}
} else if (t instanceof ParameterizedType ){
Class rawClazz = (Class) ((ParameterizedType) t).getRawType();
if (rawClazz != Provider.class && rawClazz != Optional.class) {
ConfigPropertyProducer.getGenericProperty(pip.getInjectionPoint());
}
}
}catch (Throwable de ) {
Class failingClass = null;
Bean bean = pip.getInjectionPoint().getBean();
Expand All @@ -79,7 +89,7 @@ public void validateInjectionPoint(@Observes ProcessInjectionPoint<?, ?> pip) {
} else {
failingClass = pip.getInjectionPoint().getBean().getBeanClass();
}
pip.addDefinitionError(new DeploymentException("Deploment Failure for ConfigProperty " + property.name() + " in class " + failingClass.getCanonicalName() + " Reason " + de.getMessage(),de));
pip.addDefinitionError(new DeploymentException("Deployment Failure for ConfigProperty " + property.name() + " in class " + failingClass.getCanonicalName() + " Reason " + de.getMessage(),de));
}
}
}
Expand Down Expand Up @@ -125,22 +135,52 @@ public void addDynamicProducers(@Observes AfterBeanDiscovery event, BeanManager
}
}

// we have the method
if (beanAttr != null) {
HashSet<Type> types = new HashSet<>();
types.addAll(((PayaraConfig) config).getConverterTypes());
// add string explictly
// add String explictly
types.add(String.class);

// go through each type which has a converter either custom or built in
// create a bean with a Producer method using the bean factory and with custom bean attributes
// also override the set of types depending on the type of the converter
for (final Type converterType : types) {
// go through each type which has a converter
// create a bean with a Producer method using the bean factory and with custom bean attributes
Bean<?> bean = bm.createBean(new TypesBeanAttributes<Object>(beanAttr) {

// overrides the bean types to return the type registered for a Converter
// overrides the bean types to return the types registered for a Converter
@Override
public Set<Type> getTypes() {
HashSet<Type> result = new HashSet<>();
// add the type that the converter converts
result.add(converterType);

// also indicate support array of the type.
if (converterType instanceof Class) {
Object array = Array.newInstance((Class)converterType, 0);
result.add(array.getClass());
}

// ok we will have to do specific code for wrappers
// for each wrapper indicate we can also convert the primitive
// and we can convert the primitive array
if (converterType == Long.class) {
result.add(long.class);
result.add((new long[0]).getClass());
} else if (converterType == Boolean.class) {
result.add(boolean.class);
result.add((new boolean[0]).getClass());
} else if (converterType == Integer.class) {
result.add(int.class);
result.add((new int[0]).getClass());
} else if (converterType == Float.class) {
result.add(float.class);
result.add((new float[0]).getClass());
} else if (converterType == Double.class) {
result.add(double.class);
result.add((new double[0]).getClass());
}

return result;
}
}, ConfigPropertyProducer.class, bm.getProducerFactory(method, null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@
import fish.payara.nucleus.microprofile.config.spi.PayaraConfig;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import javax.annotation.PostConstruct;
import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Produces;
Expand Down Expand Up @@ -77,6 +81,50 @@ public void postConstruct() {
public Config getConfig() {
return new InjectedPayaraConfig(ConfigProvider.getConfig(), im.getCurrentInvocation().getAppName());
}

/**
* Producer method for Sets
* @param <T> Type
* @param ip Injection Point
* @return
*/
@Produces
@ConfigProperty
public <T> Set<T> getSetProperty(InjectionPoint ip) {
ConfigProperty property = ip.getAnnotated().getAnnotation(ConfigProperty.class);
PayaraConfig config = (PayaraConfig) ConfigProvider.getConfig();
Set<T> result = new HashSet<>();
Type type = ip.getType();
if (type instanceof ParameterizedType) {
// it is an Optional
// get the class of the generic parameterized Optional
Class clazzValue = (Class) ((ParameterizedType) type).getActualTypeArguments()[0];
result = config.getSetValues(property.name(),property.defaultValue(), clazzValue);
}
return result;
}

/**
* Producer method for Lists
* @param <T> Type
* @param ip Injection Point
* @return
*/
@Produces
@ConfigProperty
public <T> List<T> getListProperty(InjectionPoint ip) {
ConfigProperty property = ip.getAnnotated().getAnnotation(ConfigProperty.class);
PayaraConfig config = (PayaraConfig) ConfigProvider.getConfig();
List<T> result = new ArrayList<>();
Type type = ip.getType();
if (type instanceof ParameterizedType) {
// it is an Optional
// get the class of the generic parameterized Optional
Class clazzValue = (Class) ((ParameterizedType) type).getActualTypeArguments()[0];
result = config.getListValues(property.name(),property.defaultValue(), clazzValue);
}
return result;
}

/**
* Produces an Optional for the property specified by the ConfigProperty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@
package fish.payara.microprofile.config.cdi;

import fish.payara.nucleus.microprofile.config.spi.PayaraConfig;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Member;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Set;
import javax.enterprise.context.Dependent;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.DeploymentException;
Expand All @@ -61,7 +64,7 @@ public class ConfigPropertyProducer {
* General producer method for injecting a property into a field annotated
* with the @ConfigProperty annotation.
* Note this does not have @Produces annotation as a synthetic bean using this method
* is created in teh CDI Extension.
* is created in the CDI Extension.
* @param ip
* @return
*/
Expand Down Expand Up @@ -89,11 +92,18 @@ public static final Object getGenericProperty(InjectionPoint ip) {
name = sb.toString();
}

Type type = ip.getType();
Type type = ip.getType();
if (type instanceof Class) {
result = config.getValue(name, property.defaultValue(),(Class<?>)type);
} else if ( type instanceof ParameterizedType) {
result = config.getValue(name, (Class<?>)((ParameterizedType)type).getRawType());
ParameterizedType ptype = (ParameterizedType)type;
if (List.class.equals(ptype.getRawType())) {
result = config.getListValues(name, property.defaultValue(), (Class<?>) ptype.getActualTypeArguments()[0]);
} else if (Set.class.equals(ptype.getRawType())) {
result = config.getSetValues(name, property.defaultValue(), (Class<?>) ptype.getActualTypeArguments()[0]);
} else {
result = config.getValue(name, (Class<?>)((ParameterizedType)type).getRawType());
}
}

if (result == null) {
Expand Down
1 change: 0 additions & 1 deletion appserver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@
<rest.monitoring.version>1.0.0-Beta</rest.monitoring.version>

<!-- Microprofile version properties -->
<microprofile-config.version>1.1-payara-p1</microprofile-config.version>
<microprofile-fault-tolerance.version>1.0.payara-p1</microprofile-fault-tolerance.version>
<microprofile-jwt-auth.version>1.0.payara-p1</microprofile-jwt-auth.version>
<microprofile-healthcheck.version>1.0.payara-p1</microprofile-healthcheck.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,13 +502,13 @@ private void processApplicationLoaded(ApplicationInfo applicationInfo) {
ComponentInvocation componentInvocation = createComponentInvocation(applicationInfo);

try {
invocationManager.preInvoke(componentInvocation);
bootstrap.startExtensions(postProcessExtensions(deploymentImpl.getExtensions(), archives));
bootstrap.startContainer(deploymentImpl.getContextId() + ".bda", SERVLET, deploymentImpl);
bootstrap.startInitialization();
fireProcessInjectionTargetEvents(bootstrap, deploymentImpl);
bootstrap.deployBeans();
bootstrap.validateBeans();
invocationManager.preInvoke(componentInvocation);
bootstrap.endInitialization();
} catch (Throwable t) {
doBootstrapShutdown(applicationInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,10 @@ holder.
<artifactId>resources-connector</artifactId>
<version>${project.version}</version>
</dependency>
<!-- <dependency>
<groupId>org.glassfish.main.resources</groupId>
<artifactId>resources-connector</artifactId>
<version>4.1.1.171-SNAPSHOT</version>
<type>jar</type>
</dependency>-->
<dependency>
<groupId>org.eclipse.microprofile.config</groupId>
<artifactId>microprofile-config-api</artifactId>
<version>1.1</version>
<version>${microprofile-config.version}</version>
<type>jar</type>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public Boolean convert(String value) {
case "y":
case "on":
result = true;
break;
default:
result = false;
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2018 Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://github.com/payara/Payara/blob/master/LICENSE.txt
* See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* The Payara Foundation designates this particular file as subject to the "Classpath"
* exception as provided by the Payara Foundation in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package fish.payara.nucleus.microprofile.config.converters;

import com.sun.enterprise.util.Utility;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.config.spi.Converter;

/**
*
* @author steve
*/
public class ClassConverter implements Converter<Class> {

@Override
public Class convert(String propertyValue) {
if (propertyValue == null || propertyValue.equals(ConfigProperty.UNCONFIGURED_VALUE)) return null;
try {
return Class.forName(propertyValue, true, Utility.getClassLoader());
} catch (ClassNotFoundException ex) {
throw new IllegalArgumentException(ex);
}
}

}