class KeyLockChecker {

  public void checkKeyLocks()
  {
    try
    {
      log.debug("checking locks for {}", InetAddress.getLocalHost().toString());
      List<ChangeKeyLock> lockList = facade.getAllChangeKeyLocksByInstance(true);
      if (hsmServiceHolder.isServiceSet())
      {
        for ( ChangeKeyLock lock : lockList )
        {
          if (!hsmServiceHolder.isWorkingOnKey(lock.getKeyName()))
          {
            facade.releaseChangeKeyLock(lock);
            log.debug("lock for key {} released", lock.getKeyName());
          }
        }
      }
      List<ChangeKeyLock> foreignLockList = facade.getAllChangeKeyLocksByInstance(false);
      for ( ChangeKeyLock lock : foreignLockList )
      {
        if (facade.obtainChangeKeyLock(lock.getKeyName(), lock.getType()) != null)
        {
          if (lock.getType() == ChangeKeyLock.TYPE_DELETE)
          {
            hsmServiceHolder.deleteKey(lock.getKeyName());
            log.debug("lock for key {} stolen, deleting key", lock.getKeyName());
          }
          else
          {
            hsmServiceHolder.distributeKey(lock.getKeyName());
            log.debug("lock for key {} stolen, distributing key", lock.getKeyName());
          }
        }
      }
    }
    catch (HSMException | IOException e)
    {
      log.error("Problem during key lock check", e);
    }
  }

  public  KeyLockChecker(TerminalPermissionAO facade, HSMServiceHolder hsmServiceHolder);

}