-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
221 additions
and
2 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
core/src/main/java/foundation/stack/datamill/configuration/BooleanValue.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,20 @@ | ||
package foundation.stack.datamill.configuration; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* Annotation to be used together with a {@link foundation.stack.datamill.configuration.impl.ConstantsClassSource} to | ||
* specify String values. For example, a constant declared in a constants class as: | ||
* <code> | ||
* \@BooleanValue(true) public static final String PROPERTY_NAME = "configuration/propertyName"; | ||
* </code> | ||
* This defines a property called "configuration/propertyName" that has the boolean value true. | ||
* | ||
* @author Ravi Chodavarapu (rchodava@gmail.com) | ||
*/ | ||
@Documented | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.FIELD}) | ||
public @interface BooleanValue { | ||
boolean value(); | ||
} |
20 changes: 20 additions & 0 deletions
20
core/src/main/java/foundation/stack/datamill/configuration/IntegerValue.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,20 @@ | ||
package foundation.stack.datamill.configuration; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* Annotation to be used together with a {@link foundation.stack.datamill.configuration.impl.ConstantsClassSource} to | ||
* specify String values. For example, a constant declared in a constants class as: | ||
* <code> | ||
* \@IntegerValue(16) public static final String PROPERTY_NAME = "configuration/propertyName"; | ||
* </code> | ||
* This defines a property called "configuration/propertyName" that has the integer value 16. | ||
* | ||
* @author Ravi Chodavarapu (rchodava@gmail.com) | ||
*/ | ||
@Documented | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.FIELD}) | ||
public @interface IntegerValue { | ||
int value(); | ||
} |
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
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
20 changes: 20 additions & 0 deletions
20
core/src/main/java/foundation/stack/datamill/configuration/StringValue.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,20 @@ | ||
package foundation.stack.datamill.configuration; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* Annotation to be used together with a {@link foundation.stack.datamill.configuration.impl.ConstantsClassSource} to | ||
* specify String values. For example, a constant declared in a constants class as: | ||
* <code> | ||
* \@StringValue("value") public static final String PROPERTY_NAME = "configuration/propertyName"; | ||
* </code> | ||
* This defines a property called "configuration/propertyName" that has the value "value". | ||
* | ||
* @author Ravi Chodavarapu (rchodava@gmail.com) | ||
*/ | ||
@Documented | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.FIELD}) | ||
public @interface StringValue { | ||
String value(); | ||
} |
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
85 changes: 85 additions & 0 deletions
85
core/src/main/java/foundation/stack/datamill/configuration/impl/ConstantsClassSource.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,85 @@ | ||
package foundation.stack.datamill.configuration.impl; | ||
|
||
import foundation.stack.datamill.configuration.BooleanValue; | ||
import foundation.stack.datamill.configuration.IntegerValue; | ||
import foundation.stack.datamill.configuration.StringValue; | ||
import javassist.Modifier; | ||
|
||
import java.lang.reflect.Field; | ||
import java.security.AccessController; | ||
import java.security.PrivilegedAction; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
/** | ||
* @author Ravi Chodavarapu (rchodava@gmail.com) | ||
*/ | ||
public class ConstantsClassSource<T> extends AbstractSource { | ||
private static String getConstantValue(Field field) { | ||
if (field != null && Modifier.isStatic(field.getModifiers())) { | ||
try { | ||
performSecure(() -> field.setAccessible(true)); | ||
return String.valueOf(field.get(null)); | ||
} catch (IllegalAccessException e) { | ||
return null; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static void performSecure(Runnable runnable) { | ||
if (System.getSecurityManager() != null) { | ||
AccessController.doPrivileged((PrivilegedAction<?>) () -> { | ||
runnable.run(); | ||
return null; | ||
}); | ||
} else { | ||
runnable.run(); | ||
} | ||
} | ||
|
||
private final Class<T> constantsClass; | ||
private Map<String, String> annotatedConstants; | ||
|
||
public ConstantsClassSource(Class<T> constantsClass) { | ||
this.constantsClass = constantsClass; | ||
} | ||
|
||
private Map<String, String> buildAnnotatedConstants() { | ||
HashMap<String, String> constants = new HashMap<>(); | ||
|
||
Field[] fields = constantsClass.getDeclaredFields(); | ||
for (Field field : fields) { | ||
String propertyName = getConstantValue(field); | ||
if (propertyName != null) { | ||
StringValue stringValue = field.getAnnotation(StringValue.class); | ||
if (stringValue != null) { | ||
constants.put(propertyName, stringValue.value()); | ||
} else { | ||
BooleanValue booleanValue = field.getAnnotation(BooleanValue.class); | ||
if (booleanValue != null) { | ||
constants.put(propertyName, String.valueOf(booleanValue.value())); | ||
} else { | ||
IntegerValue integerValue = field.getAnnotation(IntegerValue.class); | ||
if (integerValue != null) { | ||
constants.put(propertyName, String.valueOf(integerValue.value())); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return constants; | ||
} | ||
|
||
@Override | ||
protected Optional<String> getOptional(String name) { | ||
if (annotatedConstants == null) { | ||
annotatedConstants = buildAnnotatedConstants(); | ||
} | ||
|
||
return Optional.ofNullable(annotatedConstants.get(name)); | ||
} | ||
} |
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