2828import com .microsoft .azure .hdinsight .metadata .ClusterMetaDataService ;
2929import com .microsoft .azure .hdinsight .sdk .cluster .*;
3030import com .microsoft .azuretools .authmanage .AuthMethodManager ;
31+ import com .microsoft .azuretools .authmanage .SubscriptionManager ;
3132import com .microsoft .azuretools .authmanage .models .SubscriptionDetail ;
3233import com .microsoft .azuretools .azurecommons .helpers .NotNull ;
3334import com .microsoft .azuretools .azurecommons .helpers .StringHelper ;
35+ import com .microsoft .azuretools .sdkmanage .AzureManager ;
3436import com .microsoft .tooling .msservices .components .DefaultLoader ;
3537import com .microsoft .azure .hdinsight .sdk .common .AggregatedException ;
3638import com .microsoft .azure .hdinsight .sdk .common .AuthenticationErrorHandler ;
3739import com .microsoft .azure .hdinsight .sdk .common .HDIException ;
3840import com .microsoft .azure .hdinsight .sdk .storage .HDStorageAccount ;
3941
42+ import java .io .IOException ;
4043import java .util .ArrayList ;
4144import java .util .List ;
4245import java .util .Optional ;
@@ -144,9 +147,9 @@ public synchronized ImmutableList<IClusterDetail> getClusterDetails() {
144147 }
145148
146149 isListClusterSuccess = false ;
147- com . microsoft . azuretools . sdkmanage . AzureManager manager ;
150+ Optional < AzureManager > manager ;
148151 try {
149- manager = AuthMethodManager .getInstance ().getAzureManager ();
152+ manager = Optional . ofNullable ( AuthMethodManager .getInstance ().getAzureManager () );
150153 } catch (Exception ex ) {
151154 // not authenticated
152155 clusterDetails .addAll (hdinsightAdditionalClusterDetails );
@@ -155,26 +158,47 @@ public synchronized ImmutableList<IClusterDetail> getClusterDetails() {
155158 return ClusterMetaDataService .getInstance ().getCachedClusterDetails ();
156159 }
157160
158- List <SubscriptionDetail > subscriptionList = null ;
159- try {
160- subscriptionList = manager .getSubscriptionManager ().getSubscriptionDetails ();
161- clusterDetails = ClusterManager .getInstance ().getHDInsightClustersWithSpecificType (subscriptionList , OSTYPE );
162-
163- // TODO: so far we have not a good way to judge whether it is token expired as we have changed the way to list hdinsight clusters
164- isListClusterSuccess = clusterDetails .size () != 0 ;
165- } catch (AggregatedException aggregateException ) {
166- if (dealWithAggregatedException (aggregateException )) {
167- DefaultLoader .getUIHelper ().showError ("Failed to get HDInsight Cluster, Please make sure there's no login problem first" ,"List HDInsight Cluster Error" );
168- }
169- } catch (Exception ex ) {
170- DefaultLoader .getUIHelper ().showError ("Failed to get HDInsight Clusters" ,"List HDInsight Cluster Error" );
171- }
161+ Optional <List <SubscriptionDetail >> subscriptionList = manager .map (AzureManager ::getSubscriptionManager )
162+ .flatMap (subscriptionManager -> {
163+ try {
164+ return Optional .ofNullable (subscriptionManager .getSubscriptionDetails ());
165+ } catch (IOException e ) {
166+ DefaultLoader .getUIHelper ().showError ("Failed to get HDInsight Clusters. " +
167+ "Please check your subscription and login at Azure Explorer." ,
168+ "List HDInsight Cluster Error" );
169+
170+ return Optional .empty ();
171+ }
172+ });
173+
174+ subscriptionList
175+ .flatMap (list -> {
176+ try {
177+ return Optional .of (ClusterManager .getInstance ().getHDInsightClustersWithSpecificType (list , OSTYPE ));
178+ } catch (AggregatedException aggregateException ) {
179+ if (dealWithAggregatedException (aggregateException )) {
180+ DefaultLoader .getUIHelper ().showError (
181+ "Failed to get HDInsight Cluster, Please make sure there's no login problem first" ,
182+ "List HDInsight Cluster Error" );
183+ }
184+
185+ return Optional .empty ();
186+ }
187+ })
188+ .ifPresent (clusterDetailList -> {
189+ // TODO: so far we have not a good way to judge whether it is token expired as
190+ // we have changed the way to list HDInsight clusters
191+ isListClusterSuccess = clusterDetailList .size () != 0 ;
192+
193+ clusterDetailList .addAll (hdinsightAdditionalClusterDetails );
194+ clusterDetailList .addAll (emulatorClusterDetails );
195+ ClusterMetaDataService .getInstance ().addCachedClusters (clusterDetailList );
196+
197+ });
172198
173- isSelectedSubscriptionExist = subscriptionList != null && !subscriptionList .isEmpty ();
199+ isSelectedSubscriptionExist = subscriptionList .map (list -> list .stream ().anyMatch (SubscriptionDetail ::isSelected ))
200+ .orElse (false );
174201
175- clusterDetails .addAll (hdinsightAdditionalClusterDetails );
176- clusterDetails .addAll (emulatorClusterDetails );
177- ClusterMetaDataService .getInstance ().addCachedClusters (clusterDetails );
178202 return ClusterMetaDataService .getInstance ().getCachedClusterDetails ();
179203 }
180204
0 commit comments