View Javadoc

1   /*
2    * Copyright 2011 Fraunhofer Gesellschaft, Munich, Germany,
3    * for its Fraunhofer Institute for Computer Architecture and Software
4    * Technology (FIRST), Berlin, Germany. All rights reserved.
5    * http://www.first.fraunhofer.de/
6    */
7   
8   package net.kwfgrid.gwes;
9   
10  import net.kwfgrid.gwes.exception.LoggingException;
11  import org.apache.log4j.Logger;
12  
13  /**
14   * This is an abstract class for logging all user access to the GWES engine, workflows and activities.
15   * You may develop your own implementation of this class, regarding your logging requirements.
16   * The default implementation is net.kwfgrid.gwes.GWESBasicLogger.
17   * Use property "gwes.logger.class" in gwes.properties to configure the implementation of the GWESLogger.
18   * @author Andreas Hoheisel (<a href="http://www.andreas-hoheisel.de">www.andreas-hoheisel.de</a>)
19   * @version $Id$
20   */
21  public abstract class GWESLogger {
22  
23      private static GWESLogger _instance;
24  
25      static final private Logger logger = Logger.getLogger(GWESLogger.class);
26  
27      /**
28       * The GWES ID, e.g., as represented by its base URL.
29       */
30      private String gwesId;
31  
32      /**
33       * Level of logging.
34       */
35      private Level level;
36  
37      /**
38       * Level of logging.
39       * <br>Level.INFO logs event outcomes SUCCESS, WARNING, ERROR, and FATAL.
40       * <br>Level.WARN logs event outcomes WARNING, ERROR, and FATAL.
41       * <br>Level.ERROR logs event outcomes ERROR and FATAL.
42       * <br>Level.FATAL logs event outcomes FATAL.
43       * <br>Level.QUIET does not log at all.
44       */
45      public static enum Level {
46          INFO,
47          WARN,
48          ERROR,
49          FATAL,
50          QUIET
51      }
52  
53      /**
54       * Type of event.
55       */
56      public static enum Event {
57          GET_WORKFLOW_IDS,
58          GET_WORKFLOW_STATUS_ARRAY,
59          GET_AVAILABLE_RESOURCES,
60          GET_RESOURCE_DESCRIPTION,
61          WORKFLOW_INITIATE,
62          WORKFLOW_START,
63          WORKFLOW_SUSPEND,
64          WORKFLOW_RESUME,
65          WORKFLOW_ABORT,
66          WORKFLOW_RESTART,
67          WORKFLOW_STORE,
68          WORKFLOW_RESTORE,
69          WORKFLOW_REMOVE,
70          WORKFLOW_MODIFICATION_COMMIT,
71          WORKFLOW_STATUS_CHANGE,
72          WORKFLOW_GET_MODIFICATIONS_FOR_UPDATE,
73          WORKFLOW_GET_WORKFLOW_DESCRIPTION,
74          WORKFLOW_SET_WORKFLOW_DESCRIPTION,
75          WORKFLOW_WAIT_FOR_STATUS_CHANGE,
76          WORKFLOW_GET_DATA,
77          WORKFLOW_SET_DESCRIPTION,
78          WORKFLOW_GET_DESCRIPTION,
79          WORKFLOW_SET_PROPERTY,
80          WORKFLOW_GET_PROPERTY,
81          WORKFLOW_GET_PROPERTIES,
82          WORKFLOW_GET_CHECKPOINTS,
83          WORKFLOW_GET_ACTIVITY_STATUS_ARRAY,
84          WORKFLOW_GET_STATUS,
85          ACTIVITY_STATUS_CHANGE,
86          ACTIVITY_CONSTRUCT,
87          ACTIVITY_INITIATE,
88          ACTIVITY_START,
89          ACTIVITY_SUSPEND,
90          ACTIVITY_RESUME,
91          ACTIVITY_ABORT,
92          ACTIVITY_RESTART,
93          ACTIVITY_CLEAN,
94          SET_LOGGER_LEVEL,
95          GET_CREDENTIAL,
96          SET_CREDENTIAL,
97          LOAD_CREDENTIAL
98      }
99  
100     /**
101      * Outcome of the event.
102      */
103     public static enum EventOutcome {
104         SUCCESS,
105         WARNING,
106         ERROR,
107         FATAL
108     }
109 
110     /**
111      * Only for internal usage, use getInstance() to get instance of GWESLogger.
112      */
113     protected GWESLogger() {
114         gwesId = System.getProperty(Constants.PROP_SYSTEM_GWES_SERVICE_BASE_URL_EXTERNAL);
115         // set default log level INFO
116         level = Level.INFO;
117     }
118 
119     /**
120      * Get instance of GWESLogger implementation. Reads property "gwes.logger.class" from gwes.properties.
121      * @return The instance of GWESLogger as defined in gwes.properties.
122      * @throws LoggingException If it was not possible to create a GWESLogger instance.
123      */
124     public static synchronized GWESLogger getInstance() throws LoggingException {
125         if (_instance != null) {
126             return _instance;
127         }
128 
129         String classname = System.getProperty(Constants.PROP_SYSTEM_GWES_LOGGER_CLASS,Constants.PROP_SYSTEM_GWES_LOGGER_CLASS_DEFAULT);
130         Class c;
131         try {
132             c = Class.forName(classname);
133         } catch (ClassNotFoundException e) {
134             throw new LoggingException("Exception during constructing GWES logger implementation with classname '"+classname+"':" + e, e);
135         }
136 
137         try {
138             _instance = (GWESLogger) c.getConstructor().newInstance();
139             if (logger.isDebugEnabled()) logger.debug("Constructed GWES logger class \""+c.getName()+"\"");
140         } catch (Exception e) {
141             throw new LoggingException("Exception during constructing GWES logger implementation with classname '"+c.getName()+"': " + e, e);
142         }
143         return _instance;
144     }
145 
146     /**
147      * Get the logger level.
148      * @return The logger level.
149      */
150     public Level getLevel() {
151         return level;
152     }
153 
154     /**
155      * INFO enabled
156      * @return returns true if level is greater or equal Level.INFO.
157      */
158     public boolean i() {
159         return (level.ordinal() >= Level.INFO.ordinal());
160     }
161 
162     /**
163      * WARN enabled
164      * @return returns true if level is greater or equal Level.WARN.
165      */
166     public boolean w() {
167         return (level.ordinal() >= Level.WARN.ordinal());
168     }
169 
170     /**
171      * ERROR enabled
172      * @return returns true if level is greater or equal Level.ERROR.
173      */
174     public boolean e() {
175         return (level.ordinal() >= Level.ERROR.ordinal());
176     }
177 
178     /**
179      * FATAL enabled
180      * @return returns true if level is greater or equal Level.FATAL.
181      */
182     public boolean f() {
183         return (level.ordinal() >= Level.FATAL.ordinal());
184     }
185 
186     /**
187      * QUIET enabled
188      * @return returns true if level is greater or equal Level.QUIET.
189      */
190     public boolean q() {
191         return (level.ordinal() >= Level.QUIET.ordinal());
192     }
193 
194     /**
195      * Set the logger level.
196      * @param level The new logger level.
197      * @param userID The user ID of the user, who is responsible for invoking this method.
198      */
199     public void setLevel(Level level, String userID) throws LoggingException {
200         Level oldLevel = this.level;
201         logEventSP(Event.SET_LOGGER_LEVEL, userID, this, "loggerLevel",oldLevel.name(),level.name());
202         this.level = level;
203     }
204 
205     public String getGwesId() {
206         return gwesId;
207     }
208 
209     /**
210      * Log an event with single parameter.
211      * @param event The type of event, refer to GWESLogger.Event.
212      * @param eventOutcome The event outcome, refer to GWESLogger.EventOutcome.
213      * @param userID The user ID of the user who triggered this event, or <code>null</code> if not available.
214      * @param object The object which is related to the event, e.g. the Workflow or Activity instance, or <code>null</code> if not available.
215      * @param paramName The parameter names.
216      * @param oldValue The old value of the parameter.
217      * @param newValue The new value of the parameter.
218      * @param exception The exception if event outcome was not GWES.Logger.EventOutcome.SUCCESS, or <code>null</code> if not available.
219      * @throws LoggingException If logging fails.
220      */
221     public void logEventSP(Event event, EventOutcome eventOutcome, String userID, Object object, String paramName, String oldValue, String newValue, Throwable exception) throws LoggingException {
222         String[] paramNames = (paramName == null ? null : new String[]{paramName});
223         String[] oldValues = (oldValue == null ? null: new String[]{oldValue});
224         String[] newValues = (newValue == null ? null: new String[]{newValue});
225         logEvent(event, eventOutcome, userID, object, paramNames, oldValues, newValues, exception);
226     }
227 
228     /**
229      * Log an event without parameter.
230      * @param event The type of event, refer to GWESLogger.Event.
231      * @param eventOutcome The event outcome, refer to GWESLogger.EventOutcome.
232      * @param userID The user ID of the user who triggered this event, or <code>null</code> if not available.
233      * @param object The object which is related to the event, e.g. the Workflow or Activity instance, or <code>null</code> if not available.
234      * @param exception The exception if event outcome was not GWES.Logger.EventOutcome.SUCCESS, or <code>null</code> if not available.
235      * @throws LoggingException If logging fails.
236      */
237     public void logEvent(Event event, EventOutcome eventOutcome, String userID, Object object, Throwable exception) throws LoggingException {
238         logEvent(event, eventOutcome, userID, object, null, null, null, exception);
239     }
240 
241     /**
242      * Log an successful event with EventOutcome.SUCCESS and multiple parameters.
243      * @param event The type of event, refer to GWESLogger.Event.
244      * @param userID The user ID of the user who triggered this event, or <code>null</code> if not available.
245      * @param object The object which is related to the event, e.g. the Workflow or Activity instance, or <code>null</code> if not available.
246      * @param paramNames A String array of parameter names.
247      * @param oldValues A String array of the old values of the parameters.
248      * @param newValues A String array of the new values of the parameters.
249      * @throws LoggingException If logging fails.
250      */
251     public void logEvent(Event event, String userID, Object object, String[] paramNames, String[] oldValues, String[] newValues) throws LoggingException {
252         logEvent(event, EventOutcome.SUCCESS, userID, object, paramNames, oldValues, newValues, null);
253     }
254 
255     /**
256      * Log an successful event with EventOutcome.SUCCESS and with single parameter.
257      * @param event The type of event, refer to GWESLogger.Event.
258      * @param userID The user ID of the user who triggered this event, or <code>null</code> if not available.
259      * @param object The object which is related to the event, e.g. the Workflow or Activity instance, or <code>null</code> if not available.
260      * @param paramName The parameter names.
261      * @param oldValue The old value of the parameter.
262      * @param newValue The new value of the parameter.
263      * @throws LoggingException If logging fails.
264      */
265     public void logEventSP(Event event, String userID, Object object, String paramName, String oldValue, String newValue) throws LoggingException {
266         logEventSP(event, EventOutcome.SUCCESS, userID, object, paramName, oldValue, newValue, null);
267     }
268 
269     /**
270      * Log an successful event with EventOutcome.SUCCESS without parameter.
271      * @param event The type of event, refer to GWESLogger.Event.
272      * @param userID The user ID of the user who triggered this event, or <code>null</code> if not available.
273      * @param object The object which is related to the event, e.g. the Workflow or Activity instance, or <code>null</code> if not available.
274      * @throws LoggingException If logging fails.
275      */
276     public void logEvent(Event event, String userID, Object object) throws LoggingException {
277         logEvent(event, EventOutcome.SUCCESS, userID, object, null, null, null, null);
278     }
279 
280     /**
281      * Log an event. This method checks the logger level to decide, whether this event should be logged or not.
282      * @param event The type of event, refer to GWESLogger.Event.
283      * @param eventOutcome The event outcome, refer to GWESLogger.EventOutcome.
284      * @param userID The user ID of the user who triggered this event, or <code>null</code> if not available.
285      * @param object The object which is related to the event, e.g. the Workflow or Activity instance, or <code>null</code> if not available.
286      * @param paramNames A String array of parameter names.
287      * @param oldValues A String array of the old values of the parameters.
288      * @param newValues A String array of the new values of the parameters.
289      * @param exception The exception if event outcome was not GWES.Logger.EventOutcome.SUCCESS, or <code>null</code> if not available.
290      * @throws LoggingException If logging fails.
291      */
292     public void logEvent(Event event, EventOutcome eventOutcome, String userID, Object object, String[] paramNames, String[] oldValues, String[] newValues, Throwable exception) throws LoggingException {
293         if (level.ordinal() <= eventOutcome.ordinal()) {
294             logEventLevelChecked(event, eventOutcome, userID, object, paramNames, oldValues, newValues, exception);
295         }
296     }
297 
298     /**
299      * Log an event. Implement this method in all derived classes.
300      * @param event The type of event, refer to GWESLogger.Event.
301      * @param eventOutcome The event outcome, refer to GWESLogger.EventOutcome.
302      * @param userID The user ID of the user who triggered this event, or <code>null</code> if not available.
303      * @param object The object which is related to the event, e.g. the Workflow or Activity instance, or <code>null</code> if not available.
304      * @param paramNames A String array of parameter names.
305      * @param oldValues A String array of the old values of the parameters.
306      * @param newValues A String array of the new values of the parameters.
307      * @param exception The exception if event outcome was not GWES.Logger.EventOutcome.SUCCESS, or <code>null</code> if not available.
308      * @throws LoggingException If logging fails.
309      */
310     abstract protected void logEventLevelChecked(Event event, EventOutcome eventOutcome, String userID, Object object, String[] paramNames, String[] oldValues, String[] newValues, Throwable exception) throws LoggingException;
311 
312 }