X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.indexing%2Fsrc%2Forg%2Fsimantics%2Fdb%2Findexing%2Finternal%2FIndexingJob.java;fp=bundles%2Forg.simantics.db.indexing%2Fsrc%2Forg%2Fsimantics%2Fdb%2Findexing%2Finternal%2FIndexingJob.java;h=55cd56cfa3b7225baad12071f4d512263cc9211e;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=1d00a26356182110b1c5e571c05f8b2a79077ea9;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/internal/IndexingJob.java b/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/internal/IndexingJob.java index 1d00a2635..55cd56cfa 100644 --- a/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/internal/IndexingJob.java +++ b/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/internal/IndexingJob.java @@ -1,92 +1,92 @@ -/******************************************************************************* - * Copyright (c) 2016 Association for Decentralized Information Management in - * Industry THTH ry. - * 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: - * Semantum Oy - initial API and implementation - *******************************************************************************/ -package org.simantics.db.indexing.internal; - -import java.util.concurrent.Semaphore; - -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.jobs.Job; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.function.DbConsumer; - -/** - * @author Tuukka Lehtonen - * @since 1.22.2, 1.25.0 - */ -public abstract class IndexingJob extends Job { - - /** - * NOTE: this is intentionally the same as - * org.simantics.DatabaseJob.DATABASE_JOB_FAMILY in order for - * this job to cause the same UI-behavior as DatabaseJob. - */ - private static final String DATABASE_JOB_FAMILY = "org.simantics.db.inDatabaseJob"; - - public IndexingJob(String name) { - super(name); - } - - @Override - public boolean belongsTo(Object family) { - return DATABASE_JOB_FAMILY.equals(family); - } - - /** - * @param monitor - * @param jobName - * @param consumer - * @throws DatabaseException - */ - public static void jobifyIfPossible( - IProgressMonitor monitor, - String jobName, - DbConsumer consumer) - throws DatabaseException - { - // Prevent deadlocks by checking preconditions for using a job. - // JobManager is suspended e.g. during workbench startup. - if (Job.getJobManager().isSuspended() || Job.getJobManager().currentJob() != null) { - consumer.accept(monitor); - return; - } - - Semaphore barrier = new Semaphore(0); - Throwable[] err = { null }; - IndexingJob job = new IndexingJob(jobName) { - @Override - protected IStatus run(IProgressMonitor monitor) { - try { - consumer.accept(monitor); - } catch (Throwable t) { - err[0] = t; - } finally { - monitor.done(); - barrier.release(); - } - return org.eclipse.core.runtime.Status.OK_STATUS; - } - }; - job.schedule(); - try { - barrier.acquire(); - if (err[0] != null) { - if (err[0] instanceof DatabaseException) - throw (DatabaseException) err[0]; - throw new DatabaseException(err[0]); - } - } catch (InterruptedException e) { - throw new DatabaseException(e); - } - } - -} +/******************************************************************************* + * Copyright (c) 2016 Association for Decentralized Information Management in + * Industry THTH ry. + * 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: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package org.simantics.db.indexing.internal; + +import java.util.concurrent.Semaphore; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.jobs.Job; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.function.DbConsumer; + +/** + * @author Tuukka Lehtonen + * @since 1.22.2, 1.25.0 + */ +public abstract class IndexingJob extends Job { + + /** + * NOTE: this is intentionally the same as + * org.simantics.DatabaseJob.DATABASE_JOB_FAMILY in order for + * this job to cause the same UI-behavior as DatabaseJob. + */ + private static final String DATABASE_JOB_FAMILY = "org.simantics.db.inDatabaseJob"; + + public IndexingJob(String name) { + super(name); + } + + @Override + public boolean belongsTo(Object family) { + return DATABASE_JOB_FAMILY.equals(family); + } + + /** + * @param monitor + * @param jobName + * @param consumer + * @throws DatabaseException + */ + public static void jobifyIfPossible( + IProgressMonitor monitor, + String jobName, + DbConsumer consumer) + throws DatabaseException + { + // Prevent deadlocks by checking preconditions for using a job. + // JobManager is suspended e.g. during workbench startup. + if (Job.getJobManager().isSuspended() || Job.getJobManager().currentJob() != null) { + consumer.accept(monitor); + return; + } + + Semaphore barrier = new Semaphore(0); + Throwable[] err = { null }; + IndexingJob job = new IndexingJob(jobName) { + @Override + protected IStatus run(IProgressMonitor monitor) { + try { + consumer.accept(monitor); + } catch (Throwable t) { + err[0] = t; + } finally { + monitor.done(); + barrier.release(); + } + return org.eclipse.core.runtime.Status.OK_STATUS; + } + }; + job.schedule(); + try { + barrier.acquire(); + if (err[0] != null) { + if (err[0] instanceof DatabaseException) + throw (DatabaseException) err[0]; + throw new DatabaseException(err[0]); + } + } catch (InterruptedException e) { + throw new DatabaseException(e); + } + } + +}