1
1
/*
2
- * Copyright 2015-2016 the original author or authors.
2
+ * Copyright 2015-2017 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
48
48
import org .springframework .statemachine .state .State ;
49
49
import org .springframework .statemachine .support .StateMachineUtils ;
50
50
import org .springframework .util .Assert ;
51
+ import org .springframework .util .StringUtils ;
51
52
52
53
/**
53
54
* Helper class which is used from a StateMachineObjectSupport to ease handling
@@ -84,14 +85,15 @@ public void afterPropertiesSet() throws Exception {
84
85
Annotation metaAnnotation = handler .getMetaAnnotation ();
85
86
WithStateMachine withStateMachine = AnnotationUtils .findAnnotation (handler .getBeanClass (),
86
87
WithStateMachine .class );
87
- String statemachineBeanName = withStateMachine .name ();
88
- String key = metaAnnotation .annotationType ().getName () + statemachineBeanName ;
89
- List <CacheEntry > list = cache .get (key );
90
- if (list == null ) {
91
- list = new ArrayList <>();
92
- cache .put (key , list );
88
+
89
+ if (StringUtils .hasText (withStateMachine .name ())) {
90
+ updateCache (metaAnnotation .annotationType ().getName () + withStateMachine .name (),
91
+ new CacheEntry (handler , annotation , metaAnnotation ));
92
+ }
93
+ if (StringUtils .hasText (withStateMachine .id ())) {
94
+ updateCache (metaAnnotation .annotationType ().getName () + withStateMachine .id (),
95
+ new CacheEntry (handler , annotation , metaAnnotation ));
93
96
}
94
- list .add (new CacheEntry (handler , annotation , metaAnnotation ));
95
97
}
96
98
}
97
99
@@ -103,6 +105,9 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
103
105
}
104
106
105
107
public void callOnStateChanged (String stateMachineId , StateContext <S , E > stateContext ) {
108
+ if (!StringUtils .hasText (stateMachineId )) {
109
+ return ;
110
+ }
106
111
List <StateMachineHandler <? extends Annotation , S , E >> handlersList = new ArrayList <StateMachineHandler <? extends Annotation , S , E >>();
107
112
String cacheKey = OnStateChanged .class .getName () + stateMachineId ;
108
113
List <CacheEntry > list = getCacheEntries (cacheKey );
@@ -120,6 +125,9 @@ public void callOnStateChanged(String stateMachineId, StateContext<S, E> stateCo
120
125
}
121
126
122
127
public void callOnStateEntry (String stateMachineId , StateContext <S , E > stateContext ) {
128
+ if (!StringUtils .hasText (stateMachineId )) {
129
+ return ;
130
+ }
123
131
List <StateMachineHandler <? extends Annotation , S , E >> handlersList = new ArrayList <StateMachineHandler <? extends Annotation , S , E >>();
124
132
String cacheKey = OnStateEntry .class .getName () + stateMachineId ;
125
133
List <CacheEntry > list = getCacheEntries (cacheKey );
@@ -137,6 +145,9 @@ public void callOnStateEntry(String stateMachineId, StateContext<S, E> stateCont
137
145
}
138
146
139
147
public void callOnStateExit (String stateMachineId , StateContext <S , E > stateContext ) {
148
+ if (!StringUtils .hasText (stateMachineId )) {
149
+ return ;
150
+ }
140
151
List <StateMachineHandler <? extends Annotation , S , E >> handlersList = new ArrayList <StateMachineHandler <? extends Annotation , S , E >>();
141
152
String cacheKey = OnStateExit .class .getName () + stateMachineId ;
142
153
List <CacheEntry > list = getCacheEntries (cacheKey );
@@ -154,6 +165,9 @@ public void callOnStateExit(String stateMachineId, StateContext<S, E> stateConte
154
165
}
155
166
156
167
public void callOnEventNotAccepted (String stateMachineId , StateContext <S , E > stateContext ) {
168
+ if (!StringUtils .hasText (stateMachineId )) {
169
+ return ;
170
+ }
157
171
List <StateMachineHandler <? extends Annotation , S , E >> handlersList = new ArrayList <StateMachineHandler <? extends Annotation , S , E >>();
158
172
String cacheKey = OnEventNotAccepted .class .getName () + stateMachineId ;
159
173
List <CacheEntry > list = getCacheEntries (cacheKey );
@@ -175,6 +189,9 @@ public void callOnEventNotAccepted(String stateMachineId, StateContext<S, E> sta
175
189
176
190
177
191
public void callOnTransitionStart (String stateMachineId , StateContext <S , E > stateContext ) {
192
+ if (!StringUtils .hasText (stateMachineId )) {
193
+ return ;
194
+ }
178
195
List <StateMachineHandler <? extends Annotation , S , E >> handlersList = new ArrayList <StateMachineHandler <? extends Annotation , S , E >>();
179
196
String cacheKey = OnTransitionStart .class .getName () + stateMachineId ;
180
197
List <CacheEntry > list = getCacheEntries (cacheKey );
@@ -192,6 +209,9 @@ public void callOnTransitionStart(String stateMachineId, StateContext<S, E> stat
192
209
}
193
210
194
211
public void callOnTransition (String stateMachineId , StateContext <S , E > stateContext ) {
212
+ if (!StringUtils .hasText (stateMachineId )) {
213
+ return ;
214
+ }
195
215
List <StateMachineHandler <? extends Annotation , S , E >> handlersList = new ArrayList <StateMachineHandler <? extends Annotation , S , E >>();
196
216
String cacheKey = OnTransition .class .getName () + stateMachineId ;
197
217
List <CacheEntry > list = getCacheEntries (cacheKey );
@@ -209,6 +229,9 @@ public void callOnTransition(String stateMachineId, StateContext<S, E> stateCont
209
229
}
210
230
211
231
public void callOnTransitionEnd (String stateMachineId , StateContext <S , E > stateContext ) {
232
+ if (!StringUtils .hasText (stateMachineId )) {
233
+ return ;
234
+ }
212
235
List <StateMachineHandler <? extends Annotation , S , E >> handlersList = new ArrayList <StateMachineHandler <? extends Annotation , S , E >>();
213
236
String cacheKey = OnTransitionEnd .class .getName () + stateMachineId ;
214
237
List <CacheEntry > list = getCacheEntries (cacheKey );
@@ -226,6 +249,9 @@ public void callOnTransitionEnd(String stateMachineId, StateContext<S, E> stateC
226
249
}
227
250
228
251
public void callOnStateMachineStart (String stateMachineId , StateContext <S , E > stateContext ) {
252
+ if (!StringUtils .hasText (stateMachineId )) {
253
+ return ;
254
+ }
229
255
List <StateMachineHandler <? extends Annotation , S , E >> handlersList = new ArrayList <StateMachineHandler <? extends Annotation , S , E >>();
230
256
String cacheKey = OnStateMachineStart .class .getName () + stateMachineId ;
231
257
List <CacheEntry > list = getCacheEntries (cacheKey );
@@ -239,6 +265,9 @@ public void callOnStateMachineStart(String stateMachineId, StateContext<S, E> st
239
265
}
240
266
241
267
public void callOnStateMachineStop (String stateMachineId , StateContext <S , E > stateContext ) {
268
+ if (!StringUtils .hasText (stateMachineId )) {
269
+ return ;
270
+ }
242
271
List <StateMachineHandler <? extends Annotation , S , E >> handlersList = new ArrayList <StateMachineHandler <? extends Annotation , S , E >>();
243
272
String cacheKey = OnStateMachineStop .class .getName () + stateMachineId ;
244
273
List <CacheEntry > list = getCacheEntries (cacheKey );
@@ -252,6 +281,9 @@ public void callOnStateMachineStop(String stateMachineId, StateContext<S, E> sta
252
281
}
253
282
254
283
public void callOnStateMachineError (String stateMachineId , StateContext <S , E > stateContext ) {
284
+ if (!StringUtils .hasText (stateMachineId )) {
285
+ return ;
286
+ }
255
287
List <StateMachineHandler <? extends Annotation , S , E >> handlersList = new ArrayList <StateMachineHandler <? extends Annotation , S , E >>();
256
288
String cacheKey = OnStateMachineError .class .getName () + stateMachineId ;
257
289
List <CacheEntry > list = getCacheEntries (cacheKey );
@@ -265,6 +297,9 @@ public void callOnStateMachineError(String stateMachineId, StateContext<S, E> st
265
297
}
266
298
267
299
public void callOnExtendedStateChanged (String stateMachineId , Object key , Object value , StateContext <S , E > stateContext ) {
300
+ if (!StringUtils .hasText (stateMachineId )) {
301
+ return ;
302
+ }
268
303
List <StateMachineHandler <? extends Annotation , S , E >> handlersList = new ArrayList <StateMachineHandler <? extends Annotation , S , E >>();
269
304
String cacheKey = OnExtendedStateChanged .class .getName () + stateMachineId ;
270
305
List <CacheEntry > list = getCacheEntries (cacheKey );
@@ -279,6 +314,15 @@ public void callOnExtendedStateChanged(String stateMachineId, Object key, Object
279
314
getStateMachineHandlerResults (handlersList , stateContext );
280
315
}
281
316
317
+ private void updateCache (String key , CacheEntry cacheEntry ) {
318
+ List <CacheEntry > list = cache .get (key );
319
+ if (list == null ) {
320
+ list = new ArrayList <>();
321
+ cache .put (key , list );
322
+ }
323
+ list .add (cacheEntry );
324
+ }
325
+
282
326
private synchronized List <CacheEntry > getCacheEntries (String cacheKey ) {
283
327
if (stateMachineHandlerApplicationListener != null ) {
284
328
Long l = stateMachineHandlerApplicationListener .getLastRefreshTime ();
0 commit comments