Tagged: di

Injecting Property Values into EJBs from Database


references

Property.java

@Entity
@NamedQueries({
@NamedQuery(name = "Property.find",
            query = "SELECT p FROM Property AS p WHERE p.name = :name")
})
public class Property {

    @Basic(optional = false)
    @Column(name = "NAME", nullable = false, unique = true)
    @NotNull
    private String name;

    @Basic(Optional = false)
    @Column(name = "VALUE_", nullable = false)
    @NotNull
    private String value = "";
}

find

public static Property findNamed(final EntityManager entityManager,
                                 final String name) {
    final TypedQuery typedQuery
        = entityManager.createNamedQuery("Property.find", Property.class);
    typedQuery.setParameter("name", name);
    try {
        return typedQuery.getSingleResult();
    } catch (final NoResultException nre) {
        return null;
    }
}

public static Property findCriteria(final EntityManager entityManager,
                                    final String name) {
    final CriteriaBuilder criteriaBuilder
        = entityManager.getCriteriaBuilder();
    final CriteriaQuery criteriaQuery
        = criteriaBuilder.createQuery(Property.class);
    final Root<Property> property = criteriaQuery.from(Property.class);
    criteriaQuery.select(property);
    criteriaQuery.where(
        criteriaBuilder.equal(property.get(Property_.name), name));
    final TypedQuery typedQuery
        = entityManager.createQuery(criteriaQuery);
    try {
        return typedQuery.getSingleResult();
    } catch (final NoResultException nre) {
        return null;
    }
}

public static Property find(final EntityManager entityManager,
                            final String name) {
    if (current().nextBoolean()) {
        return findNamed(entityManager, name);
    }
    return findCriteria(entityManager, name);
}

PropertyValue.java

@Qualifier
@Retention(RUNTIME)
@Target({FIELD, METHOD, PARAMETER, TYPE})
public @interface PropertyValue {

    @Nonbinding
    String name() default "";

    @Nonbinding
    boolean optional() default true;
}

PropertyFactory.java

@Dependent
public class PropertyValueFactory {

@Produces
@PropertyValue
public String producePropertyValue(final InjectionPoint injectionPoint) {
    final PropertyValue annotation
        = injectionPoint.getAnnotated().getAnnotation(PropertyValue.class);
    final String name = annotation.name();
    final boolean optional = annotation.optional();
    final Property property = Property.find(entityManger, name);
    if (property == null && !optional) {
        throw new InjectionException("property not found; name= " + name);
    }
    return ofNullable(property).map(Property::getValue).orElse(null);
}

public void diposePropertyValue(
    @Disposes @PropertyValue final String value) {
    // empty


@PersistenceContext
private EntityManager entityManger;
}

StorageObjectServiceFtp.java

 @Stateless
 public class StorageObjectServiceFtp extends StorageObjectService {

    @Inject
    @PropertyValue(name = "ftp.hostname", optional = false)
    private String hostname;

    @Inject
    @PropertyValue(name = "ftp.username", optional = false)
    private String username;

    @Inject
    @PropertyValue(name = "ftp.password", optional = false)
    private String password;

    @Inject
    @PropertyValue(name = "ftp.rootpath", optional = true)
    private String rootpath;
}
Advertisements