1 | package com.framsticks.core; |
---|
2 | |
---|
3 | import org.apache.logging.log4j.Logger; |
---|
4 | import org.apache.logging.log4j.LogManager; |
---|
5 | |
---|
6 | import com.framsticks.params.Access; |
---|
7 | import com.framsticks.params.CompositeParam; |
---|
8 | import com.framsticks.params.EventListener; |
---|
9 | import com.framsticks.params.FramsClass; |
---|
10 | import com.framsticks.params.ParamBuilder; |
---|
11 | import com.framsticks.params.PrimitiveParam; |
---|
12 | import com.framsticks.params.annotations.AutoAppendAnnotation; |
---|
13 | import com.framsticks.params.annotations.FramsClassAnnotation; |
---|
14 | import com.framsticks.params.types.EventParam; |
---|
15 | import com.framsticks.params.types.ProcedureParam; |
---|
16 | import com.framsticks.util.FramsticksException; |
---|
17 | import com.framsticks.util.dispatching.Dispatcher; |
---|
18 | import com.framsticks.util.dispatching.DispatcherSetable; |
---|
19 | import com.framsticks.util.dispatching.Dispatching; |
---|
20 | import com.framsticks.util.dispatching.Future; |
---|
21 | import com.framsticks.util.dispatching.Joinable; |
---|
22 | import com.framsticks.util.dispatching.JoinableState; |
---|
23 | |
---|
24 | import static com.framsticks.core.TreeOperations.*; |
---|
25 | |
---|
26 | @FramsClassAnnotation |
---|
27 | public final class LocalTree extends AbstractTree { |
---|
28 | private static final Logger log = LogManager.getLogger(LocalTree.class); |
---|
29 | |
---|
30 | protected Joinable joinableRootObject; |
---|
31 | |
---|
32 | /** |
---|
33 | * |
---|
34 | */ |
---|
35 | public LocalTree() { |
---|
36 | super(); |
---|
37 | bufferedDispatcher.setBuffer(false); |
---|
38 | } |
---|
39 | |
---|
40 | @SuppressWarnings({ "rawtypes", "unchecked" }) |
---|
41 | @AutoAppendAnnotation |
---|
42 | public void setRootObject(Object object) { |
---|
43 | final Class<?> javaClass = object.getClass(); |
---|
44 | registry.registerAndBuild(javaClass); |
---|
45 | |
---|
46 | Access access = registry.createAccess(javaClass); |
---|
47 | |
---|
48 | assignRootParam(access.buildParam(new ParamBuilder()).id(getName()).finish(CompositeParam.class)); |
---|
49 | assignRootObject(object); |
---|
50 | |
---|
51 | if (object instanceof Joinable) { |
---|
52 | joinableRootObject = (Joinable) object; |
---|
53 | } |
---|
54 | if (object instanceof DispatcherSetable) { |
---|
55 | DispatcherSetable<?> setable = (DispatcherSetable<?>) object; |
---|
56 | setable.setDispatcher((Dispatcher) this); |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | public Object getRootObject() { |
---|
61 | Object result = getAssignedRoot().getObject(); |
---|
62 | if (result == null) { |
---|
63 | throw new FramsticksException().msg("object tree is empty").arg("tree", this); |
---|
64 | } |
---|
65 | return result; |
---|
66 | } |
---|
67 | |
---|
68 | public <T> T getRootObject(Class<T> type) { |
---|
69 | Object result = getRootObject(); |
---|
70 | if (!type.isInstance(result)) { |
---|
71 | throw new FramsticksException().msg("object tree holds object of different kind").arg("object", result).arg("requested", type).arg("tree", this); |
---|
72 | } |
---|
73 | return type.cast(result); |
---|
74 | } |
---|
75 | |
---|
76 | @Override |
---|
77 | public void get(Path path, Future<Path> future) { |
---|
78 | assert isActive(); |
---|
79 | log.debug("requesting: {}", path); |
---|
80 | path = resolveTopSync(path); |
---|
81 | future.pass(path); |
---|
82 | } |
---|
83 | |
---|
84 | // @Override |
---|
85 | // public void get(Path path, ValueParam param, Future<Object> future) { |
---|
86 | // assert isActive(); |
---|
87 | // path = resolveTopSync(path); |
---|
88 | // future.pass(bindAccess(path).get(param, Object.class)); |
---|
89 | // } |
---|
90 | |
---|
91 | @Override |
---|
92 | public void call(Path path, ProcedureParam param, Object[] arguments, Future<Object> future) { |
---|
93 | assert isActive(); |
---|
94 | try { |
---|
95 | future.pass(bindAccess(path).call(param, arguments)); |
---|
96 | } catch (FramsticksException e) { |
---|
97 | future.handle(e); |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | @Override |
---|
102 | public void info(Path path, Future<FramsClass> future) { |
---|
103 | assert isActive(); |
---|
104 | Path p = path.tryResolveIfNeeded(); |
---|
105 | Class<?> javaClass = p.getTopObject().getClass(); |
---|
106 | FramsClass framsClass = registry.registerReflectedIfNeeded(javaClass); |
---|
107 | if (framsClass != null) { |
---|
108 | future.pass(framsClass); |
---|
109 | } else { |
---|
110 | future.handle(new FramsticksException().msg("failed to find info for class").arg("java class", javaClass)); |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | protected Path resolveTopSync(Path path) { |
---|
115 | assert isActive(); |
---|
116 | assert path.isOwner(this); |
---|
117 | if (path.getTop().getObject() != null) { |
---|
118 | return path; |
---|
119 | } |
---|
120 | Access access = bindAccess(path.getUnder()); |
---|
121 | Object object = access.get(path.getTop().getParam(), Object.class); |
---|
122 | if (object == null) { |
---|
123 | throw new FramsticksException().msg("failed to resolve").arg("path", path); |
---|
124 | } |
---|
125 | return path.appendResolution(object); |
---|
126 | } |
---|
127 | |
---|
128 | |
---|
129 | @Override |
---|
130 | public void set(Path path, PrimitiveParam<?> param, Object value, final Future<Integer> future) { |
---|
131 | assert isActive(); |
---|
132 | future.pass(bindAccess(path).set(param, value)); |
---|
133 | } |
---|
134 | |
---|
135 | public <A> void addListener(Path path, EventParam param, EventListener<A> listener, Class<A> argumentType, Future<Void> future) { |
---|
136 | assert isActive(); |
---|
137 | try { |
---|
138 | bindAccess(path).reg(param, listener); |
---|
139 | future.pass(null); |
---|
140 | } catch (FramsticksException e) { |
---|
141 | future.handle(e); |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | public void removeListener(Path path, EventParam param, EventListener<?> listener, Future<Void> future) { |
---|
146 | assert isActive(); |
---|
147 | try { |
---|
148 | bindAccess(path).regRemove(param, listener); |
---|
149 | future.pass(null); |
---|
150 | } catch (FramsticksException e) { |
---|
151 | future.handle(e); |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | @Override |
---|
156 | protected void joinableStart() { |
---|
157 | super.joinableStart(); |
---|
158 | if (joinableRootObject != null) { |
---|
159 | Dispatching.use(joinableRootObject, this); |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | @Override |
---|
164 | protected void joinableInterrupt() { |
---|
165 | if (joinableRootObject != null) { |
---|
166 | Dispatching.drop(joinableRootObject, this); |
---|
167 | } |
---|
168 | super.joinableInterrupt(); |
---|
169 | } |
---|
170 | |
---|
171 | @Override |
---|
172 | protected void joinableFinish() { |
---|
173 | super.joinableFinish(); |
---|
174 | } |
---|
175 | |
---|
176 | @Override |
---|
177 | protected void joinableJoin() throws InterruptedException { |
---|
178 | if (joinableRootObject != null) { |
---|
179 | Dispatching.join(joinableRootObject); |
---|
180 | } |
---|
181 | super.joinableJoin(); |
---|
182 | } |
---|
183 | |
---|
184 | @Override |
---|
185 | public void childChangedState(Joinable joinable, JoinableState state) { |
---|
186 | super.childChangedState(joinable, state); |
---|
187 | if (joinable == joinableRootObject) { |
---|
188 | proceedToState(state); |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | } |
---|