Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add client side auto refresh capability
- Loading branch information
Showing
27 changed files
with
719 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
apollo-client/src/main/java/com/ctrip/apollo/ConfigChangeListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.ctrip.apollo; | ||
|
||
import com.ctrip.apollo.model.ConfigChangeEvent; | ||
|
||
/** | ||
* @author Jason Song(song_s@ctrip.com) | ||
*/ | ||
public interface ConfigChangeListener { | ||
public void onChange(ConfigChangeEvent changeEvent); | ||
} |
84 changes: 84 additions & 0 deletions
84
apollo-client/src/main/java/com/ctrip/apollo/internals/AbstractConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.ctrip.apollo.internals; | ||
|
||
import com.google.common.base.Objects; | ||
import com.google.common.collect.Lists; | ||
import com.google.common.collect.Sets; | ||
|
||
import com.ctrip.apollo.Config; | ||
import com.ctrip.apollo.ConfigChangeListener; | ||
import com.ctrip.apollo.enums.PropertyChangeType; | ||
import com.ctrip.apollo.model.ConfigChange; | ||
import com.ctrip.apollo.model.ConfigChangeEvent; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.List; | ||
import java.util.Properties; | ||
import java.util.Set; | ||
|
||
/** | ||
* @author Jason Song(song_s@ctrip.com) | ||
*/ | ||
public abstract class AbstractConfig implements Config { | ||
private static final Logger logger = LoggerFactory.getLogger(AbstractConfig.class); | ||
private List<ConfigChangeListener> m_listeners = Lists.newCopyOnWriteArrayList(); | ||
|
||
@Override | ||
public void addChangeListener(ConfigChangeListener listener) { | ||
if (!m_listeners.contains(listener)) { | ||
m_listeners.add(listener); | ||
} | ||
} | ||
|
||
protected void fireConfigChange(ConfigChangeEvent changeEvent) { | ||
for (ConfigChangeListener listener : m_listeners) { | ||
try { | ||
listener.onChange(changeEvent); | ||
} catch (Throwable t) { | ||
logger.error("Failed to invoke config change listener {}", listener.getClass(), t); | ||
} | ||
} | ||
} | ||
|
||
List<ConfigChange> calcPropertyChanges(Properties previous, | ||
Properties current) { | ||
if (previous == null) { | ||
previous = new Properties(); | ||
} | ||
|
||
if (current == null) { | ||
current = new Properties(); | ||
} | ||
|
||
Set<String> previousKeys = previous.stringPropertyNames(); | ||
Set<String> currentKeys = current.stringPropertyNames(); | ||
|
||
Set<String> commonKeys = Sets.intersection(previousKeys, currentKeys); | ||
Set<String> newKeys = Sets.difference(currentKeys, commonKeys); | ||
Set<String> removedKeys = Sets.difference(previousKeys, commonKeys); | ||
|
||
List<ConfigChange> changes = Lists.newArrayList(); | ||
|
||
for (String newKey : newKeys) { | ||
changes.add(new ConfigChange(newKey, null, current.getProperty(newKey), PropertyChangeType.NEW)); | ||
} | ||
|
||
for (String removedKey : removedKeys) { | ||
changes.add(new ConfigChange(removedKey, previous.getProperty(removedKey), null, | ||
PropertyChangeType.DELETED)); | ||
} | ||
|
||
for (String commonKey : commonKeys) { | ||
String previousValue = previous.getProperty(commonKey); | ||
String currentValue = current.getProperty(commonKey); | ||
if (Objects.equal(previousValue, currentValue)) { | ||
continue; | ||
} | ||
changes.add(new ConfigChange(commonKey, previousValue, | ||
currentValue, PropertyChangeType.MODIFIED)); | ||
} | ||
|
||
return changes; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
apollo-client/src/main/java/com/ctrip/apollo/internals/AbstractConfigRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.ctrip.apollo.internals; | ||
|
||
import com.google.common.collect.Lists; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.List; | ||
import java.util.Properties; | ||
|
||
/** | ||
* @author Jason Song(song_s@ctrip.com) | ||
*/ | ||
public abstract class AbstractConfigRepository implements ConfigRepository { | ||
private static final Logger logger = LoggerFactory.getLogger(AbstractConfigRepository.class); | ||
private List<RepositoryChangeListener> m_listeners = Lists.newCopyOnWriteArrayList(); | ||
|
||
@Override | ||
public void addChangeListener(RepositoryChangeListener listener) { | ||
if (!m_listeners.contains(listener)) { | ||
m_listeners.add(listener); | ||
} | ||
} | ||
|
||
@Override | ||
public void removeChangeListener(RepositoryChangeListener listener) { | ||
m_listeners.remove(listener); | ||
} | ||
|
||
protected void fireRepositoryChange(String namespace, Properties newProperties) { | ||
for (RepositoryChangeListener listener : m_listeners) { | ||
try { | ||
listener.onRepositoryChange(namespace, newProperties); | ||
} catch (Throwable t) { | ||
logger.error("Failed to invoke repository change listener {}", listener.getClass(), t); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.