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.beans.stats.listeners;
12  
13  import org.slf4j.Logger;
14  import org.slf4j.LoggerFactory;
15  
16  /**
17   * The listener interface for receiving abstractStatsCollection events. The class that is interested
18   * in processing a abstractStatsCollection event implements this interface, and the object created
19   * with that class is registered with a component using the component's
20   * {@code addAbstractStatsCollectionListener} method. When the abstractStatsCollection event occurs,
21   * that object's appropriate method is invoked.
22   */
23  public abstract class AbstractStatsCollectionListener implements StatsCollectionListener {
24  
25    /** The logger. */
26    protected final Logger logger = LoggerFactory.getLogger(getClass());
27  
28    /** The property category. */
29    private String propertyCategory;
30  
31    /** The enabled. */
32    private boolean enabled = true;
33  
34    @Override
35    public boolean isEnabled() {
36      return enabled;
37    }
38  
39    /**
40     * Sets the enabled.
41     *
42     * @param enabled the new enabled
43     */
44    protected void setEnabled(boolean enabled) {
45      this.enabled = enabled;
46    }
47  
48    /**
49     * Gets the property value.
50     *
51     * @param name the name
52     * @param attribute the attribute
53     *
54     * @return the property value
55     */
56    protected String getPropertyValue(String name, String attribute) {
57      String value = getPropertyValue(getPropertyKey(name, attribute));
58      if (value == null) {
59        value = getPropertyValue(getPropertyKey(null, attribute));
60      }
61      if (value == null) {
62        value = getPropertyValue(getPropertyKey(null, null, attribute));
63      }
64      return value;
65    }
66  
67    /**
68     * Gets the property value.
69     *
70     * @param key the key
71     *
72     * @return the property value
73     */
74    protected String getPropertyValue(String key) {
75      return System.getProperty(key);
76    }
77  
78    /**
79     * Gets the property key.
80     *
81     * @param name the name
82     * @param attribute the attribute
83     *
84     * @return the property key
85     */
86    protected String getPropertyKey(String name, String attribute) {
87      return getPropertyKey(getPropertyCategory(), name, attribute);
88    }
89  
90    /**
91     * Gets the property key.
92     *
93     * @param category the category
94     * @param name the name
95     * @param attribute the attribute
96     *
97     * @return the property key
98     */
99    private String getPropertyKey(String category, String name, String attribute) {
100     StringBuilder result = new StringBuilder().append(getClass().getPackage().getName());
101     if (category != null) {
102       result.append('.').append(category);
103     }
104     if (name != null) {
105       result.append('.').append(name);
106     }
107     if (attribute == null) {
108       throw new IllegalArgumentException("key cannot be null");
109     }
110     result.append('.').append(attribute);
111     return result.toString();
112   }
113 
114   /**
115    * Reset.
116    */
117   public void reset() {
118     // Not Implemented;
119   }
120 
121   /**
122    * Gets the property category.
123    *
124    * @return the property category
125    */
126   public String getPropertyCategory() {
127     return propertyCategory;
128   }
129 
130   /**
131    * Sets the property category.
132    *
133    * @param propertyCategory the new property category
134    */
135   public void setPropertyCategory(String propertyCategory) {
136     this.propertyCategory = propertyCategory;
137   }
138 
139 }