comparison artifact-database/src/main/java/de/intevation/artifactdatabase/state/FacetActivity.java @ 438:f3bf22423c95

More documentation and debugging output for FacetActivity System.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Wed, 17 Oct 2012 22:16:17 +0200
parents 15179c77aa1d
children f503374f516c
comparison
equal deleted inserted replaced
436:15179c77aa1d 438:f3bf22423c95
5 import java.util.ArrayList; 5 import java.util.ArrayList;
6 import java.util.HashMap; 6 import java.util.HashMap;
7 import java.util.List; 7 import java.util.List;
8 import java.util.Map; 8 import java.util.Map;
9 9
10 import org.apache.log4j.Logger;
10 11
11 /** Magical system. */ 12
13 /**
14 * System used in practice used by AttributeWriter in flys-artifacts to decide
15 * whether a facet is initially active.
16 * Provides a singleton Registry into which FacetActivities can be registered
17 * under a key (in practice the artifacts name. This Registry is queried for
18 * new Facets in order to find whether they are active or inactive.
19 */
12 public interface FacetActivity 20 public interface FacetActivity
13 { 21 {
14 /** Static 'activity' that lets all facets be active. */ 22 /** Static 'activity' that lets all facets be active. */
15 public static final FacetActivity ACTIVE = new FacetActivity() { 23 public static final FacetActivity ACTIVE = new FacetActivity() {
16 @Override 24 @Override
39 47
40 /** Singleton registry, that maps artifact names to the activities, which 48 /** Singleton registry, that maps artifact names to the activities, which
41 * decide whether or not a facet should be (initially) active. */ 49 * decide whether or not a facet should be (initially) active. */
42 public static final class Registry { 50 public static final class Registry {
43 51
52 /** The logger for this class. */
53 private static Logger logger = Logger.getLogger(Registry.class);
54
44 /** Singleton instance. */ 55 /** Singleton instance. */
45 private static final Registry INSTANCE = new Registry(); 56 private static final Registry INSTANCE = new Registry();
46 57
58 /** Map of keys (artifact names) to the activities. */
47 private Map<String, List<FacetActivity>> activities; 59 private Map<String, List<FacetActivity>> activities;
48 60
49 /** Private singleton constructor for the Facet-Activity-Registry. */ 61 /** Private singleton constructor for the Facet-Activity-Registry. */
50 private Registry() { 62 private Registry() {
51 activities = new HashMap<String, List<FacetActivity>>(); 63 activities = new HashMap<String, List<FacetActivity>>();
63 Facet facet, 75 Facet facet,
64 String output 76 String output
65 ) { 77 ) {
66 List<FacetActivity> activityList = activities.get(key); 78 List<FacetActivity> activityList = activities.get(key);
67 if (activityList == null) { 79 if (activityList == null) {
80 logger.debug("FacetActivity.Registry: No activity "
81 "registered for " + key);
68 return true; 82 return true;
83 }
84 if (activityList.size() != 1) {
85 logger.warn("FacetActivity.Registry: More than one "
86 "FacetActivity registered for " + key);
69 } 87 }
70 for (FacetActivity activity: activityList) { 88 for (FacetActivity activity: activityList) {
71 Boolean isActive = 89 Boolean isActive =
72 activity.isInitialActive(artifact, facet, output); 90 activity.isInitialActive(artifact, facet, output);
91 // Nice. Only, in practice they never return NULL.
73 if (isActive != null) { 92 if (isActive != null) {
74 return isActive; 93 return isActive;
75 } 94 }
76 } 95 }
77 return true; 96 return true;
78 } 97 }
79 98
99
100 /** Add a FacetActivity under given key (usually artifacts name). */
80 public synchronized void register(String key, FacetActivity activity) { 101 public synchronized void register(String key, FacetActivity activity) {
81 List<FacetActivity> activityList = activities.get(key); 102 List<FacetActivity> activityList = activities.get(key);
82 if (activityList == null) { 103 if (activityList == null) {
83 activityList = new ArrayList<FacetActivity>(3); 104 activityList = new ArrayList<FacetActivity>(3);
84 activities.put(key, activityList); 105 activities.put(key, activityList);

http://dive4elements.wald.intevation.org