ConnectionRepository acts as local cache of a plug-in because it keeps all live connections. Any read operation that is related to a live connection must pass through the ConnectionRepository interface.
/* * The ConnectionRepository implements the ConfigurationChangeListener, because we want to be subscribed to all * changes related to configurations. ApplicationContextAware and InitializingBean are spring-related interfaces. */ @Component public class ConnectionRepository implements ApplicationContextAware, InitializingBean, ConfigurationChangeListener { /* * Injecting the ConnectionPersister */ @Autowired private ConnectionPersister persister; private ApplicationContext context; /* * The local map (cache) of live connections */ private final Map<Sid, Connection> connections; public ConnectionRepository() { connections = new ConcurrentHashMap<Sid, Connection>(); } /* * The public interface returns a live connection by its ID */ public Connection findLiveConnection(Sid anyId) { return connections.get(anyId.getId()); } /* * The public interface returns all live connections from the local cache */ public Collection<Connection> findAll() { return connections.values(); } /* * Spring-specifics - storing a reference to the spring context */ @Override public void setApplicationContext(ApplicationContext context) throws BeansException { this.context = context; } /* * Spring specifics - this method is being called automatically by the spring container * after all the fields are set and before the bean is being provided for usage. * This method will be called when the plug-in is being loaded - on server start-up. */ @Override public void afterPropertiesSet() throws Exception { //Subscribing the Repository for any configuration changes that occur in the Persister persister.addChangeListener(this); //Initializing the Persister. By doing that, the persister will invoke the connectionUpdated() method //and since we are subscribed to those events, the local cache will be populated with all the available connections. persister.load(); } private Connection createConnection(ConnectionInfo info) { //This call will create a new spring-managed bean from the context return (Connection) context.getBean("connection", info); } /* * This method will be called from the ConnectionPersister when a new connection * is added or an existing one is updated. */ @Override public void connectionUpdated(ConnectionInfo info) { Connection live = connections.get(info.getId()); if (live != null) { live.update(info); } else { // connection just added, create it live = createConnection(info); connections.put(info.getId(), live); } } /* * This method will be called from the ConnectionPersister when a connection * is removed. */ @Override public void connectionRemoved(ConnectionInfo info) { Connection live = connections.remove(info.getId()); if (live != null) { live.destroy(); } } }
The difference between ConnectionPersister and ConnectionRepository is that the persister manages only the ConnectionInfo POJOs that remain behind the live connections, whereas ConnectionRepository provides the same instance of a third-party connection. In other words, ConnectionRepository handles connections and ConnectionPersister handles ConnectionInfo objects.
ConnectionRepository provides two methods - findLiveConnection(..) and findAll(..). If you want to edit or delete a connection from the repository, you use the ConnectionPersister interface.
When the persister updates or deletes a connection, the change propagates to the repository because ConnectionRepository is subscribed to update or delete events that ConnectionPersister handles.