blob: 53d045b6c74b36ae4f1960375283f3e256f9e4db [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2020 RBEI and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v. 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Adhith Gopal - Initial contribution and API
* Pavithra Krishna Reddy
* Santhosh Gokhale D
*******************************************************************************/
package org.eclipse.blockchain.server.ui.wizard.fragment;
import org.eclipse.blockchain.core.BlockchainCore;
import org.eclipse.blockchain.core.CoreCommandExecutor;
import org.eclipse.blockchain.core.Web3jHandler;
import org.eclipse.blockchain.ui.views.EtherAccountViewPart;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchListener;
import org.eclipse.ui.PlatformUI;
import org.eclipse.wst.server.ui.wizard.IWizardHandle;
import org.eclipse.wst.server.ui.wizard.WizardFragment;
/**
* This is just a proto-type class and not constructed fully.
*/
public class GethWizardFragment extends WizardFragment {
private GethWizardComposite gethComposite;
/**
* {@inheritDoc}
*/
@Override
public boolean hasComposite() {
return true;
}
/**
* {@inheritDoc}
*/
@Override
public Composite createComposite(final Composite parent,
final IWizardHandle handle) {
this.gethComposite = new GethWizardComposite(parent, handle);
return this.gethComposite;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isComplete() {
if (this.gethComposite != null) {
return this.gethComposite.isWizardComplete();
}
return false;
}
/**
* {@inheritDoc}
*/
@Override
public void performFinish(final IProgressMonitor monitor)
throws CoreException {
this.gethComposite.prePerformFinish();
String anyErrors = startServer();
if (anyErrors.isEmpty()) {
Display.getDefault().syncExec(() -> {
EtherAccountViewPart.updateView();
});
// Below activation should happen from UI-Thread
// PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
// .activate(PlatformUI.getWorkbench().getViewRegistry().find("").createView().getSite().getPart());
super.performFinish(monitor);
} else {
getTaskModel().putObject("server", null);
super.performCancel(monitor);
}
}
/**
* This will restrict other default child fragments from getting displayed.
* {@inheritDoc}
*/
@Override
public boolean isForceLastFragment() {
return true;
}
private String startServer() {
try {
String anyErrors = CoreCommandExecutor.getInstance()
.startGethServer(this.gethComposite.getDataDirectory(),
this.gethComposite.getGenesisFile(),
this.gethComposite.getGethOptions());
Web3jHandler web3j = Web3jHandler.getInstance();
if (!anyErrors.isEmpty()) {
showError(anyErrors);
return anyErrors;
}
anyErrors = web3j.createInitialAccounts();
if (!anyErrors.isEmpty()) {
showError(anyErrors);
return anyErrors;
}
PlatformUI.getWorkbench().getDisplay();
Display.getDefault().syncExec(() -> {
EtherAccountViewPart.updateView();
});
} catch (Exception e) {
BlockchainCore.getInstance().logException(
"org.eclipse.blockchain.server.ui", e.getMessage(), e);
}
PlatformUI.getWorkbench()
.addWorkbenchListener(new IWorkbenchListener() {
@Override
public boolean preShutdown(final IWorkbench workbench,
final boolean forced) {
CoreCommandExecutor.getInstance().terminateGethServer();
return true;
}
@Override
public void postShutdown(final IWorkbench workbench) {
/**
* Not required
*/
}
});
return "";
}
private void showError(final String anyErrors) {
Display.getDefault().syncExec(() -> {
MessageDialog errorDialog = new MessageDialog(
Display.getDefault().getActiveShell(),
"Geth Server Start Error", null, anyErrors,
MessageDialog.ERROR, new String[]{"OK"}, 0);
errorDialog.open();
});
}
}