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