Same goes for RevisionListener for the envers auditing:
public class RuleRevisionListener implements RevisionListener {
@Inject
AuditUserData auditUserData;
public void newRevision(Object revisionEntity) {
RuleRevisionEntity ruleRevisionEntity = (RuleRevisionEntity) revisionEntity;
ruleRevisionEntity.setUsername(auditUserData.getUsername());
ruleRevisionEntity.setApplication(auditUserData.getApplication());
}
}
@Entity
@RevisionEntity(RuleRevisionListener.class)
public class RuleRevisionEntity {
@Id
@GeneratedValue
@RevisionNumber
private int id;
@RevisionTimestamp
private long timestamp;
private String username;
private String application;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getApplication() {
return application;
}
public void setApplication(String application) {
this.application = application;
}
With this code AuditUserData is null. If I try to get it myself through the BeanManager I only can achieve it if I do the look into the newRevision method:
public class RuleRevisionListener implements RevisionListener {
public void newRevision(Object revisionEntity) {
AuditUserData auditUserData=getAuditUserData();
RuleRevisionEntity ruleRevisionEntity = (RuleRevisionEntity) revisionEntity;
ruleRevisionEntity.setUsername(auditUserData.getUsername());
ruleRevisionEntity.setApplication(auditUserData.getApplication());
}
private AuditUserData getAuditUserData() {
BeanManager beanManager = getBeanManager();
Bean<AuditUserData> bean = (Bean<AuditUserData>) beanManager.getBeans(AuditUserData.class).iterator().next();
CreationalContext<AuditUserData> ctx = beanManager.createCreationalContext(bean);
AuditUserData auditUserData = (AuditUserData) beanManager.getReference(bean, AuditUserData.class, ctx);
return auditUserData;
}
private BeanManager getBeanManager() {
try {
InitialContext initialContext = new InitialContext();
return (BeanManager) initialContext.lookup("java:comp/BeanManager");
} catch (NamingException e) {
throw new RuntimeException(e);
}
}
}
This code above is working while doing the lookup in a PostConstruct method doesn't work(BeanManager not found in jndi). |