com.microsoft.azure.management.keyvault.implementation.KeyVaultFutures Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of azure-mgmt-keyvault Show documentation
Show all versions of azure-mgmt-keyvault Show documentation
This package contains Microsoft Azure Key Vault Management SDK. This package has been deprecated. A replacement package com.azure.resourcemanager:azure-resourcemanager-keyvault is available as of 31-March-2022. We strongly encourage you to upgrade to continue receiving updates. See Migration Guide https://aka.ms/java-track2-migration-guide for guidance on upgrading. Refer to our deprecation policy https://azure.github.io/azure-sdk/policies_support.html for more details.
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/
package com.microsoft.azure.management.keyvault.implementation;
import com.microsoft.azure.ListOperationCallback;
import com.microsoft.azure.management.apigeneration.LangDefinition;
import com.microsoft.rest.ServiceCallback;
import com.microsoft.rest.ServiceFuture;
import rx.Emitter;
import rx.Emitter.BackpressureMode;
import rx.Observable;
import rx.Subscription;
import rx.functions.Action1;
import rx.functions.Func0;
import rx.functions.Func1;
import java.util.List;
/**
* A collection of utilities for converting futures in Key Vault client to observables.
*/
@LangDefinition
final class KeyVaultFutures {
abstract static class ServiceFutureConverter {
abstract ServiceFuture callAsync();
abstract T wrapModel(TInner inner);
ServiceFuture toFuture(final ServiceCallback callback) {
final KeyVaultFuture future = new KeyVaultFuture<>();
Observable.from(callAsync())
.subscribe(new Action1() {
@Override
public void call(TInner inner) {
T fluent = wrapModel(inner);
if (callback != null) {
callback.success(fluent);
}
future.success(fluent);
}
}, new Action1() {
@Override
public void call(Throwable throwable) {
if (callback != null) {
callback.failure(throwable);
}
future.failure(throwable);
}
});
return future;
}
public Observable toObservable() {
return Observable.defer(new Func0>() {
@Override
public Observable call() {
return Observable.from(toFuture(null));
}
});
}
}
abstract static class ListCallbackObserver {
abstract void list(ListOperationCallback callback);
abstract Observable typeConvertAsync(TInner inner);
Observable toObservable() {
return Observable
.create(new Action1>>() {
@Override
public void call(final Emitter> emitter) {
list(new ListOperationCallback() {
@Override
public PagingBehavior progress(List partial) {
emitter.onNext(partial);
return PagingBehavior.CONTINUE;
}
@Override
public void success() {
emitter.onCompleted();
}
@Override
public void failure(Throwable t) {
emitter.onError(t);
}
});
}
}, BackpressureMode.BUFFER)
.flatMap(new Func1, Observable>() {
@Override
public Observable call(List secretItems) {
return Observable.from(secretItems);
}
}).flatMap(new Func1>() {
@Override
public Observable call(TInner tInner) {
return typeConvertAsync(tInner);
}
});
}
}
static class KeyVaultFuture extends ServiceFuture {
KeyVaultFuture() {
super();
}
@Override
protected void setSubscription(Subscription subscription) {
super.setSubscription(subscription);
}
boolean failure(Throwable t) {
return setException(t);
}
}
}