kishore jetty
kishore jetty

Reputation: 5

What is happening in the following snippet?

I am not able to understand the following snipet. I mean what is happening exactly. Can any one explain me what is happening?

This is the snippet:

protected NodeService getUnprotectedNodeService()

   { 

      if (this.unprotectedNodeService == null)
          {
         this.unprotectedNodeService = (NodeService) FacesHelper.getManagedBean(FacesContext.getCurrentInstance(), "nodeService");
      }
      return this.unprotectedNodeService;
   }

Upvotes: 0

Views: 76

Answers (4)

amit
amit

Reputation: 178491

it sets this.unprotectedNodeService if it not set yet(and also returns it..), and if it is already set, it just returns it.
seems like a caching mechanism to prevent calling heavy methods more then once

Upvotes: 3

Bohemian
Bohemian

Reputation: 425238

This is the lazy initializion pattern.

The field unprotectedNodeService is initialized when it's first used (and not before).

Note that this code is not thread safe.

Upvotes: 0

NPE
NPE

Reputation: 500773

  • If this.unprotectedNodeService is null, a bean is looked up and stored in this.unprotectedNodeService.
  • this.unprotectedNodeService is returned from the function.

This is an example of lazy initialization.

Upvotes: 1

planetjones
planetjones

Reputation: 12633

If the unprotectedNodeService instance variable is not set, it's looking up a ManagedBean stored in scope under the "nodeService" key. If it doesn't exist it will get created. It's being casted to a NodeService Object and returned, as well as being set as the instance variable. This must be from a JSF application. Look in faces-config.xml or relevant @ManagedBean annotations to find more info.

Upvotes: 0

Related Questions