Search in sources :

Example 1 with Generate

use of org.platformlayer.core.model.Generate in project platformlayer by platformlayer.

the class ServiceProviderBase method autoPopulate.

public void autoPopulate(Object item) throws OpsException {
    Class<? extends Object> itemClass = item.getClass();
    for (Field field : itemClass.getFields()) {
        Generate defaultAnnotation = field.getAnnotation(Generate.class);
        if (defaultAnnotation != null) {
            Class<?> fieldType = field.getType();
            Object value;
            try {
                value = field.get(item);
            } catch (IllegalAccessException e) {
                throw new IllegalStateException("Error getting field: " + field, e);
            }
            if (value == null) {
                String defaultValue = defaultAnnotation.value();
                if (!Strings.isNullOrEmpty(defaultValue)) {
                    value = defaultValue;
                } else {
                    if (fieldType == Secret.class) {
                        Passwords passwords = new Passwords();
                        Secret secret = passwords.generateRandomPassword(12);
                        value = secret;
                    }
                }
                if (value != null) {
                    try {
                        field.set(item, value);
                    } catch (IllegalAccessException e) {
                        throw new IllegalStateException("Error setting field: " + field, e);
                    }
                }
            }
        }
    }
}
Also used : Secret(org.platformlayer.core.model.Secret) Field(java.lang.reflect.Field) Generate(org.platformlayer.core.model.Generate) Passwords(org.platformlayer.ops.crypto.Passwords)

Aggregations

Field (java.lang.reflect.Field)1 Generate (org.platformlayer.core.model.Generate)1 Secret (org.platformlayer.core.model.Secret)1 Passwords (org.platformlayer.ops.crypto.Passwords)1