View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * The server info class makes some information about the system and application available in the demo.
40   * This is enabled by default, but can be disabled by configuration in a properties file with the key
41   * <code>{@value #ENABLED_KEY}</code>.
42   * The default file name is <code>{@value #CONFIG_FILE_DEFAULT}</code>.
43   * The file name can be changed with a system property with name <code>{@value #CONFIG_FILE}</code>.
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     * Enabled the Server Info. May be disabled for security reasons. Default is true.
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      // the tobago version should be set in any case
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 }