cn.dockerfoundry.ide.eclipse.server.ui.internal.wizards.MappedURLsWizard.java Source code

Java tutorial

Introduction

Here is the source code for cn.dockerfoundry.ide.eclipse.server.ui.internal.wizards.MappedURLsWizard.java

Source

/*******************************************************************************
 * Copyright (c) 2012, 2015 Pivotal Software, Inc. 
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License, 
 * Version 2.0 (the "License"); you may not use this file except in compliance 
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *  
 *  Contributors:
 *     Pivotal Software, Inc. - initial API and implementation
 ********************************************************************************/
package cn.dockerfoundry.ide.eclipse.server.ui.internal.wizards;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.osgi.util.NLS;
import org.eclipse.wst.server.core.IModule;

import cn.dockerfoundry.ide.eclipse.server.core.AbstractApplicationDelegate;
import cn.dockerfoundry.ide.eclipse.server.core.internal.DockerFoundryPlugin;
import cn.dockerfoundry.ide.eclipse.server.core.internal.DockerFoundryServer;
import cn.dockerfoundry.ide.eclipse.server.core.internal.application.ApplicationRegistry;
import cn.dockerfoundry.ide.eclipse.server.core.internal.client.DockerFoundryApplicationModule;
import cn.dockerfoundry.ide.eclipse.server.core.internal.client.DeploymentInfoWorkingCopy;
import cn.dockerfoundry.ide.eclipse.server.ui.internal.Messages;

/**
 * @author Terry Denney
 * @author Steffen Pingel
 * @author Christian Dupuis
 */
public class MappedURLsWizard extends Wizard {

    private final String appName;

    private final DockerFoundryServer cloudServer;

    private List<String> existingURIs;

    private DockerFoundryApplicationModule applicationModule;

    private MappedURLsWizardPage page;

    public MappedURLsWizard(DockerFoundryServer cloudServer, DockerFoundryApplicationModule applicationModule,
            List<String> existingURIs) {
        Assert.isNotNull(applicationModule);
        this.cloudServer = cloudServer;
        this.appName = applicationModule.getDeployedApplicationName();
        this.applicationModule = applicationModule;
        this.existingURIs = existingURIs;

        setWindowTitle(Messages.MappedURLsWizard_TITLE_MOD_MAPPED_URL);
        setNeedsProgressMonitor(true);
    }

    @Override
    public void addPages() {
        page = new MappedURLsWizardPage(cloudServer, existingURIs, applicationModule);
        addPage(page);
    }

    public List<String> getURLs() {
        return page.getURLs();
    }

    public boolean requiresURL() {
        IModule localModule = applicationModule.getLocalModule();

        if (localModule == null) {
            return true;
        }

        AbstractApplicationDelegate delegate = ApplicationRegistry.getApplicationDelegate(localModule);

        return delegate == null || delegate.requiresURL();
    }

    @Override
    public boolean performFinish() {
        page.setErrorMessage(null);

        final IStatus[] result = new IStatus[1];

        IRunnableWithProgress runnable = null;

        page.setMessage(Messages.MappedURLsWizard_TEXT_UPDATE_URL);

        // If the app module is not deployed, set the URIs in the deployment
        // descriptor.
        if (!applicationModule.isDeployed()) {
            runnable = new IRunnableWithProgress() {
                public void run(IProgressMonitor monitor) {
                    try {
                        DeploymentInfoWorkingCopy wc = applicationModule.resolveDeploymentInfoWorkingCopy(monitor);
                        wc.setUris(page.getURLs());
                        wc.save();
                    } catch (CoreException e) {
                        result[0] = e.getStatus();
                    }
                }
            };
        } else {
            runnable = new IRunnableWithProgress() {
                public void run(IProgressMonitor monitor) {
                    try {
                        cloudServer.getBehaviour().operations().mappedUrlsUpdate(appName, page.getURLs())
                                .run(monitor);
                    } catch (CoreException e) {
                        result[0] = e.getStatus();
                    }
                }
            };
        }

        try {
            getContainer().run(true, true, runnable);
        } catch (InvocationTargetException e) {
            result[0] = DockerFoundryPlugin.getErrorStatus(e);
        } catch (InterruptedException e) {
            result[0] = DockerFoundryPlugin.getErrorStatus(e);
        }

        if (result[0] != null && !result[0].isOK()) {
            page.setErrorMessage(NLS.bind(Messages.MappedURLsWizard_ERROR_CHANGE_URL, result[0].getMessage()));
            return false;
        } else {
            return true;
        }

    }
}