X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=server%2Fruntime%2Forg.argeo.server.jcr%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fjcr%2FJcrUtils.java;h=0a2377b61b52ec7ba4d7d7da5856d38beae5f0c8;hb=3ff30fc87c733541ee27246e7cc3fecc52efde0c;hp=442e70d777577aa19403064f6f8b1780f7ad3696;hpb=72b40d55c0481e506ba9eabd6fcd684ef23e2f5a;p=lgpl%2Fargeo-commons.git diff --git a/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java b/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java index 442e70d77..0a2377b61 100644 --- a/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java +++ b/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java @@ -286,6 +286,30 @@ public class JcrUtils implements ArgeoJcrConstants { return path.substring(index + 1); } + /** + * Call {@link Node#getName()} without exceptions (useful in super + * constructors). + */ + public static String getNameQuietly(Node node) { + try { + return node.getName(); + } catch (RepositoryException e) { + throw new ArgeoException("Cannot get name from " + node, e); + } + } + + /** + * Call {@link Node#getProperty(String)} without exceptions (useful in super + * constructors). + */ + public static String getStringPropertyQuietly(Node node, String propertyName) { + try { + return node.getProperty(propertyName).getString(); + } catch (RepositoryException e) { + throw new ArgeoException("Cannot get name from " + node, e); + } + } + /** * Routine that get the child with this name, adding id it does not already * exist @@ -1205,7 +1229,7 @@ public class JcrUtils implements ArgeoJcrConstants { * Convenience method for adding a single privilege to a principal (user or * role), typically jcr:all */ - public static void addPrivilege(Session session, String path, + public synchronized static void addPrivilege(Session session, String path, String principal, String privilege) throws RepositoryException { List privileges = new ArrayList(); privileges.add(session.getAccessControlManager().privilegeFromName( @@ -1215,28 +1239,32 @@ public class JcrUtils implements ArgeoJcrConstants { /** * Add privileges on a path to a {@link Principal}. The path must already - * exist. Session is saved. + * exist. Session is saved. Synchronized to prevent concurrent modifications + * of the same node. */ - public static void addPrivileges(Session session, String path, + public synchronized static void addPrivileges(Session session, String path, Principal principal, List privs) throws RepositoryException { + // make sure the session is in line with the persisted state + session.refresh(false); AccessControlManager acm = session.getAccessControlManager(); AccessControlList acl = getAccessControlList(acm, path); acl.addAccessControlEntry(principal, privs.toArray(new Privilege[privs.size()])); acm.setPolicy(path, acl); - if (log.isDebugEnabled()) { - StringBuffer privBuf = new StringBuffer(); - for (Privilege priv : privs) - privBuf.append(priv.getName()); - log.debug("Added privileges " + privBuf + " to " + principal - + " on " + path); - } +// if (log.isTraceEnabled()) { +// StringBuffer privBuf = new StringBuffer(); +// for (Privilege priv : privs) +// privBuf.append(priv.getName()); +// log.trace("Added privileges " + privBuf + " to " + principal +// + " on " + path); +// } + session.refresh(true); session.save(); } /** Gets access control list for this path, throws exception if not found */ - public static AccessControlList getAccessControlList( + public synchronized static AccessControlList getAccessControlList( AccessControlManager acm, String path) throws RepositoryException { // search for an access control list AccessControlList acl = null; @@ -1263,8 +1291,8 @@ public class JcrUtils implements ArgeoJcrConstants { } /** Clear authorizations for a user at this path */ - public static void clearAccessControList(Session session, String path, - String username) throws RepositoryException { + public synchronized static void clearAccessControList(Session session, + String path, String username) throws RepositoryException { AccessControlManager acm = session.getAccessControlManager(); AccessControlList acl = getAccessControlList(acm, path); for (AccessControlEntry ace : acl.getAccessControlEntries()) {