1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.io.common;
|
11
|
|
12
|
import java.util.HashSet;
|
13
|
import java.util.Set;
|
14
|
|
15
|
import eu.etaxonomy.cdm.io.common.events.IIoEvent;
|
16
|
import eu.etaxonomy.cdm.io.common.events.IIoObserver;
|
17
|
import eu.etaxonomy.cdm.io.common.events.IoProblemEvent;
|
18
|
|
19
|
/**
|
20
|
* @author a.mueller
|
21
|
* @created 30.03.2012
|
22
|
*
|
23
|
*/
|
24
|
public class ObservableBase implements IIoObservable {
|
25
|
|
26
|
private Set<IIoObserver> observers = new HashSet<IIoObserver>();
|
27
|
|
28
|
/* (non-Javadoc)
|
29
|
* @see eu.etaxonomy.cdm.io.common.IObservable#getObservers()
|
30
|
*/
|
31
|
@Override
|
32
|
public Set<IIoObserver> getObservers() {
|
33
|
return observers;
|
34
|
}
|
35
|
|
36
|
/* (non-Javadoc)
|
37
|
* @see eu.etaxonomy.cdm.io.common.IIoObservable#countObservers()
|
38
|
*/
|
39
|
@Override
|
40
|
public int countObservers(){
|
41
|
return observers.size();
|
42
|
}
|
43
|
|
44
|
public void setObservers(Set<IIoObserver> observers) {
|
45
|
this.observers = observers;
|
46
|
}
|
47
|
|
48
|
/* (non-Javadoc)
|
49
|
* @see eu.etaxonomy.cdm.io.common.IObservable#addObserver(eu.etaxonomy.cdm.io.common.events.IIoObserver)
|
50
|
*/
|
51
|
@Override
|
52
|
public boolean addObserver(IIoObserver observer){
|
53
|
return this.observers.add(observer);
|
54
|
}
|
55
|
|
56
|
|
57
|
/* (non-Javadoc)
|
58
|
* @see eu.etaxonomy.cdm.io.common.IIoObservable#removeObservers()
|
59
|
*/
|
60
|
public void removeObservers(){
|
61
|
observers.removeAll(observers);
|
62
|
}
|
63
|
|
64
|
@Override
|
65
|
public void addObservers(Set<IIoObserver> newObservers) {
|
66
|
for (IIoObserver observer : newObservers){
|
67
|
this.observers.add(observer);
|
68
|
}
|
69
|
}
|
70
|
|
71
|
/* (non-Javadoc)
|
72
|
* @see eu.etaxonomy.cdm.io.common.IObservable#removeObserver(eu.etaxonomy.cdm.io.common.events.IIoObserver)
|
73
|
*/
|
74
|
@Override
|
75
|
public boolean removeObserver(IIoObserver observer){
|
76
|
return this.observers.remove(observer);
|
77
|
}
|
78
|
|
79
|
/* (non-Javadoc)
|
80
|
* @see eu.etaxonomy.cdm.io.common.ICdmIO#fire(eu.etaxonomy.cdm.io.common.events.IIoEvent)
|
81
|
*/
|
82
|
protected void fire(IIoEvent event){
|
83
|
for (IIoObserver observer: observers){
|
84
|
observer.handleEvent(event);
|
85
|
}
|
86
|
}
|
87
|
|
88
|
protected void fireWarningEvent(String message, String dataLocation, Integer severity) {
|
89
|
fireWarningEvent(message, dataLocation, severity, 1);
|
90
|
}
|
91
|
|
92
|
protected void fireWarningEvent(String message, String dataLocation, Integer severity, int stackDepth) {
|
93
|
stackDepth++;
|
94
|
StackTraceElement[] stackTrace = new Exception().getStackTrace();
|
95
|
int lineNumber = stackTrace[stackDepth].getLineNumber();
|
96
|
String methodName = stackTrace[stackDepth].getMethodName();
|
97
|
String className = stackTrace[stackDepth].getClassName();
|
98
|
Class<?> declaringClass;
|
99
|
try {
|
100
|
declaringClass = Class.forName(className);
|
101
|
} catch (ClassNotFoundException e) {
|
102
|
declaringClass = this.getClass();
|
103
|
}
|
104
|
|
105
|
IoProblemEvent event = IoProblemEvent.NewInstance(declaringClass, message, dataLocation,
|
106
|
lineNumber, severity, methodName);
|
107
|
|
108
|
//for performance improvement one may read:
|
109
|
//http://stackoverflow.com/questions/421280/in-java-how-do-i-find-the-caller-of-a-method-using-stacktrace-or-reflection
|
110
|
// Object o = new SecurityManager().getSecurityContext();
|
111
|
|
112
|
|
113
|
fire(event);
|
114
|
}
|
115
|
|
116
|
}
|