| 1 | |
package com.ctrip.apollo.internals; |
| 2 | |
|
| 3 | |
import com.google.common.collect.Lists; |
| 4 | |
|
| 5 | |
import com.ctrip.apollo.util.ExceptionUtil; |
| 6 | |
import com.dianping.cat.Cat; |
| 7 | |
|
| 8 | |
import org.slf4j.Logger; |
| 9 | |
import org.slf4j.LoggerFactory; |
| 10 | |
|
| 11 | |
import java.util.List; |
| 12 | |
import java.util.Properties; |
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | 26 | public abstract class AbstractConfigRepository implements ConfigRepository { |
| 18 | 1 | private static final Logger logger = LoggerFactory.getLogger(AbstractConfigRepository.class); |
| 19 | 26 | private List<RepositoryChangeListener> m_listeners = Lists.newCopyOnWriteArrayList(); |
| 20 | |
|
| 21 | |
protected boolean trySync() { |
| 22 | |
try { |
| 23 | 43 | sync(); |
| 24 | 26 | return true; |
| 25 | 16 | } catch (Throwable ex) { |
| 26 | 16 | Cat.logError(ex); |
| 27 | 16 | logger |
| 28 | 32 | .warn("Sync config failed, will retry. Repository {}, reason: {}", this.getClass(), ExceptionUtil |
| 29 | 16 | .getDetailMessage(ex)); |
| 30 | |
} |
| 31 | 16 | return false; |
| 32 | |
} |
| 33 | |
|
| 34 | |
protected abstract void sync(); |
| 35 | |
|
| 36 | |
@Override |
| 37 | |
public void addChangeListener(RepositoryChangeListener listener) { |
| 38 | 19 | if (!m_listeners.contains(listener)) { |
| 39 | 19 | m_listeners.add(listener); |
| 40 | |
} |
| 41 | 19 | } |
| 42 | |
|
| 43 | |
@Override |
| 44 | |
public void removeChangeListener(RepositoryChangeListener listener) { |
| 45 | 0 | m_listeners.remove(listener); |
| 46 | 0 | } |
| 47 | |
|
| 48 | |
protected void fireRepositoryChange(String namespace, Properties newProperties) { |
| 49 | 25 | for (RepositoryChangeListener listener : m_listeners) { |
| 50 | |
try { |
| 51 | 16 | listener.onRepositoryChange(namespace, newProperties); |
| 52 | 0 | } catch (Throwable ex) { |
| 53 | 0 | Cat.logError(ex); |
| 54 | 0 | logger.error("Failed to invoke repository change listener {}", listener.getClass(), ex); |
| 55 | 16 | } |
| 56 | 16 | } |
| 57 | 25 | } |
| 58 | |
} |