2828import com .optimizely .ab .event .internal .*;
2929import com .optimizely .ab .event .internal .payload .EventBatch ;
3030import com .optimizely .ab .notification .*;
31+ import com .optimizely .ab .odp .*;
3132import com .optimizely .ab .optimizelyconfig .OptimizelyConfig ;
3233import com .optimizely .ab .optimizelyconfig .OptimizelyConfigManager ;
3334import com .optimizely .ab .optimizelyconfig .OptimizelyConfigService ;
@@ -96,6 +97,9 @@ public class Optimizely implements AutoCloseable {
9697 @ Nullable
9798 private final UserProfileService userProfileService ;
9899
100+ @ Nullable
101+ private final ODPManager odpManager ;
102+
99103 private Optimizely (@ Nonnull EventHandler eventHandler ,
100104 @ Nonnull EventProcessor eventProcessor ,
101105 @ Nonnull ErrorHandler errorHandler ,
@@ -104,7 +108,8 @@ private Optimizely(@Nonnull EventHandler eventHandler,
104108 @ Nonnull ProjectConfigManager projectConfigManager ,
105109 @ Nullable OptimizelyConfigManager optimizelyConfigManager ,
106110 @ Nonnull NotificationCenter notificationCenter ,
107- @ Nonnull List <OptimizelyDecideOption > defaultDecideOptions
111+ @ Nonnull List <OptimizelyDecideOption > defaultDecideOptions ,
112+ @ Nullable ODPManager odpManager
108113 ) {
109114 this .eventHandler = eventHandler ;
110115 this .eventProcessor = eventProcessor ;
@@ -115,6 +120,15 @@ private Optimizely(@Nonnull EventHandler eventHandler,
115120 this .optimizelyConfigManager = optimizelyConfigManager ;
116121 this .notificationCenter = notificationCenter ;
117122 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+ }
118132 }
119133
120134 /**
@@ -128,8 +142,6 @@ public boolean isValid() {
128142 return getProjectConfig () != null ;
129143 }
130144
131-
132-
133145 /**
134146 * Checks if eventHandler {@link EventHandler} and projectConfigManager {@link ProjectConfigManager}
135147 * are Closeable {@link Closeable} and calls close on them.
@@ -141,6 +153,9 @@ public void close() {
141153 tryClose (eventProcessor );
142154 tryClose (eventHandler );
143155 tryClose (projectConfigManager );
156+ if (odpManager != null ) {
157+ tryClose (odpManager );
158+ }
144159 }
145160
146161 //======== activate calls ========//
@@ -674,9 +689,9 @@ public OptimizelyJSON getFeatureVariableJSON(@Nonnull String featureKey,
674689 */
675690 @ Nullable
676691 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 ) {
680695
681696 return getFeatureVariableValueForType (
682697 featureKey ,
@@ -688,10 +703,10 @@ public OptimizelyJSON getFeatureVariableJSON(@Nonnull String featureKey,
688703
689704 @ VisibleForTesting
690705 <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 ) {
695710 if (featureKey == null ) {
696711 logger .warn ("The featureKey parameter must be nonnull." );
697712 return null ;
@@ -878,7 +893,7 @@ public OptimizelyJSON getAllFeatureVariables(@Nonnull String featureKey,
878893 }
879894 } else {
880895 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 );
882897 }
883898
884899 Map <String , Object > valuesMap = new HashMap <String , Object >();
@@ -1142,7 +1157,7 @@ public OptimizelyConfig getOptimizelyConfig() {
11421157 * @param userId The user ID to be used for bucketing.
11431158 * @param attributes: A map of attribute names to current user attribute values.
11441159 * @return An OptimizelyUserContext associated with this OptimizelyClient.
1145- */
1160+ */
11461161 public OptimizelyUserContext createUserContext (@ Nonnull String userId ,
11471162 @ Nonnull Map <String , ?> attributes ) {
11481163 if (userId == null ) {
@@ -1413,6 +1428,53 @@ public <T> int addNotificationHandler(Class<T> clazz, NotificationHandler<T> han
14131428 return notificationCenter .addNotificationHandler (clazz , handler );
14141429 }
14151430
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+
14161478 //======== Builder ========//
14171479
14181480 /**
@@ -1467,6 +1529,7 @@ public static class Builder {
14671529 private UserProfileService userProfileService ;
14681530 private NotificationCenter notificationCenter ;
14691531 private List <OptimizelyDecideOption > defaultDecideOptions ;
1532+ private ODPManager odpManager ;
14701533
14711534 // For backwards compatibility
14721535 private AtomicProjectConfigManager fallbackConfigManager = new AtomicProjectConfigManager ();
@@ -1562,6 +1625,11 @@ public Builder withDefaultDecideOptions(List<OptimizelyDecideOption> defaultDeci
15621625 return this ;
15631626 }
15641627
1628+ public Builder withODPManager (ODPManager odpManager ) {
1629+ this .odpManager = odpManager ;
1630+ return this ;
1631+ }
1632+
15651633 // Helper functions for making testing easier
15661634 protected Builder withBucketing (Bucketer bucketer ) {
15671635 this .bucketer = bucketer ;
@@ -1636,7 +1704,7 @@ public Optimizely build() {
16361704 defaultDecideOptions = Collections .emptyList ();
16371705 }
16381706
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 );
16401708 }
16411709 }
16421710}
0 commit comments