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
RevLine 
[77]1package com.framsticks.remote;
2
[97]3
[100]4import com.framsticks.params.Access;
[84]5import com.framsticks.params.CompositeParam;
[105]6import com.framsticks.structure.Node;
7import com.framsticks.structure.Path;
8import com.framsticks.structure.Tree;
9import com.framsticks.util.dispatching.FutureHandler;
[84]10import com.framsticks.util.dispatching.Future;
[97]11import com.framsticks.util.dispatching.ThrowExceptionHandler;
[96]12import com.framsticks.util.FramsticksException;
13import com.framsticks.util.Logging;
[77]14import com.framsticks.util.Stopwatch;
[100]15import org.apache.logging.log4j.Logger;
16import org.apache.logging.log4j.LogManager;
[105]17
18import static com.framsticks.structure.TreeOperations.*;
[84]19import static com.framsticks.util.lang.Containers.filterInstanceof;
[85]20import com.framsticks.util.dispatching.RunAt;
[77]21
22/**
23 * @author Piotr Sniegowski
24 */
25public 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}
Note: See TracBrowser for help on using the repository browser.