Example #1
0
  @Override
  public void SetDefaultGroup(PDPGroup group) throws PAPException {

    boolean changesMade = false;
    for (PDPGroup aGroup : groups) {
      if (aGroup.getId().equals(group.getId())) {
        if (!aGroup.isDefaultGroup()) {
          // TODO - since the original code checked for type we do also.
          if (aGroup instanceof StdPDPGroup) {
            ((StdPDPGroup) aGroup).setDefault(true);
            changesMade = true;
          } else {
            throw new IllegalArgumentException(
                "Group in groups of unknown type '" + aGroup.getClass().getName() + "'");
          }
        }
      } else {
        // not the new default group
        if (aGroup.isDefaultGroup()) {
          // TODO - since the original code checked for type we do also.
          if (aGroup instanceof StdPDPGroup) {
            ((StdPDPGroup) aGroup).setDefault(false);
            changesMade = true;
          } else {
            throw new IllegalArgumentException(
                "Group in groups of unknown type '" + aGroup.getClass().getName() + "'");
          }
        }
      }
    }
    if (changesMade) {
      this.doSave();
    }
  }
Example #2
0
 @Override
 public void movePDP(PDP pdp, PDPGroup newGroup) throws PAPException {
   if (newGroup == null) {
     throw new NullPointerException("You must specify which group the PDP will belong to.");
   }
   PDPGroup currentGroup = this.getPDPGroup(pdp);
   if (currentGroup == null) {
     throw new PAPException("PDP must already belong to a group.");
   }
   if (currentGroup.equals(newGroup)) {
     logger.warn("Already in that group.");
     return;
   }
   if (currentGroup instanceof StdPDPGroup && newGroup instanceof StdPDPGroup) {
     if (((StdPDPGroup) currentGroup).removePDP(pdp)) {
       boolean result = ((StdPDPGroup) newGroup).addPDP(pdp);
       if (result) {
         //
         // Save the configuration
         //
         this.doSave();
       } else {
         logger.error("Failed to add to new group, putting back into original group.");
         if (!((StdPDPGroup) currentGroup).removePDP(pdp)) {
           logger.error("Failed to put PDP back into original group.");
         }
       }
     }
   } else {
     String message =
         "Unknown PDP group class: "
             + newGroup.getClass().getCanonicalName()
             + " and "
             + currentGroup.getClass().getCanonicalName();
     logger.warn(message);
     throw new PAPException(message);
   }
 }
Example #3
0
 @Override
 public void removePDP(PDP pdp) throws PAPException {
   PDPGroup group = this.getPDPGroup(pdp);
   if (group == null) {
     throw new NullPointerException();
   }
   if (group instanceof StdPDPGroup) {
     boolean result = ((StdPDPGroup) group).removePDP(pdp);
     if (result) {
       this.doSave();
     }
     return;
   }
   String message = "Unknown PDP group class: " + group.getClass().getCanonicalName();
   logger.warn(message);
   throw new PAPException(message);
 }
Example #4
0
  @Override
  public void removeGroup(PDPGroup group, PDPGroup newGroup)
      throws PAPException, NullPointerException {
    if (group == null) {
      throw new NullPointerException();
    }
    //
    // Does this group exist?
    //
    if (!this.groups.contains(group)) {
      logger.error("This group doesn't exist.");
      throw new PAPException("The group '" + group.getId() + "' does not exist");
    }
    //
    // Is it the default group?
    //
    if (group.isDefaultGroup()) {
      throw new PAPException("You cannot delete the default group.");
    }
    Set<PDP> pdps = group.getPdps();
    //
    // Are there PDPs? If so, then we need a target group
    //
    if (!pdps.isEmpty() && newGroup == null) {
      throw new NullPointerException(
          "Group targeted for deletion has PDPs, you must provide a new group for them.");
    }
    //
    // Move the PDPs
    //
    if (!pdps.isEmpty()) {
      if (!(newGroup instanceof StdPDPGroup)) {
        throw new PAPException(
            "Unexpected class for newGroup: " + newGroup.getClass().getCanonicalName());
      }
      // The movePDP function will modify the set of PDPs in the group.
      // To avoid concurrent modification exceptions we need to duplicate the list before calling
      // that
      // function.
      List<PDP> pdpList = new ArrayList<PDP>();
      for (PDP pdp : pdps) {
        pdpList.add(pdp);
      }
      // now we can use the PDPs from the list without having ConcurrentAccessExceptions
      for (PDP pdp : pdpList) {
        this.movePDP(pdp, newGroup);
      }
    }
    //
    // remove the directory for the group
    //
    String id = group.getId();
    Path groupPath = Paths.get(this.repository.toString(), id);
    //
    // If it exists already
    //
    if (!Files.exists(groupPath)) {
      logger.warn("removeGroup " + id + " directory does not exist" + groupPath.toString());
    } else {
      try {
        Files.walkFileTree(
            groupPath,
            new SimpleFileVisitor<Path>() {

              @Override
              public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                  throws IOException {
                Files.delete(file);
                return super.visitFile(file, attrs);
              }
            });
        //
        // delete the directory
        //
        Files.delete(groupPath);
      } catch (IOException e) {
        logger.error("Failed to delete " + groupPath + ": " + e);
        throw new PAPException("Failed to delete " + id);
      }
    }

    // remove the group from the set of all groups
    groups.remove(group);

    //
    // Save changes
    //
    changed();
    this.doSave();
  }