1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.example.demo;
21
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import javax.enterprise.context.ApplicationScoped;
26 import javax.enterprise.context.NormalScope;
27 import javax.faces.context.FacesContext;
28 import javax.inject.Named;
29 import javax.servlet.ServletContext;
30 import java.io.FileInputStream;
31 import java.io.IOException;
32 import java.lang.invoke.MethodHandles;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Properties;
37
38
39
40
41
42
43
44
45 @ApplicationScoped
46 @Named
47 public class ServerInfo {
48
49 private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
50
51 private static final String CONFIG_FILE = "org.apache.myfaces.tobago.example.demo.config.file";
52 private static final String CONFIG_FILE_DEFAULT = "/etc/tobago-example-demo.properties";
53 private static final String ENABLED_KEY = "server.info.enabled";
54
55 private String version;
56
57
58
59
60 private boolean enabled = true;
61
62 public ServerInfo() {
63 String file = System.getProperty(CONFIG_FILE);
64 try {
65 if (file == null) {
66 file = CONFIG_FILE_DEFAULT;
67 }
68 LOG.info("Loading config from file '" + file + "'");
69 final Properties config = new Properties();
70 config.load(new FileInputStream(file));
71 enabled = Boolean.parseBoolean(config.getProperty(ENABLED_KEY));
72 } catch (final IOException e) {
73 LOG.warn("Can't load config: " + e.getMessage());
74 }
75
76 LOG.info("server.info.enabled=" + enabled);
77 version = Package.getPackage("org.apache.myfaces.tobago.component").getImplementationVersion();
78 }
79
80 public String getServerInfo() {
81 if (enabled) {
82 return ((ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext()).getServerInfo();
83 } else {
84 return null;
85 }
86 }
87
88 public Properties getSystemProperties() {
89 return enabled ? System.getProperties() : null;
90 }
91
92 public List<Map.Entry<Object, Object>> getSystemPropertiesAsList() {
93 return enabled ? new ArrayList<>(getSystemProperties().entrySet()) : null;
94 }
95
96 public String getVersion() {
97 return version;
98 }
99
100 public String getJsfTitle() {
101 return enabled ? FacesContext.class.getPackage().getImplementationTitle() : null;
102 }
103
104 public String getJsfVersion() {
105 return enabled ? FacesContext.class.getPackage().getImplementationVersion() : null;
106 }
107
108 public String getCdiTitle() {
109 return enabled ? NormalScope.class.getPackage().getImplementationTitle() : null;
110 }
111
112 public String getCdiVersion() {
113 return enabled ? NormalScope.class.getPackage().getImplementationVersion() : null;
114 }
115
116 public boolean isEnabled() {
117 return enabled;
118 }
119 }