I have this @Startup EJB which reads and writes to a .properties file on the classpath different application configuration variables:
@Singleton
@Startup
@Production
public class CompanyPropertiesImpl implements CompanyProperties {
...
public CompanyPropertiesImpl() {
uriProperties = PropertiesProvider.getUriProperties();
...
}
It is then injected into a Session Scoped CDI Bean which interacts with a UI web page:
@Named(value = "companyPropertiesBean")
@SessionScoped
public class UriSetterBean implements Serializable {
...
@Inject
@Production
private CompanyProperties companyProperties;
public UriSetterBean() {
this.urisUpdated = false;
this.updateTried = false;
serviceSuffix = "/RimmaNew/rest";
if (companyProperties==null) {
System.out.println("true");
}else{
System.out.println("false");
}
}
public void setCompanyProperties(CompanyProperties companyProperties) {
this.companyProperties = companyProperties;
}
...
public void updateUriProperties() {
if (hostName == null || hostName.equals("") || schemeName == null || schemeName
.equals("")) {
updateTried = true;
urisUpdated = false;
} else {
companyProperties.setHostName(hostName);
companyProperties.setSchemeName(schemeName);
if (valueOf(port) == null || port != 0) {
companyProperties.setPort(port);
} else //i.e. port is not part of the uri
{
companyProperties.setPort(-1);
}
updateTried = true;
urisUpdated = true;
}
...
public String getJavascriptLink() {
updateJavascriptLink();
return javascriptLink;
}
I have pasted the if statement into the UriSetterBean constructor to confirm my suspicions - the EJB is null on the CDI construction. However if I navigate to the mentioned above UI web page and invoke the updateUriProperties method the EJB is no longer null:
<h:commandButton action="#{companyPropertiesBean.updateUriProperties()}" class="btn btn-default" id="submitUriChanges"
style="margin-top: 0.5em" value="#{bigcopy.submitUriChanges}"
type="button">
<f:ajax event="click" render="updateUriStatus" execute="@form"></f:ajax>
</h:commandButton>
This however is not an acceptable solution for me. This is why.
UriSetterBean is only supposed to be updated by a User in role SUPERUSER. But I use the UriSetterBean's getJavascriptLink() property accessor in another page used by a User in role ADMIN. The getJavascriptLink() outputs the base url for rest services.
How can I "force" initialize the EJB CompanyPropertiesImpl on the UriSetterBean's initialization? Thank you for your consideration and attention. I realize it may not be trivial.