28
28
import com .optimizely .ab .event .internal .*;
29
29
import com .optimizely .ab .event .internal .payload .EventBatch ;
30
30
import com .optimizely .ab .notification .*;
31
+ import com .optimizely .ab .odp .*;
31
32
import com .optimizely .ab .optimizelyconfig .OptimizelyConfig ;
32
33
import com .optimizely .ab .optimizelyconfig .OptimizelyConfigManager ;
33
34
import com .optimizely .ab .optimizelyconfig .OptimizelyConfigService ;
@@ -96,6 +97,9 @@ public class Optimizely implements AutoCloseable {
96
97
@ Nullable
97
98
private final UserProfileService userProfileService ;
98
99
100
+ @ Nullable
101
+ private final ODPManager odpManager ;
102
+
99
103
private Optimizely (@ Nonnull EventHandler eventHandler ,
100
104
@ Nonnull EventProcessor eventProcessor ,
101
105
@ Nonnull ErrorHandler errorHandler ,
@@ -104,7 +108,8 @@ private Optimizely(@Nonnull EventHandler eventHandler,
104
108
@ Nonnull ProjectConfigManager projectConfigManager ,
105
109
@ Nullable OptimizelyConfigManager optimizelyConfigManager ,
106
110
@ Nonnull NotificationCenter notificationCenter ,
107
- @ Nonnull List <OptimizelyDecideOption > defaultDecideOptions
111
+ @ Nonnull List <OptimizelyDecideOption > defaultDecideOptions ,
112
+ @ Nullable ODPManager odpManager
108
113
) {
109
114
this .eventHandler = eventHandler ;
110
115
this .eventProcessor = eventProcessor ;
@@ -115,6 +120,15 @@ private Optimizely(@Nonnull EventHandler eventHandler,
115
120
this .optimizelyConfigManager = optimizelyConfigManager ;
116
121
this .notificationCenter = notificationCenter ;
117
122
this .defaultDecideOptions = defaultDecideOptions ;
123
+ this .odpManager = odpManager ;
124
+
125
+ if (odpManager != null ) {
126
+ odpManager .getEventManager ().start ();
127
+ if (getProjectConfig () != null ) {
128
+ updateODPSettings ();
129
+ }
130
+ addUpdateConfigNotificationHandler (configNotification -> { updateODPSettings (); });
131
+ }
118
132
}
119
133
120
134
/**
@@ -128,8 +142,6 @@ public boolean isValid() {
128
142
return getProjectConfig () != null ;
129
143
}
130
144
131
-
132
-
133
145
/**
134
146
* Checks if eventHandler {@link EventHandler} and projectConfigManager {@link ProjectConfigManager}
135
147
* are Closeable {@link Closeable} and calls close on them.
@@ -141,6 +153,9 @@ public void close() {
141
153
tryClose (eventProcessor );
142
154
tryClose (eventHandler );
143
155
tryClose (projectConfigManager );
156
+ if (odpManager != null ) {
157
+ tryClose (odpManager );
158
+ }
144
159
}
145
160
146
161
//======== activate calls ========//
@@ -674,9 +689,9 @@ public OptimizelyJSON getFeatureVariableJSON(@Nonnull String featureKey,
674
689
*/
675
690
@ Nullable
676
691
public OptimizelyJSON getFeatureVariableJSON (@ Nonnull String featureKey ,
677
- @ Nonnull String variableKey ,
678
- @ Nonnull String userId ,
679
- @ Nonnull Map <String , ?> attributes ) {
692
+ @ Nonnull String variableKey ,
693
+ @ Nonnull String userId ,
694
+ @ Nonnull Map <String , ?> attributes ) {
680
695
681
696
return getFeatureVariableValueForType (
682
697
featureKey ,
@@ -688,10 +703,10 @@ public OptimizelyJSON getFeatureVariableJSON(@Nonnull String featureKey,
688
703
689
704
@ VisibleForTesting
690
705
<T > T getFeatureVariableValueForType (@ Nonnull String featureKey ,
691
- @ Nonnull String variableKey ,
692
- @ Nonnull String userId ,
693
- @ Nonnull Map <String , ?> attributes ,
694
- @ Nonnull String variableType ) {
706
+ @ Nonnull String variableKey ,
707
+ @ Nonnull String userId ,
708
+ @ Nonnull Map <String , ?> attributes ,
709
+ @ Nonnull String variableType ) {
695
710
if (featureKey == null ) {
696
711
logger .warn ("The featureKey parameter must be nonnull." );
697
712
return null ;
@@ -878,7 +893,7 @@ public OptimizelyJSON getAllFeatureVariables(@Nonnull String featureKey,
878
893
}
879
894
} else {
880
895
logger .info ("User \" {}\" was not bucketed into any variation for feature flag \" {}\" . " +
881
- "The default values are being returned." , userId , featureKey );
896
+ "The default values are being returned." , userId , featureKey );
882
897
}
883
898
884
899
Map <String , Object > valuesMap = new HashMap <String , Object >();
@@ -1142,7 +1157,7 @@ public OptimizelyConfig getOptimizelyConfig() {
1142
1157
* @param userId The user ID to be used for bucketing.
1143
1158
* @param attributes: A map of attribute names to current user attribute values.
1144
1159
* @return An OptimizelyUserContext associated with this OptimizelyClient.
1145
- */
1160
+ */
1146
1161
public OptimizelyUserContext createUserContext (@ Nonnull String userId ,
1147
1162
@ Nonnull Map <String , ?> attributes ) {
1148
1163
if (userId == null ) {
@@ -1413,6 +1428,53 @@ public <T> int addNotificationHandler(Class<T> clazz, NotificationHandler<T> han
1413
1428
return notificationCenter .addNotificationHandler (clazz , handler );
1414
1429
}
1415
1430
1431
+ public List <String > fetchQualifiedSegments (String userId , @ Nonnull List <ODPSegmentOption > segmentOptions ) {
1432
+ if (odpManager != null ) {
1433
+ synchronized (odpManager ) {
1434
+ return odpManager .getSegmentManager ().getQualifiedSegments (userId , segmentOptions );
1435
+ }
1436
+ }
1437
+ logger .error ("Audience segments fetch failed (ODP is not enabled)." );
1438
+ return null ;
1439
+ }
1440
+
1441
+ public void fetchQualifiedSegments (String userId , ODPSegmentManager .ODPSegmentFetchCallback callback , List <ODPSegmentOption > segmentOptions ) {
1442
+ if (odpManager == null ) {
1443
+ logger .error ("Audience segments fetch failed (ODP is not enabled)." );
1444
+ callback .onCompleted (null );
1445
+ } else {
1446
+ odpManager .getSegmentManager ().getQualifiedSegments (userId , callback , segmentOptions );
1447
+ }
1448
+ }
1449
+
1450
+ @ Nullable
1451
+ public ODPManager getODPManager () {
1452
+ return odpManager ;
1453
+ }
1454
+
1455
+ public void sendODPEvent (@ Nullable String type , @ Nonnull String action , @ Nullable Map <String , String > identifiers , @ Nullable Map <String , Object > data ) {
1456
+ if (odpManager != null ) {
1457
+ ODPEvent event = new ODPEvent (type , action , identifiers , data );
1458
+ odpManager .getEventManager ().sendEvent (event );
1459
+ } else {
1460
+ logger .error ("ODP event send failed (ODP is not enabled)" );
1461
+ }
1462
+ }
1463
+
1464
+ public void identifyUser (@ Nonnull String userId ) {
1465
+ ODPManager odpManager = getODPManager ();
1466
+ if (odpManager != null ) {
1467
+ odpManager .getEventManager ().identifyUser (userId );
1468
+ }
1469
+ }
1470
+
1471
+ private void updateODPSettings () {
1472
+ if (odpManager != null && getProjectConfig () != null ) {
1473
+ ProjectConfig projectConfig = getProjectConfig ();
1474
+ odpManager .updateSettings (projectConfig .getHostForODP (), projectConfig .getPublicKeyForODP (), projectConfig .getAllSegments ());
1475
+ }
1476
+ }
1477
+
1416
1478
//======== Builder ========//
1417
1479
1418
1480
/**
@@ -1467,6 +1529,7 @@ public static class Builder {
1467
1529
private UserProfileService userProfileService ;
1468
1530
private NotificationCenter notificationCenter ;
1469
1531
private List <OptimizelyDecideOption > defaultDecideOptions ;
1532
+ private ODPManager odpManager ;
1470
1533
1471
1534
// For backwards compatibility
1472
1535
private AtomicProjectConfigManager fallbackConfigManager = new AtomicProjectConfigManager ();
@@ -1562,6 +1625,11 @@ public Builder withDefaultDecideOptions(List<OptimizelyDecideOption> defaultDeci
1562
1625
return this ;
1563
1626
}
1564
1627
1628
+ public Builder withODPManager (ODPManager odpManager ) {
1629
+ this .odpManager = odpManager ;
1630
+ return this ;
1631
+ }
1632
+
1565
1633
// Helper functions for making testing easier
1566
1634
protected Builder withBucketing (Bucketer bucketer ) {
1567
1635
this .bucketer = bucketer ;
@@ -1636,7 +1704,7 @@ public Optimizely build() {
1636
1704
defaultDecideOptions = Collections .emptyList ();
1637
1705
}
1638
1706
1639
- return new Optimizely (eventHandler , eventProcessor , errorHandler , decisionService , userProfileService , projectConfigManager , optimizelyConfigManager , notificationCenter , defaultDecideOptions );
1707
+ return new Optimizely (eventHandler , eventProcessor , errorHandler , decisionService , userProfileService , projectConfigManager , optimizelyConfigManager , notificationCenter , defaultDecideOptions , odpManager );
1640
1708
}
1641
1709
}
1642
1710
}
0 commit comments