diff --git a/pom.xml b/pom.xml index 513bf4c..74262f4 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ ru.simplex_software wicket-springdata - 1.4 + 1.5 wicket-springdata http://www.simplex-software.ru @@ -29,6 +29,13 @@ + + + org.apache.commons + commons-lang3 + 3.8 + + com.esotericsoftware diff --git a/src/main/java/ru/simplex_software/wicket_springdata/property/Property.java b/src/main/java/ru/simplex_software/wicket_springdata/property/Property.java new file mode 100644 index 0000000..fbd45d8 --- /dev/null +++ b/src/main/java/ru/simplex_software/wicket_springdata/property/Property.java @@ -0,0 +1,15 @@ +package ru.simplex_software.wicket_springdata.property; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Аннотация вля внедрения свойств в поля. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface Property { + String value() default ""; +} \ No newline at end of file diff --git a/src/main/java/ru/simplex_software/wicket_springdata/property/WicketPropertyInjector.java b/src/main/java/ru/simplex_software/wicket_springdata/property/WicketPropertyInjector.java new file mode 100644 index 0000000..22a3e7e --- /dev/null +++ b/src/main/java/ru/simplex_software/wicket_springdata/property/WicketPropertyInjector.java @@ -0,0 +1,77 @@ +package ru.simplex_software.wicket_springdata.property; + +import org.apache.commons.lang3.reflect.FieldUtils; +import org.apache.wicket.Component; +import org.apache.wicket.application.IComponentInstantiationListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.util.StringUtils; + +import java.lang.reflect.Field; +import java.util.List; +import java.util.function.Function; + +/** + * Внедряет значения из property файлов в поля, помеченные аннотацией {@link Property}. + */ +public class WicketPropertyInjector implements IComponentInstantiationListener { + private static final Logger LOG = LoggerFactory.getLogger(WicketPropertyInjector.class); + + /** + * Находит значение свойства по названию. + */ + private Function propertyResolver; + + public WicketPropertyInjector(Function propertyResolver) { + this.propertyResolver = propertyResolver; + } + + @Override + public void onInstantiation(Component component) { + injectProperties(component); + } + + /** + * Внедрение свойств в поля компонента. + * + * @param component компонент. + */ + private void injectProperties(Component component) { + List fields = FieldUtils.getFieldsListWithAnnotation(component.getClass(), Property.class); + for (Field field : fields) { + // Проверка типа поля. + if (!field.getType().equals(String.class)) { + throw new IllegalArgumentException("Annotated field " + field.getName() + " must have type java.lang.String"); + } + // Получение названия и проверка наличия свойства. + String aValue = getPropertyName(field); + String pValue = propertyResolver.apply(aValue); + if (pValue == null) { + String message = String.format("Cannot find property %s for %s", aValue, field.getName()); + throw new IllegalArgumentException(message); + } + // Установка значения. + try { + field.setAccessible(true); + field.set(component, pValue); + field.setAccessible(false); + } catch (IllegalAccessException e) { + LOG.warn("Cannot set property"); + } + } + } + + /** + * Получение названия свойства. + * + * @param field поле. + * @return название свойства. + */ + private String getPropertyName(Field field) { + String name = field.getAnnotation(Property.class).value(); + if (StringUtils.isEmpty(name)) { + name = field.getName(); + } + return name; + } +}