0. Introduction
To retrieve information stored in resources files, if the amount of memory to be used is not very high, it is interesting considering a Singleton component.
In a CDI scenario it can be achieved by means of @ApplicationScoped annotation.
1. ComponentParams class
This class populates all components and attribute types. The JsonUtils class is shown in an elder entry.
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | package org.ximodante.jsf.component; import java.io.IOException; import java.io.Serializable; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import javax.annotation.PostConstruct; import javax.enterprise.context.ApplicationScoped; import javax.inject.Named; import org.ximodante.utils.json.JsonUtils; import com.fasterxml.jackson.core.type.TypeReference; import lombok.Getter; @Named @ApplicationScoped /** * Loads the components and attribute catalog from resources/config folder * It is a Singleton object and stores a map of components and resources. * * @author Ximo Dante * */ public class ComponentParams implements Serializable{ private static final long serialVersionUID = 1L; @Getter private Map<String,ComponentAttribute> allAttributes; @Getter private Map<String,ComponentType> allComponents; @PostConstruct public void init() { System.out.println("Initializing ComponentParams..."); try { // 1. populate allAttributes List<ComponentAttribute> myListA= JsonUtils.readObject(true, "config/component-attributes.json", new TypeReference<List<ComponentAttribute>>() { } ); allAttributes = myListA.stream() .collect(Collectors .toMap(x -> x.getName(), x -> x)); // 2. Populate allComponents List<ComponentType> myListT= JsonUtils.readObject(true, "config/component-types.json", new TypeReference<List<ComponentType>>() { } ); allComponents = myListT.stream() .collect(Collectors .toMap(x -> x.getName(), x -> x)); } catch (IOException e) { e.printStackTrace(); } } /* public static void main(String[] args) { ComponentParams cp= new ComponentParams(); System.out.println("AllAttributes------"); for (String s:cp.getAllAttributes().keySet()) { System.out.println(s + "=" + cp.getAllAttributes().get(s).toString()); } System.out.println("AllComponents------"); for (String s:cp.getAllComponents().keySet()) { System.out.println(s + "=" + cp.getAllComponents().get(s).toString()); } } */ } |
No hay comentarios:
Publicar un comentario