]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/internal/IndexingJob.java
55cd56cfa3b7225baad12071f4d512263cc9211e
[simantics/platform.git] / bundles / org.simantics.db.indexing / src / org / simantics / db / indexing / internal / IndexingJob.java
1 /*******************************************************************************
2  * Copyright (c) 2016 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.indexing.internal;
13
14 import java.util.concurrent.Semaphore;
15
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
22 /**
23  * @author Tuukka Lehtonen
24  * @since 1.22.2, 1.25.0
25  */
26 public abstract class IndexingJob extends Job {
27
28     /**
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.
32      */
33     private static final String DATABASE_JOB_FAMILY = "org.simantics.db.inDatabaseJob";
34
35     public IndexingJob(String name) {
36         super(name);
37     }
38
39     @Override
40     public boolean belongsTo(Object family) {
41         return DATABASE_JOB_FAMILY.equals(family);
42     }
43
44     /**
45      * @param monitor
46      * @param jobName
47      * @param consumer
48      * @throws DatabaseException
49      */
50     public static void jobifyIfPossible(
51             IProgressMonitor monitor,
52             String jobName,
53             DbConsumer<IProgressMonitor> consumer)
54                     throws DatabaseException
55     {
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);
60             return;
61         }
62
63         Semaphore barrier = new Semaphore(0);
64         Throwable[] err = { null };
65         IndexingJob job = new IndexingJob(jobName) {
66             @Override
67             protected IStatus run(IProgressMonitor monitor) {
68                 try {
69                     consumer.accept(monitor);
70                 } catch (Throwable t) {
71                     err[0] = t;
72                 } finally {
73                     monitor.done();
74                     barrier.release();
75                 }
76                 return org.eclipse.core.runtime.Status.OK_STATUS;
77             }
78         };
79         job.schedule();
80         try {
81             barrier.acquire();
82             if (err[0] != null) {
83                 if (err[0] instanceof DatabaseException)
84                     throw (DatabaseException) err[0];
85                 throw new DatabaseException(err[0]);
86             }
87         } catch (InterruptedException e) {
88             throw new DatabaseException(e);
89         }
90     }
91
92 }