]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics/src/org/simantics/SleepingDatabaseJob.java
SimanticsPlatform.startUp ensures updateness of installed feature groups
[simantics/platform.git] / bundles / org.simantics / src / org / simantics / SleepingDatabaseJob.java
1 /*******************************************************************************
2  * Copyright (c) 2013 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;
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.Status;
19 import org.eclipse.core.runtime.jobs.Job;
20
21 /**
22  * A dummy database-family job that can be used for signaling that a large
23  * database job is in progress although it is technically not running in a
24  * {@link Job}.
25  * 
26  * <p>
27  * To start the job and wait for it to start, use
28  * {@link #scheduleAndWaitForRunning()}. To end the job and wait for it to die,
29  * use {@link #disposeAndJoin()}.
30  * 
31  * @author Tuukka Lehtonen
32  */
33 public class SleepingDatabaseJob extends DatabaseJob {
34
35     private final Semaphore start = new Semaphore(0);
36     private final Semaphore end = new Semaphore(0);
37
38     public SleepingDatabaseJob(String name) {
39         super(name);
40     }
41
42     @Override
43     protected final IStatus run(IProgressMonitor monitor) {
44         start.release();
45         try {
46             return work(monitor);
47         } finally {
48             try {
49                 end.acquire();
50             } catch (InterruptedException e) {
51                 // Some other party wanted to kill the job. So be it.
52             }
53         }
54     }
55
56     protected IStatus work(IProgressMonitor monitor) {
57         return Status.OK_STATUS;
58     }
59
60     public SleepingDatabaseJob scheduleAndWaitForRunning() throws InterruptedException {
61         schedule();
62         start.acquire();
63         start.release();
64         return this;
65     }
66
67     public void scheduleAndWaitForRunningUninterruptibly() {
68         schedule();
69         start.acquireUninterruptibly();
70         start.release();
71     }
72
73     public void disposeAndJoin() throws InterruptedException {
74         end.release();
75         join();
76     }
77
78     /**
79      * @param name
80      * @param runnable
81      * @throws InterruptedException
82      */
83     public static void sleepWhile(String name, Runnable runnable) throws InterruptedException {
84         SleepingDatabaseJob dbjob = new SleepingDatabaseJob(name);
85         try {
86             dbjob.scheduleAndWaitForRunning();
87             runnable.run();
88         } finally {
89             dbjob.disposeAndJoin();
90         }
91     }
92
93     /**
94      * @param name
95      * @param runnable
96      * @throws InterruptedException
97      */
98     public static void sleepUninterruptiblyWhile(String name, Runnable runnable) {
99         SleepingDatabaseJob dbjob = new SleepingDatabaseJob(name);
100         try {
101             dbjob.scheduleAndWaitForRunningUninterruptibly();
102             runnable.run();
103         } finally {
104             try {
105                 dbjob.disposeAndJoin();
106             } catch (InterruptedException e) {
107             }
108         }
109     }
110
111 }