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;
21 import org.simantics.db.indexing.exception.IndexingException;
24 * @author Tuukka Lehtonen
25 * @since 1.22.2, 1.25.0
27 public abstract class IndexingJob extends Job {
30 * NOTE: this is intentionally the same as
31 * <code>org.simantics.DatabaseJob.DATABASE_JOB_FAMILY</code> in order for
32 * this job to cause the same UI-behavior as DatabaseJob.
34 private static final String DATABASE_JOB_FAMILY = "org.simantics.db.inDatabaseJob";
36 public IndexingJob(String name) {
41 public boolean belongsTo(Object family) {
42 return DATABASE_JOB_FAMILY.equals(family);
49 * @throws DatabaseException
51 public static void jobifyIfPossible(
52 IProgressMonitor monitor,
54 DbConsumer<IProgressMonitor> consumer)
55 throws DatabaseException
57 // Prevent deadlocks by checking preconditions for using a job.
58 // JobManager is suspended e.g. during workbench startup.
59 if (Job.getJobManager().isSuspended() || Job.getJobManager().currentJob() != null) {
60 consumer.accept(monitor);
64 Semaphore barrier = new Semaphore(0);
65 Throwable[] err = { null };
66 IndexingJob job = new IndexingJob(jobName) {
68 protected IStatus run(IProgressMonitor monitor) {
70 consumer.accept(monitor);
71 } catch (Throwable t) {
77 return org.eclipse.core.runtime.Status.OK_STATUS;
84 if (err[0] instanceof IndexingException)
85 throw (DatabaseException) err[0];
86 throw new IndexingException(err[0]);
88 } catch (InterruptedException e) {
89 throw new IndexingException(e);