EOS 2  1.1.0
Einfache Objektbasierte Sprache
MessageHandler.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.base.util;
2 
3 import java.util.LinkedList;
4 
12 public class MessageHandler {
13  private ErrorBehavior eb = ErrorBehavior.WARN;
14 
15  public void clear() {
16  logListener.forEach(log -> log.clearMessages());
17  }
18 
19  public void sendStatus(String id) {
21  }
22 
23  public void sendInformation(String information) {
24  fireMessage(information, ErrorLevel.INFORMATION);
25  }
26 
27  public void sendWarning(String warning) {
28  fireMessage(warning, ErrorLevel.WARNING);
29  }
30 
31  public void sendError(String error) {
33  }
34 
35  public void sendFatal(String fatal) {
37  }
38  public void setErrorBehavior(ErrorBehavior eb) {
39  this.eb = eb;
40  }
41 
42  public <T extends Exception> void handleOrThrowException(T e) throws T {
43  switch (eb) {
44  case WARN:
45  sendWarning(e.getLocalizedMessage());
46  try {
47  Thread.sleep(10);
48  } catch (InterruptedException e1) {
49  }
50  break;
51  case IGNORE:
52  // well ignore it :)
53  break;
54  case TRACE:
55  sendWarning(e.getLocalizedMessage());
56  e.printStackTrace();
57  try {
58  Thread.sleep(10);
59  } catch (InterruptedException e1) {
60  }
61  break;
62  case ABORT:
63  throw e;
64  }
65  }
66 
67  public void handle(Exception e) {
68  switch (eb) {
69  case WARN:
70  sendWarning(e.getLocalizedMessage());
71  break;
72  case IGNORE:
73  // well ignore it :)
74  break;
75  case ABORT:
76  sendError(e.getLocalizedMessage());
77  break;
78  case TRACE:
79  sendWarning(e.getLocalizedMessage());
80  e.printStackTrace();
81  try {
82  Thread.sleep(10);
83  } catch (InterruptedException e1) {
84  }
85  }
86  }
87 
88  public void handleError(Exception e) {
89  sendError(e.getLocalizedMessage());
90  }
91 
92  private LinkedList<LogListener> logListener = new LinkedList<>();
93 
94  public synchronized void addLogListener(LogListener log) {
95  logListener.add(log);
96  }
97 
98  public synchronized void removeConfigurationListener(LogListener log) {
99  logListener.remove(log);
100  }
101 
102  public synchronized void fireMessage(String msg, ErrorLevel level) {
103  logListener.forEach(log -> log.message(msg, level));
104  }
105 
106  public interface LogListener {
107  public void message(String msg, ErrorLevel level);
108 
109  public void clearMessages();
110  }
111 }
synchronized void removeConfigurationListener(LogListener log)
synchronized void fireMessage(String msg, ErrorLevel level)
void sendInformation(String information)
synchronized void addLogListener(LogListener log)
void message(String msg, ErrorLevel level)
Impressum