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

Last change on this file since 85 was 85, checked in by psniegowski, 11 years ago

HIGHLIGHTS:

  • upgrade to Java 7
    • use try-multi-catch clauses
    • use try-with-resources were appropriate
  • configure FindBugs? (use mvn site and then navigate in browser to the report)
    • remove most bugs found
  • parametrize Dispatching environment (Dispatcher, RunAt?) to enforce more control on the place of closures actual call

CHANGELOG:
Rework FavouritesXMLFactory.

FindBugs?. Thread start.

FindBugs?. Minor change.

FindBugs?. Iterate over entrySet.

FindBugs?. Various.

FindBug?.

FindBug?. Encoding.

FindBug?. Final fields.

FindBug?.

Remove synchronization bug in ClientConnection?.

Experiments with findbugs.

Finish parametrization.

Make RunAt? an abstract class.

More changes in parametrization.

More changes in parametrizing dispatching.

Several changes to parametrize tasks.

Rename Runnable to RunAt?.

Add specific framsticks Runnable.

Add JSR305 (annotations).

Add findbugs reporting.

More improvements to ParamBuilder? wording.

Make FramsClass? accept also ParamBuilder?.

Change wording of ParamBuilder?.

Change wording of Request creation.

Use Java 7 exception catch syntax.

Add ScopeEnd? class.

Upgrade to Java 7.

File size: 2.6 KB
Line 
1package com.framsticks.remote;
2
3import com.framsticks.core.Node;
4import com.framsticks.core.Path;
5import com.framsticks.params.AccessInterface;
6import com.framsticks.params.CompositeParam;
7import com.framsticks.params.FramsClass;
8import com.framsticks.core.Instance;
9import com.framsticks.util.dispatching.Future;
10import com.framsticks.util.StateFunctor;
11import com.framsticks.util.Stopwatch;
12import org.apache.log4j.Logger;
13import static com.framsticks.util.lang.Containers.filterInstanceof;
14import com.framsticks.util.dispatching.RunAt;
15
16/**
17 * @author Piotr Sniegowski
18 */
19public class RecursiveFetcher {
20
21        private final static Logger log = Logger.getLogger(RecursiveFetcher.class.getName());
22
23        protected final Instance instance;
24        protected final StateFunctor stateFunctor;
25        protected int dispatched;
26        protected final Stopwatch stopwatch = new Stopwatch();
27
28        public RecursiveFetcher(Instance instance, final Path path, StateFunctor stateFunctor) {
29                this.instance = instance;
30                this.stateFunctor = stateFunctor;
31                dispatched = 1;
32                process(path);
33        }
34
35        protected void finished() {
36                assert instance.isActive();
37                log.info("recursively fetched in " + stopwatch);
38                stateFunctor.call(null);
39        }
40
41        protected void process(final Path path) {
42                assert instance.isActive();
43                if (path == null || !path.isResolved()) {
44                        log.warn("path " + path + " is not resolved - skipping");
45                } else {
46                        AccessInterface access = instance.bindAccess(path);
47                        FramsClass framsClass = access.getFramsClass();
48                        assert framsClass != null;
49                        for (CompositeParam p : filterInstanceof(access.getParams(), CompositeParam.class)) {
50                                Object child = access.get(p, Object.class);
51                                final Path childPath = path.appendNode(new Node(p, child));
52                                if (childPath.isResolved() && instance.getInfoFromCache(childPath) != null) {
53                                        ++dispatched;
54                                        instance.invokeLater(new RunAt<Instance>() {
55                                                @Override
56                                                public void run() {
57                                                        fetch(childPath);
58                                                }
59                                        });
60                                        continue;
61                                }
62                                ++dispatched;
63                                instance.resolve(childPath, new Future<Path>() {
64                                        @Override
65                                        public void result(Path result, Exception e) {
66                                                assert instance.isActive();
67                                                if (e != null) {
68                                                        log.error(e);
69                                                        return;
70                                                }
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                instance.fetchValues(path, new StateFunctor() {
84                        @Override
85                        public void call(Exception e) {
86                                if (e != null) {
87                                        log.error("failed to fetch values for " + path + ": " + e);
88                                        process(null);
89                                        return;
90                                }
91                                process(path);
92                        }
93                });
94        }
95
96}
Note: See TracBrowser for help on using the repository browser.