-
Notifications
You must be signed in to change notification settings - Fork 43
/
PostConfiguringClusterListener.java
58 lines (49 loc) · 2.26 KB
/
PostConfiguringClusterListener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.kuprowski.helenos.context;
import com.kuprowski.helenos.ClusterConfiguration;
import com.kuprowski.helenos.jdbc.core.support.ClusterConfigDao;
import com.kuprowski.helenos.service.ClusterConfigAware;
import java.util.Map;
import me.prettyprint.hector.api.Cluster;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.web.context.support.XmlWebApplicationContext;
/**
* ********************************************************
* Copyright: 2012 Tomek Kuprowski
*
* License: GPLv2: http://www.gnu.org/licences/gpl.html
*
* @author Tomek Kuprowski (tomekkuprowski at gmail dot com)
* *******************************************************
*/
public class PostConfiguringClusterListener implements ApplicationListener<ContextRefreshedEvent> {
@Autowired
private ClusterConfigDao clusterConfigDao;
public PostConfiguringClusterListener() {
}
@Required
public void setClusterConfigDao(ClusterConfigDao clusterConfigDao) {
this.clusterConfigDao = clusterConfigDao;
}
private boolean isReady(ContextRefreshedEvent event) {
XmlWebApplicationContext context = (XmlWebApplicationContext) event.getSource();
return context.getServletConfig() != null;
}
public static void propagadeConfigChanges(ApplicationContext applicationContext, Cluster cluster) {
Map<String, ClusterConfigAware> beans = applicationContext.getBeansOfType(ClusterConfigAware.class);
for (ClusterConfigAware bean : beans.values()) {
bean.setNewCluster(cluster);
}
}
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if (isReady(event)) {
ApplicationContext applicationContext = event.getApplicationContext();
ClusterConfiguration configuration = clusterConfigDao.getActive();
propagadeConfigChanges(applicationContext.getParent(), configuration.createCluster());
}
}
}