5
5
import org .slf4j .LoggerFactory ;
6
6
import org .springframework .beans .factory .annotation .Value ;
7
7
import org .springframework .stereotype .Repository ;
8
+ import ru .qatools .beanloader .BeanChangeListener ;
9
+ import ru .qatools .beanloader .BeanLoader ;
10
+ import ru .qatools .beanloader .BeanWatcher ;
8
11
import ru .qatools .gridrouter .config .Browser ;
9
12
import ru .qatools .gridrouter .config .Browsers ;
10
13
import ru .qatools .gridrouter .config .Version ;
11
14
import ru .qatools .gridrouter .json .JsonCapabilities ;
12
15
13
16
import javax .annotation .PostConstruct ;
14
- import javax .annotation .PreDestroy ;
15
- import javax .xml .bind .JAXB ;
16
- import javax .xml .bind .JAXBException ;
17
17
import java .io .File ;
18
18
import java .io .IOException ;
19
- import java .nio .file .DirectoryStream ;
20
19
import java .nio .file .Path ;
21
20
import java .util .HashMap ;
22
21
import java .util .Map ;
23
22
24
- import static java .nio .file .Files .newDirectoryStream ;
25
- import static ru .qatools .gridrouter .utils .DirectoryWatcher .newWatcher ;
26
-
27
23
/**
28
24
* @author Alexander Andyashin [email protected]
29
25
* @author Dmitry Baev [email protected]
30
26
* @author Innokenty Shuvalov [email protected]
31
27
*/
32
28
@ Repository
33
- public class ConfigRepository {
29
+ public class ConfigRepository implements BeanChangeListener < Browsers > {
34
30
35
31
private static final Logger LOGGER = LoggerFactory .getLogger (ConfigRepository .class );
36
32
37
- public static final String XML_GLOB = "*.xml" ;
33
+ private static final String XML_GLOB = "*.xml" ;
38
34
39
35
@ Value ("${grid.config.quota.directory}" )
40
36
private File quotaDirectory ;
@@ -46,117 +42,51 @@ public class ConfigRepository {
46
42
47
43
private Map <String , String > routes = new HashMap <>();
48
44
49
- private Thread quotaWatcherThread ;
50
-
51
45
@ PostConstruct
52
- public void init () throws JAXBException , IOException {
53
- initBrowsers (getQuotaPath ());
54
- if (isQuotaHotReload ()) {
55
- startQuotaWatcher ();
56
- }
57
- }
58
-
59
- @ PreDestroy
60
- public void destroy () {
61
- if (isQuotaHotReload ()) {
62
- stopQuotaWatcher ();
63
- }
64
- }
65
-
66
- private void startQuotaWatcher () {
67
- LOGGER .debug ("Starting quota watcher" );
68
- setQuotaWatcherThread (newWatcher (getQuotaPath (), "glob:" + XML_GLOB , (kind , browserPath ) -> {
69
- LOGGER .info ("Reload configuration [{}] on event [{}]" , browserPath , kind .name ());
70
- initBrowsers (getQuotaPath ());
71
- }));
72
- getQuotaWatcherThread ().start ();
73
-
74
- }
75
-
76
- private void stopQuotaWatcher () {
77
- LOGGER .debug ("Stopping quota watcher" );
78
- if (getQuotaWatcherThread () != null && getQuotaWatcherThread ().isAlive ()) {
79
- getQuotaWatcherThread ().interrupt ();
80
- }
81
- }
82
-
83
- public void initBrowsers (Path quotaPath ) {
84
- Map <String , Browsers > temporaryUserBrowsers = new HashMap <>(getUserBrowsers ());
85
- Map <String , String > temporaryRoutes = new HashMap <>(getRoutes ());
86
- try (DirectoryStream <Path > stream = newDirectoryStream (quotaPath , XML_GLOB )) {
87
- for (Path browsersPath : stream ) {
88
- LOGGER .info ("Load configuration from [{}]" , browsersPath );
89
- String user = getFileName (browsersPath );
90
- try {
91
- Browsers browsers = JAXB .unmarshal (browsersPath .toFile (), Browsers .class );
92
- temporaryUserBrowsers .put (user , browsers );
93
- temporaryRoutes .putAll (browsers .getRoutesMap ());
94
-
95
- LOGGER .info ("Loaded configuration for [{}] from [{}]: \n \n {}" ,
96
- user , browsersPath , browsers .toXml ());
97
- } catch (Exception e ) {
98
- LOGGER .error ("Loaded configuration failed for [{}]: \n \n {}" , browsersPath , e );
99
- }
46
+ public void init () {
47
+ try {
48
+ if (quotaHotReload ) {
49
+ LOGGER .debug ("Starting quota watcher" );
50
+ BeanWatcher .watchFor (Browsers .class , quotaDirectory .toPath (), XML_GLOB , this );
51
+ } else {
52
+ LOGGER .debug ("Loading quota configuration" );
53
+ BeanLoader .loadAll (Browsers .class , quotaDirectory .toPath (), XML_GLOB , this );
100
54
}
101
- setUserBrowsers (temporaryUserBrowsers );
102
- setRoutes (temporaryRoutes );
103
55
} catch (IOException e ) {
104
- LOGGER .error ("Loaded configuration failed: \n \n {} " , e );
56
+ LOGGER .error ("Quota configuration loading failed " , e );
105
57
}
106
58
}
107
59
108
- protected boolean isQuotaHotReload () {
109
- return quotaHotReload ;
110
- }
111
-
112
- protected Path getQuotaPath () {
113
- return quotaDirectory .toPath ();
60
+ @ Override
61
+ public void beanChanged (Path filename , Browsers browsers ) {
62
+ if (browsers == null ) {
63
+ LOGGER .info ("Configuration file [{}] was deleted. "
64
+ + "It is not purged from the running gridrouter process though." , filename );
65
+ } else {
66
+ LOGGER .info ("Loading quota configuration file [{}]" , filename );
67
+ String user = FilenameUtils .getBaseName (filename .toString ());
68
+ userBrowsers .put (user , browsers );
69
+ routes .putAll (browsers .getRoutesMap ());
70
+ LOGGER .info ("Loaded quota configuration for [{}] from [{}]: \n \n {}" ,
71
+ user , filename , browsers .toXml ());
72
+ }
114
73
}
115
74
116
75
public Map <String , String > getRoutes () {
117
76
return routes ;
118
77
}
119
78
120
- protected void setRoutes (Map <String , String > routes ) {
121
- this .routes = routes ;
122
- }
123
-
124
- public Map <String , Browsers > getUserBrowsers () {
125
- return this .userBrowsers ;
126
- }
127
-
128
- protected Browsers getUserBrowsers (String user ) {
129
- return getUserBrowsers ().get (user );
130
- }
131
-
132
- protected void setUserBrowsers (Map <String , Browsers > userBrowsers ) {
133
- this .userBrowsers = userBrowsers ;
134
- }
135
-
136
79
public Version findVersion (String user , JsonCapabilities caps ) {
137
80
return userBrowsers .get (user ).find (caps .getBrowserName (), caps .getVersion ());
138
81
}
139
82
140
- private static String getFileName (Path path ) {
141
- return FilenameUtils .getBaseName (path .toString ());
142
- }
143
-
144
83
public Map <String , Integer > getBrowsersCountMap (String user ) {
145
84
HashMap <String , Integer > countMap = new HashMap <>();
146
- for (Browser browser : getUserBrowsers (user ).getBrowsers ()) {
85
+ for (Browser browser : this . userBrowsers . get (user ).getBrowsers ()) {
147
86
for (Version version : browser .getVersions ()) {
148
87
countMap .put (browser .getName () + ":" + version .getNumber (), version .getCount ());
149
88
}
150
89
}
151
90
return countMap ;
152
91
}
153
-
154
- public Thread getQuotaWatcherThread () {
155
- return quotaWatcherThread ;
156
- }
157
-
158
- public void setQuotaWatcherThread (Thread quotaWatcherThread ) {
159
- this .quotaWatcherThread = quotaWatcherThread ;
160
- }
161
-
162
92
}
0 commit comments