/** * Creates a new group with the specified name. * * @param groupName the name of the group * @param ensure {@code true} to return the group if it already exists, or {@code false} to throw an exception * @return the group * @throws GroupAlreadyExistsException if the group already exists and {@code ensure} is {@code false} * @throws MetadataRepositoryException if the group could not be created */ @Nonnull private UserGroup createGroup(@Nonnull final String groupName, final boolean ensure) { final Session session = getSession(); final String safeGroupName = encodeGroupName(groupName); final String groupPath = UsersPaths.groupPath(safeGroupName).toString(); try { final Node groupsNode = session.getRootNode().getNode(UsersPaths.GROUPS.toString()); if (session.getRootNode().hasNode(groupPath)) { if (ensure) { return JcrUtil.getJcrObject(groupsNode, safeGroupName, JcrUserGroup.class); } else { throw new GroupAlreadyExistsException(groupName); } } else { return JcrUtil.getOrCreateNode(groupsNode, safeGroupName, JcrUserGroup.NODE_TYPE, JcrUserGroup.class); } } catch (RepositoryException e) { throw new MetadataRepositoryException("Failed attempting to create a new group with name: " + groupName, e); } }