Java tutorial
/******************************************************************************* * 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.actions; import java.util.ArrayList; import java.util.List; import org.cloudfoundry.client.lib.domain.CloudService; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.jface.viewers.IStructuredSelection; import cn.dockerfoundry.ide.eclipse.server.core.internal.CloudErrorUtil; import cn.dockerfoundry.ide.eclipse.server.core.internal.client.DockerFoundryApplicationModule; import cn.dockerfoundry.ide.eclipse.server.core.internal.client.DockerFoundryServerBehaviour; import cn.dockerfoundry.ide.eclipse.server.core.internal.client.DeploymentInfoWorkingCopy; import cn.dockerfoundry.ide.eclipse.server.core.internal.client.ICloudFoundryOperation; import cn.dockerfoundry.ide.eclipse.server.core.internal.client.LocalCloudService; import cn.dockerfoundry.ide.eclipse.server.ui.internal.editor.DockerFoundryApplicationsEditorPage; /** * Action for modifying list of services for an application. Subclasses are * responsible for defining the list of services to add or to remove. * @author Terry Denney * @author Steffen Pingel * @author Christian Dupuis */ public abstract class ModifyServicesForApplicationAction extends EditorAction { private DockerFoundryApplicationModule appModule; public ModifyServicesForApplicationAction(DockerFoundryApplicationModule appModule, DockerFoundryServerBehaviour serverBehaviour, DockerFoundryApplicationsEditorPage editorPage) { super(editorPage, RefreshArea.DETAIL); this.appModule = appModule; } abstract public List<String> getServicesToAdd(); abstract public List<String> getServicesToRemove(); protected void setApplicationModule(DockerFoundryApplicationModule appModule) { this.appModule = appModule; } @Override protected ICloudFoundryOperation getOperation(IProgressMonitor monitor) throws CoreException { List<String> existingServices = null; final List<String> updatedServices = new ArrayList<String>(); DeploymentInfoWorkingCopy workingCopy = appModule.resolveDeploymentInfoWorkingCopy(monitor); // Check the deployment information to see if it has an existing list of // bound services. existingServices = workingCopy.asServiceBindingList(); // Must iterate rather than passing to constructor or using // addAll, as some // of the entries in existing services may be null. if (existingServices != null) { for (String existingService : existingServices) { if (existingService != null) { updatedServices.add(existingService); } } } // This leads to duplicate services, as a user could drop an existing // service already // added to an application boolean serviceChanges = false; List<String> servicesToAdd = getServicesToAdd(); for (String serviceToAdd : servicesToAdd) { if (!updatedServices.contains(serviceToAdd)) { updatedServices.add(serviceToAdd); serviceChanges = true; } } serviceChanges |= updatedServices.removeAll(getServicesToRemove()); if (serviceChanges) { // Save the changes even if an app is not deployed List<CloudService> boundServices = new ArrayList<CloudService>(); for (String serName : updatedServices) { boundServices.add(new LocalCloudService(serName)); } workingCopy.setServices(boundServices); workingCopy.save(); if (appModule.getApplication() != null) { // update services right away, if app is already deployed return getBehaviour().operations().bindServices(appModule, updatedServices); } } return null; } @Override protected boolean shouldLogException(CoreException e) { return !CloudErrorUtil.isNotFoundException(e); } public static List<String> getServiceNames(IStructuredSelection selection) { Object[] objects = selection.toArray(); List<String> services = new ArrayList<String>(); for (Object object : objects) { if (object instanceof CloudService) { services.add(((CloudService) object).getName()); } } return services; } public static List<CloudService> getServices(IStructuredSelection selection) { Object[] objects = selection.toArray(); List<CloudService> services = new ArrayList<CloudService>(); for (Object object : objects) { if (object instanceof CloudService) { services.add(((CloudService) object)); } } return services; } }