1
1
component singleton accessors = " true" {
2
2
3
+ property name = " settings" inject = " coldbox:moduleSettings:unleashsdk" ;
3
4
property name = " client" inject = " UnleashHyperClient@unleashsdk" ;
4
- property name = " cache" inject = " cachebox:default" ;
5
5
property name = " log" inject = " logbox:logger:{this}" ;
6
+ property name = " cache" inject = " cachebox:default" ;
7
+ property name = " wirebox" inject = " wirebox" ;
6
8
7
9
variables .strategies = {
8
- " default" : " DefaultStrategy" ,
9
- " userWithId" : " UserWithIdStrategy" ,
10
- " flexibleRollout" : " FlexibleRolloutStrategy" ,
11
- " remoteAddress" : " RemoteAddressStrategy" ,
12
- " applicationHostname" : " ApplicationHostnameStrategy"
10
+ " default" : " DefaultStrategy@unleashsdk " ,
11
+ " userWithId" : " UserWithIdStrategy@unleashsdk " ,
12
+ " flexibleRollout" : " FlexibleRolloutStrategy@unleashsdk " ,
13
+ " remoteAddress" : " RemoteAddressStrategy@unleashsdk " ,
14
+ " applicationHostname" : " ApplicationHostnameStrategy@unleashsdk "
13
15
};
14
16
15
17
public boolean function isEnabled ( required string name , boolean defaultValue = false ) {
16
- var feature = findFeature ( arguments .name );
18
+ var feature = getFeature ( arguments .name );
17
19
if ( isNull ( feature ) ) {
18
20
return arguments .defaultValue ;
19
21
}
@@ -29,27 +31,31 @@ component singleton accessors="true" {
29
31
}
30
32
31
33
param strategyData .constraints = [];
32
- if ( ! strategy .satisfiesConstraints ( strategyData .constraints ) ) {
34
+ if ( ! strategy .satisfiesConstraints ( strategyData .constraints , getContext () ) ) {
33
35
continue ;
34
36
}
35
37
36
38
param strategyData .parameters = {};
37
- if ( ! strategy .isEnabled ( strategyData .parameters ) ) {
39
+ if ( ! strategy .isEnabled ( strategyData .parameters , getContext () ) ) {
38
40
return false ;
39
41
}
40
42
}
41
43
42
44
return true ;
43
45
}
44
46
47
+ public boolean function isDisabled ( required string name , boolean defaultValue = false ) {
48
+ return ! isEnabled ( argumentCollection = arguments );
49
+ }
50
+
45
51
private any function getStrategy ( required string name ) {
46
52
if ( ! variables .strategies .keyExists ( arguments .name ) ) {
47
53
log .warn ( " No Unleash strategy found for [#arguments .name #]" );
48
54
return javacast ( " null" , " " );
49
55
}
50
56
51
57
if ( isSimpleValue ( variables .strategies [ arguments .name ] ) ) {
52
- variables .strategies [ arguments .name ] = new " unleashsdk.models.strategies.# variables .strategies [ arguments .name ]# " ( );
58
+ variables .strategies [ arguments .name ] = wirebox . getInstance ( variables .strategies [ arguments .name ] );
53
59
}
54
60
55
61
return variables .strategies [ arguments .name ];
@@ -81,29 +87,52 @@ component singleton accessors="true" {
81
87
boolean enabled = true ,
82
88
array strategies = []
83
89
) {
84
- var feature = findFeature ( arguments .name );
90
+ var feature = getFeature ( arguments .name );
85
91
if ( ! isNull ( feature ) ) {
86
92
return feature ;
87
93
}
88
94
return createFeature ( argumentCollection = arguments );
89
95
}
90
96
91
- private any function findFeature ( required string name ) {
92
- return arrayFindFirst ( fetchFeatures (), function ( feature ) {
97
+ public any function getFeature ( required string name ) {
98
+ return arrayFindFirst ( getFeatures (), function ( feature ) {
93
99
return feature .name == name ;
94
100
} );
95
101
}
96
102
97
103
public array function getFeatures () {
98
- return cache .getOrSet ( " unleashsdk-features" , function () {
99
- return fetchFeatures ();
100
- }, createTimespan ( 0 , 0 , 10 , 0 ) );
104
+ try {
105
+ return cache .getOrSet ( " unleashsdk-features" , function () {
106
+ var features = fetchFeatures ();
107
+ cache .set ( " unleashsdk-failover" , features , 0 );
108
+ return features ;
109
+ }, variables .settings .cacheTimeout );
110
+ } catch ( any e ) {
111
+ if ( log .canError () ) {
112
+ log .error ( " Exception occurred while retrieving Unleash features. Using failover" , e );
113
+ }
114
+ var features = cache .get ( " unleashsdk-failover" );
115
+ if ( isNull ( features ) ) {
116
+ return [];
117
+ }
118
+ return features ;
119
+ }
101
120
}
102
121
103
122
private array function fetchFeatures () {
104
123
return variables .client .get ( " /client/features" ).json ().features ;
105
124
}
106
125
126
+ private struct function getContext () {
127
+ param request .unleashContext = generateContext ();
128
+ return request .unleashContext ;
129
+ }
130
+
131
+ private struct function generateContext () {
132
+ var contextProvider = wirebox .getInstance ( variables .settings .contextProvider );
133
+ return contextProvider .getContext ();
134
+ }
135
+
107
136
private any function arrayFindFirst ( required array items , required function predicate ) {
108
137
for ( var item in arguments .items ) {
109
138
if ( arguments .predicate ( item ) ) {
0 commit comments