Repository: deltaspike Updated Branches: refs/heads/master 965f572a6 -> 4039f6fb9
DELTASPIKE-837 PropertyFileConfig via ServiceLoader Add a way to pick up PropertyFileConfig also via java.util.ServiceLoader mechanism. This is useful if you need the configured values also during boot time and if you have to deal with EARs in some broken containers. Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/4039f6fb Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/4039f6fb Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/4039f6fb Branch: refs/heads/master Commit: 4039f6fb956cd76829a1090f952c496069a0a634 Parents: 965f572 Author: Mark Struberg <[email protected]> Authored: Mon Feb 23 11:55:54 2015 +0100 Committer: Mark Struberg <[email protected]> Committed: Mon Feb 23 11:55:54 2015 +0100 ---------------------------------------------------------------------- .../core/api/config/PropertyFileConfig.java | 37 +++++++++++++++-- .../impl/config/ConfigurationExtension.java | 8 ++++ .../config/DefaultConfigSourceProvider.java | 18 +++++++++ .../MyCustomBootTimePropertyFileConfig.java | 42 ++++++++++++++++++++ .../MyCustomPropertyFileConfig.java | 4 +- .../MyNotPickedUpPropertyFileConfig.java | 42 ++++++++++++++++++++ .../PropertyConfigSourceTest.java | 8 ++++ ...eltaspike.core.api.config.PropertyFileConfig | 20 ++++++++++ .../impl/src/test/resources/application.xml | 26 ++++++------ .../test/resources/myboottimeconfig.properties | 22 ++++++++++ .../impl/src/test/resources/myconfig.properties | 2 + .../resources/mynotpickedupconfig.properties | 22 ++++++++++ 12 files changed, 234 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4039f6fb/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/PropertyFileConfig.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/PropertyFileConfig.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/PropertyFileConfig.java index 5d25b3a..9f65ddf 100644 --- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/PropertyFileConfig.java +++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/PropertyFileConfig.java @@ -20,12 +20,14 @@ package org.apache.deltaspike.core.api.config; /** * <p> - * If you implement this interface inside a Bean Archive (a JAR or ClassPath entry with a META-INF/beans.xml file), the - * property files with the given file name will be registered as + * If you implement this interface, the property files with the given file name will be registered as * {@link org.apache.deltaspike.core.spi.config.ConfigSource}s.</p> + *<p>There are 2 ways to register a {@code PropertyFileConfig}</p> * + * <h3>1. Automatic pickup via {@code ProcessAnnotatedType} phase</h3> * <p> - * DeltaSpike will automatically pick up all the implementations during the + * DeltaSpike will automatically pick up all the implementations which are + * inside a Bean Archive (a JAR or ClassPath entry with a META-INF/beans.xml file) during the * {@link javax.enterprise.inject.spi.ProcessAnnotatedType} phase and create a new instance via reflection. Thus the * implementations will need a non-private default constructor. There is <b>no</b> CDI injection being performed in * those instances! The scope of the implementations will also be ignored as they will not get picked up as CDI @@ -45,6 +47,35 @@ package org.apache.deltaspike.core.api.config; * {@link org.apache.deltaspike.core.spi.config.ConfigSource} or * {@link org.apache.deltaspike.core.spi.config.ConfigSourceProvider} via the * {@link java.util.ServiceLoader} mechanism described there.</p>. + * + * <h3>2. Automatic pickup via {@code java.util.ServiceLoader} mechanism</h3> + * <p>In case you have an EAR or you need the configured values already during the CDI container start + * then you can also register the PropertyFileConfig via the {@code java.util.ServiceLoader} mechanism. + * To not have this configuration picked up twice it is required to annotate your own + * {@code PropertyFileConfig} implementation with {@link org.apache.deltaspike.core.api.exclude.Exclude}.</p> + * + * <p>The {@code ServiceLoader} mechanism requires to have a file + * <pre> + * META-INF/services/org.apache.deltaspike.core.api.config.PropertyFileConfig + * </pre> + * containing the fully qualified Class name of your own {@code PropertyFileConfig} implementation class. + * <pre> + * com.acme.my.own.SomeSpecialPropertyFileConfig + * </pre> + * The implementation will look like the following: + * <pre> + * @Exclude + * public class SomeSpecialPropertyFileConfig implements PropertyFileConfig { + * public String getPropertyFileName() { + * return "myconfig/specialconfig.properties" + * } + * public boolean isOptional() { + * return false; + * } + * } + * </pre> + * </p> + * */ public interface PropertyFileConfig extends DeltaSpikeConfig { http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4039f6fb/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigurationExtension.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigurationExtension.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigurationExtension.java index d578312..48e0f17 100644 --- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigurationExtension.java +++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigurationExtension.java @@ -34,6 +34,7 @@ import java.util.concurrent.ConcurrentHashMap; import org.apache.deltaspike.core.api.config.ConfigResolver; import org.apache.deltaspike.core.api.config.PropertyFileConfig; +import org.apache.deltaspike.core.api.exclude.Exclude; import org.apache.deltaspike.core.spi.activation.Deactivatable; import org.apache.deltaspike.core.spi.config.ConfigSource; import org.apache.deltaspike.core.util.ClassDeactivationUtils; @@ -91,6 +92,13 @@ public class ConfigurationExtension implements Extension, Deactivatable return; } + if (pat.getAnnotatedType().isAnnotationPresent(Exclude.class)) + { + // We only pick up PropertyFileConfigs if they are not excluded + // This can be the case for PropertyFileConfigs which are registered via java.util.ServiceLoader + return; + } + propertyFileConfigClasses.add(pcsClass); } http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4039f6fb/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DefaultConfigSourceProvider.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DefaultConfigSourceProvider.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DefaultConfigSourceProvider.java index 126ec06..e813019 100644 --- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DefaultConfigSourceProvider.java +++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/DefaultConfigSourceProvider.java @@ -18,8 +18,10 @@ */ package org.apache.deltaspike.core.impl.config; +import org.apache.deltaspike.core.api.config.PropertyFileConfig; import org.apache.deltaspike.core.spi.config.ConfigSource; import org.apache.deltaspike.core.spi.config.ConfigSourceProvider; +import org.apache.deltaspike.core.util.ServiceUtils; import java.util.ArrayList; import java.util.List; @@ -51,6 +53,22 @@ public class DefaultConfigSourceProvider implements ConfigSourceProvider EnvironmentPropertyConfigSourceProvider epcsp = new EnvironmentPropertyConfigSourceProvider(PROPERTY_FILE_NAME, true); configSources.addAll(epcsp.getConfigSources()); + + registerPropertyFileConfigs(); + } + + private void registerPropertyFileConfigs() + { + List<? extends PropertyFileConfig> propertyFileConfigs = + ServiceUtils.loadServiceImplementations(PropertyFileConfig.class); + for (PropertyFileConfig propertyFileConfig : propertyFileConfigs) + { + EnvironmentPropertyConfigSourceProvider environmentPropertyConfigSourceProvider + = new EnvironmentPropertyConfigSourceProvider(propertyFileConfig.getPropertyFileName(), + propertyFileConfig.isOptional()); + + configSources.addAll(environmentPropertyConfigSourceProvider.getConfigSources()); + } } /** http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4039f6fb/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/propertyconfigsource/MyCustomBootTimePropertyFileConfig.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/propertyconfigsource/MyCustomBootTimePropertyFileConfig.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/propertyconfigsource/MyCustomBootTimePropertyFileConfig.java new file mode 100644 index 0000000..705e10e --- /dev/null +++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/propertyconfigsource/MyCustomBootTimePropertyFileConfig.java @@ -0,0 +1,42 @@ +/* + * 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.deltaspike.test.core.api.config.propertyconfigsource; + +import org.apache.deltaspike.core.api.config.PropertyFileConfig; +import org.apache.deltaspike.core.api.exclude.Exclude; + +/** + * Custom PropertyFileConfig which gets picked up via {@code java.util.ServiceLoader} + * The values will already be available <em>before</em> the container gets booted! + */ +@Exclude // this is important to let the ConfigurationExtension know that it should not handle it +public class MyCustomBootTimePropertyFileConfig implements PropertyFileConfig +{ + @Override + public String getPropertyFileName() + { + return "myboottimeconfig.properties"; + } + + @Override + public boolean isOptional() + { + return false; + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4039f6fb/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/propertyconfigsource/MyCustomPropertyFileConfig.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/propertyconfigsource/MyCustomPropertyFileConfig.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/propertyconfigsource/MyCustomPropertyFileConfig.java index 49c4a2f..edd7007 100644 --- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/propertyconfigsource/MyCustomPropertyFileConfig.java +++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/propertyconfigsource/MyCustomPropertyFileConfig.java @@ -21,7 +21,9 @@ package org.apache.deltaspike.test.core.api.config.propertyconfigsource; import org.apache.deltaspike.core.api.config.PropertyFileConfig; /** - * Custom + * Custom PropertyFileConfig which gets picked up during + * {@link javax.enterprise.inject.spi.ProcessAnnotatedType}. + * The values will be available <em>after</em> the container got booted! */ public class MyCustomPropertyFileConfig implements PropertyFileConfig { http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4039f6fb/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/propertyconfigsource/MyNotPickedUpPropertyFileConfig.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/propertyconfigsource/MyNotPickedUpPropertyFileConfig.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/propertyconfigsource/MyNotPickedUpPropertyFileConfig.java new file mode 100644 index 0000000..35c8ac2 --- /dev/null +++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/propertyconfigsource/MyNotPickedUpPropertyFileConfig.java @@ -0,0 +1,42 @@ +/* + * 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.deltaspike.test.core.api.config.propertyconfigsource; + +import org.apache.deltaspike.core.api.config.PropertyFileConfig; +import org.apache.deltaspike.core.api.exclude.Exclude; + +/** + * Custom PropertyFileConfig which gets picked up via {@code java.util.ServiceLoader} + * The values will already be available <em>before</em> the container gets booted! + */ +@Exclude // this is important to let the ConfigurationExtension know that it should not handle it +public class MyNotPickedUpPropertyFileConfig implements PropertyFileConfig +{ + @Override + public String getPropertyFileName() + { + return "mynotpickedupconfig.properties"; + } + + @Override + public boolean isOptional() + { + return false; + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4039f6fb/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/propertyconfigsource/PropertyConfigSourceTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/propertyconfigsource/PropertyConfigSourceTest.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/propertyconfigsource/PropertyConfigSourceTest.java index 7653ec4..554faf1 100644 --- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/propertyconfigsource/PropertyConfigSourceTest.java +++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/config/propertyconfigsource/PropertyConfigSourceTest.java @@ -37,6 +37,8 @@ import org.junit.Test; public class PropertyConfigSourceTest { private final static String CONFIG_FILE_NAME = "myconfig.properties"; + private final static String BOOTCONFIG_FILE_NAME = "myboottimeconfig.properties"; + private final static String NOT_PICKED_UP_CONFIG_FILE_NAME = "mynotpickedupconfig.properties"; /** *X TODO creating a WebArchive is only a workaround because JavaArchive cannot contain other archives. @@ -47,6 +49,8 @@ public class PropertyConfigSourceTest JavaArchive testJar = ShrinkWrap.create(JavaArchive.class, "PropertyConfigSourceTest.jar") .addPackage(PropertyConfigSourceTest.class.getPackage()) .addAsResource(CONFIG_FILE_NAME) + .addAsResource(BOOTCONFIG_FILE_NAME) + .addAsResource(NOT_PICKED_UP_CONFIG_FILE_NAME) .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); return ShrinkWrap.create(WebArchive.class, "beanProvider.war") @@ -65,6 +69,10 @@ public class PropertyConfigSourceTest String value = ConfigResolver.getPropertyValue("some.propertykey"); Assert.assertNotNull(value); Assert.assertEquals("somevalue", value); + + String bootTimeValue = ConfigResolver.getPropertyValue("some.boottimekey"); + Assert.assertNotNull(bootTimeValue); + Assert.assertEquals("correctvalue", bootTimeValue); } } http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4039f6fb/deltaspike/core/impl/src/test/resources/META-INF/services/org.apache.deltaspike.core.api.config.PropertyFileConfig ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/test/resources/META-INF/services/org.apache.deltaspike.core.api.config.PropertyFileConfig b/deltaspike/core/impl/src/test/resources/META-INF/services/org.apache.deltaspike.core.api.config.PropertyFileConfig new file mode 100644 index 0000000..a7f802c --- /dev/null +++ b/deltaspike/core/impl/src/test/resources/META-INF/services/org.apache.deltaspike.core.api.config.PropertyFileConfig @@ -0,0 +1,20 @@ +# 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. + + +# dummy PropertyFileConfig which gets picked up at boot time +org.apache.deltaspike.test.core.api.config.propertyconfigsource.MyCustomBootTimePropertyFileConfig \ No newline at end of file http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4039f6fb/deltaspike/core/impl/src/test/resources/application.xml ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/test/resources/application.xml b/deltaspike/core/impl/src/test/resources/application.xml index 9b9b5a5..f9528c3 100644 --- a/deltaspike/core/impl/src/test/resources/application.xml +++ b/deltaspike/core/impl/src/test/resources/application.xml @@ -18,17 +18,17 @@ under the License. --> <application xmlns="http://java.sun.com/xml/ns/javaee" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" - version="6"> - <module> - <ejb>ejb-jar.jar</ejb> - </module> - <module> - <web> - <web-uri>test.war</web-uri> - <context-root>test</context-root> - </web> - </module> - <library-directory>lib</library-directory> + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" + version="6"> + <module> + <ejb>ejb-jar.jar</ejb> + </module> + <module> + <web> + <web-uri>test.war</web-uri> + <context-root>test</context-root> + </web> + </module> + <library-directory>lib</library-directory> </application> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4039f6fb/deltaspike/core/impl/src/test/resources/myboottimeconfig.properties ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/test/resources/myboottimeconfig.properties b/deltaspike/core/impl/src/test/resources/myboottimeconfig.properties new file mode 100644 index 0000000..8693c90 --- /dev/null +++ b/deltaspike/core/impl/src/test/resources/myboottimeconfig.properties @@ -0,0 +1,22 @@ +#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. + +# a user defined property file which is available during container boot already + +deltaspike_ordinal = 120 + +some.boottimekey = correctvalue http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4039f6fb/deltaspike/core/impl/src/test/resources/myconfig.properties ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/test/resources/myconfig.properties b/deltaspike/core/impl/src/test/resources/myconfig.properties index 05b0831..aa791c2 100644 --- a/deltaspike/core/impl/src/test/resources/myconfig.properties +++ b/deltaspike/core/impl/src/test/resources/myconfig.properties @@ -18,3 +18,5 @@ # a user defined property file some.propertykey = somevalue + +some.boottimekey = wrongvalue http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4039f6fb/deltaspike/core/impl/src/test/resources/mynotpickedupconfig.properties ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/test/resources/mynotpickedupconfig.properties b/deltaspike/core/impl/src/test/resources/mynotpickedupconfig.properties new file mode 100644 index 0000000..d377800 --- /dev/null +++ b/deltaspike/core/impl/src/test/resources/mynotpickedupconfig.properties @@ -0,0 +1,22 @@ +#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. + +# a user defined property file which is not being activated at all + +deltaspike_ordinal = 130 + +some.boottimekey = this value should not get picked up at all!
