From 63b20a75677e65c1a6b5f750427114b1d24b7059 Mon Sep 17 00:00:00 2001 From: anuchan Date: Wed, 10 Aug 2016 17:03:53 -0700 Subject: [PATCH 1/3] Fixing a bunch of RawType usages and minor improvments in simplying callback --- .../implementation/AvailabilitySetImpl.java | 34 +++-- .../implementation/VirtualMachineImpl.java | 37 +++--- .../implementation/LoadBalancerImpl.java | 41 +++--- .../network/implementation/NetworkImpl.java | 34 +++-- .../implementation/NetworkInterfaceImpl.java | 35 +++-- .../NetworkSecurityGroupImpl.java | 35 +++-- .../implementation/PublicIpAddressImpl.java | 25 ++-- .../CreatableResourcesImpl.java | 14 +- .../resources/fluentcore/model/Appliable.java | 2 +- .../model/implementation/CreatableImpl.java | 6 +- .../implementation/CreatorTaskGroup.java | 4 +- .../model/implementation/CreatorTaskItem.java | 2 +- .../implementation/ResourceServiceCall.java | 124 ++++++++++++++++++ .../implementation/DeploymentImpl.java | 4 +- .../implementation/GenericResourceImpl.java | 23 +--- .../implementation/ResourceGroupImpl.java | 23 +--- .../implementation/StorageAccountImpl.java | 49 +++---- .../com/microsoft/azure/TaskGroupBase.java | 9 +- .../java/com/microsoft/azure/TaskItem.java | 2 +- 19 files changed, 296 insertions(+), 207 deletions(-) create mode 100644 azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/ResourceServiceCall.java diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/AvailabilitySetImpl.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/AvailabilitySetImpl.java index a5e161e544a7d..fbe354fb2d762 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/AvailabilitySetImpl.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/AvailabilitySetImpl.java @@ -10,6 +10,7 @@ import com.microsoft.azure.management.compute.InstanceViewStatus; import com.microsoft.azure.management.resources.fluentcore.arm.models.Resource; import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.GroupableResourceImpl; +import com.microsoft.azure.management.resources.fluentcore.model.implementation.ResourceServiceCall; import com.microsoft.rest.ServiceCall; import com.microsoft.rest.ServiceCallback; import com.microsoft.rest.ServiceResponse; @@ -92,12 +93,12 @@ public AvailabilitySetImpl withFaultDomainCount(int faultDomainCount) { } @Override - public AvailabilitySetImpl apply() throws Exception { + public AvailabilitySet apply() throws Exception { return this.create(); } @Override - public ServiceCall applyAsync(ServiceCallback callback) { + public ServiceCall applyAsync(ServiceCallback callback) { return this.createAsync(callback); } @@ -112,21 +113,18 @@ public Resource createResource() throws Exception { } @Override - public ServiceCall createResourceAsync(final ServiceCallback callback) { - final AvailabilitySetImpl self = this; - return this.client.createOrUpdateAsync(this.resourceGroupName(), this.name(), this.inner(), - new ServiceCallback() { - @Override - public void failure(Throwable t) { - callback.failure(t); - } - - @Override - public void success(ServiceResponse response) { - self.setInner(response.getBody()); - idOfVMsInSet = null; - callback.success(new ServiceResponse(self, response.getResponse())); - } - }); + public ServiceCall createResourceAsync(final ServiceCallback callback) { + ResourceServiceCall serviceCall = new ResourceServiceCall<>(this); + serviceCall.withSuccessHandler(new ResourceServiceCall.SuccessHandler() { + @Override + public void success(ServiceResponse response) { + idOfVMsInSet = null; + } + }); + this.client.createOrUpdateAsync(this.resourceGroupName(), + this.name(), + this.inner(), + serviceCall.wrapCallBack(callback)); + return serviceCall; } } diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineImpl.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineImpl.java index 97108681279e3..3d2d0068a7911 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineImpl.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineImpl.java @@ -43,6 +43,7 @@ import com.microsoft.azure.management.resources.fluentcore.arm.models.Resource; import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.GroupableResourceImpl; import com.microsoft.azure.management.resources.fluentcore.model.Creatable; +import com.microsoft.azure.management.resources.fluentcore.model.implementation.ResourceServiceCall; import com.microsoft.azure.management.resources.fluentcore.utils.PagedListConverter; import com.microsoft.azure.management.resources.fluentcore.utils.ResourceNamer; import com.microsoft.azure.management.resources.implementation.PageImpl; @@ -150,12 +151,12 @@ public VirtualMachine refresh() throws Exception { } @Override - public VirtualMachineImpl apply() throws Exception { + public VirtualMachine apply() throws Exception { return this.create(); } @Override - public ServiceCall applyAsync(ServiceCallback callback) { + public ServiceCall applyAsync(ServiceCallback callback) { return this.createAsync(callback); } @@ -876,42 +877,34 @@ public Resource createResource() throws Exception { } @Override - public ServiceCall createResourceAsync(final ServiceCallback callback) { + public ServiceCall createResourceAsync(final ServiceCallback callback) { if (isInCreateMode()) { setOSDiskAndOSProfileDefaults(); setHardwareProfileDefaults(); } DataDiskImpl.setDataDisksDefaults(this.dataDisks, this.vmName); - final VirtualMachineImpl self = this; - final ServiceCall call = new ServiceCall(null); + final ResourceServiceCall serviceCall = new ResourceServiceCall<>(this); handleStorageSettingsAsync(new ServiceCallback() { @Override public void failure(Throwable t) { - callback.failure(t); + serviceCall.failure(t); } @Override public void success(ServiceResponse result) { handleNetworkSettings(); handleAvailabilitySettings(); - call.newCall(client.createOrUpdateAsync(resourceGroupName(), vmName, inner(), - new ServiceCallback() { - @Override - public void failure(Throwable t) { - callback.failure(t); - } - - @Override - public void success(ServiceResponse response) { - self.setInner(response.getBody()); - clearCachedRelatedResources(); - initializeDataDisks(); - callback.success(new ServiceResponse(self, response.getResponse())); - } - }).getCall()); + serviceCall.withSuccessHandler(new ResourceServiceCall.SuccessHandler() { + @Override + public void success(ServiceResponse response) { + clearCachedRelatedResources(); + initializeDataDisks(); + } + }); + client.createOrUpdateAsync(resourceGroupName(), vmName, inner(), serviceCall.wrapCallBack(callback)); } }); - return call; + return serviceCall; } // Helpers diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/LoadBalancerImpl.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/LoadBalancerImpl.java index ce9774c2c3878..0cd2f5b1b15d4 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/LoadBalancerImpl.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/LoadBalancerImpl.java @@ -19,6 +19,7 @@ import com.microsoft.azure.management.resources.fluentcore.arm.models.Resource; import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.GroupableResourceImpl; import com.microsoft.azure.management.resources.fluentcore.model.Creatable; +import com.microsoft.azure.management.resources.fluentcore.model.implementation.ResourceServiceCall; import com.microsoft.rest.ServiceCall; import com.microsoft.rest.ServiceCallback; import com.microsoft.rest.ServiceResponse; @@ -60,12 +61,12 @@ public LoadBalancerImpl refresh() throws Exception { } @Override - public LoadBalancerImpl apply() throws Exception { + public LoadBalancer apply() throws Exception { return this.create(); } @Override - public ServiceCall applyAsync(ServiceCallback callback) { + public ServiceCall applyAsync(ServiceCallback callback) { return createAsync(callback); } @@ -225,27 +226,21 @@ public Resource createResource() throws Exception { } @Override - public ServiceCall createResourceAsync(final ServiceCallback callback) { - final LoadBalancerImpl self = this; + public ServiceCall createResourceAsync(final ServiceCallback callback) { ensureCreationPrerequisites(); - return this.innerCollection.createOrUpdateAsync(this.resourceGroupName(), this.name(), this.inner(), - new ServiceCallback() { - @Override - public void failure(Throwable t) { - callback.failure(t); - } - - @Override - public void success(ServiceResponse response) { - self.setInner(response.getBody()); - callback.success(new ServiceResponse(self, response.getResponse())); - try { - runPostCreationTasks(); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - }); + ResourceServiceCall serviceCall = new ResourceServiceCall<>(this); + serviceCall.withSuccessHandler(new ResourceServiceCall.SuccessHandler() { + @Override + public void success(ServiceResponse response) { + try { + runPostCreationTasks(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + }); + this.innerCollection.createOrUpdateAsync(this.resourceGroupName(), this.name(), this.inner(), serviceCall.wrapCallBack(callback)); + return serviceCall; } } \ No newline at end of file diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkImpl.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkImpl.java index a15de6ca7f915..79532a1c3ccd2 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkImpl.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkImpl.java @@ -9,6 +9,7 @@ import com.microsoft.azure.management.network.Subnet; import com.microsoft.azure.management.resources.fluentcore.arm.models.Resource; import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.GroupableResourceImpl; +import com.microsoft.azure.management.resources.fluentcore.model.implementation.ResourceServiceCall; import com.microsoft.rest.ServiceCall; import com.microsoft.rest.ServiceCallback; import com.microsoft.rest.ServiceResponse; @@ -65,12 +66,12 @@ public NetworkImpl refresh() throws Exception { } @Override - public NetworkImpl apply() throws Exception { + public Network apply() throws Exception { return this.create(); } @Override - public ServiceCall applyAsync(ServiceCallback callback) { + public ServiceCall applyAsync(ServiceCallback callback) { return createAsync(callback); } @@ -192,22 +193,19 @@ public Resource createResource() throws Exception { } @Override - public ServiceCall createResourceAsync(final ServiceCallback callback) { - final NetworkImpl self = this; + public ServiceCall createResourceAsync(final ServiceCallback callback) { ensureCreationPrerequisites(); - return this.innerCollection.createOrUpdateAsync(this.resourceGroupName(), this.name(), this.inner(), - new ServiceCallback() { - @Override - public void failure(Throwable t) { - callback.failure(t); - } - - @Override - public void success(ServiceResponse response) { - self.setInner(response.getBody()); - initializeSubnetsFromInner(); - callback.success(new ServiceResponse(self, response.getResponse())); - } - }); + ResourceServiceCall serviceCall = new ResourceServiceCall<>(this); + serviceCall.withSuccessHandler(new ResourceServiceCall.SuccessHandler() { + @Override + public void success(ServiceResponse response) { + initializeSubnetsFromInner(); + } + }); + this.innerCollection.createOrUpdateAsync(this.resourceGroupName(), + this.name(), + this.inner(), + serviceCall.wrapCallBack(callback)); + return serviceCall; } } \ No newline at end of file diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkInterfaceImpl.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkInterfaceImpl.java index 91d30d80eb877..ec60d39dd06e3 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkInterfaceImpl.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkInterfaceImpl.java @@ -19,6 +19,7 @@ import com.microsoft.azure.management.resources.fluentcore.arm.models.Resource; import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.GroupableResourceImpl; import com.microsoft.azure.management.resources.fluentcore.model.Creatable; +import com.microsoft.azure.management.resources.fluentcore.model.implementation.ResourceServiceCall; import com.microsoft.azure.management.resources.fluentcore.utils.ResourceNamer; import com.microsoft.azure.management.resources.fluentcore.utils.Utils; import com.microsoft.rest.ServiceCall; @@ -86,12 +87,12 @@ public NetworkInterface refresh() throws Exception { } @Override - public NetworkInterfaceImpl apply() throws Exception { + public NetworkInterface apply() throws Exception { return this.create(); } @Override - public ServiceCall applyAsync(ServiceCallback callback) { + public ServiceCall applyAsync(ServiceCallback callback) { return createAsync(callback); } @@ -373,26 +374,22 @@ public Resource createResource() throws Exception { } @Override - public ServiceCall createResourceAsync(final ServiceCallback callback) { - final NetworkInterfaceImpl self = this; + public ServiceCall createResourceAsync(final ServiceCallback callback) { NicIpConfigurationImpl.ensureConfigurations(this.nicIpConfigurations); - return this.client.createOrUpdateAsync(this.resourceGroupName(), + ResourceServiceCall serviceCall = new ResourceServiceCall<>(this); + serviceCall.withSuccessHandler(new ResourceServiceCall.SuccessHandler() { + @Override + public void success(ServiceResponse response) { + clearCachedRelatedResources(); + initializeNicIpConfigurations(); + } + }); + + this.client.createOrUpdateAsync(this.resourceGroupName(), this.nicName, this.inner(), - new ServiceCallback() { - @Override - public void failure(Throwable t) { - callback.failure(t); - } - - @Override - public void success(ServiceResponse response) { - self.setInner(response.getBody()); - clearCachedRelatedResources(); - initializeNicIpConfigurations(); - callback.success(new ServiceResponse(self, response.getResponse())); - } - }); + serviceCall.wrapCallBack((callback))); + return serviceCall; } /**************************************************. diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkSecurityGroupImpl.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkSecurityGroupImpl.java index 6cdd031083234..6cd7e4406e4d0 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkSecurityGroupImpl.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/NetworkSecurityGroupImpl.java @@ -9,6 +9,7 @@ import com.microsoft.azure.management.network.NetworkSecurityRule; import com.microsoft.azure.management.resources.fluentcore.arm.models.Resource; import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.GroupableResourceImpl; +import com.microsoft.azure.management.resources.fluentcore.model.implementation.ResourceServiceCall; import com.microsoft.rest.ServiceCall; import com.microsoft.rest.ServiceCallback; import com.microsoft.rest.ServiceResponse; @@ -91,12 +92,12 @@ public NetworkSecurityGroupImpl refresh() throws Exception { } @Override - public NetworkSecurityGroupImpl apply() throws Exception { + public NetworkSecurityGroup apply() throws Exception { return this.create(); } @Override - public ServiceCall applyAsync(ServiceCallback callback) { + public ServiceCall applyAsync(ServiceCallback callback) { return createAsync(callback); } @@ -161,21 +162,19 @@ public Resource createResource() throws Exception { } @Override - public ServiceCall createResourceAsync(final ServiceCallback callback) { - final NetworkSecurityGroupImpl self = this; - return this.innerCollection.createOrUpdateAsync(this.resourceGroupName(), this.name(), this.inner(), - new ServiceCallback() { - @Override - public void failure(Throwable t) { - callback.failure(t); - } - - @Override - public void success(ServiceResponse response) { - self.setInner(response.getBody()); - initializeRulesFromInner(); - callback.success(new ServiceResponse(self, response.getResponse())); - } - }); + public ServiceCall createResourceAsync(final ServiceCallback callback) { + ResourceServiceCall serviceCall = new ResourceServiceCall<>(this); + serviceCall.withSuccessHandler(new ResourceServiceCall.SuccessHandler() { + @Override + public void success(ServiceResponse response) { + initializeRulesFromInner(); + } + }); + + this.innerCollection.createOrUpdateAsync(this.resourceGroupName(), + this.name(), + this.inner(), + serviceCall.wrapCallBack(callback)); + return serviceCall; } } \ No newline at end of file diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/PublicIpAddressImpl.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/PublicIpAddressImpl.java index 0178507cd9c39..3edfc1f60aa8d 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/PublicIpAddressImpl.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/PublicIpAddressImpl.java @@ -5,11 +5,13 @@ */ package com.microsoft.azure.management.network.implementation; +import com.microsoft.azure.management.network.Network; import com.microsoft.azure.management.network.PublicIpAddress; import com.microsoft.azure.management.network.IPAllocationMethod; import com.microsoft.azure.management.network.PublicIPAddressDnsSettings; import com.microsoft.azure.management.resources.fluentcore.arm.models.Resource; import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.GroupableResourceImpl; +import com.microsoft.azure.management.resources.fluentcore.model.implementation.ResourceServiceCall; import com.microsoft.rest.ServiceCall; import com.microsoft.rest.ServiceCallback; import com.microsoft.rest.ServiceResponse; @@ -41,12 +43,12 @@ class PublicIpAddressImpl // Verbs @Override - public PublicIpAddressImpl apply() throws Exception { + public PublicIpAddress apply() throws Exception { return this.create(); } @Override - public ServiceCall applyAsync(ServiceCallback callback) { + public ServiceCall applyAsync(ServiceCallback callback) { return this.createAsync(callback); } @@ -159,8 +161,7 @@ public Resource createResource() throws Exception { } @Override - public ServiceCall createResourceAsync(final ServiceCallback callback) { - final PublicIpAddressImpl self = this; + public ServiceCall createResourceAsync(final ServiceCallback callback) { // Clean up empty DNS settings final PublicIPAddressDnsSettings dnsSettings = this.inner().dnsSettings(); if (dnsSettings != null) { @@ -171,18 +172,8 @@ public ServiceCall createResourceAsync(final ServiceCallback callback) } } - return this.client.createOrUpdateAsync(this.resourceGroupName(), this.name(), this.inner(), - new ServiceCallback() { - @Override - public void failure(Throwable t) { - callback.failure(t); - } - - @Override - public void success(ServiceResponse response) { - self.setInner(response.getBody()); - callback.success(new ServiceResponse(self, response.getResponse())); - } - }); + ResourceServiceCall serviceCall = new ResourceServiceCall<>(this); + this.client.createOrUpdateAsync(this.resourceGroupName(), this.name(), this.inner(), serviceCall.wrapCallBack(callback)); + return serviceCall; } } \ No newline at end of file diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/CreatableResourcesImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/CreatableResourcesImpl.java index 70685b4ba5768..6c23147bc59c8 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/CreatableResourcesImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/CreatableResourcesImpl.java @@ -35,14 +35,16 @@ protected CreatableResourcesImpl() { } @Override - public CreatedResources create(Creatable ... creatables) throws Exception { + @SafeVarargs + public final CreatedResources create(Creatable ... creatables) throws Exception { ServiceCall> serviceCall = createAsync(null, creatables); serviceCall.wait(); return serviceCall.get().getBody(); } @Override - public ServiceCall> createAsync(final ServiceCallback> callback, + @SafeVarargs + public final ServiceCall> createAsync(final ServiceCallback> callback, Creatable ... creatables) { CreatableResourcesRootImpl rootResource = new CreatableResourcesRootImpl<>(); rootResource.addCreatableDependencies(creatables); @@ -227,7 +229,7 @@ public List createdTopLevelResources() { @Override public Resource createdRelatedResource(String key) { - return (Resource) creatorTaskGroup().createdResource(key); + return creatorTaskGroup().createdResource(key); } void addCreatableDependencies(Creatable ... creatables) { @@ -238,7 +240,7 @@ void addCreatableDependencies(Creatable ... creatables) { } @Override - public ServiceCall createResourceAsync(ServiceCallback serviceCallback) { + public ServiceCall createResourceAsync(ServiceCallback serviceCallback) { serviceCallback.success(new ServiceResponse(this, null)); return null; } @@ -253,7 +255,7 @@ public Resource createResource() throws Exception { // resources. @Override - public CreatableResourcesRoot refresh() throws Exception { + public CreatableResourcesRoot refresh() throws Exception { return null; } @@ -309,7 +311,7 @@ public void setInnerServiceCall(ServiceCall> inner) { * Cancels all the service calls currently executing. */ public void cancel() { - this.innerServiceCall.cancel(); + this.innerServiceCall.cancel(true); } /** diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/Appliable.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/Appliable.java index bc26b679f8df5..8b05f6feb7a04 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/Appliable.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/Appliable.java @@ -29,5 +29,5 @@ public interface Appliable extends Indexable { * @param callback the callback for success and failure * @return the handle to the REST call */ - ServiceCall applyAsync(ServiceCallback callback); + ServiceCall applyAsync(ServiceCallback callback); } diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableImpl.java index a6de99f9a22d3..aa8f2d0dd32c5 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatableImpl.java @@ -66,11 +66,11 @@ public String name() { * @throws Exception when anything goes wrong */ @SuppressWarnings("unchecked") - public FluentModelImplT create() throws Exception { + public FluentModelT create() throws Exception { if (creatorTaskGroup.isPreparer()) { creatorTaskGroup.prepare(); creatorTaskGroup.execute(); - return (FluentModelImplT) this; + return (FluentModelT) this; } throw new IllegalStateException("Internal Error: create can be called only on preparer"); } @@ -93,7 +93,7 @@ public ServiceCall createAsync(ServiceCallback callb /** * @return the task group associated with this creatable. */ - public CreatorTaskGroup creatorTaskGroup() { + public CreatorTaskGroup creatorTaskGroup() { return this.creatorTaskGroup; } } diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatorTaskGroup.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatorTaskGroup.java index a22a70403bb6a..ea577a3f210d5 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatorTaskGroup.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatorTaskGroup.java @@ -22,7 +22,7 @@ interface ResourceCreator { * @param serviceCallback the callback to be invoked after the creation of resource * @return the service call reference */ - ServiceCall createResourceAsync(ServiceCallback serviceCallback); + ServiceCall createResourceAsync(ServiceCallback serviceCallback); /** * Creates the resource synchronously. @@ -35,7 +35,7 @@ interface ResourceCreator { /** * @return Gets the task group. */ - CreatorTaskGroup creatorTaskGroup(); + CreatorTaskGroup creatorTaskGroup(); } /** diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatorTaskItem.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatorTaskItem.java index 495b6f4612eaa..30fe57eff8594 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatorTaskItem.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/CreatorTaskItem.java @@ -38,7 +38,7 @@ public void execute() throws Exception { } @Override - public ServiceCall executeAsync(final ServiceCallback callback) { + public ServiceCall executeAsync(final ServiceCallback callback) { final CreatorTaskItem self = this; return (this.resourceCreator).createResourceAsync(new ServiceCallback() { @Override diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/ResourceServiceCall.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/ResourceServiceCall.java new file mode 100644 index 0000000000000..78cdf80656f4b --- /dev/null +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/ResourceServiceCall.java @@ -0,0 +1,124 @@ +package com.microsoft.azure.management.resources.fluentcore.model.implementation; + + +import com.microsoft.azure.management.resources.fluentcore.arm.models.Resource; +import com.microsoft.rest.ServiceCall; +import com.microsoft.rest.ServiceCallback; +import com.microsoft.rest.ServiceResponse; + +/** + * Type representing a service-call that returns Resource {@link Resource}. + * + * @param + * @param + * @param + */ +public class ResourceServiceCall & Resource> extends ServiceCall { + private final FluentModelImplT fluentModelImpl; + private SuccessHandler successHandler; + private FailureHandler failureHandler; + + /** + * Creates ResourceServiceCall. + * + * @param model the fluent model + */ + public ResourceServiceCall(FluentModelImplT model) { + super(null); + this.fluentModelImpl = model; + } + + /** + * Wraps a callback. + * + * @param callback the callback to wrap + * @return new callback that wraps the given callback + */ + public ServiceCallback wrapCallBack(final ServiceCallback callback) { + return wrapCallBack(callback, true); + } + + /** + * Wraps a callback. + * + * @param callback the callback to wrap + * @param reportSuccess true to invoke callback.success on success + * @return new callback that wraps the given callback + */ + public ServiceCallback wrapCallBack(final ServiceCallback callback, final boolean reportSuccess) { + final ResourceServiceCall self = this; + return new ServiceCallback() { + @Override + public void failure(Throwable t) { + if (self.failureHandler != null) { + self.failureHandler.failure(t); + } + self.failure(t); + callback.failure(t); + } + + @Override + public void success(ServiceResponse response) { + self.fluentModelImpl.setInner(response.getBody()); + if (self.successHandler != null) { + self.successHandler.success(response); + } + if (reportSuccess) { + self.success(new ServiceResponse(self.fluentModelImpl, response.getResponse())); + callback.success(new ServiceResponse(self.fluentModelImpl, response.getResponse())); + } + } + }; + } + + /** + * Sets the success handler. + * + * @param handler the success handler. + * @return ResourceServiceCall + */ + public ResourceServiceCall withSuccessHandler(SuccessHandler handler) { + this.successHandler = handler; + return this; + } + + /** + * Sets the failure handler. + * + * @param handler the failure handler. + * @return ResourceServiceCall + */ + public ResourceServiceCall withFailureHandler(FailureHandler handler) { + this.failureHandler = handler; + return this; + } + + /** + * Handler to handle success. + * + * @param the type of result + */ + public interface SuccessHandler { + /** + * handle success. + * + * @param response the response with result. + */ + void success(ServiceResponse response); + } + + /** + * Handler to handle failure. + */ + public interface FailureHandler { + /** + * handle failure. + * + * @param t the exception. + */ + void failure(Throwable t); + } +} + diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/DeploymentImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/DeploymentImpl.java index f83b2b9265359..fd772cb66c287 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/DeploymentImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/DeploymentImpl.java @@ -381,7 +381,7 @@ public void success(ServiceResponse response) { } @Override - public DeploymentImpl apply() throws Exception { + public Deployment apply() throws Exception { if (this.templateLink() != null && this.template() != null) { this.withTemplate(null); } @@ -392,7 +392,7 @@ public DeploymentImpl apply() throws Exception { } @Override - public ServiceCall applyAsync(ServiceCallback callback) { + public ServiceCall applyAsync(ServiceCallback callback) { try { if (this.templateLink() != null && this.template() != null) { this.withTemplate(null); diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/GenericResourceImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/GenericResourceImpl.java index 8c71cc62bfe3b..eec36fc6067cf 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/GenericResourceImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/GenericResourceImpl.java @@ -10,6 +10,7 @@ import com.microsoft.azure.management.resources.fluentcore.arm.models.Resource; import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.GroupableResourceImpl; import com.microsoft.azure.management.resources.Plan; +import com.microsoft.azure.management.resources.fluentcore.model.implementation.ResourceServiceCall; import com.microsoft.rest.ServiceCall; import com.microsoft.rest.ServiceCallback; import com.microsoft.rest.ServiceResponse; @@ -150,7 +151,7 @@ public GenericResourceImpl apply() throws Exception { } @Override - public ServiceCall applyAsync(ServiceCallback callback) { + public ServiceCall applyAsync(ServiceCallback callback) { return createAsync(callback); } @@ -172,9 +173,9 @@ public Resource createResource() throws Exception { } @Override - public ServiceCall createResourceAsync(final ServiceCallback callback) { - final GenericResourceImpl self = this; - return client.createOrUpdateAsync( + public ServiceCall createResourceAsync(final ServiceCallback callback) { + ResourceServiceCall serviceCall = new ResourceServiceCall<>(this); + client.createOrUpdateAsync( resourceGroupName(), resourceProviderNamespace, parentResourceId, @@ -182,17 +183,7 @@ public ServiceCall createResourceAsync(final ServiceCallback callback) name(), apiVersion, inner(), - new ServiceCallback() { - @Override - public void failure(Throwable t) { - callback.failure(t); - } - - @Override - public void success(ServiceResponse response) { - self.setInner(response.getBody()); - callback.success(new ServiceResponse(self, response.getResponse())); - } - }); + serviceCall.wrapCallBack(callback)); + return serviceCall; } } diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/ResourceGroupImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/ResourceGroupImpl.java index 08d64e0559b00..e7315342e9f4a 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/ResourceGroupImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/ResourceGroupImpl.java @@ -13,6 +13,7 @@ import com.microsoft.azure.management.resources.fluentcore.arm.Region; import com.microsoft.azure.management.resources.fluentcore.arm.models.Resource; import com.microsoft.azure.management.resources.fluentcore.model.implementation.CreatableUpdatableImpl; +import com.microsoft.azure.management.resources.fluentcore.model.implementation.ResourceServiceCall; import com.microsoft.rest.ServiceCall; import com.microsoft.rest.ServiceCallback; import com.microsoft.rest.ServiceResponse; @@ -120,12 +121,12 @@ public ResourceGroupImpl withoutTag(String key) { } @Override - public ResourceGroupImpl apply() throws Exception { + public ResourceGroup apply() throws Exception { return this.create(); } @Override - public ServiceCall applyAsync(ServiceCallback callback) { + public ServiceCall applyAsync(ServiceCallback callback) { return createAsync(callback); } @@ -146,22 +147,12 @@ public Resource createResource() throws Exception { } @Override - public ServiceCall createResourceAsync(final ServiceCallback callback) { - final ResourceGroupImpl self = this; + public ServiceCall createResourceAsync(final ServiceCallback callback) { ResourceGroupInner params = new ResourceGroupInner(); params.withLocation(this.inner().location()); params.withTags(this.inner().tags()); - return client.createOrUpdateAsync(this.name(), params, new ServiceCallback() { - @Override - public void failure(Throwable t) { - callback.failure(t); - } - - @Override - public void success(ServiceResponse response) { - self.setInner(response.getBody()); - callback.success(new ServiceResponse(self, response.getResponse())); - } - }); + ResourceServiceCall serviceCall = new ResourceServiceCall<>(this); + client.createOrUpdateAsync(this.name(), params, serviceCall.wrapCallBack(callback)); + return serviceCall; } } diff --git a/azure-mgmt-storage/src/main/java/com/microsoft/azure/management/storage/implementation/StorageAccountImpl.java b/azure-mgmt-storage/src/main/java/com/microsoft/azure/management/storage/implementation/StorageAccountImpl.java index 546d2ab7307fd..0b1ee97ea8d95 100644 --- a/azure-mgmt-storage/src/main/java/com/microsoft/azure/management/storage/implementation/StorageAccountImpl.java +++ b/azure-mgmt-storage/src/main/java/com/microsoft/azure/management/storage/implementation/StorageAccountImpl.java @@ -9,6 +9,7 @@ import com.microsoft.azure.CloudException; import com.microsoft.azure.management.resources.fluentcore.arm.models.Resource; import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.GroupableResourceImpl; +import com.microsoft.azure.management.resources.fluentcore.model.implementation.ResourceServiceCall; import com.microsoft.azure.management.storage.PublicEndpoints; import com.microsoft.azure.management.storage.StorageAccount; import com.microsoft.azure.management.storage.AccessTier; @@ -194,7 +195,7 @@ public StorageAccountImpl update() { } @Override - public StorageAccountImpl apply() throws Exception { + public StorageAccount apply() throws Exception { this.setInner(client.update(resourceGroupName(), name(), updateParameters).getBody()); return this; } @@ -271,34 +272,36 @@ public Resource createResource() throws Exception { } @Override - public ServiceCall createResourceAsync(final ServiceCallback callback) { + public ServiceCall createResourceAsync(final ServiceCallback callback) { final StorageAccountImpl self = this; createParameters.withLocation(this.regionName()); createParameters.withTags(this.inner().getTags()); - return this.client.createAsync(this.resourceGroupName(), this.name(), createParameters, - new ServiceCallback() { - @Override - public void failure(Throwable t) { - callback.failure(t); - } + final ResourceServiceCall createServiceCall = new ResourceServiceCall<>(this); + final ResourceServiceCall serviceCall = new ResourceServiceCall<>(this); + + createServiceCall.withSuccessHandler(new ResourceServiceCall.SuccessHandler() { + @Override + public void success(ServiceResponse response) { + serviceCall.withSuccessHandler(new ResourceServiceCall.SuccessHandler() { @Override - public void success(ServiceResponse result) { - client.getPropertiesAsync(resourceGroupName(), name(), - new ServiceCallback() { - @Override - public void failure(Throwable t) { - callback.failure(t); - } - - @Override - public void success(ServiceResponse response) { - self.setInner(response.getBody()); - clearWrapperProperties(); - callback.success(new ServiceResponse(self, response.getResponse())); - } - }); + public void success(ServiceResponse response) { + clearWrapperProperties(); } }); + client.getPropertiesAsync(self.resourceGroupName(), self.name(), serviceCall.wrapCallBack(callback)); + } + }).withFailureHandler(new ResourceServiceCall.FailureHandler() { + @Override + public void failure(Throwable t) { + serviceCall.failure(t); + } + }); + + this.client.createAsync(this.resourceGroupName(), + this.name(), + createParameters, + createServiceCall.wrapCallBack(callback, false)); + return serviceCall; } } diff --git a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/TaskGroupBase.java b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/TaskGroupBase.java index 8f178480c43be..e53bfa0a01962 100644 --- a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/TaskGroupBase.java +++ b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/TaskGroupBase.java @@ -21,7 +21,14 @@ */ public abstract class TaskGroupBase> implements TaskGroup { + /** + * Stores the tasks in this group and their dependency information. + */ private DAGraph> dag; + /** + * ServiceCall instance that aggregate a set of ServiceCalls each associated with + * the task in this group. + */ private ParallelServiceCall parallelServiceCall; /** @@ -87,7 +94,7 @@ public T taskResult(String taskId) { private void executeReadyTasksAsync(final ServiceCallback callback) { DAGNode nextNode = dag.getNext(); while (nextNode != null) { - ServiceCall serviceCall = nextNode.data().executeAsync(taskCallback(nextNode, callback)); + ServiceCall serviceCall = nextNode.data().executeAsync(taskCallback(nextNode, callback)); this.parallelServiceCall.addCall(serviceCall); nextNode = dag.getNext(); } diff --git a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/TaskItem.java b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/TaskItem.java index 8f0a3459a2e92..1a4bd9a61bf40 100644 --- a/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/TaskItem.java +++ b/runtimes/azure-client-runtime/src/main/java/com/microsoft/azure/TaskItem.java @@ -38,5 +38,5 @@ public interface TaskItem { * @param callback callback to call on success or failure * @return the handle of the REST call */ - ServiceCall executeAsync(ServiceCallback callback); + ServiceCall executeAsync(ServiceCallback callback); } From ef075cc8dbe7758af48cd6db0f8a98b0d92135b1 Mon Sep 17 00:00:00 2001 From: anuchan Date: Wed, 10 Aug 2016 17:43:54 -0700 Subject: [PATCH 2/3] Removing unused import from publicIpAddressImpl --- .../management/network/implementation/PublicIpAddressImpl.java | 1 - 1 file changed, 1 deletion(-) diff --git a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/PublicIpAddressImpl.java b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/PublicIpAddressImpl.java index 3edfc1f60aa8d..d85736c5fdc09 100644 --- a/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/PublicIpAddressImpl.java +++ b/azure-mgmt-network/src/main/java/com/microsoft/azure/management/network/implementation/PublicIpAddressImpl.java @@ -5,7 +5,6 @@ */ package com.microsoft.azure.management.network.implementation; -import com.microsoft.azure.management.network.Network; import com.microsoft.azure.management.network.PublicIpAddress; import com.microsoft.azure.management.network.IPAllocationMethod; import com.microsoft.azure.management.network.PublicIPAddressDnsSettings; From 5b2c095a6be7b9d56097a46b961e7694dfb7d073 Mon Sep 17 00:00:00 2001 From: anuchan Date: Thu, 11 Aug 2016 15:06:56 -0700 Subject: [PATCH 3/3] Invoke callback first then signal future --- .../collection/implementation/CreatableResourcesImpl.java | 5 ++--- .../fluentcore/model/implementation/ResourceServiceCall.java | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/CreatableResourcesImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/CreatableResourcesImpl.java index 6c23147bc59c8..6442d4447686f 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/CreatableResourcesImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/collection/implementation/CreatableResourcesImpl.java @@ -38,7 +38,6 @@ protected CreatableResourcesImpl() { @SafeVarargs public final CreatedResources create(Creatable ... creatables) throws Exception { ServiceCall> serviceCall = createAsync(null, creatables); - serviceCall.wait(); return serviceCall.get().getBody(); } @@ -326,18 +325,18 @@ ServiceCallback> wrapCallback(final ServiceCallback>() { @Override public void failure(Throwable t) { - self.failure(t); if (innerCallback != null) { innerCallback.failure((t)); } + self.failure(t); // Signal Future } @Override public void success(ServiceResponse> result) { - self.success(new ServiceResponse>(new CreatedResourcesImpl<>(result.getBody()), null)); if (innerCallback != null) { innerCallback.success(new ServiceResponse>(new CreatedResourcesImpl<>(result.getBody()), null)); } + self.success(new ServiceResponse>(new CreatedResourcesImpl<>(result.getBody()), null)); // Signal Future } }; } diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/ResourceServiceCall.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/ResourceServiceCall.java index 78cdf80656f4b..498f877f56b5f 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/ResourceServiceCall.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/ResourceServiceCall.java @@ -55,8 +55,8 @@ public void failure(Throwable t) { if (self.failureHandler != null) { self.failureHandler.failure(t); } - self.failure(t); callback.failure(t); + self.failure(t); // Signal Future } @Override @@ -66,8 +66,8 @@ public void success(ServiceResponse response) { self.successHandler.success(response); } if (reportSuccess) { - self.success(new ServiceResponse(self.fluentModelImpl, response.getResponse())); callback.success(new ServiceResponse(self.fluentModelImpl, response.getResponse())); + self.success(new ServiceResponse(self.fluentModelImpl, response.getResponse())); // Signal Future } } };