]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics/src/org/simantics/SleepingDatabaseJob.java
Include acorn db in db.client feature and make it the default db driver
[simantics/platform.git] / bundles / org.simantics / src / org / simantics / SleepingDatabaseJob.java
1 /*******************************************************************************\r
2  * Copyright (c) 2013 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     Semantum Oy - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics;\r
13 \r
14 import java.util.concurrent.Semaphore;\r
15 \r
16 import org.eclipse.core.runtime.IProgressMonitor;\r
17 import org.eclipse.core.runtime.IStatus;\r
18 import org.eclipse.core.runtime.Status;\r
19 import org.eclipse.core.runtime.jobs.Job;\r
20 \r
21 /**\r
22  * A dummy database-family job that can be used for signaling that a large\r
23  * database job is in progress although it is technically not running in a\r
24  * {@link Job}.\r
25  * \r
26  * <p>\r
27  * To start the job and wait for it to start, use\r
28  * {@link #scheduleAndWaitForRunning()}. To end the job and wait for it to die,\r
29  * use {@link #disposeAndJoin()}.\r
30  * \r
31  * @author Tuukka Lehtonen\r
32  */\r
33 public class SleepingDatabaseJob extends DatabaseJob {\r
34 \r
35     private final Semaphore start = new Semaphore(0);\r
36     private final Semaphore end = new Semaphore(0);\r
37 \r
38     public SleepingDatabaseJob(String name) {\r
39         super(name);\r
40     }\r
41 \r
42     @Override\r
43     protected final IStatus run(IProgressMonitor monitor) {\r
44         start.release();\r
45         try {\r
46             return work(monitor);\r
47         } finally {\r
48             try {\r
49                 end.acquire();\r
50             } catch (InterruptedException e) {\r
51                 // Some other party wanted to kill the job. So be it.\r
52             }\r
53         }\r
54     }\r
55 \r
56     protected IStatus work(IProgressMonitor monitor) {\r
57         return Status.OK_STATUS;\r
58     }\r
59 \r
60     public SleepingDatabaseJob scheduleAndWaitForRunning() throws InterruptedException {\r
61         schedule();\r
62         start.acquire();\r
63         start.release();\r
64         return this;\r
65     }\r
66 \r
67     public void scheduleAndWaitForRunningUninterruptibly() {\r
68         schedule();\r
69         start.acquireUninterruptibly();\r
70         start.release();\r
71     }\r
72 \r
73     public void disposeAndJoin() throws InterruptedException {\r
74         end.release();\r
75         join();\r
76     }\r
77 \r
78     /**\r
79      * @param name\r
80      * @param runnable\r
81      * @throws InterruptedException\r
82      */\r
83     public static void sleepWhile(String name, Runnable runnable) throws InterruptedException {\r
84         SleepingDatabaseJob dbjob = new SleepingDatabaseJob(name);\r
85         try {\r
86             dbjob.scheduleAndWaitForRunning();\r
87             runnable.run();\r
88         } finally {\r
89             dbjob.disposeAndJoin();\r
90         }\r
91     }\r
92 \r
93     /**\r
94      * @param name\r
95      * @param runnable\r
96      * @throws InterruptedException\r
97      */\r
98     public static void sleepUninterruptiblyWhile(String name, Runnable runnable) {\r
99         SleepingDatabaseJob dbjob = new SleepingDatabaseJob(name);\r
100         try {\r
101             dbjob.scheduleAndWaitForRunningUninterruptibly();\r
102             runnable.run();\r
103         } finally {\r
104             try {\r
105                 dbjob.disposeAndJoin();\r
106             } catch (InterruptedException e) {\r
107             }\r
108         }\r
109     }\r
110 \r
111 }\r