##### Coverage Report - com.ctrip.apollo.internals.AbstractConfig

Classes in this File | Line Coverage | Branch Coverage | Complexity
AbstractConfig |
62% | 33/53
40% | 12/30
| 2.6;2.6

1 |  |
     package com.ctrip.apollo.internals;
2 |  |
3 |  |
     import com.google.common.base.Objects;
4 |  |
     import com.google.common.collect.Lists;
5 |  |
     import com.google.common.collect.Sets;
6 |  |
7 |  |
     import com.ctrip.apollo.Config;
8 |  |
     import com.ctrip.apollo.ConfigChangeListener;
9 |  |
     import com.ctrip.apollo.enums.PropertyChangeType;
10 |  |
     import com.ctrip.apollo.model.ConfigChange;
11 |  |
     import com.ctrip.apollo.model.ConfigChangeEvent;
12 |  |
     import com.dianping.cat.Cat;
13 |  |
14 |  |
     import org.slf4j.Logger;
15 |  |
     import org.slf4j.LoggerFactory;
16 |  |
17 |  |
     import java.util.List;
18 |  |
     import java.util.Properties;
19 |  |
     import java.util.Set;
20 |  |
21 |  |
     /**
22 |  |
      * @author Jason Song(song_s@ctrip.com)
23 |  |
      */
24 | 20 |
     public abstract class AbstractConfig implements Config {
25 | 1 |
       private static final Logger logger = LoggerFactory.getLogger(AbstractConfig.class);
26 | 20 |
       private List<ConfigChangeListener> m_listeners = Lists.newCopyOnWriteArrayList();
27 |  |
28 |  |
       @Override
29 |  |
       public void addChangeListener(ConfigChangeListener listener) {
30 | 4 |
         if (!m_listeners.contains(listener)) {
31 | 4 |
           m_listeners.add(listener);
32 |  |
         }
33 | 4 |
       }
34 |  |
35 |  |
       @Override
36 |  |
       public Integer getIntProperty(String key, Integer defaultValue) {
37 | 0 |
         String value = getProperty(key, null);
38 | 0 |
         return value == null ? defaultValue : Integer.parseInt(value);
39 |  |
       }
40 |  |
41 |  |
       @Override
42 |  |
       public Long getLongProperty(String key, Long defaultValue) {
43 | 0 |
         String value = getProperty(key, null);
44 | 0 |
         return value == null ? defaultValue : Long.parseLong(value);
45 |  |
       }
46 |  |
47 |  |
       @Override
48 |  |
       public Short getShortProperty(String key, Short defaultValue) {
49 | 0 |
         String value = getProperty(key, null);
50 | 0 |
         return value == null ? defaultValue : Short.parseShort(value);
51 |  |
       }
52 |  |
53 |  |
       @Override
54 |  |
       public Float getFloatProperty(String key, Float defaultValue) {
55 | 0 |
         String value = getProperty(key, null);
56 | 0 |
         return value == null ? defaultValue : Float.parseFloat(value);
57 |  |
       }
58 |  |
59 |  |
       @Override
60 |  |
       public Double getDoubleProperty(String key, Double defaultValue) {
61 | 0 |
         String value = getProperty(key, null);
62 | 0 |
         return value == null ? defaultValue : Double.parseDouble(value);
63 |  |
       }
64 |  |
65 |  |
       @Override
66 |  |
       public Byte getByteProperty(String key, Byte defaultValue) {
67 | 0 |
         String value = getProperty(key, null);
68 | 0 |
         return value == null ? defaultValue : Byte.parseByte(value);
69 |  |
       }
70 |  |
71 |  |
       @Override
72 |  |
       public Boolean getBooleanProperty(String key, Boolean defaultValue) {
73 | 0 |
         String value = getProperty(key, null);
74 | 0 |
         return value == null ? defaultValue : Boolean.parseBoolean(value);
75 |  |
       }
76 |  |
77 |  |
       protected void fireConfigChange(ConfigChangeEvent changeEvent) {
78 | 5 |
         for (ConfigChangeListener listener : m_listeners) {
79 |  |
           try {
80 | 5 |
             listener.onChange(changeEvent);
81 | 0 |
           } catch (Throwable ex) {
82 | 0 |
             Cat.logError(ex);
83 | 0 |
             logger.error("Failed to invoke config change listener {}", listener.getClass(), ex);
84 | 5 |
           }
85 | 5 |
         }
86 | 5 |
       }
87 |  |
88 |  |
       List<ConfigChange> calcPropertyChanges(String namespace,
89 |  |
                                              Properties previous,
90 |  |
                                              Properties current) {
91 | 5 |
         if (previous == null) {
92 | 0 |
           previous = new Properties();
93 |  |
         }
94 |  |
95 | 5 |
         if (current == null) {
96 | 0 |
           current = new Properties();
97 |  |
         }
98 |  |
99 | 5 |
         Set<String> previousKeys = previous.stringPropertyNames();
100 | 5 |
         Set<String> currentKeys = current.stringPropertyNames();
101 |  |
102 | 5 |
         Set<String> commonKeys = Sets.intersection(previousKeys, currentKeys);
103 | 5 |
         Set<String> newKeys = Sets.difference(currentKeys, commonKeys);
104 | 5 |
         Set<String> removedKeys = Sets.difference(previousKeys, commonKeys);
105 |  |
106 | 5 |
         List<ConfigChange> changes = Lists.newArrayList();
107 |  |
108 | 5 |
         for (String newKey : newKeys) {
109 | 2 |
           changes
110 | 2 |
               .add(new ConfigChange(namespace, newKey, null, current.getProperty(newKey),
111 |  |
                   PropertyChangeType.ADDED));
112 | 2 |
         }
113 |  |
114 | 5 |
         for (String removedKey : removedKeys) {
115 | 3 |
           changes.add(new ConfigChange(namespace, removedKey, previous.getProperty(removedKey), null,
116 |  |
               PropertyChangeType.DELETED));
117 | 3 |
         }
118 |  |
119 | 5 |
         for (String commonKey : commonKeys) {
120 | 6 |
           String previousValue = previous.getProperty(commonKey);
121 | 6 |
           String currentValue = current.getProperty(commonKey);
122 | 6 |
           if (Objects.equal(previousValue, currentValue)) {
123 | 0 |
             continue;
124 |  |
           }
125 | 6 |
           changes.add(new ConfigChange(namespace, commonKey, previousValue,
126 |  |
               currentValue, PropertyChangeType.MODIFIED));
127 | 6 |
         }
128 |  |
129 | 5 |
         return changes;
130 |  |
       }
131 |  |
     }