0

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>

enter image description here

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
vasigorc
  • 882
  • 11
  • 22
  • Injection takes place **after** the constructor has been called. Is that your problem? Then use an @PostConstruct annotated method. If it is not, I find it kind of hard to understand what your real issue is. – Kukeltje Aug 01 '16 at 11:43
  • Thank you! It sounds like it, but how do I use it? I cannot do new CompanyPropertiesImpl() in the init method (annotated with PostConstruct). After all the EJB is created at the application startup. Can I forcefully retrieve it from somewhere? You can put your comment in the 'answer'. You got the idea. – vasigorc Aug 02 '16 at 02:08
  • No need to do a 'new', just try. The ejb will be there – Kukeltje Aug 02 '16 at 03:39
  • I am sorry if I lack knowledge, but if I don't do 'new' how will I retrieve the EJB? This is the CDI bean: https://github.com/vasigorc/rimmanew/blob/master/src/main/java/com/vgorcinschi/rimmanew/model/UriSetterBean.java I can't create an empty @PostConstruct public void init(){} right? How can I 'activate' the EJB? – vasigorc Aug 02 '16 at 04:06
  • Well, if you don't need to do any work in the method, there is no need for it. The ejb will be automagically there But see what happens if you just put the if/the/else with the sysouts in it. – Kukeltje Aug 02 '16 at 05:47

1 Answers1

0

Resolved by Kukeltje

by adding the @PostConstruct annotated method and retrieving the required values from the EJB:

@Named(value = "companyPropertiesBean")
@SessionScoped
public class UriSetterBean implements Serializable {

    private String hostName, schemeName, serviceSuffix, javascriptLink;
    private int port;
    ...

    @Inject
    @Production
    private CompanyProperties companyProperties;

    //THE ADDED METHOD
    @PostConstruct
    public void init(){
        setHostName(companyProperties.getHostName());
        setSchemeName(companyProperties.getSchemeName());
        setPort(companyProperties.getPort());
    }

    public UriSetterBean() {
        this.urisUpdated = false;
        this.updateTried = false;
        serviceSuffix = "/RimmaNew/rest";
    }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
vasigorc
  • 882
  • 11
  • 22