Search in sources :

Example 1 with SyncerCallback

use of org.olat.core.util.coordinate.SyncerCallback in project OpenOLAT by OpenOLAT.

the class OLATResourceManager method findOrPersistResourceable.

/**
 * Find the OLATResource for the resourceable. If not found, a new
 * OLATResource is created and returned.
 *
 * @param resourceable
 * @return an OLATResource representing the resourceable.
 */
public OLATResource findOrPersistResourceable(final OLATResourceable resourceable) {
    if (resourceable.getResourceableTypeName() == null)
        throw new AssertException("typename of olatresourceable can not be null");
    // First try to find resourceable without synchronization
    OLATResource ores = findResourceable(resourceable);
    if (ores != null) {
        return ores;
    }
    // Second there exists no resourcable => try to find and create(if no exists) in a synchronized block
    // o_clusterOK by:cg
    ores = CoordinatorManager.getInstance().getCoordinator().getSyncer().doInSync(resourceable, new SyncerCallback<OLATResource>() {

        public OLATResource execute() {
            logDebug("start synchronized-block in findOrPersistResourceable");
            OLATResource oresSync = findResourceable(resourceable);
            // if not found, persist it.
            if (oresSync == null) {
                if (CourseModule.ORES_TYPE_COURSE.equals(resourceable.getResourceableTypeName())) {
                    logInfo("OLATResourceManager - createOLATResourceInstance if not found: " + resourceable.getResourceableTypeName() + " " + resourceable.getResourceableId());
                }
                oresSync = createOLATResourceInstance(resourceable);
                saveOLATResource(oresSync);
            }
            return oresSync;
        }
    });
    return ores;
}
Also used : AssertException(org.olat.core.logging.AssertException) SyncerCallback(org.olat.core.util.coordinate.SyncerCallback)

Example 2 with SyncerCallback

use of org.olat.core.util.coordinate.SyncerCallback in project OpenOLAT by OpenOLAT.

the class FileResourceManager method unzipFileResource.

private final File unzipFileResource(final OLATResourceable res, final File dir) {
    if (!dir.exists()) {
        return null;
    }
    File zipTargetDir = new File(dir, ZIPDIR);
    if (!zipTargetDir.exists()) {
        // if not unzipped yet, synchronize all unzipping processes
        // o_clusterOK by:ld
        zipTargetDir = CoordinatorManager.getInstance().getCoordinator().getSyncer().doInSync(res, new SyncerCallback<File>() {

            public File execute() {
                File targetDir = null;
                // now we are the only one unzipping. We
                // only need to unzip when the previous
                // threads that aquired this lock have not unzipped "our" version's
                // resources yet
                targetDir = new File(dir, ZIPDIR);
                if (!targetDir.exists()) {
                    // means I am the first to unzip this
                    // version's resource
                    targetDir.mkdir();
                    File zipFile = getFileResource(res);
                    if (!ZipUtil.unzip(zipFile, targetDir)) {
                        return null;
                    }
                }
                return targetDir;
            }
        });
    }
    return zipTargetDir;
}
Also used : SyncerCallback(org.olat.core.util.coordinate.SyncerCallback) File(java.io.File)

Example 3 with SyncerCallback

use of org.olat.core.util.coordinate.SyncerCallback in project OpenOLAT by OpenOLAT.

the class CoordinatorTest method testNestedAssertExceptionInDoInSync.

@Test(expected = AssertException.class)
public void testNestedAssertExceptionInDoInSync() {
    final OLATResourceable ores = OresHelper.createOLATResourceableInstance("testNestedAssertExceptionInDoInSync", new Long("123"));
    CoordinatorManager.getInstance().getCoordinator().getSyncer().doInSync(ores, new SyncerCallback<Boolean>() {

        public Boolean execute() {
            log.info("testNestedAssertExceptionInDoInSync: execute doInSync 1");
            // Do agin in sync => nested => no allowed!
            CoordinatorManager.getInstance().getCoordinator().getSyncer().doInSync(ores, new SyncerCallback<Boolean>() {

                public Boolean execute() {
                    log.info("testNestedAssertExceptionInDoInSync: execute doInSync 2");
                    fail("No NestedAssertException thrown");
                    return Boolean.TRUE;
                }
            });
            return Boolean.TRUE;
        }
    });
// end syncerCallback
}
Also used : OLATResourceable(org.olat.core.id.OLATResourceable) SyncerCallback(org.olat.core.util.coordinate.SyncerCallback) Test(org.junit.Test)

Example 4 with SyncerCallback

use of org.olat.core.util.coordinate.SyncerCallback in project openolat by klemens.

the class CollaborationTools method getForum.

public Forum getForum() {
    final ForumManager fom = ForumManager.getInstance();
    final NarrowedPropertyManager npm = NarrowedPropertyManager.getInstance(ores);
    Property forumProperty = npm.findProperty(null, null, PROP_CAT_BG_COLLABTOOLS, KEY_FORUM);
    Forum forum;
    if (forumProperty != null) {
        forum = fom.loadForum(forumProperty.getLongValue());
    } else {
        forum = coordinatorManager.getCoordinator().getSyncer().doInSync(ores, new SyncerCallback<Forum>() {

            @Override
            public Forum execute() {
                Forum aforum;
                Long forumKey;
                Property forumKeyProperty = npm.findProperty(null, null, PROP_CAT_BG_COLLABTOOLS, KEY_FORUM);
                if (forumKeyProperty == null) {
                    // First call of forum, create new forum and save
                    aforum = fom.addAForum();
                    forumKey = aforum.getKey();
                    if (log.isDebug()) {
                        log.debug("created new forum in collab tools: foid::" + forumKey.longValue() + " for ores::" + ores.getResourceableTypeName() + "/" + ores.getResourceableId());
                    }
                    forumKeyProperty = npm.createPropertyInstance(null, null, PROP_CAT_BG_COLLABTOOLS, KEY_FORUM, null, forumKey, null, null);
                    npm.saveProperty(forumKeyProperty);
                } else {
                    // Forum does already exist, load forum with key from properties
                    forumKey = forumKeyProperty.getLongValue();
                    aforum = fom.loadForum(forumKey);
                    if (aforum == null) {
                        throw new AssertException("Unable to load forum with key " + forumKey.longValue() + " for ores " + ores.getResourceableTypeName() + " with key " + ores.getResourceableId());
                    }
                    if (log.isDebug()) {
                        log.debug("loading forum in collab tools from properties: foid::" + forumKey.longValue() + " for ores::" + ores.getResourceableTypeName() + "/" + ores.getResourceableId());
                    }
                }
                return aforum;
            }
        });
    }
    return forum;
}
Also used : AssertException(org.olat.core.logging.AssertException) ForumManager(org.olat.modules.fo.manager.ForumManager) NarrowedPropertyManager(org.olat.properties.NarrowedPropertyManager) SyncerCallback(org.olat.core.util.coordinate.SyncerCallback) Property(org.olat.properties.Property) Forum(org.olat.modules.fo.Forum)

Example 5 with SyncerCallback

use of org.olat.core.util.coordinate.SyncerCallback in project openolat by klemens.

the class OLATResourceManager method findOrPersistResourceable.

/**
 * Find the OLATResource for the resourceable. If not found, a new
 * OLATResource is created and returned.
 *
 * @param resourceable
 * @return an OLATResource representing the resourceable.
 */
public OLATResource findOrPersistResourceable(final OLATResourceable resourceable) {
    if (resourceable.getResourceableTypeName() == null)
        throw new AssertException("typename of olatresourceable can not be null");
    // First try to find resourceable without synchronization
    OLATResource ores = findResourceable(resourceable);
    if (ores != null) {
        return ores;
    }
    // Second there exists no resourcable => try to find and create(if no exists) in a synchronized block
    // o_clusterOK by:cg
    ores = CoordinatorManager.getInstance().getCoordinator().getSyncer().doInSync(resourceable, new SyncerCallback<OLATResource>() {

        public OLATResource execute() {
            logDebug("start synchronized-block in findOrPersistResourceable");
            OLATResource oresSync = findResourceable(resourceable);
            // if not found, persist it.
            if (oresSync == null) {
                if (CourseModule.ORES_TYPE_COURSE.equals(resourceable.getResourceableTypeName())) {
                    logInfo("OLATResourceManager - createOLATResourceInstance if not found: " + resourceable.getResourceableTypeName() + " " + resourceable.getResourceableId());
                }
                oresSync = createOLATResourceInstance(resourceable);
                saveOLATResource(oresSync);
            }
            return oresSync;
        }
    });
    return ores;
}
Also used : AssertException(org.olat.core.logging.AssertException) SyncerCallback(org.olat.core.util.coordinate.SyncerCallback)

Aggregations

SyncerCallback (org.olat.core.util.coordinate.SyncerCallback)8 AssertException (org.olat.core.logging.AssertException)4 File (java.io.File)2 Test (org.junit.Test)2 OLATResourceable (org.olat.core.id.OLATResourceable)2 Forum (org.olat.modules.fo.Forum)2 ForumManager (org.olat.modules.fo.manager.ForumManager)2 NarrowedPropertyManager (org.olat.properties.NarrowedPropertyManager)2 Property (org.olat.properties.Property)2