Tagged: java

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;
}

format/parse ISO 8601 ignoring the fraction


    static Instant parseIsoInstant(final String formatted) {
        return Instant.from(DateTimeFormatter.ISO_INSTANT.parse(formatted))
                .with(ChronoField.NANO_OF_SECOND, 0L);
    }

    static String formatIsoInstant(final TemporalAccessor parsed) {
        return DateTimeFormatter.ISO_INSTANT.format(
                Instant.from(parsed).with(ChronoField.NANO_OF_SECOND, 0L));
    }

    static Date parseIsoDate(final String formatted) {
        return Date.from(parseIsoInstant(formatted));
    }

    static String formatIsoDate(final Date parsed) {
        return formatIsoInstant(parsed.toInstant());
    }

how to set final variable in catch block?


references

as final as possible, but not final-er.

꽤나 오랫동안 궁금했었다.

final SecureRandom random;
try {
    random = SecureRandom.getInstanceStrong();
} catch (final NoSuchAlgorithmException nsae) {
    random = new SecureRandom(); // won't compile
    // variable random might already have been assigned
}

let it go, let it go~~~

그냥 이렇게 하면 되지 뭐…

SecureRandom random;
try {
    random = SecureRandom.getInstanceStrong();
} catch (final NoSuchAlgorithmException nsae) {
    random = new SecureRandom();
}

// ----------------------------------
// 권팀장 죽어버려!!! -퇴사자-
random = null;
// ----------------------------------

final int i = random.nextInt(128); // NPE

we will find a way. we always have.

지저분해 보일지도 모르겠다.

final SecureRandom random;
{
    Random random_;
    try {
        random_ = SecureRandom.getInstanceStrong();
    } catch (final NoSuchAlgorithmException nsae) {
        random_ = new SecureRandom();
    }
    random = random_;
}

메서드로 따로 빼 놓고 써도 되고, 뭐…

static SecureRandom secureRandomPossiblyStrong() {
    try {
        return SecureRandom.getInstanceStrong();
    } catch (final NoSuchAlgorithmException nsae) {
        return new SecureRandom();
    }
}

Supplier 를 사용해도 될 듯…

final static SecureRandom RANDOM;
static {
    RANDOM = ((Supplier<SecureRandom>)() ->{
        try {
            return SecureRandom.getInstanceStrong();
        } catch (final NoSuchAlgorithmException nsae) {
            return new SecureRandom();
        } 
    }).get();
}

 

exclusive or with java


references

^

XOR 을 사용할 일은 많이 없을 듯 하지만, 의외로 사용할 일이 있을 때 모르고 지나치는 일이 있다. 아래 코드는 condition1condition2중 하나만 true 일 때 특정 작업을 수행하는 코드이다.

void some() {
    if (condition1) {
        if (!condition2) {
            // do some!                     // T F
            return;
        }
    } else { // !condition1
        if (condition2) {
            // do some!                     // F T
            return;
        }
    }
    assert (condition1 && condition2)       // T T
           || (!condition1 && !condition2); // F F
}

Exclusive OR 를 다음과 같이 사용할 수 있다.

void some() {
    if (condition1 ^ condition2) {            // T F | F T
        // do some!
    }
}