Skip to content

Commit 2c0f3ee

Browse files
committed
Improve AddNewCluster MVC design with more abstrcts
New IdeSchedulers interface is introduced. And saved much more the IDE view codes. Made the ctrl provider more common for both IDEA and Eclipse. Signed-off-by: Wei Zhang <[email protected]>
1 parent 1255e70 commit 2c0f3ee

File tree

7 files changed

+222
-183
lines changed

7 files changed

+222
-183
lines changed

PluginsAndFeatures/azure-toolkit-for-eclipse/com.microsoft.azuretools.azureexplorer/src/com/microsoft/azuretools/azureexplorer/forms/AddNewClusterForm.java

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,17 @@
2525
import com.microsoft.azure.hdinsight.serverexplore.AddNewClusterCtrlProvider;
2626
import com.microsoft.azure.hdinsight.serverexplore.AddNewClusterModel;
2727
import com.microsoft.azure.hdinsight.serverexplore.hdinsightnode.HDInsightRootModule;
28+
import com.microsoft.azuretools.azurecommons.helpers.NotNull;
29+
import com.microsoft.azuretools.azureexplorer.Activator;
2830
import com.microsoft.azuretools.core.components.AzureTitleAreaDialogWrapper;
2931
import com.microsoft.azuretools.core.rxjava.EclipseSchedulers;
3032
import com.microsoft.azuretools.core.utils.Messages;
3133
import com.microsoft.azuretools.telemetry.AppInsightsClient;
3234

3335
public class AddNewClusterForm extends AzureTitleAreaDialogWrapper implements SettableControl<AddNewClusterModel> {
36+
@NotNull
37+
private AddNewClusterCtrlProvider ctrlProvider;
38+
3439
private Text clusterNameField;
3540
private Text userNameField;
3641
private Text storageNameField;
@@ -47,6 +52,7 @@ public class AddNewClusterForm extends AzureTitleAreaDialogWrapper implements Se
4752
public AddNewClusterForm(Shell parentShell, HDInsightRootModule module) {
4853
super(parentShell);
4954
this.hdInsightModule = module;
55+
this.ctrlProvider = new AddNewClusterCtrlProvider(this, new EclipseSchedulers(Activator.PLUGIN_ID));
5056
}
5157

5258
@Override
@@ -57,20 +63,9 @@ protected void configureShell(Shell newShell) {
5763

5864
}
5965

60-
private AddNewClusterCtrlProvider prepareCtrl() {
61-
AddNewClusterModel current = new AddNewClusterModel();
62-
63-
getData(current);
64-
65-
return new AddNewClusterCtrlProvider(current);
66-
}
67-
6866
private void refreshContainers() {
69-
prepareCtrl()
70-
.refreshContainers()
71-
.subscribeOn(EclipseSchedulers.processBarVisibleAsync("Getting storage account containers..."))
72-
.observeOn(EclipseSchedulers.dispatchThread())
73-
.subscribe(this::setData);
67+
ctrlProvider.refreshContainers()
68+
.subscribe();
7469
}
7570

7671
@Override
@@ -219,21 +214,13 @@ public void focusLost(FocusEvent e) {
219214

220215
@Override
221216
protected void okPressed() {
222-
prepareCtrl().validateAndAdd()
223-
.subscribeOn(EclipseSchedulers.processBarVisibleAsync("Validating the cluster settings..."))
224-
.observeOn(EclipseSchedulers.dispatchThread())
225-
.doOnNext(this::setData)
226-
.map(AddNewClusterModel::getErrorMessage)
227-
.filter(StringUtils::isEmpty)
217+
ctrlProvider.validateAndAdd()
228218
.subscribe(toUpdate -> {
229219
hdInsightModule.refreshWithoutAsync();
230220
AppInsightsClient.create(Messages.HDInsightAddNewClusterAction, null);
231221

232222
super.okPressed();
233-
},
234-
err -> {
235-
setErrorMessage(err.getMessage());
236-
});
223+
});
237224
}
238225

239226
@Override

PluginsAndFeatures/azure-toolkit-for-eclipse/com.microsoft.azuretools.core/src/com/microsoft/azuretools/core/rxjava/EclipseSchedulers.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,34 @@
2727
import org.eclipse.core.runtime.jobs.Job;
2828
import org.eclipse.swt.widgets.Display;
2929

30+
import com.microsoft.azure.hdinsight.common.mvc.IdeSchedulers;
3031
import com.microsoft.azuretools.azurecommons.helpers.NotNull;
3132

3233
import rx.Scheduler;
3334
import rx.schedulers.Schedulers;
3435

35-
public class EclipseSchedulers {
36-
public static Scheduler processBarVisibleAsync(@NotNull String title) {
36+
public class EclipseSchedulers implements IdeSchedulers {
37+
@NotNull
38+
private String pluginId = "unknown";
39+
40+
/**
41+
* @param pluginId
42+
* plug in ID for task execution status
43+
*/
44+
public EclipseSchedulers(@NotNull String pluginId) {
45+
this.pluginId = pluginId;
46+
}
47+
48+
public EclipseSchedulers() {
49+
}
50+
51+
public Scheduler processBarVisibleAsync(@NotNull String title) {
3752
return Schedulers.from(command -> {
3853
Job job = Job.create(title, monitor -> {
3954
try {
4055
command.run();
4156
} catch (Exception ex) {
42-
return new Status(IStatus.ERROR, "unknown", ex.getMessage(), ex);
57+
return new Status(IStatus.ERROR, pluginId, ex.getMessage(), ex);
4358
}
4459

4560
return Status.OK_STATUS;
@@ -49,13 +64,13 @@ public static Scheduler processBarVisibleAsync(@NotNull String title) {
4964
});
5065
}
5166

52-
public static Scheduler processBarVisibleSync(@NotNull String title) {
67+
public Scheduler processBarVisibleSync(@NotNull String title) {
5368
return Schedulers.from(command -> {
5469
Job job = Job.create(title, monitor -> {
5570
try {
5671
command.run();
5772
} catch (Exception ex) {
58-
return new Status(IStatus.ERROR, "unknown", ex.getMessage(), ex);
73+
return new Status(IStatus.ERROR, pluginId, ex.getMessage(), ex);
5974
}
6075

6176
return Status.OK_STATUS;
@@ -71,7 +86,7 @@ public static Scheduler processBarVisibleSync(@NotNull String title) {
7186
});
7287
}
7388

74-
public static Scheduler dispatchThread() {
89+
public Scheduler dispatchUIThread() {
7590
return Schedulers.from(command -> Display.getDefault().asyncExec(command));
7691
}
7792
}

PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/hdinsight/serverexplore/ui/AddNewClusterFrom.java

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
import java.util.stream.IntStream;
4747

4848
public class AddNewClusterFrom extends DialogWrapper implements SettableControl<AddNewClusterModel> {
49-
@Nullable
50-
private Project project;
49+
@NotNull
50+
private AddNewClusterCtrlProvider ctrlProvider;
5151

5252
private JPanel addNewClusterPanel;
5353
private JTextField clusterNameFiled;
@@ -71,7 +71,7 @@ public class AddNewClusterFrom extends DialogWrapper implements SettableControl<
7171

7272
public AddNewClusterFrom(@Nullable final Project project, @NotNull HDInsightRootModule hdInsightModule) {
7373
super(project, true);
74-
this.project = project;
74+
this.ctrlProvider = new AddNewClusterCtrlProvider(this, new IdeaSchedulers(project));
7575

7676
myHelpAction = new HelpAction();
7777

@@ -100,19 +100,9 @@ public void focusLost(FocusEvent e) {
100100
});
101101
}
102102

103-
private AddNewClusterCtrlProvider prepareCtrl() {
104-
AddNewClusterModel current = new AddNewClusterModel();
105-
106-
getData(current);
107-
108-
return new AddNewClusterCtrlProvider(current);
109-
}
110-
111103
private void refreshContainers() {
112-
prepareCtrl()
113-
.refreshContainers()
114-
.subscribeOn(IdeaSchedulers.processBarVisibleAsync(this.project, "Getting storage account containers..."))
115-
.subscribe(this::setData);
104+
ctrlProvider.refreshContainers()
105+
.subscribe();
116106
}
117107

118108
@Override
@@ -175,13 +165,8 @@ protected void doHelpAction() {
175165

176166
@Override
177167
protected void doOKAction() {
178-
prepareCtrl()
168+
ctrlProvider
179169
.validateAndAdd()
180-
.subscribeOn(IdeaSchedulers.processBarVisibleAsync(this.project, "Validating the cluster settings..."))
181-
.doOnNext(this::setData)
182-
.map(AddNewClusterModel::getErrorMessage)
183-
.filter(StringUtils::isEmpty)
184-
.observeOn(IdeaSchedulers.dispatchThread()) // UI operation needs to be in dispatch thread
185170
.subscribe(toUpdate -> {
186171
hdInsightModule.refreshWithoutAsync();
187172
AppInsightsClient.create(HDInsightBundle.message("HDInsightAddNewClusterAction"), null);

PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/hdinsight/spark/run/SparkBatchJobRemoteProcess.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
import org.apache.commons.io.output.NullOutputStream;
3535
import org.jetbrains.annotations.NotNull;
3636
import org.jetbrains.annotations.Nullable;
37-
import rx.*;
38-
import rx.schedulers.Schedulers;
37+
import rx.Observable;
38+
import rx.Subscription;
3939
import rx.subjects.PublishSubject;
4040

4141
import java.io.IOException;
@@ -52,6 +52,8 @@ public class SparkBatchJobRemoteProcess extends RemoteProcess {
5252
@NotNull
5353
private Project project;
5454
@NotNull
55+
private IdeaSchedulers schedulers;
56+
@NotNull
5557
private SparkSubmitModel submitModel;
5658
@NotNull
5759
private final PublishSubject<SimpleImmutableEntry<MessageInfoType, String>> ctrlSubject;
@@ -72,6 +74,7 @@ public SparkBatchJobRemoteProcess(@NotNull Project project, @NotNull SparkSubmit
7274
@NotNull PublishSubject<SimpleImmutableEntry<MessageInfoType, String>> ctrlSubject)
7375
throws ExecutionException {
7476
this.project = project;
77+
this.schedulers = new IdeaSchedulers(project);
7578
this.submitModel = sparkSubmitModel;
7679
this.ctrlSubject = ctrlSubject;
7780

@@ -158,7 +161,7 @@ public void start() {
158161
.orElseThrow(() -> propagate(new SparkJobException("Can't find jar path to upload"))),
159162
submitModel.getSubmissionParameter().getClusterName(),
160163
ctrlSubject)
161-
.subscribeOn(IdeaSchedulers.processBarVisibleAsync(project, "Deploy the jar file into cluster")))
164+
.subscribeOn(schedulers.processBarVisibleAsync("Deploy the jar file into cluster")))
162165
.toObservable()
163166
.flatMap(this::submitJob)
164167
.flatMap(job -> Observable.zip(
@@ -169,7 +172,7 @@ public void start() {
169172
return job;
170173
}))
171174
.flatMap(runningJob -> runningJob.getJobDoneObservable()
172-
.subscribeOn(IdeaSchedulers.processBarVisibleAsync(project, "Spark batch job is running")))
175+
.subscribeOn(schedulers.processBarVisibleAsync("Spark batch job is running")))
173176
.subscribe(sdPair -> {
174177
if (sdPair.getKey() == SparkBatchJobState.SUCCESS) {
175178
logInfo("Job run successfully.");
@@ -188,7 +191,7 @@ public void start() {
188191
private Observable<SparkBatchJob> attachJobInputStream(SparkJobLogInputStream inputStream, SparkBatchJob job) {
189192
return Observable.just(inputStream)
190193
.map(stream -> stream.attachJob(job))
191-
.subscribeOn(IdeaSchedulers.processBarVisibleAsync(project, "Attach Spark batch job outputs " + inputStream.getLogType()))
194+
.subscribeOn(schedulers.processBarVisibleAsync("Attach Spark batch job outputs " + inputStream.getLogType()))
192195
.retryWhen(attempts -> attempts.flatMap(err -> {
193196
try {
194197
final String state = job.getState();
@@ -238,7 +241,7 @@ private Observable<SparkBatchJob> submitJob(SimpleImmutableEntry<IClusterDetail,
238241
IClusterDetail cluster = clusterArtifactUriPair.getKey();
239242
submitModel.getSubmissionParameter().setFilePath(clusterArtifactUriPair.getValue());
240243
return JobUtils.submit(cluster, submitModel.getSubmissionParameter())
241-
.subscribeOn(IdeaSchedulers.processBarVisibleAsync(project, "Submit the Spark batch job"))
244+
.subscribeOn(schedulers.processBarVisibleAsync("Submit the Spark batch job"))
242245
.toObservable()
243246
.flatMap(this::startJobSubmissionLogReceiver); // To receive the Livy submission log
244247
}

PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/intellij/rxjava/IdeaSchedulers.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,22 @@
2828
import com.intellij.openapi.progress.Task;
2929
import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator;
3030
import com.intellij.openapi.project.Project;
31+
import com.microsoft.azure.hdinsight.common.mvc.IdeSchedulers;
3132
import org.jetbrains.annotations.NotNull;
3233
import org.jetbrains.annotations.Nullable;
3334
import rx.Scheduler;
3435
import rx.schedulers.Schedulers;
3536

3637
import javax.swing.*;
37-
import java.lang.reflect.InvocationTargetException;
3838

39-
public class IdeaSchedulers {
40-
public static Scheduler processBarVisibleAsync(@Nullable Project project, @NotNull String title) {
39+
public class IdeaSchedulers implements IdeSchedulers {
40+
@Nullable final private Project project;
41+
42+
public IdeaSchedulers(@Nullable Project project) {
43+
this.project = project;
44+
}
45+
46+
public Scheduler processBarVisibleAsync(@NotNull String title) {
4147
return Schedulers.from(command -> ApplicationManager.getApplication().invokeLater(() -> {
4248
final Task.Backgroundable task = new Task.Backgroundable(project, title, false) {
4349
@Override
@@ -52,7 +58,7 @@ public void run(@NotNull ProgressIndicator progressIndicator) {
5258
}));
5359
}
5460

55-
public static Scheduler processBarVisibleSync(@Nullable Project project, @NotNull String title) {
61+
public Scheduler processBarVisibleSync( @NotNull String title) {
5662
return Schedulers.from(command -> ApplicationManager.getApplication().invokeAndWait(() -> {
5763
final Task.Backgroundable task = new Task.Backgroundable(project, title, false) {
5864
@Override
@@ -67,7 +73,7 @@ public void run(@NotNull ProgressIndicator progressIndicator) {
6773
}));
6874
}
6975

70-
public static Scheduler dispatchThread() {
76+
public Scheduler dispatchUIThread() {
7177
return Schedulers.from(SwingUtilities::invokeLater);
7278
}
7379
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation
3+
*
4+
* All rights reserved.
5+
*
6+
* MIT License
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
9+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
10+
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
11+
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
14+
* the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
17+
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package com.microsoft.azure.hdinsight.common.mvc;
24+
25+
import com.microsoft.azuretools.azurecommons.helpers.NotNull;
26+
import rx.Scheduler;
27+
28+
public interface IdeSchedulers {
29+
public Scheduler processBarVisibleAsync(@NotNull String title);
30+
31+
public Scheduler processBarVisibleSync(@NotNull String title);
32+
33+
public Scheduler dispatchUIThread();
34+
}

0 commit comments

Comments
 (0)