@@ -85,7 +85,8 @@ public class Optimizely implements AutoCloseable {
85
85
86
86
private final ProjectConfigManager projectConfigManager ;
87
87
88
- public final NotificationCenter notificationCenter = new NotificationCenter ();
88
+ // TODO should be private
89
+ public final NotificationCenter notificationCenter ;
89
90
90
91
@ Nullable
91
92
private final UserProfileService userProfileService ;
@@ -94,15 +95,17 @@ private Optimizely(@Nonnull EventHandler eventHandler,
94
95
@ Nonnull EventFactory eventFactory ,
95
96
@ Nonnull ErrorHandler errorHandler ,
96
97
@ Nonnull DecisionService decisionService ,
97
- @ Nonnull UserProfileService userProfileService ,
98
- @ Nonnull ProjectConfigManager projectConfigManager
98
+ @ Nullable UserProfileService userProfileService ,
99
+ @ Nonnull ProjectConfigManager projectConfigManager ,
100
+ @ Nonnull NotificationCenter notificationCenter
99
101
) {
100
102
this .decisionService = decisionService ;
101
103
this .eventHandler = eventHandler ;
102
104
this .eventFactory = eventFactory ;
103
105
this .errorHandler = errorHandler ;
104
106
this .userProfileService = userProfileService ;
105
107
this .projectConfigManager = projectConfigManager ;
108
+ this .notificationCenter = notificationCenter ;
106
109
}
107
110
108
111
/**
@@ -951,17 +954,34 @@ public NotificationCenter getNotificationCenter() {
951
954
* Convenience method for adding DecisionNotification Handlers
952
955
*/
953
956
public int addDecisionNotificationHandler (NotificationHandler <DecisionNotification > handler ) {
954
- NotificationManager <DecisionNotification > manager = notificationCenter
955
- .getNotificationManager (DecisionNotification .class );
956
- return manager .addHandler (handler );
957
+ return addNotificationHandler (DecisionNotification .class , handler );
957
958
}
958
959
959
960
/**
960
961
* Convenience method for adding TrackNotification Handlers
961
962
*/
962
963
public int addTrackNotificationHandler (NotificationHandler <TrackNotification > handler ) {
963
- NotificationManager <TrackNotification > notificationManager =
964
- notificationCenter .getNotificationManager (TrackNotification .class );
964
+ return addNotificationHandler (TrackNotification .class , handler );
965
+ }
966
+
967
+ /**
968
+ * Convenience method for adding UpdateConfigNotification Handlers
969
+ */
970
+ public int addUpdateConfigNotificationHandler (NotificationHandler <UpdateConfigNotification > handler ) {
971
+ return addNotificationHandler (UpdateConfigNotification .class , handler );
972
+ }
973
+
974
+ /**
975
+ * Convenience method for adding UpdateConfigNotification Handlers
976
+ */
977
+ private <T > int addNotificationHandler (Class <T > clazz , NotificationHandler <T > handler ) {
978
+ NotificationManager <T > notificationManager = notificationCenter .getNotificationManager (clazz );
979
+
980
+ if (notificationManager == null ) {
981
+ logger .warn ("{} not supported by the NotificationCenter." , clazz );
982
+ return -1 ;
983
+ }
984
+
965
985
return notificationManager .addHandler (handler );
966
986
}
967
987
@@ -999,6 +1019,7 @@ public static class Builder {
999
1019
private ProjectConfig projectConfig ;
1000
1020
private ProjectConfigManager projectConfigManager ;
1001
1021
private UserProfileService userProfileService ;
1022
+ private NotificationCenter notificationCenter ;
1002
1023
1003
1024
// For backwards compatibility
1004
1025
private AtomicProjectConfigManager fallbackConfigManager = new AtomicProjectConfigManager ();
@@ -1063,6 +1084,11 @@ protected Builder withEventBuilder(EventFactory eventFactory) {
1063
1084
return this ;
1064
1085
}
1065
1086
1087
+ protected Builder withNotificationCenter (NotificationCenter notificationCenter ) {
1088
+ this .notificationCenter = notificationCenter ;
1089
+ return this ;
1090
+ }
1091
+
1066
1092
public Optimizely build () {
1067
1093
1068
1094
if (clientEngine == null ) {
@@ -1112,7 +1138,11 @@ public Optimizely build() {
1112
1138
projectConfigManager = fallbackConfigManager ;
1113
1139
}
1114
1140
1115
- return new Optimizely (eventHandler , eventFactory , errorHandler , decisionService , userProfileService , projectConfigManager );
1141
+ if (notificationCenter == null ) {
1142
+ notificationCenter = new NotificationCenter ();
1143
+ }
1144
+
1145
+ return new Optimizely (eventHandler , eventFactory , errorHandler , decisionService , userProfileService , projectConfigManager , notificationCenter );
1116
1146
}
1117
1147
}
1118
1148
}
0 commit comments