-/*******************************************************************************\r
- * Copyright (c) 2016 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * Semantum Oy - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.db.indexing.internal;\r
-\r
-import java.util.concurrent.Semaphore;\r
-\r
-import org.eclipse.core.runtime.IProgressMonitor;\r
-import org.eclipse.core.runtime.IStatus;\r
-import org.eclipse.core.runtime.jobs.Job;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.function.DbConsumer;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- * @since 1.22.2, 1.25.0\r
- */\r
-public abstract class IndexingJob extends Job {\r
-\r
- /**\r
- * NOTE: this is intentionally the same as\r
- * <code>org.simantics.DatabaseJob.DATABASE_JOB_FAMILY</code> in order for\r
- * this job to cause the same UI-behavior as DatabaseJob.\r
- */\r
- private static final String DATABASE_JOB_FAMILY = "org.simantics.db.inDatabaseJob";\r
-\r
- public IndexingJob(String name) {\r
- super(name);\r
- }\r
-\r
- @Override\r
- public boolean belongsTo(Object family) {\r
- return DATABASE_JOB_FAMILY.equals(family);\r
- }\r
-\r
- /**\r
- * @param monitor\r
- * @param jobName\r
- * @param consumer\r
- * @throws DatabaseException\r
- */\r
- public static void jobifyIfPossible(\r
- IProgressMonitor monitor,\r
- String jobName,\r
- DbConsumer<IProgressMonitor> consumer)\r
- throws DatabaseException\r
- {\r
- // Prevent deadlocks by checking preconditions for using a job.\r
- // JobManager is suspended e.g. during workbench startup.\r
- if (Job.getJobManager().isSuspended() || Job.getJobManager().currentJob() != null) {\r
- consumer.accept(monitor);\r
- return;\r
- }\r
-\r
- Semaphore barrier = new Semaphore(0);\r
- Throwable[] err = { null };\r
- IndexingJob job = new IndexingJob(jobName) {\r
- @Override\r
- protected IStatus run(IProgressMonitor monitor) {\r
- try {\r
- consumer.accept(monitor);\r
- } catch (Throwable t) {\r
- err[0] = t;\r
- } finally {\r
- monitor.done();\r
- barrier.release();\r
- }\r
- return org.eclipse.core.runtime.Status.OK_STATUS;\r
- }\r
- };\r
- job.schedule();\r
- try {\r
- barrier.acquire();\r
- if (err[0] != null) {\r
- if (err[0] instanceof DatabaseException)\r
- throw (DatabaseException) err[0];\r
- throw new DatabaseException(err[0]);\r
- }\r
- } catch (InterruptedException e) {\r
- throw new DatabaseException(e);\r
- }\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2016 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Semantum Oy - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.indexing.internal;
+
+import java.util.concurrent.Semaphore;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.jobs.Job;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.function.DbConsumer;
+
+/**
+ * @author Tuukka Lehtonen
+ * @since 1.22.2, 1.25.0
+ */
+public abstract class IndexingJob extends Job {
+
+ /**
+ * NOTE: this is intentionally the same as
+ * <code>org.simantics.DatabaseJob.DATABASE_JOB_FAMILY</code> in order for
+ * this job to cause the same UI-behavior as DatabaseJob.
+ */
+ private static final String DATABASE_JOB_FAMILY = "org.simantics.db.inDatabaseJob";
+
+ public IndexingJob(String name) {
+ super(name);
+ }
+
+ @Override
+ public boolean belongsTo(Object family) {
+ return DATABASE_JOB_FAMILY.equals(family);
+ }
+
+ /**
+ * @param monitor
+ * @param jobName
+ * @param consumer
+ * @throws DatabaseException
+ */
+ public static void jobifyIfPossible(
+ IProgressMonitor monitor,
+ String jobName,
+ DbConsumer<IProgressMonitor> consumer)
+ throws DatabaseException
+ {
+ // Prevent deadlocks by checking preconditions for using a job.
+ // JobManager is suspended e.g. during workbench startup.
+ if (Job.getJobManager().isSuspended() || Job.getJobManager().currentJob() != null) {
+ consumer.accept(monitor);
+ return;
+ }
+
+ Semaphore barrier = new Semaphore(0);
+ Throwable[] err = { null };
+ IndexingJob job = new IndexingJob(jobName) {
+ @Override
+ protected IStatus run(IProgressMonitor monitor) {
+ try {
+ consumer.accept(monitor);
+ } catch (Throwable t) {
+ err[0] = t;
+ } finally {
+ monitor.done();
+ barrier.release();
+ }
+ return org.eclipse.core.runtime.Status.OK_STATUS;
+ }
+ };
+ job.schedule();
+ try {
+ barrier.acquire();
+ if (err[0] != null) {
+ if (err[0] instanceof DatabaseException)
+ throw (DatabaseException) err[0];
+ throw new DatabaseException(err[0]);
+ }
+ } catch (InterruptedException e) {
+ throw new DatabaseException(e);
+ }
+ }
+
+}