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