View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.controllers.system;
12  
13  import java.util.ArrayList;
14  import java.util.HashMap;
15  import java.util.List;
16  import java.util.Map;
17  
18  import javax.inject.Inject;
19  import javax.servlet.http.HttpServletRequest;
20  import javax.servlet.http.HttpServletResponse;
21  
22  import org.springframework.web.servlet.ModelAndView;
23  
24  import psiprobe.beans.RuntimeInfoAccessorBean;
25  import psiprobe.controllers.AbstractTomcatContainerController;
26  import psiprobe.model.SystemInformation;
27  import psiprobe.tools.SecurityUtils;
28  
29  /**
30   * Creates an instance of SystemInformation.
31   */
32  public class BaseSysInfoController extends AbstractTomcatContainerController {
33  
34    /** The filter out keys. */
35    private List<String> filterOutKeys = new ArrayList<>();
36  
37    /** The runtime info accessor. */
38    @Inject
39    private RuntimeInfoAccessorBean runtimeInfoAccessor;
40  
41    /** The collection period. */
42    private long collectionPeriod;
43  
44    /**
45     * Gets the filter out keys.
46     *
47     * @return the filter out keys
48     */
49    public List<String> getFilterOutKeys() {
50      return filterOutKeys;
51    }
52  
53    /**
54     * Sets the filter out keys.
55     *
56     * @param filterOutKeys the new filter out keys
57     */
58    public void setFilterOutKeys(List<String> filterOutKeys) {
59      this.filterOutKeys = filterOutKeys;
60    }
61  
62    /**
63     * Gets the runtime info accessor.
64     *
65     * @return the runtime info accessor
66     */
67    public RuntimeInfoAccessorBean getRuntimeInfoAccessor() {
68      return runtimeInfoAccessor;
69    }
70  
71    /**
72     * Sets the runtime info accessor.
73     *
74     * @param runtimeInfoAccessor the new runtime info accessor
75     */
76    public void setRuntimeInfoAccessor(RuntimeInfoAccessorBean runtimeInfoAccessor) {
77      this.runtimeInfoAccessor = runtimeInfoAccessor;
78    }
79  
80    /**
81     * Gets the collection period.
82     *
83     * @return the collection period
84     */
85    public long getCollectionPeriod() {
86      return collectionPeriod;
87    }
88  
89    /**
90     * Sets the collection period.
91     *
92     * @param collectionPeriod the new collection period
93     */
94    public void setCollectionPeriod(long collectionPeriod) {
95      this.collectionPeriod = collectionPeriod;
96    }
97  
98    @Override
99    protected ModelAndView handleRequestInternal(HttpServletRequest request,
100       HttpServletResponse response) throws Exception {
101 
102     SystemInformation systemInformation = new SystemInformation();
103     systemInformation
104         .setAppBase(getContainerWrapper().getTomcatContainer().getAppBase().getAbsolutePath());
105     systemInformation.setConfigBase(getContainerWrapper().getTomcatContainer().getConfigBase());
106 
107     Map<String, String> sysProps = new HashMap<>();
108     for (String propertyName : System.getProperties().stringPropertyNames()) {
109       String propertyValue = System.getProperty(propertyName);
110       sysProps.put(propertyName, propertyValue);
111     }
112 
113     if (!SecurityUtils.hasAttributeValueRole(getServletContext())) {
114       for (String key : filterOutKeys) {
115         sysProps.remove(key);
116       }
117     }
118 
119     systemInformation.setSystemProperties(sysProps);
120 
121     ModelAndView mv = new ModelAndView(getViewName());
122     mv.addObject("systemInformation", systemInformation);
123     mv.addObject("runtime", getRuntimeInfoAccessor().getRuntimeInformation());
124     mv.addObject("collectionPeriod", getCollectionPeriod());
125     return mv;
126   }
127 
128 }