source: java/main/src/main/java/com/framsticks/remote/RecursiveFetcher.java @ 193

Last change on this file since 193 was 193, checked in by Maciej Komosinski, 10 years ago

Set svn:eol-style native for all textual files

  • Property svn:eol-style set to native
File size: 2.7 KB
Line 
1package com.framsticks.remote;
2
3
4import com.framsticks.params.Access;
5import com.framsticks.params.CompositeParam;
6import com.framsticks.structure.Node;
7import com.framsticks.structure.Path;
8import com.framsticks.structure.Tree;
9import com.framsticks.util.dispatching.FutureHandler;
10import com.framsticks.util.dispatching.Future;
11import com.framsticks.util.dispatching.ThrowExceptionHandler;
12import com.framsticks.util.FramsticksException;
13import com.framsticks.util.Logging;
14import com.framsticks.util.Stopwatch;
15import org.apache.logging.log4j.Logger;
16import org.apache.logging.log4j.LogManager;
17
18import static com.framsticks.structure.TreeOperations.*;
19import static com.framsticks.util.lang.Containers.filterInstanceof;
20import com.framsticks.util.dispatching.RunAt;
21
22/**
23 * @author Piotr Sniegowski
24 */
25public 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}
Note: See TracBrowser for help on using the repository browser.