Example #1
0
  /**
   * Deletes a topic, including all of its messages and TopicUsers
   *
   * @param topic The topic to delete
   * @param userRequestingDeletion The user who requested the topic be deleted
   * @throws AccessDeniedException
   */
  public void deleteTopic(JMSTopic topic, JMSUser userRequestingDeletion)
      throws AccessDeniedException {
    if (userRequestingDeletion.equals(topic.getOwner())) {
      // If the topic does not contain null fields
      if (isValidTopic(topic)) {
        try {
          Transaction transaction = TransactionHelper.getTransaction(10000l);

          space.takeIfExists(topic, transaction, 3000l);

          deleteAllTopicUsers(topic, transaction);
          MessageService.getMessageService().deleteAllTopicMessages(topic, transaction);

          // Writes a JMSTopicDeleted object so listeners know the
          // topic
          // has been removed
          space.write(new JMSTopicDeleted(topic), transaction, 1000l * 60l);

          transaction.commit();
        } catch (Exception e) {
          e.printStackTrace();
        }
      } else {
        System.err.println(
            "Attempted to delete topic with on or more null fields.  "
                + "Due to how JavaSpaces work, this will delete one at random and is not allowed.");
      }
    } else {
      throw new AccessDeniedException("Only the topic's owner can delete the topic");
    }
  }
Example #2
0
  /**
   * Writes a topic to the JavaSpace
   *
   * @param topic
   * @throws Exception
   */
  public Lease createTopic(JMSTopic topic)
      throws DuplicateEntryException, InvalidAttributeValueException, RemoteException,
          TransactionException {
    Transaction transaction = TransactionHelper.getTransaction(3000);
    Lease lease = null;

    try {
      // If the topic is valid (non-null name, base name, and ID)
      if (isValidTopic(topic)) {
        // If the topic does not already exist in the space
        if (!topicExistsInSpace(topic, transaction)) {
          // Write it to the space and commit the transaction
          lease = space.write(topic, transaction, Lease.FOREVER);
          transaction.commit();
        } else {
          // Otherwise it's a duplicate, so throw an exception
          throw new DuplicateEntryException(
              "Failed to create topic.  Topic baseName or id matches with an existing topic.");
        }
      } else {
        // Otherwise the Topic object is invalid, so throw an exception
        throw new InvalidAttributeValueException(
            "Topic being creates is invalid (one or more fields null)");
      }
    } catch (DuplicateEntryException
        | InvalidAttributeValueException
        | RemoteException
        | TransactionException e) {
      // If anything in the try block throws an error, abort the
      // transaction before rethrowing the error
      if (transaction != null) {
        try {
          transaction.abort();
        } catch (Exception e1) {
          System.err.println("Failed to abort transaction");
          e1.printStackTrace();
        }
      }

      throw e;
    }

    return lease;
  }
Example #3
0
  /**
   * Removes a given user from a given topic
   *
   * @param topic The topic to remove the user from
   * @param user The user to remove from the topic
   */
  public void removeTopicUser(JMSTopic topic, JMSUser user) {
    try {
      Transaction transaction = TransactionHelper.getTransaction();

      boolean removed = false;
      JMSTopicUser template = new JMSTopicUser(topic, user);

      // If the space is in a bad state and has duplicate users in a
      // topic, this while loop ensures they are all removed
      while (space.takeIfExists(template, transaction, 1000) != null) {
        removed = true;
      }

      // Put a JMSTopicUserRemoved object in the space so listeners can
      // pick them up and remove them from other user's lists
      if (removed) {
        JMSTopicUserRemoved removedTopicUser =
            new JMSTopicUserRemoved(template.getTopic(), template.getUser());

        // Writes the JMSTopicUserRemoved with a 60 second lease, so
        // listeners have 60 seconds to act on it
        space.write(removedTopicUser, transaction, 1000l * 60l);
      }

      transaction.commit();
    } catch (RemoteException
        | UnusableEntryException
        | TransactionException
        | InterruptedException e) {
      System.err.println(
          "Failed to remove user from topic.  "
              + "User ID: '"
              + user.getId().toString()
              + "' && Topic ID: '"
              + topic.getId().toString()
              + "'");
      e.printStackTrace();
    }
  }
Example #4
0
  /**
   * Adds a given user to a given topic.
   *
   * @param topic The topic to add the user to
   * @param user The user to add to the topic
   */
  public Lease addTopicUser(JMSTopic topic, JMSUser user) {
    JMSTopicUser topicUser = new JMSTopicUser(topic, user);
    Lease lease = null;

    try {
      Transaction transaction = TransactionHelper.getTransaction();

      // Only add the TopicUser if it isn't already in there...
      if (space.readIfExists(topicUser, transaction, 1000) == null) {
        lease = space.write(topicUser, transaction, Lease.FOREVER);
      }

      transaction.commit();
    } catch (RemoteException
        | UnusableEntryException
        | TransactionException
        | InterruptedException e) {
      System.err.println("Failed to add user to topic");
      e.printStackTrace();
    }

    return lease;
  }