/** * Checks if all resources in the given set of resources are be available for the given resource * user. * * @param resources The set of resources to be checked. * @param resourceUser The resource user for which to check. * @return <code>true</code> if, and only if, all resources in the given set are available for the * given resource user. */ private boolean resourcesAvailableForUser(Set<TCSResource> resources, ResourceUser resourceUser) { assert resources != null; assert resourceUser != null; for (TCSResource curResource : resources) { // Check if the resource is available. ReservationEntry entry = getReservationEntry(curResource); if (!entry.isFree() && !entry.isAllocatedBy(resourceUser)) { log.fine(resourceUser.getId() + ": Resource unavailable: " + entry.getResource()); return false; } } return true; }
/** * Returns a set of resources that is a subset of the given set of resources and is reserved/could * be released by the given resource user. * * @param resources The set of resources to be filtered for resources that could be released. * @param resourceUser The resource user that should be able to release the returned resources. * @return A set of resources that is a subset of the given set of resources and is reserved/could * be released by the given resource user. */ private Set<TCSResource> getFreeableResources( Set<TCSResource> resources, ResourceUser resourceUser) { // Make sure we're freeing only resources that are allocated by us. final Set<TCSResource> freeableResources = new HashSet<>(); for (TCSResource curRes : resources) { ReservationEntry entry = getReservationEntry(curRes); if (entry.isFree() || !entry.getResourceUser().equals(resourceUser)) { log.warning("Freed resource not reserved: " + entry.getResource()); } else { freeableResources.add(curRes); } } return freeableResources; }
@Override public void allocateNow(ResourceUser resourceUser, Set<TCSResource> resources) throws ResourceAllocationException { Objects.requireNonNull(resourceUser, "resourceUser is null"); Objects.requireNonNull(resources, "resources is null"); synchronized (reservations) { // Check if all resources are available. final Set<TCSResource> availableResources = new HashSet<>(); for (TCSResource curResource : resources) { ReservationEntry entry = getReservationEntry(curResource); if (!entry.isFree() && !entry.getResourceUser().equals(resourceUser)) { log.severe("Resource unavailable: " + curResource.getName()); // XXX DO something about it?! } else { availableResources.add(curResource); } } // Allocate all requested resources that are available. for (TCSResource curResource : availableResources) { getReservationEntry(curResource).allocate(resourceUser); } } }