summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/modules/monitor/src/main/java/org/apache/tuscany/sca/monitor/Monitor.java
blob: 91f6bc9a9fb15b1e786ed292179c139ee9c0ac2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */

package org.apache.tuscany.sca.monitor;

import java.util.List;
import java.util.logging.Logger;

import org.apache.tuscany.sca.monitor.Problem.Severity;

/**
 * A monitor for the watching for validation problems
 *
 * @version $Rev$ $Date$
 * @tuscany.spi.extension.inheritfrom
 */
public abstract class Monitor {
    private static class ContextFinder extends SecurityManager {
        private final static ContextFinder instance = new ContextFinder();
        
        // This is sensitive to the calling stack
        private static Class<?> getContextClass() {
            Class[] classes = instance.getClassContext();
            // 0: ContextFinder (getClassContext)
            // 1: ContextFinder (getContextClass)
            // 2: Monitor (getSourceClassName)
            // 3: Monitor (error/warning)
            return classes[4];
        }
        
    }
    private final static Logger logger = Logger.getLogger(Monitor.class.getName());

    /**
     * A utility function for raising an error. It creates the problem and 
     * adds it to the monitor
     * 
     * @param monitor
     * @param reportingObject
     * @param messageBundle
     * @param messageId
     * @param messageParameters
     */
    public static void error (Monitor monitor, 
                              Object reportingObject,
                              String messageBundle,
                              String messageId, 
                              Object... messageParameters){
        String contextClassName = getSourceClassName(reportingObject);
        if (monitor != null) {
            Problem problem =
                monitor.createProblem(contextClassName,
                                      messageBundle,
                                      Severity.ERROR,
                                      reportingObject,
                                      messageId,
                                      messageParameters);
            monitor.problem(problem);
        } else {
            logNullMonitor(messageId, contextClassName);
        }
    }

    /**
     * A utility function for raising an error. It creates the problem and 
     * adds it to the monitor
     * 
     * @param monitor
     * @param reportingObject
     * @param messageBundle
     * @param messageId
     * @param exception
     */
    public static void error (Monitor monitor, 
                              Object reportingObject,
                              String messageBundle,
                              String messageId, 
                              Throwable cause){
        String contextClassName = getSourceClassName(reportingObject);
        if (monitor != null) {
            Problem problem =
                monitor.createProblem(contextClassName,
                                      messageBundle,
                                      Severity.ERROR,
                                      reportingObject,
                                      messageId,
                                      cause);
            monitor.problem(problem);
        } else {
            logNullMonitor(messageId, contextClassName);
        }
    }

    /**
     * A utility function for raising an error. It creates the problem and 
     * adds it to the monitor
     * 
     * @param monitor
     * @param reportingObject
     * @param messageBundle
     * @param messageId
     * @param exception
     */
    public static void error (Monitor monitor, 
                              Object reportingObject,
                              String messageBundle,
                              String messageId, 
                              Throwable cause,
                              Object... messageParameters) {
        String contextClassName = getSourceClassName(reportingObject);
        if (monitor != null) {
            Problem problem =
                monitor.createProblem(contextClassName,
                                      messageBundle,
                                      Severity.ERROR,
                                      reportingObject,
                                      messageId,
                                      cause,
                                      messageParameters);
            monitor.problem(problem);
        } else {
            logNullMonitor(messageId, contextClassName);
        }
    }

    private static String getSourceClassName(Object reportingObject) {
        String contextClassName = null;
        if (reportingObject != null) {
            contextClassName = reportingObject.getClass().getName();
        } else {
            contextClassName = ContextFinder.getContextClass().getName();
        }
        return contextClassName;
    }

    private static void logNullMonitor(String messageId, String contextClassName) {
        logger.warning("Attempt to report error with id " + 
                messageId + 
                " from class " + 
                contextClassName +
                " but the monitor object was null");
    }

    /**
     * A utility function for raising a warning. It creates the problem and 
     * adds it to the monitor
     * 
     * @param monitor
     * @param reportingObject
     * @param messageBundle
     * @param messageId
     * @param messageParameters
     */
    public static void warning (Monitor monitor, 
                                Object reportingObject,
                                String messageBundle,
                                String messageId, 
                                Object... messageParameters){
        String contextClassName = getSourceClassName(reportingObject);
        if (monitor != null) {
            Problem problem =
                monitor.createProblem(contextClassName,
                                      messageBundle,
                                      Severity.WARNING,
                                      reportingObject,
                                      messageId,
                                      messageParameters);
            monitor.problem(problem);
        } else {
            logNullMonitor(messageId, contextClassName);
        }
    }
    
    // =====================================================
    // TUSCANY-3132 - new approach to monitoring errors
    //           
    
    /**
     * A utility function for raising an error. It creates the problem and 
     * adds it to the monitor
     * 
     * @param monitor
     * @param reportingObject
     * @param messageBundle
     * @param messageId
     * @param exception
     */
    public static void warning (Monitor monitor, 
                              Object reportingObject,
                              String messageBundle,
                              String messageId, 
                              Throwable cause){
        String contextClassName = getSourceClassName(reportingObject);
        if (monitor != null) {
            Problem problem =
                monitor.createProblem(contextClassName,
                                      messageBundle,
                                      Severity.WARNING,
                                      reportingObject,
                                      messageId,
                                      cause);
            monitor.problem(problem);
        } else {
            logNullMonitor(messageId, contextClassName);
        }
    }
    
    /**
     * A utility function for raising an error. It creates the problem and 
     * adds it to the monitor
     * 
     * @param monitor
     * @param reportingObject
     * @param messageBundle
     * @param messageId
     * @param exception
     */
    public static void warning (Monitor monitor, 
                              Object reportingObject,
                              String messageBundle,
                              String messageId, 
                              Throwable cause,
                              Object... messageParameters) {
        String contextClassName = getSourceClassName(reportingObject);
        if (monitor != null) {
            Problem problem =
                monitor.createProblem(contextClassName,
                                      messageBundle,
                                      Severity.WARNING,
                                      reportingObject,
                                      messageId,
                                      cause,
                                      messageParameters);
            monitor.problem(problem);
        } else {
            logNullMonitor(messageId, contextClassName);
        }
    }
    
    /**
     * Create a new problem.
     *  
     * @param sourceClassName   the class name reporting the problem
     * @param bundleName        the name of the message bundle to use
     * @param severity          the severity of the problem
     * @param problemObject     the model object for which the problem is being reported
     * @param messageId         the id of the problem message
     * @param messageParams     the parameters of the problem message
     * @return
     */
    public abstract Problem createProblem(String sourceClassName,
                                   String bundleName,
                                   Severity severity,
                                   Object problemObject,
                                   String messageId,
                                   Object... messageParams);
    
    /**
     * Create a new problem.
     * 
     * @param sourceClassName   the class name reporting the problem
     * @param bundleName        the name of the message bundle to use
     * @param severity          the severity of the problem
     * @param problemObject     the model object for which the problem is being reported
     * @param messageId         the id of the problem message
     * @param cause             the exception which caused the problem
     * @return
     */
    public abstract Problem createProblem(String sourceClassName,
                                          String bundleName,
                                          Severity severity,
                                          Object problemObject,
                                          String messageId,
                                          Throwable cause);

    /**
     * Get the name of the artifact for which errors are Monitored
     * @return the name of the Artifact or null if no artifact name has been set
     */
    public abstract String getArtifactName();

    /**
     * Returns the last logged problem.
     * 
     * @return
     */
    public abstract Problem getLastProblem();
   
    /** 
     * Returns a list of reported problems. 
     * 
     * @return the list of problems. The list may be empty
     */
    public abstract List<Problem> getProblems();

    /**
     * Remove the most recent context string from the 
     * context stack
     * @return The object popped 
     */
    public abstract Object popContext();

    /**
     * Reports a build problem.
     * 
     * @param problem
     */
    public abstract void problem(Problem problem);
    
    /**
     * Add a context string to the context stack
     * @param context the context string to add
     */
    public abstract void pushContext(Object context);
    
    /**
     * Clear context and problems
     */
    public abstract void reset();    
    /**
     * Set the name of an artifact for which errors are Monitored
     * @param artifactName the artifact name
     */
    public abstract void setArtifactName(String artifactName);
    
    // =====================================================

    /**
     * Checks the Monitor for any Problems with s severity of ERROR and
     * if one is found then throw a ValidationException.
     * This will also call reset() on this Monitor.
     */
    public void analyzeProblems() throws ValidationException {
        try {
            for (Problem problem : getProblems()) {
                if ((problem.getSeverity() == Severity.ERROR)) {
                    if (problem.getCause() != null) {
                        throw new ValidationException(problem.getCause());
                    } else {
                        throw new ValidationException(problem.toString());
                    }
                }
            }
        } finally {
            reset();
        }
    }
    
    public boolean isErrorDetected() {
        
        boolean errorDetected = false;
        
        for (Problem problem : getProblems()) {
            if ((problem.getSeverity() == Severity.ERROR)) {
                errorDetected = true;
                break;
            }
        }
        
        return errorDetected;
    }
    
    /**
     * Helper method to retrieve a localized message from a given bundle with a given 
     * message ID string
     * 
     * @Param loggerName - the name of the logger to use
     * @param messageBundleName - the name of the bundle to use
     * @param messageID - the ID of the message to retrieve
     * @return the message string
     */
    public abstract String getMessageString(String loggerName, String messageBundleName, String messageID);
}