/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.wgadesigner.wizards;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.wizard.IWizardPage;
import de.innovationgate.eclipse.editors.design.wizards.ExportWGADesignWizardBase;
import de.innovationgate.eclipse.utils.WorkbenchUtils;
import de.innovationgate.eclipse.utils.ui.CancelWizardPage;
import de.innovationgate.eclipse.wgadesigner.WGADesignerPlugin;
import de.innovationgate.eclipse.wgadesigner.models.WGARemoteServer;
import de.innovationgate.eclipse.wgadesigner.preferences.PreferenceConstants;
import de.innovationgate.utils.WGUtils;
import de.innovationgate.wgaservices.WGAServiceException;
public class ExportWGAPluginRemote extends ExportWGADesignWizardBase {
protected ExportWGAPluginRemoteServerPage _serverPage;
protected ExportWGAPluginRemoteUploadPage _uploadPage;
public ExportWGAPluginRemote() {
super();
setWindowTitle("Export WGA Plugin");
setNeedsProgressMonitor(true);
}
class UploadRunable implements IRunnableWithProgress {
private List<WGARemoteServer> _serverList = new ArrayList<WGARemoteServer>();
public UploadRunable(List<WGARemoteServer> servers){
_serverList.addAll(servers);
}
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
File tmpFile=null;
try {
tmpFile = File.createTempFile("WGADesignPlugin", ".wgaplugin");
tmpFile.deleteOnExit();
monitor.beginTask("Exporting plugin '" + _model.getPluginUniqueName() + "'.", IProgressMonitor.UNKNOWN);
Iterator<WGARemoteServer> it = _serverList.iterator();
while (it.hasNext()) {
WGARemoteServer current = it.next();
monitor.setTaskName("Exporting plugin '" + _model.getPluginUniqueName() + "' to server '" + current.getName() + "'.");
_model.exportPlugin(tmpFile, _serverPage.getBuild(), false, false);
List<DataSource> dataSources = new ArrayList<DataSource>();
dataSources.add(new FileDataSource(tmpFile));
try {
current.getServices().installPlugins(current.getSession(), dataSources);
monitor.worked(1);
} catch (WGAServiceException e) {
throw new InvocationTargetException(e, "Plugin export to server '" + current.getName() + " (" + current.getUrl() + ") failed.");
}
if (monitor.isCanceled()) {
break;
}
}
} catch (Exception e) {
if (e instanceof InvocationTargetException) {
throw (InvocationTargetException) e;
} else if (e instanceof InterruptedException) {
throw (InterruptedException) e;
} else {
throw new InvocationTargetException(e, "Upload of plugin failed.");
}
} finally {
monitor.done();
tmpFile.delete();
}
}
}
@Override
protected List<IWizardPage> createPages() {
List<IWizardPage> pages = new ArrayList<IWizardPage>();
if (_model.hasPluginConfig()) {
_serverPage = new ExportWGAPluginRemoteServerPage(_model);
_uploadPage = new ExportWGAPluginRemoteUploadPage(_model);
pages.add(_serverPage);
pages.add(_uploadPage);
}
else {
pages.add(new CancelWizardPage("No plugin configuration found.", "This design has no plugin configuration yet. You can create a plugin config within the design editor."));
setOpenDesignEditor(true);
}
return pages;
}
@Override
protected boolean innerPerformFinish() {
try {
ProgressMonitorDialog progressMonitor = new ProgressMonitorDialog(getShell());
progressMonitor.open();
progressMonitor.run(true, true, new UploadRunable(_serverPage.getSelectedServers()));
List<String> serverNames = new ArrayList<String>();
Iterator<WGARemoteServer> remoteServers = _serverPage.getSelectedServers().iterator();
while(remoteServers.hasNext()){
WGARemoteServer current = remoteServers.next();
serverNames.add(current.getName());
}
String lastSelectedRemoteServers = WGUtils.serializeCollection(serverNames, ",");
WGADesignerPlugin.getDefault().getPreferenceStore().putValue(PreferenceConstants.LAST_SELECTED_REMOTEESERVERS+"_"+_model.getPluginUniqueName(), lastSelectedRemoteServers);
MessageDialog.openInformation(getShell(), "Upload successful", "Plugin upload successfully finished.");
return true;
} catch (Exception e) {
WorkbenchUtils.showErrorDialog(WGADesignerPlugin.getDefault(), getShell(), "Plugin export failed", "Unable to export plugin.", e);
}
return false;
}
@Override
public IWizardPage getNextPage(IWizardPage page) {
if (page.equals(_serverPage)) {
_uploadPage.init(_serverPage.getSelectedServers());
}
return super.getNextPage(page);
}
}