3232import java .util .concurrent .TimeUnit ;
3333
3434/**
35- * HttpProjectConfigManager is an implementation of a ProjectConfigManager
35+ * HttpProjectConfigManager is an implementation of a {@link PollingProjectConfigManager}
3636 * backed by a datafile. Currently this is loosely tied to Apache HttpClient
3737 * implementation which is the client of choice in this package.
38- *
39- * Note that this implementation is blocking and stateless. This is best used in
40- * conjunction with the {@link PollingProjectConfigManager} to provide caching
41- * and asynchronous fetching.
4238 */
4339public class HttpProjectConfigManager extends PollingProjectConfigManager {
4440
@@ -48,8 +44,8 @@ public class HttpProjectConfigManager extends PollingProjectConfigManager {
4844 private final URI uri ;
4945 private String datafileLastModified ;
5046
51- private HttpProjectConfigManager (long period , TimeUnit timeUnit , OptimizelyHttpClient httpClient , String url ) {
52- super (period , timeUnit );
47+ private HttpProjectConfigManager (long period , TimeUnit timeUnit , OptimizelyHttpClient httpClient , String url , long blockingTimeoutPeriod , TimeUnit blockingTimeoutUnit ) {
48+ super (period , timeUnit , blockingTimeoutPeriod , blockingTimeoutUnit );
5349 this .httpClient = httpClient ;
5450 this .uri = URI .create (url );
5551 }
@@ -128,9 +124,13 @@ public static class Builder {
128124 private String url ;
129125 private String format = "https://cdn.optimizely.com/datafiles/%s.json" ;
130126 private OptimizelyHttpClient httpClient ;
127+
131128 private long period = 5 ;
132129 private TimeUnit timeUnit = TimeUnit .MINUTES ;
133130
131+ private long blockingTimeoutPeriod = 10 ;
132+ private TimeUnit blockingTimeoutUnit = TimeUnit .SECONDS ;
133+
134134 public Builder withDatafile (String datafile ) {
135135 this .datafile = datafile ;
136136 return this ;
@@ -156,6 +156,23 @@ public Builder withOptimizelyHttpClient(OptimizelyHttpClient httpClient) {
156156 return this ;
157157 }
158158
159+ /**
160+ * Configure time to block before Completing the future. This timeout is used on the first call
161+ * to {@link PollingProjectConfigManager#getConfig()}. If the timeout is exceeded then the
162+ * PollingProjectConfigManager will begin returning null immediately until the call to Poll
163+ * succeeds.
164+ */
165+ public Builder withBlockingTimeout (long period , TimeUnit timeUnit ) {
166+ if (timeUnit == null ) {
167+ throw new NullPointerException ("Must provide valid timeUnit" );
168+ }
169+
170+ this .blockingTimeoutPeriod = period ;
171+ this .blockingTimeoutUnit = timeUnit ;
172+
173+ return this ;
174+ }
175+
159176 public Builder withPollingInterval (long period , TimeUnit timeUnit ) {
160177 if (timeUnit == null ) {
161178 throw new NullPointerException ("Must provide valid timeUnit" );
@@ -186,17 +203,15 @@ public HttpProjectConfigManager build(boolean defer) {
186203 httpClient = HttpClientUtils .getDefaultHttpClient ();
187204 }
188205
189- if (url != null ) {
190- return new HttpProjectConfigManager (period , timeUnit , httpClient , url );
191- }
206+ if (url == null ) {
207+ if (sdkKey == null ) {
208+ throw new NullPointerException ("sdkKey cannot be null" );
209+ }
192210
193- if (sdkKey == null ) {
194- throw new NullPointerException ("sdkKey cannot be null" );
211+ url = String .format (format , sdkKey );
195212 }
196213
197- url = String .format (format , sdkKey );
198-
199- HttpProjectConfigManager httpProjectManager = new HttpProjectConfigManager (period , timeUnit , httpClient , url );
214+ HttpProjectConfigManager httpProjectManager = new HttpProjectConfigManager (period , timeUnit , httpClient , url , blockingTimeoutPeriod , blockingTimeoutUnit );
200215
201216 if (datafile != null ) {
202217 try {
0 commit comments