MemoryLockManager extends AbstractLockingManagement { @SuppressFBWarnings( value = {"MWN_MISMATCHED_WAIT"}, justification = "incorrect flag; lock held (in synchronized block)") @Override public void lock(final AuthorizedLock lock, final String featureID) { AuthorizedLock featureLock = null; synchronized (locks) { featureLock = locks.get(featureID); if ((featureLock == null) || featureLock.isStale()) { featureLock = lock; locks.put(featureID, lock); return; } else if (featureLock.isAuthorized(lock)) { return; } } while (featureLock != lock) { synchronized (featureLock) { while (!featureLock.isStale()) { try { featureLock.wait( Math.min(5000, featureLock.getExpireTime() - System.currentTimeMillis())); } catch (final InterruptedException ex) { } catch (final Exception e) { LOGGER.error( "Memory lock manager filed to wait for lock release. Will cycle till lock is stale.", e); } } } synchronized (locks) { featureLock = locks.get(featureID); if ((featureLock == null) || featureLock.isStale()) { locks.put(featureID, lock); featureLock = lock; } } } } MemoryLockManager(final String instanceName);  MemoryLockManager(final GeoWavePluginConfig pluginConfig); @Override void releaseAll(final AuthorizedLock lock); @Override void resetAll(final AuthorizedLock lock); @SuppressFBWarnings( value = {"MWN_MISMATCHED_WAIT"}, justification = "incorrect flag; lock held (in synchronized block)") @Override void lock(final AuthorizedLock lock, final String featureID); @Override boolean exists(final String authID); @Override void unlock(final AuthorizedLock lock, final String featureID);  }
@Test public void testRelockLock() throws InterruptedException, IOException { final LockingManagement memoryLockManager = new MemoryLockManager("default"); final DefaultTransaction t1 = new DefaultTransaction(); memoryLockManager.lock(t1, "f8"); memoryLockManager.lock(t1, "f8"); t1.commit(); t1.close(); }
@Test public void testLockWithProperAuth() throws InterruptedException, IOException { final LockingManagement memoryLockManager = new MemoryLockManager("default"); final Transaction t1 = Transaction.AUTO_COMMIT; final DefaultTransaction t2 = new DefaultTransaction(); t2.addAuthorization("auth5"); final FeatureLock lock = new FeatureLock("auth5", 1 ); memoryLockManager.lockFeatureID("sometime", "f5", t1, lock); final Thread commiter = new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(4000); memoryLockManager.release("auth5", t1); } catch (final InterruptedException e) { e.printStackTrace(); throw new RuntimeException(e); } catch (final IOException e) { e.printStackTrace(); throw new RuntimeException(e); } } }); final long currentTime = System.currentTimeMillis(); commiter.start(); memoryLockManager.lock(t2, "f5"); assertTrue((System.currentTimeMillis() - currentTime) < 4000); commiter.join(); }
@Test public void testBlockinLock() throws InterruptedException, IOException { final LockingManagement memoryLockManager = new MemoryLockManager(UUID.randomUUID().toString()); final DefaultTransaction t1 = new DefaultTransaction(); memoryLockManager.lock(t1, "f3"); final DefaultTransaction t2 = new DefaultTransaction(); final Thread commiter = new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(4000); t1.commit(); } catch (final InterruptedException e) { e.printStackTrace(); throw new RuntimeException(e); } catch (final IOException e) { e.printStackTrace(); throw new RuntimeException(e); } } }); final long currentTime = System.currentTimeMillis(); commiter.start(); memoryLockManager.lock(t2, "f3"); final long endTime = System.currentTimeMillis(); assertTrue((endTime - currentTime) >= 3800); commiter.join(); t2.commit(); t2.close(); t1.close(); }