1 | package com.framsticks.observers; |
---|
2 | |
---|
3 | import com.framsticks.core.Instance; |
---|
4 | import com.framsticks.core.InstanceListener; |
---|
5 | import com.framsticks.core.Path; |
---|
6 | import com.framsticks.params.FramsClass; |
---|
7 | import com.framsticks.core.ListChange; |
---|
8 | import com.framsticks.util.dispatching.Dispatcher; |
---|
9 | import org.apache.log4j.Logger; |
---|
10 | import com.framsticks.util.dispatching.RunAt; |
---|
11 | |
---|
12 | /** |
---|
13 | * @author Piotr Sniegowski |
---|
14 | */ |
---|
15 | public class Endpoint implements Dispatcher<Instance>, InstanceListener { |
---|
16 | private final static Logger log = Logger.getLogger(Endpoint.class.getName()); |
---|
17 | |
---|
18 | protected Instance instance; |
---|
19 | protected Observer observer; |
---|
20 | protected String name; |
---|
21 | |
---|
22 | public Endpoint() { |
---|
23 | } |
---|
24 | |
---|
25 | public final void configurePublic(Observer observer, Instance instance, String name) { |
---|
26 | assert instance != null; |
---|
27 | this.name = name; |
---|
28 | this.observer = observer; |
---|
29 | this.instance = instance; |
---|
30 | instance.addListener(this); |
---|
31 | } |
---|
32 | |
---|
33 | public void start() { |
---|
34 | instance.start(); |
---|
35 | } |
---|
36 | |
---|
37 | @Override |
---|
38 | public void onListChange(Path path, ListChange change) { |
---|
39 | log.info("change " + change + " in " + path); |
---|
40 | } |
---|
41 | |
---|
42 | @Override |
---|
43 | public void onRun(Exception e) { |
---|
44 | } |
---|
45 | |
---|
46 | @Override |
---|
47 | public void onStop(Exception e) { |
---|
48 | } |
---|
49 | |
---|
50 | @Override |
---|
51 | public void onFetch(Path path) { |
---|
52 | log.trace("fetched " + path); |
---|
53 | } |
---|
54 | |
---|
55 | @Override |
---|
56 | public String toString() { |
---|
57 | return instance.toString(); |
---|
58 | } |
---|
59 | |
---|
60 | public final Instance getInstance() { |
---|
61 | return instance; |
---|
62 | } |
---|
63 | |
---|
64 | @Override |
---|
65 | public final boolean isActive() { |
---|
66 | return instance.isActive(); |
---|
67 | } |
---|
68 | |
---|
69 | @Override |
---|
70 | public final void invokeLater(RunAt<? extends Instance> runnable) { |
---|
71 | instance.invokeLater(runnable); |
---|
72 | } |
---|
73 | |
---|
74 | public Observer getObserver() { |
---|
75 | return observer; |
---|
76 | } |
---|
77 | |
---|
78 | public final String getName() { |
---|
79 | return name; |
---|
80 | } |
---|
81 | |
---|
82 | public static void constructFramsClass(FramsClass.Constructor constructor) { |
---|
83 | constructor.method("getName").method("getInstance"); |
---|
84 | } |
---|
85 | |
---|
86 | } |
---|