-
Notifications
You must be signed in to change notification settings - Fork 116
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
CDI API for injection of configuration values #42
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
58d7198
Added CDI API and tests in TCK
OndroMih e5b15b2
Doc for ConfigProperty
OndroMih f4920a2
Replaced Provider with Instance for cleaner code
OndroMih f256476
Added more documentation
OndroMih 290a32f
Added option to overwrite checkstyle method format.
OndroMih dc5f183
Removed netbeans configuration file
OndroMih 83b8fd5
Adding description of how a default config key is generated.
f6ab693
Improved description of how a default config key is generated.
e93a16a
Fixes of compile errors.
OndroMih 5a67cd4
Fixes of copyright headers after review.
OndroMih 04a0b21
Removed injection of a config value without the ConfigValue wrapper.
OndroMih 8975d2c
Removed unused imports
OndroMih File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,4 @@ tck/.checkstyle | |
tck/bin/ | ||
.project | ||
build/ | ||
.classpath | ||
.classpath |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
api/src/main/java/io/microprofile/config/inject/ConfigProperty.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright (c) 2016-2017 Payara Services Ltd. and others | ||
* | ||
* Licensed 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 io.microprofile.config.inject; | ||
|
||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.PARAMETER; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
import javax.enterprise.util.Nonbinding; | ||
import javax.inject.Qualifier; | ||
|
||
/** | ||
* Binds the injection point with a configured value. | ||
* Should annotate injection points of type ConfigValue<T>, where T can be String and all types which have appropriate converters. | ||
* <p> | ||
* Example: | ||
* <pre> | ||
* <code> | ||
* | ||
* {@literal @Inject} | ||
* {@literal @ConfigProperty("my.long.property")} | ||
* {@literal ConfigValue<Long>} longConfigValue; | ||
* // injects a ConfigValue for the value of my.long.property property to resolve the property dynamically | ||
* </code> | ||
* </pre> | ||
* @author Ondrej Mihalyi | ||
*/ | ||
@Qualifier | ||
@Retention(RUNTIME) | ||
@Target({METHOD, FIELD, PARAMETER, TYPE}) | ||
public @interface ConfigProperty { | ||
/** | ||
* The kay of the config property used to look up the configuration value. | ||
* If it is not specified, it will be derived automatically as {@code <class_name>.<injetion_point_name>}, | ||
* where {@code injection_point_name} is either a field name or a property name in case of field/property injection, | ||
* {@code class_name} is the simple name of the class being injected to. | ||
* If one of the {@code class_name} or {@code injection_point_name} cannot be determined, the value has to be provided. | ||
* | ||
* @return Name (key) of the config property to inject | ||
*/ | ||
@Nonbinding | ||
String value() default ""; | ||
} |
49 changes: 49 additions & 0 deletions
49
api/src/main/java/org/eclipse/microprofile/config/ConfigValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (c) 2016-2017 Payara Services Ltd. and others | ||
* | ||
* Licensed 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.eclipse.microprofile.config; | ||
|
||
/** | ||
* Accessor to a configured value. Suitable when a configured value should be | ||
* accessed dynamically or additional metadata is necessary on top of the | ||
* configured value. | ||
* | ||
* @author Ondrej Mihalyi | ||
* | ||
* @param <T_VALUE> Type of the configuration value | ||
*/ | ||
public interface ConfigValue<T_VALUE> { | ||
|
||
/** | ||
* Returns the converted resolved value. | ||
* | ||
* @return the resolved value | ||
*/ | ||
T_VALUE getValue(); | ||
|
||
/** | ||
* Whether the value is defined in one of the config sources or not. | ||
* @return True if a value for the key is defined, false otherwise | ||
*/ | ||
boolean isValueAvailable(); | ||
|
||
/** | ||
* Returns the key used to retrieve the configuration value. | ||
* | ||
* @return the original key | ||
*/ | ||
String getKey(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright (c) 2016-2017 Payara Services Ltd. and others | ||
* | ||
* Licensed 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.eclipse.microprofile.config.tck; | ||
|
||
import io.microprofile.config.inject.ConfigProperty; | ||
import javax.enterprise.context.Dependent; | ||
import javax.inject.Inject; | ||
import org.eclipse.microprofile.config.ConfigValue; | ||
import static org.eclipse.microprofile.config.tck.testsupport.TestSetup.ensure_property_defined; | ||
import static org.eclipse.microprofile.config.tck.testsupport.TestSetup.ensure_property_undefined; | ||
import static org.eclipse.microprofile.config.tck.testsupport.TestSetup.get_bean_of_type; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.is; | ||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.arquillian.junit.Arquillian; | ||
import org.jboss.shrinkwrap.api.Archive; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.EmptyAsset; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import static org.junit.Assert.assertThat; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
/** | ||
* Test cases for CDI-based API that test retrieving values from the configuration. | ||
* The tests depend only on CDI 1.2. | ||
* @author Ondrej Mihalyi | ||
*/ | ||
@RunWith(Arquillian.class) | ||
public class CDIPlainInjectionTest { | ||
|
||
@Deployment | ||
public static Archive deployment() { | ||
return ShrinkWrap.create(JavaArchive.class) | ||
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); | ||
} | ||
|
||
@Test | ||
public void can_inject_dynamic_values_via_ConfigValue() { | ||
clear_all_property_values(); | ||
|
||
DynamicValuesBean bean = get_bean_of_type(DynamicValuesBean.class); | ||
ConfigValue<Integer> intPropertyValue = bean.getIntPropertyValue(); | ||
|
||
assertThat(intPropertyValue.isValueAvailable(), is(false)); | ||
|
||
ensure_all_property_values_are_defined(); | ||
|
||
assertThat(intPropertyValue.isValueAvailable(), is(true)); | ||
assertThat(intPropertyValue.getValue(), is(equalTo(5))); | ||
} | ||
|
||
private void ensure_all_property_values_are_defined() { | ||
ensure_property_defined("my.string.property", "text"); | ||
ensure_property_defined("my.boolean.property", "true"); | ||
ensure_property_defined("my.int.property", "5"); | ||
ensure_property_defined("my.long.property", "10"); | ||
ensure_property_defined("my.float.property", "10.5"); | ||
ensure_property_defined("my.double.property", "11.5"); | ||
ensure_property_defined("my.date.property", "2017-01-30"); | ||
ensure_property_defined("my.localdate.property", "2016-01-30"); | ||
ensure_property_defined("my.datetime.property", "2015-01-30T10:00"); | ||
} | ||
|
||
private void clear_all_property_values() { | ||
ensure_property_undefined("my.string.property"); | ||
ensure_property_undefined("my.boolean.property"); | ||
ensure_property_undefined("my.int.property"); | ||
ensure_property_undefined("my.long.property"); | ||
ensure_property_undefined("my.float.property"); | ||
ensure_property_undefined("my.double.property"); | ||
ensure_property_undefined("my.date.property"); | ||
ensure_property_undefined("my.localdate.property"); | ||
ensure_property_undefined("my.datetime.property"); | ||
} | ||
|
||
@Dependent | ||
public static class DynamicValuesBean { | ||
|
||
@Inject | ||
@ConfigProperty("my.int.property") | ||
private ConfigValue<Integer> intPropertyValue; | ||
|
||
public ConfigValue<Integer> getIntPropertyValue() { | ||
return intPropertyValue; | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So far, I've seen people using 2 different naming conventions for this kind of annotation and a new injection point type (discussed here):
@Inject @PropertyConfig("mykey") PropertyValue<String> mykeyValue
@Inject @ConfigProperty("mykey") ConfigValue<String> mykeyValue
Please comment here to let me know which is better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Inject @ConfigProperty("mykey") ConfigValue mykeyValue
makes more sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For developers, I don't think the default name would start with class name. What is the normal prefix? I think for the config source under meta-inf\microprofile-config.properties, the key will not have any prefix. For the config source files outside the app, it depends. If the source is for a particular app, it might have the prefix with the app name etc. Therefore, I think it is better not to prebake any prefix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree classname.package name is a good default as @OndrejM says this is more amenable to refactoring while still providing a level of name scoping. Obviously the developer can still specify the name directly in the annotation. I'm just advocating this as a default if no name is specified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recommend to use classname.fieldName for the default. It is not common to use package name as part of property name. By the way, the classname should be the classname with the first letter in lower case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is already stated in the javadoc as you recommended: "
class_name
is the simple name of the class being injected to". Is that OK or am I missing something?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the javadoc, {@code classname} is the simple name of the class being injected to with the first letter being decapitalised. e.g. If the class name
Public class File{
@Inject @ConfigProperty ConfigValue name; => this should map to file.name property.
}