Example usage for java.util Collection clear

List of usage examples for java.util Collection clear

Introduction

In this page you can find the example usage for java.util Collection clear.

Prototype

void clear();

Source Link

Document

Removes all of the elements from this collection (optional operation).

Usage

From source file:org.sakaiproject.content.impl.BaseContentService.java

/**
 * {@inheritDoc}// w  w w. jav a2s .  c  om
 */
public Collection getEntityAuthzGroups(Reference ref, String userId) {
    // static code review possible NPE fix -AZ
    if (ref == null || ref.getId() == null) {
        M_log.warn("ref passed into getEntityAuthzGroups is not valid (ref or ref.getId is null): " + ref);
        return null;
    }

    // double check that it's mine
    if (!APPLICATION_ID.equals(ref.getType()))
        return null;

    // form a key for thread-local caching
    String threadLocalKey = "getEntityAuthzGroups@" + userId + "@" + ref.getReference();
    Collection rv = (Collection) threadLocalManager.get(threadLocalKey);
    if (rv != null) {
        return new ArrayList(rv);
    }

    // use the resources realm, all container (folder) realms

    rv = new ArrayList();
    rv.addAll(getEntityHierarchyAuthzGroups(ref));

    try {
        boolean isDropbox = false;
        boolean attachmentOverride = false;
        // special check for group-user : the grant's in the user's My Workspace site
        String parts[] = StringUtil.split(ref.getId(), Entity.SEPARATOR);
        if ((parts.length > 3) && (parts[1].equals("group-user"))) {
            rv.add(m_siteService.siteReference(m_siteService.getUserSiteId(parts[3])));
            isDropbox = true;
        }

        // If this is a site-scoped attachment, use the site grant as the only grant
        // Old attachments format: (use /content/attachment realm)
        //   /content/attachment/guid/filename.pd
        // New attachment format:
        //   /content/attachment/siteid/type/guid/filename.pd
        // But since we need to protect all paths from 
        //   /content/attachment/siteid/
        // and below we simply check to see f the guid is a valid site ID.
        if (m_siteAttachments && (parts.length >= 3) && (parts[1].equals("attachment"))) {
            String siteId = parts[2];
            if (m_siteService.siteExists(siteId)) {
                rv.clear(); // Ignore the hierarchical inheritance in /attachment
                rv.add(m_siteService.siteReference(siteId));
                rv.add(getReference(ref.getId())); // SAK-15657
                attachmentOverride = true; // Nothing else is needed
            }
        }

        ContentEntity entity = null;
        if (ref.getId().endsWith(Entity.SEPARATOR)) {
            entity = findCollection(ref.getId());
        } else {
            entity = findResource(ref.getId());
        }

        // this piece of code only comes into effect when the ref id is not resolveable as is
        if (entity == null) {
            String refId = ref.getId();
            while (entity == null && refId != null && !refId.trim().equals("")) {
                refId = isolateContainingId(refId);
                if (refId != null && !refId.trim().equals("")) {
                    entity = findCollection(refId);
                }
            }
        }

        // this will ensure the NPE does not happen
        if (entity == null) {
            M_log.warn("ref (" + ref + ") is not resolveable as an entity (it is null)");
            return null;
        }

        boolean inherited = false;
        AccessMode access = entity.getAccess();

        if (attachmentOverride) {
            // No further inheritance
        } else if (AccessMode.INHERITED.equals(access)) {
            inherited = true;
            access = entity.getInheritedAccess();
        }
        if (isDropbox || AccessMode.SITE == access || AccessMode.INHERITED == access) {
            // site
            ref.addSiteContextAuthzGroup(rv);
        } else if (AccessMode.GROUPED.equals(access)) {
            String siteReference = m_siteService.siteReference(ref.getContext());
            boolean useSiteAsContext = false;
            if (siteReference != null && userId != null) {
                useSiteAsContext = m_securityService.unlock(userId, AUTH_RESOURCE_ALL_GROUPS, siteReference);
            }
            if (useSiteAsContext) {
                ref.addSiteContextAuthzGroup(rv);
            } else if (inherited) {
                rv.addAll(entity.getInheritedGroups());
            } else {
                rv.addAll(entity.getGroups());
            }
        }
    } catch (Exception e) {
    }

    // cache in the thread
    threadLocalManager.set(threadLocalKey, new ArrayList(rv));

    if (M_log.isDebugEnabled()) {
        M_log.debug("getEntityAuthzGroups for: ref: " + ref.getReference() + " user: " + userId);
        for (Iterator i = rv.iterator(); i.hasNext();) {
            M_log.debug("** -- " + i.next());
        }
    }
    return rv;
}