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.tools.logging;
12  
13  import java.lang.reflect.InvocationTargetException;
14  
15  import org.apache.commons.beanutils.PropertyUtils;
16  import org.apache.commons.lang3.reflect.FieldUtils;
17  import org.apache.commons.lang3.reflect.MethodUtils;
18  import org.slf4j.Logger;
19  import org.slf4j.LoggerFactory;
20  
21  import psiprobe.model.Application;
22  
23  /**
24   * The Class DefaultAccessor.
25   */
26  public class DefaultAccessor {
27  
28    /** The Constant logger. */
29    protected static final Logger logger = LoggerFactory.getLogger(DefaultAccessor.class);
30  
31    /** The application. */
32    private Application application;
33  
34    /** The target. */
35    private Object target;
36  
37    /**
38     * Gets the application.
39     *
40     * @return the application
41     */
42    public Application getApplication() {
43      return application;
44    }
45  
46    /**
47     * Sets the application.
48     *
49     * @param application the new application
50     */
51    public void setApplication(Application application) {
52      this.application = application;
53    }
54  
55    /**
56     * Gets the target.
57     *
58     * @return the target
59     */
60    public Object getTarget() {
61      return target;
62    }
63  
64    /**
65     * Sets the target.
66     *
67     * @param target the new target
68     */
69    public void setTarget(Object target) {
70      this.target = target;
71    }
72  
73    /**
74     * Gets the target class.
75     *
76     * @return the target class
77     */
78    public String getTargetClass() {
79      return getTarget().getClass().getName();
80    }
81  
82    /**
83     * Gets the property.
84     *
85     * @param obj the obj
86     * @param name the name
87     * @param defaultValue the default value
88     * @param forced whether or not to force access to the field
89     *
90     * @return the property
91     */
92    protected Object getProperty(Object obj, String name, Object defaultValue, boolean forced) {
93      try {
94        if (forced) {
95          return FieldUtils.readField(obj, name, forced);
96        }
97        return PropertyUtils.isReadable(obj, name) ? PropertyUtils.getProperty(obj, name)
98            : defaultValue;
99      } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException
100         | NoSuchMethodException e) {
101       logger.error("", e);
102     }
103     logger.debug("Could not access property '{}' of object '{}'", name, obj);
104     return defaultValue;
105   }
106 
107   /**
108    * Gets the property.
109    *
110    * @param obj the obj
111    * @param name the name
112    * @param defaultValue the default value
113    *
114    * @return the property
115    */
116   protected Object getProperty(Object obj, String name, Object defaultValue) {
117     return getProperty(obj, name, defaultValue, false);
118   }
119 
120   /**
121    * Invoke method.
122    *
123    * @param object the object
124    * @param name the name
125    * @param param the param
126    * @param defaultValue the default value
127    *
128    * @return the object
129    */
130   protected Object invokeMethod(Object object, String name, Object param, Object defaultValue) {
131     try {
132       if (param == null) {
133         return MethodUtils.invokeMethod(object, name);
134       }
135       return MethodUtils.invokeMethod(object, name, param);
136     } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
137       logger.error("", e);
138     }
139     return defaultValue;
140   }
141 
142 }