]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/internal/IndexingJob.java
Replace instantiations of DatabaseException in indexing
[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 import org.simantics.db.indexing.exception.IndexingException;
22
23 /**
24  * @author Tuukka Lehtonen
25  * @since 1.22.2, 1.25.0
26  */
27 public abstract class IndexingJob extends Job {
28
29     /**
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.
33      */
34     private static final String DATABASE_JOB_FAMILY = "org.simantics.db.inDatabaseJob";
35
36     public IndexingJob(String name) {
37         super(name);
38     }
39
40     @Override
41     public boolean belongsTo(Object family) {
42         return DATABASE_JOB_FAMILY.equals(family);
43     }
44
45     /**
46      * @param monitor
47      * @param jobName
48      * @param consumer
49      * @throws DatabaseException
50      */
51     public static void jobifyIfPossible(
52             IProgressMonitor monitor,
53             String jobName,
54             DbConsumer<IProgressMonitor> consumer)
55                     throws DatabaseException
56     {
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);
61             return;
62         }
63
64         Semaphore barrier = new Semaphore(0);
65         Throwable[] err = { null };
66         IndexingJob job = new IndexingJob(jobName) {
67             @Override
68             protected IStatus run(IProgressMonitor monitor) {
69                 try {
70                     consumer.accept(monitor);
71                 } catch (Throwable t) {
72                     err[0] = t;
73                 } finally {
74                     monitor.done();
75                     barrier.release();
76                 }
77                 return org.eclipse.core.runtime.Status.OK_STATUS;
78             }
79         };
80         job.schedule();
81         try {
82             barrier.acquire();
83             if (err[0] != null) {
84                 if (err[0] instanceof IndexingException)
85                     throw (DatabaseException) err[0];
86                 throw new IndexingException(err[0]);
87             }
88         } catch (InterruptedException e) {
89             throw new IndexingException(e);
90         }
91     }
92
93 }