1 | package com.framsticks.remote; |
---|
2 | |
---|
3 | |
---|
4 | import com.framsticks.params.Access; |
---|
5 | import com.framsticks.params.CompositeParam; |
---|
6 | import com.framsticks.structure.Node; |
---|
7 | import com.framsticks.structure.Path; |
---|
8 | import com.framsticks.structure.Tree; |
---|
9 | import com.framsticks.util.dispatching.FutureHandler; |
---|
10 | import com.framsticks.util.dispatching.Future; |
---|
11 | import com.framsticks.util.dispatching.ThrowExceptionHandler; |
---|
12 | import com.framsticks.util.FramsticksException; |
---|
13 | import com.framsticks.util.Logging; |
---|
14 | import com.framsticks.util.Stopwatch; |
---|
15 | import org.apache.logging.log4j.Logger; |
---|
16 | import org.apache.logging.log4j.LogManager; |
---|
17 | |
---|
18 | import static com.framsticks.structure.TreeOperations.*; |
---|
19 | import static com.framsticks.util.lang.Containers.filterInstanceof; |
---|
20 | import com.framsticks.util.dispatching.RunAt; |
---|
21 | |
---|
22 | /** |
---|
23 | * @author Piotr Sniegowski |
---|
24 | */ |
---|
25 | public class RecursiveFetcher { |
---|
26 | |
---|
27 | private final static Logger log = LogManager.getLogger(RecursiveFetcher.class.getName()); |
---|
28 | |
---|
29 | protected final Tree tree; |
---|
30 | protected final FutureHandler<Void> future; |
---|
31 | protected int dispatched; |
---|
32 | protected final Stopwatch stopwatch = new Stopwatch(); |
---|
33 | |
---|
34 | public RecursiveFetcher(Tree tree, final Path path, FutureHandler<Void> future) { |
---|
35 | this.tree = tree; |
---|
36 | this.future = future; |
---|
37 | dispatched = 1; |
---|
38 | process(path); |
---|
39 | } |
---|
40 | |
---|
41 | protected void finished() { |
---|
42 | assert tree.isActive(); |
---|
43 | log.info("recursively fetched in {}", stopwatch); |
---|
44 | future.pass(null); |
---|
45 | } |
---|
46 | |
---|
47 | protected void process(final Path path) { |
---|
48 | assert tree.isActive(); |
---|
49 | if (path == null || !path.isResolved()) { |
---|
50 | log.warn("path {} is not resolved - skipping", path); |
---|
51 | } else { |
---|
52 | Access access = bindAccess(path); |
---|
53 | for (CompositeParam p : filterInstanceof(access.getParams(), CompositeParam.class)) { |
---|
54 | Object child = access.get(p, Object.class); |
---|
55 | final Path childPath = path.appendNode(new Node(path.getTree(), p, child)); |
---|
56 | if (childPath.isResolved() && getInfoFromCache(childPath) != null) { |
---|
57 | ++dispatched; |
---|
58 | tree.dispatch(new RunAt<Tree>(ThrowExceptionHandler.getInstance()) { |
---|
59 | @Override |
---|
60 | protected void runAt() { |
---|
61 | fetch(childPath); |
---|
62 | } |
---|
63 | }); |
---|
64 | continue; |
---|
65 | } |
---|
66 | ++dispatched; |
---|
67 | tree.get(childPath, new Future<Path>(Logging.logger(log, "resolve", RecursiveFetcher.this)) { |
---|
68 | @Override |
---|
69 | protected void result(Path result) { |
---|
70 | assert tree.isActive(); |
---|
71 | fetch(result); |
---|
72 | } |
---|
73 | }); |
---|
74 | } |
---|
75 | } |
---|
76 | --dispatched; |
---|
77 | if (dispatched == 0) { |
---|
78 | finished(); |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | protected void fetch(final Path path) { |
---|
83 | tree.get(path, new FutureHandler<Path>() { |
---|
84 | |
---|
85 | @Override |
---|
86 | public void handle(FramsticksException e) { |
---|
87 | log.error("failed to fetch values for {}: ", path, e); |
---|
88 | process(null); |
---|
89 | } |
---|
90 | |
---|
91 | @Override |
---|
92 | protected void result(Path result) { |
---|
93 | process(result); |
---|
94 | } |
---|
95 | }); |
---|
96 | } |
---|
97 | |
---|
98 | } |
---|