-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add PropertyEnvironment to log4j-kit
#2307
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
Merged
Merged
Changes from 2 commits
Commits
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 hidden or 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
35 changes: 35 additions & 0 deletions
35
...j-kit/src/main/java/org/apache/logging/log4j/kit/env/ConfigurablePropertyEnvironment.java
This file contains hidden or 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,35 @@ | ||
| /* | ||
| * 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.logging.log4j.kit.env; | ||
|
|
||
| /** | ||
| * Provides methods to modify the set of {@link PropertySource}s used by a {@link PropertyEnvironment}. | ||
| */ | ||
| public interface ConfigurablePropertyEnvironment extends PropertyEnvironment { | ||
|
|
||
| /** | ||
| * Adds a property source to the environment. | ||
| * @param source A property source. | ||
| */ | ||
| void addPropertySource(PropertySource source); | ||
|
|
||
| /** | ||
| * Removes a property source from the environment. | ||
| * @param source A property source. | ||
| */ | ||
| void removePropertySource(PropertySource source); | ||
| } |
51 changes: 51 additions & 0 deletions
51
log4j-kit/src/main/java/org/apache/logging/log4j/kit/env/Log4jProperty.java
This file contains hidden or 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,51 @@ | ||
| /* | ||
| * 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.logging.log4j.kit.env; | ||
|
|
||
| import java.lang.annotation.Documented; | ||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /** | ||
| * Annotates a class or parameter that stores Log4j API configuration properties. | ||
| * <p> | ||
| * This annotation is required for root property classes. | ||
| * </p> | ||
| */ | ||
| @Documented | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Target({ElementType.TYPE, ElementType.PARAMETER, ElementType.FIELD}) | ||
| public @interface Log4jProperty { | ||
|
|
||
| /** | ||
| * Provides a name for the configuration property. | ||
| * <p> | ||
| * | ||
| * </p> | ||
| */ | ||
| String name() default ""; | ||
|
|
||
| /** | ||
| * Provides the default value of the property. | ||
| * <p> | ||
| * This only applies to scalar values. | ||
| * </p> | ||
| */ | ||
| String defaultValue() default ""; | ||
| } | ||
187 changes: 187 additions & 0 deletions
187
log4j-kit/src/main/java/org/apache/logging/log4j/kit/env/PropertyEnvironment.java
This file contains hidden or 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,187 @@ | ||
| /* | ||
| * 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.logging.log4j.kit.env; | ||
|
|
||
| import java.nio.charset.Charset; | ||
| import java.time.Duration; | ||
| import org.apache.logging.log4j.kit.env.internal.PropertiesUtilPropertyEnvironment; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| /** | ||
| * Represents the main access point to Log4j properties. | ||
| * <p> | ||
| * It provides as typesafe way to access properties stored in multiple {@link PropertySource}s, type conversion | ||
| * methods and property aggregation methods (cf. {@link #getProperty(Class)}). | ||
| * </p> | ||
| */ | ||
| public interface PropertyEnvironment { | ||
|
|
||
| static PropertyEnvironment getGlobal() { | ||
| return PropertiesUtilPropertyEnvironment.INSTANCE; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the named property as a boolean value. If the property matches the string {@code "true"} (case-insensitive), | ||
| * then it is returned as the boolean value {@code true}. Any other non-{@code null} text in the property is | ||
| * considered {@code false}. | ||
| * | ||
| * @param name the name of the property to look up | ||
| * @return the boolean value of the property or {@code false} if undefined. | ||
| */ | ||
| default boolean getBooleanProperty(final String name) { | ||
| return getBooleanProperty(name, false); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the named property as a boolean value. | ||
| * | ||
| * @param name the name of the property to look up | ||
| * @param defaultValue the default value to use if the property is undefined | ||
| * @return the boolean value of the property or {@code defaultValue} if undefined. | ||
| */ | ||
| Boolean getBooleanProperty(String name, Boolean defaultValue); | ||
|
|
||
| /** | ||
| * Gets the named property as a Charset value. | ||
| * | ||
| * @param name the name of the property to look up | ||
| * @return the Charset value of the property or {@link Charset#defaultCharset()} if undefined. | ||
| */ | ||
| @SuppressWarnings("null") | ||
| default Charset getCharsetProperty(final String name) { | ||
| return getCharsetProperty(name, Charset.defaultCharset()); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the named property as a Charset value. | ||
| * | ||
| * @param name the name of the property to look up | ||
| * @param defaultValue the default value to use if the property is undefined | ||
| * @return the Charset value of the property or {@code defaultValue} if undefined. | ||
| */ | ||
| Charset getCharsetProperty(String name, Charset defaultValue); | ||
|
|
||
| /** | ||
| * Gets the named property as a Class value. | ||
| * | ||
| * @param name the name of the property to look up | ||
| * @param upperBound the upper bound for the class | ||
| * @return the Class value of the property or {@code null} if it can not be loaded. | ||
| */ | ||
| <T> @Nullable Class<? extends T> getClassProperty(final String name, final Class<T> upperBound); | ||
|
|
||
| /** | ||
| * Gets the named property as a subclass of {@code upperBound}. | ||
| * | ||
| * @param name the name of the property to look up | ||
| * @param defaultValue the default value to use if the property is undefined | ||
| * @param upperBound the upper bound for the class | ||
| * @return the Class value of the property or {@code defaultValue} if it can not be loaded. | ||
| */ | ||
| <T> Class<? extends T> getClassProperty(String name, Class<? extends T> defaultValue, Class<T> upperBound); | ||
|
|
||
| /** | ||
| * Gets the named property as {@link Duration}. | ||
| * | ||
| * @param name The property name. | ||
| * @return The value of the String as a Duration or {@link Duration#ZERO} if it was undefined or could not be parsed. | ||
| */ | ||
| default Duration getDurationProperty(final String name) { | ||
| return getDurationProperty(name, Duration.ZERO); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the named property as {@link Duration}. | ||
| * | ||
| * @param name The property name. | ||
| * @param defaultValue The default value. | ||
| * @return The value of the String as a Duration or {@code defaultValue} if it was undefined or could not be parsed. | ||
| */ | ||
| Duration getDurationProperty(String name, Duration defaultValue); | ||
|
|
||
| /** | ||
| * Gets the named property as an integer. | ||
| * | ||
| * @param name the name of the property to look up | ||
| * @return the parsed integer value of the property or {@code 0} if it was undefined or could not be | ||
| * parsed. | ||
| */ | ||
| default int getIntegerProperty(final String name) { | ||
| return getIntegerProperty(name, 0); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the named property as an integer. | ||
| * | ||
| * @param name the name of the property to look up | ||
| * @param defaultValue the default value to use if the property is undefined | ||
| * @return the parsed integer value of the property or {@code defaultValue} if it was undefined or could not be | ||
| * parsed. | ||
| */ | ||
| Integer getIntegerProperty(String name, Integer defaultValue); | ||
|
|
||
| /** | ||
| * Gets the named property as a long. | ||
| * | ||
| * @param name the name of the property to look up | ||
| * @return the parsed long value of the property or {@code 0} if it was undefined or could not be | ||
| * parsed. | ||
| */ | ||
| default long getLongProperty(final String name) { | ||
| return getLongProperty(name, 0L); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the named property as a long. | ||
| * | ||
| * @param name the name of the property to look up | ||
| * @param defaultValue the default value to use if the property is undefined | ||
| * @return the parsed long value of the property or {@code defaultValue} if it was undefined or could not be parsed. | ||
| */ | ||
| Long getLongProperty(String name, Long defaultValue); | ||
|
|
||
| /** | ||
| * Gets the named property as a String. | ||
| * | ||
| * @param name the name of the property to look up | ||
| * @return the String value of the property or {@code null} if undefined. | ||
| */ | ||
| @Nullable | ||
| String getStringProperty(String name); | ||
|
|
||
| /** | ||
| * Gets the named property as a String. | ||
| * | ||
| * @param name the name of the property to look up | ||
| * @param defaultValue the default value to use if the property is undefined | ||
| * @return the String value of the property or {@code defaultValue} if undefined. | ||
| */ | ||
| default String getStringProperty(final String name, final String defaultValue) { | ||
| final String prop = getStringProperty(name); | ||
| return (prop == null) ? defaultValue : prop; | ||
| } | ||
|
|
||
| /** | ||
| * Binds properties to class {@code T}. | ||
| * <p> | ||
| * The implementation should at least support binding Java records with a single public constructor and enums. | ||
| * </p> | ||
| * @param propertyClass a class annotated by {@link Log4jProperty}. | ||
| * @return an instance of T with all JavaBean properties bound. | ||
| */ | ||
| <T> T getProperty(final Class<T> propertyClass); | ||
| } |
48 changes: 48 additions & 0 deletions
48
log4j-kit/src/main/java/org/apache/logging/log4j/kit/env/PropertySource.java
This file contains hidden or 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,48 @@ | ||
| /* | ||
| * 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.logging.log4j.kit.env; | ||
|
|
||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| /** | ||
| * Basic interface to retrieve property values. | ||
| * <p> | ||
| * We can not reuse the property sources from 2.x, since those required some sort of {@code log4j} prefix to be | ||
| * included. In 3.x we want to use keys without a prefix. | ||
| * </p> | ||
| */ | ||
| public interface PropertySource { | ||
| /** | ||
| * Provides the priority of the property source. | ||
| * <p> | ||
| * Property sources are ordered according to the natural ordering of their priority. Sources with lower | ||
| * numerical value take precedence over those with higher numerical value. | ||
| * </p> | ||
| * | ||
| * @return priority value | ||
| */ | ||
| int getPriority(); | ||
ppkarwasz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Gets the named property as a String. | ||
| * | ||
| * @param name the name of the property to look up | ||
| * @return the String value of the property or {@code null} if undefined. | ||
| */ | ||
| @Nullable | ||
| String getProperty(String name); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.