1 /*******************************************************************************
2 * Copyright (c) 2016 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.indexing.internal;
14 import java.util.concurrent.Semaphore;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.jobs.Job;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.function.DbConsumer;
23 * @author Tuukka Lehtonen
24 * @since 1.22.2, 1.25.0
26 public abstract class IndexingJob extends Job {
29 * NOTE: this is intentionally the same as
30 * <code>org.simantics.DatabaseJob.DATABASE_JOB_FAMILY</code> in order for
31 * this job to cause the same UI-behavior as DatabaseJob.
33 private static final String DATABASE_JOB_FAMILY = "org.simantics.db.inDatabaseJob";
35 public IndexingJob(String name) {
40 public boolean belongsTo(Object family) {
41 return DATABASE_JOB_FAMILY.equals(family);
48 * @throws DatabaseException
50 public static void jobifyIfPossible(
51 IProgressMonitor monitor,
53 DbConsumer<IProgressMonitor> consumer)
54 throws DatabaseException
56 // Prevent deadlocks by checking preconditions for using a job.
57 // JobManager is suspended e.g. during workbench startup.
58 if (Job.getJobManager().isSuspended() || Job.getJobManager().currentJob() != null) {
59 consumer.accept(monitor);
63 Semaphore barrier = new Semaphore(0);
64 Throwable[] err = { null };
65 IndexingJob job = new IndexingJob(jobName) {
67 protected IStatus run(IProgressMonitor monitor) {
69 consumer.accept(monitor);
70 } catch (Throwable t) {
76 return org.eclipse.core.runtime.Status.OK_STATUS;
83 if (err[0] instanceof DatabaseException)
84 throw (DatabaseException) err[0];
85 throw new DatabaseException(err[0]);
87 } catch (InterruptedException e) {
88 throw new DatabaseException(e);