Commit 68ad6d44 authored by R.W.Majeed's avatar R.W.Majeed

preferences simplified to read only properties

parent d9546276
...@@ -51,6 +51,12 @@ ...@@ -51,6 +51,12 @@
<version>0.8-SNAPSHOT</version> <version>0.8-SNAPSHOT</version>
</dependency> </dependency>
--> -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
<scope>provided</scope>
</dependency>
<!-- unit tests --> <!-- unit tests -->
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
......
package org.aktin;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;
/**
* Marker interface to allow automatic injection of configuration preferences
*
* @author R.W.Majeed
*
*/
public interface Configurable {
public default List<String> getPreferences(){
List<String> prefs = new LinkedList<>();
Method[] methods = getClass().getDeclaredMethods();
for( int i=0; i<methods.length; i++ ){
Preference pref = methods[i].getAnnotation(Preference.class);
if( pref != null ){
prefs.add(pref.id());
}
}
return prefs;
}
}
package org.aktin; package org.aktin;
public abstract class Module implements Configurable{ public abstract class Module {
public String getVersion(){ public String getVersion(){
return getClass().getPackage().getImplementationVersion(); return getClass().getPackage().getImplementationVersion();
......
...@@ -2,19 +2,25 @@ package org.aktin; ...@@ -2,19 +2,25 @@ package org.aktin;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
/** /**
* AKTIN preference. If the annotation is used, also implement the {@link Configurable} interface. * AKTIN preference. Can be used to inject preference values into any class.
* The preferences can not be changed during runtime.
* *
* TODO add annotation processor to verify at compile time that each preference id is unique and method types are void(SingleArg)
* *
* @author R.W.Majeed * @author R.W.Majeed
* *
*/ */
@Qualifier
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.CONSTRUCTOR })
public @interface Preference { public @interface Preference {
// no default meaning a value is mandatory
String id(); //@Nonbinding
String example() default ""; String name();
} }
package org.aktin; package org.aktin;
import java.lang.annotation.Retention; import java.util.Set;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME) public interface Preferences {
public @interface Preferences {
String group(); String get(String key);
// TODO add Class<?> validator() to validate preferences
Set<String> keySet();
} }
package org.aktin.prefs;
/**
* All preferences can be read/written by the API (system).
* Some preferences might be readable/writable publicly
* e.g. by an authenticated user.
*
* @author R.W.Majeed
*
* @param <T> value type
*/
public interface Preference<T> {
Preferences getPreferences();
/**
* Get the preference value data type
* @return data type for the value
*/
Class<T> getType();
/**
* Get the preference value
* @return value
*/
T getValue();
/**
* Get the preference name / key
* @return key name
*/
String getKey();
boolean isPublicWritable();
boolean isPublicReadable();
/**
* Perform a public update by user
* @param user user name
* @param value value
*/
public void setValue(T value, String user);
/**
* Set the value by the system user
* @param value value
*/
public void setValue(T value);
/**
* Set the value to the given string representation. The value
* is changed by the system user.
* @param value string representing the value
* @throws IllegalArgumentException if the string conversion fails
*/
public void setValueString(String value) throws IllegalArgumentException;
/**
* Set the value to the given string representation. The specified
* user name is used to perform the change.
*
* @param value string value
* @param user user name
* @throws IllegalArgumentException if string conversion fails
*/
public void setValueString(String value, String user) throws IllegalArgumentException;
}
package org.aktin.prefs;
import java.io.IOException;
/**
* API for AKTIN preferences.
* <p>
* To access preferences, use a CDI injection
* in your class (e.g with {@code javax.inject.Inject}
* annotation.
*
* @author R.W.Majeed
*
*/
public interface Preferences {
public String getString(String key);
public Integer getInteger(String key);
public void putString(String key, String value);
public void putInteger(String key, Integer value);
public Preference<?> get(String key);
/**
* Write the changed preferences to the underlying
* storage (e.g. database)
* @throws IOException flush error
*/
public void flush() throws IOException;
}
package org.aktin.prefs;
/**
* Validates preference settings.
* A validator may work for one or more preferences,
* e.g for R executable (one preference) or email server settings (multiple)
* @author R.W.Majeed
*
*/
public interface Validator {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment