jueves, 17 de agosto de 2017

JEE & JSF 5th Part: Accessing property files EASIER

0. Introduction

To show formatted text Hilite is used

The goals of this post are:

  1. Locating the properties file
  2. Creating an ApplicationScoped bean to store these properties
  3. Accessing this bean by injection from another bean.


1. Locating the properties file.


We need to know where to locate the property files. Let's create an easy class to locate the property file. this class is Configuration.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
package org.ximodante.jsf.config;


import lombok.Getter;

/**
 * Locate of configuration files "application.properties"
 * @author Ximo Dante
 *
 */
public class Configuration {
 @Getter
 private static final String PropertyFile="config/application.properties";
 
}


This path is relative to src/main/resources folder


2. ApplicationProperty class

This class gets the path to file "application.properties" from last class, loads the properties included in this file and provides a getter method for retrieving the properties. But this class is application scoped, which means that it behaves as a singleton.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package org.ximodante.utils.property;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.Properties;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;

import org.ximodante.jsf.config.Configuration;

@Named
@ApplicationScoped
/**
 * Stores properties from file Application.properies whose path can be read in Configuraion.getPropertyfile() 
 * @author Ximo Dante
 *
 */
public class ApplicationProperties implements Serializable{
 private static final long serialVersionUID = 1L;

 private Properties properties;
 
 /**
  * Getter method of the properties by a key
  * @param key
  * @return
  */
 public String getProperty(String key){
  return properties.getProperty(key);
 }
 
 /**
  * The properties are load in the initilization part
  */
 @PostConstruct
    public void init() {
        this.properties = new Properties();
        
        String path = Thread.currentThread().getContextClassLoader().getResource("").getPath() + Configuration.getPropertyFile();
        System.out.println("PROPERTIES.PATH=" + path);
        
        try {
            //this.properties.load(stream);
         this.properties.load(new FileInputStream(path));
        } catch (final IOException e) {
            throw new RuntimeException("Application.properties could not be loaded!");
        }
    }
}


The file application.properties has this content:



1
2
3
4
5
webEnvironment=true
menuBeanReaderType=MenuBeanReaderJSONRelativePath
comment=This is a test commennt
kk=Other property
greet.first=Good Morning!

3. Retrieving properties from a bean


Let's create a bean called TestProperties that injects the last class and retrieves a property, for instance, "menuBeanReaderType" whose value is "MenuBeanReaderJSONRelativePath"



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package org.ximodante.utils.property;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

import lombok.Getter;

@Named
@ViewScoped
public class TestProperties implements Serializable{
 private static final long serialVersionUID = 1L;
 
 @Inject 
 ApplicationProperties appProps;
 
 @Getter
 private String myReaderType;
 
 @PostConstruct
    public void init() {
        
     myReaderType=this.appProps.getProperty("menuBeanReaderType");
     System.out.println("Accessed property is:" + myReaderType);
 }
}

And let's modify out later Facelets file tutorial01-menu.xhtml to view the attribute myReaderType as follows


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<html xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:h="http://java.sun.com/jsf/html" 
      xmlns:f="http://java.sun.com/jsf/core" 
      xmlns:p="http://primefaces.org/ui">  
     
  <h:head>  
  </h:head>  
      
  <h:body>  
    
      
      <h:form>
        <p:layout fullPage="true">
 
        
          <p:layoutUnit position="west" size="200" header="Left" resizable="true" closable="true" collapsible="true" effect="drop">
          
          
          
            <p:growl id="messages" showDetail="false"/>
              
            <p:panelMenu id="menu01" model="#{menuBean.menuModel}"/>
 
        
          </p:layoutUnit>
        
          <p:layoutUnit position="center">
            Content panel PROPERTY GOT:--> #{testProperties.myReaderType}    
            <p:commandButton value="Update Menu02" id="menuupd02" actionListener="#{menuBean.readMenu(true,'config/menu02.json')}" icon="ui-icon-disk" update="menu01"/>       
            <p:commandButton value="Update Menu"   id="menuupd"   actionListener="#{menuBean.readMenu(true,'config/menu.json')}"   icon="ui-icon-disk" update="menu01"/>       
            
          </p:layoutUnit>
 
        </p:layout>
     </h:form>
      
   
  </h:body>  
</html>Serializable{
 private static final long serialVersionUID = 1L;
 
 @Inject 
 ApplicationProperties appProps;
 
 @Getter
 private String myReaderType;
 
 @PostConstruct
    public void init() {
        
     myReaderType=this.appProps.getProperty("menuBeanReaderType");
     System.out.println("Accessed property is:" + myReaderType);
 }
}


4. Running ...

Right click on the file "tutorial01-menu.xhtml" and select Run As- Run On Server

and the result is:





No hay comentarios:

Publicar un comentario

JEE & JSF16th Part: Creating an abstraction view layer to JSF components and Forms (5/5). Frequent problems

1. ERROR #1: Using a bean that does not exists In the previos entry we used this facelet file: 1 2 3 4 5 6 7 8 9 10 11 1...