I would like to bring dependency injection to my persistent entities, but I am not sure how it can be done.
A salted hash algorithm in my GWT application required a Base64 implementation. GWT ships with an old version of commons-codec. Due to the name conflict—I do not use Maven—I could either figure out how to use the old one or use another implementation, such as Base64.iharder.net.
After adapting several alternatives, I created an interface and adapter classes for each. I injected one implementation. It seemed like a classic use case.
Everything worked well when the persistent entities were created. After storing and retrieving them, however, the fields that were previously injected and not persisted were instantiated with null values.
The issue makes perfect sense. I use DataNucleus, which adds a no-arg constructor. DataNucleus does not inject the dependencies again.
How can I ask my persistence framework to re-inject the dependencies when retrieving the object from the data store?
Thank you.
// salted hash for password storage
@PersistenceCapable
public class SaltedHash implements Serializable {
private static final long serialVersionUID = 1L;
private String salt;
private String hash;
@NotPersistent
private final Base64Codec base64Codec;
@NotPersistent
private final Sha265Hash sha256Hash;
@NotPersistent
private final Random random;
@Inject
public SaltedHash(Base64Codec b64, Sha256Hash sha256, Random rnd) {
base64Codec = b64;
sha256Hash = sha256;
random = rnd;
}
public void setSecret(String secret) {
salt = base64Codec.encode(generateSalt());
hash = base64Codec.encode(sha256Hash.hash(salt + secret));
}
public boolean matches(String secret) {
String maybe = base64Codec.encode(sha256Hash.hash(salt + secret));
return hash.equals(maybe);
}
private byte[] generateSalt() {
// use random to generate a salt
}
}