/* * Copyright 2000-2020 JetBrains s.r.o. * * Licensed under 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. */ package jetbrains.teamcity; import jetbrains.buildServer.ConnectionStatusListener; import jetbrains.buildServer.IdeAlarm; import jetbrains.buildServer.TeamCitySnapshot; import jetbrains.teamcity.core.Debug; import jetbrains.teamcity.core.Messages; import jetbrains.teamcity.core.Util; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.jobs.Job; import org.eclipse.ui.progress.IProgressService; import org.eclipse.ui.progress.IWorkbenchSiteProgressService; import org.jetbrains.annotations.Nullable; import java.text.MessageFormat; public class EclipseIDEAlarm implements IdeAlarm { private volatile boolean myIsDisposed; private static final String UPDATE_SUMMARY_REQUEST_TASK_NAME = "Update Summary"; public void addRequest(@Nullable final Runnable action, int delay, @Nullable final String taskName) { addRequest(action, delay, taskName, null, false); } public void addRequest(@Nullable final Runnable action, int delay, @Nullable String taskName, @Nullable final IProgressService service, boolean user) { if (action == null) { return; } if (myIsDisposed) { return; } if (!Util.isLoggedIn()) { return; } boolean isUpdate = false; // Check task name if (taskName == null) { taskName = Messages.getString("TeamCityIDEAlarm.system.job.name"); } else if (UPDATE_SUMMARY_REQUEST_TASK_NAME.equals(taskName)) { isUpdate = true; taskName = Messages.getString("TeamCityIDEAlarm.update.summary.job.name"); //$NON-NLS-1$ } final String finalTaskName = taskName; Job job = new Job(finalTaskName) { //$NON-NLS-1$ @SuppressWarnings("ThrowableResultOfMethodCallIgnored") @Override protected IStatus run(IProgressMonitor monitor) { try { if (!monitor.isCanceled()) { final TeamCitySnapshot snapshot = Activator.getDefault().getSnapshot(); snapshot.setProblem(null); snapshot.getProcessManager().performAction(action, finalTaskName, ConnectionStatusListener.ErrorProcessingKind.RETHROW); final Throwable problem = snapshot.getProblem(); //check Error and generate warning if (problem != null) { return new Status(IStatus.WARNING, Activator.PLUGIN_ID, IStatus.OK, snapshot.getProblemDescription(), problem); } } else { Debug.getInstance().log(Debug.DEBUG_PLATFORM, MessageFormat.format("TeamCityIDEAlarm: Job \"{0}\" execution canceled", this)); //$NON-NLS-1$ } } catch (Throwable th) { return new Status(IStatus.WARNING, Activator.PLUGIN_ID, IStatus.OK, th.getMessage() != null ? th.getMessage() : "Unexpected error", th); //$NON-NLS-1$ } return Status.OK_STATUS; } @Override public boolean belongsTo(Object family) { return family == EclipseIDEAlarm.this; } }; job.setRule(Constants.TEAMCITY_SCHEDULING_RULE); if (service != null) { job.setSystem(false); if (service instanceof IWorkbenchSiteProgressService) { ((IWorkbenchSiteProgressService) service).schedule(job, delay, false); } else { service.showInDialog(null, job); } } else { job.setSystem(!isUpdate && !user); job.schedule(delay); } } public void cancelAllRequests() { Util.cancelJobs(Job.getJobManager().find(this)); } public void dispose() { cancelAllRequests(); myIsDisposed = true; } }