private int setAcl(StoredObjectImpl so, Acl acl) {
   int aclId;
   if (null == acl || acl.getAces().isEmpty()) {
     aclId = 0;
   } else {
     aclId = getAclId(null, acl, null);
   }
   so.setAclId(aclId);
   return aclId;
 }
 private Acl applyAcl(StoredObject so, Acl addAces, Acl removeAces) {
   int aclId = getAclId((StoredObjectImpl) so, addAces, removeAces);
   ((StoredObjectImpl) so).setAclId(aclId);
   return getAcl(aclId);
 }
  public int getAclId(StoredObjectImpl so, Acl addACEs, Acl removeACEs) {
    InMemoryAcl newAcl;
    boolean removeDefaultAcl = false;
    int aclId = 0;

    if (so == null) {
      newAcl = new InMemoryAcl();
    } else {
      aclId = so.getAclId();
      newAcl = getInMemoryAcl(aclId);
      if (null == newAcl) {
        newAcl = new InMemoryAcl();
      } else {
        // copy list so that we can safely change it without effecting
        // the original
        newAcl = new InMemoryAcl(newAcl.getAces());
      }
    }

    if (newAcl.size() == 0 && addACEs == null && removeACEs == null) {
      return 0;
    }

    if (null != removeACEs) {
      for (Ace ace : removeACEs.getAces()) {
        InMemoryAce inMemAce = new InMemoryAce(ace);
        if (inMemAce.equals(InMemoryAce.getDefaultAce())) {
          removeDefaultAcl = true;
        }
      }
    }

    if (so != null && 0 == aclId && !removeDefaultAcl) {
      return 0; // if object grants full access to everyone and it will
      // not be removed we do nothing
    }

    // add ACEs
    if (null != addACEs) {
      for (Ace ace : addACEs.getAces()) {
        InMemoryAce inMemAce = new InMemoryAce(ace);
        if (inMemAce.equals(InMemoryAce.getDefaultAce())) {
          return 0; // if everyone has full access there is no need to
        }
        // add additional ACLs.
        newAcl.addAce(inMemAce);
      }
    }

    // remove ACEs
    if (null != removeACEs) {
      for (Ace ace : removeACEs.getAces()) {
        InMemoryAce inMemAce = new InMemoryAce(ace);
        newAcl.removeAce(inMemAce);
      }
    }

    if (newAcl.size() > 0) {
      return addAcl(newAcl);
    } else {
      return 0;
    }
  }