Exemplo n.º 1
0
  /** Handles get resource keys commit. */
  protected Set<String> getResourceKeys(Commit<GetResourceKeys> commit) {
    try {
      if (commit.operation().type() == 0) {
        return keys.keySet();
      }

      ResourceType resourceType = registry.lookup(commit.operation().type());
      if (resourceType == null) {
        throw new IllegalArgumentException("unknown resource type: " + commit.operation().type());
      }

      return resources
          .entrySet()
          .stream()
          .filter(e -> e.getValue().type.equals(resourceType))
          .map(e -> e.getValue().key)
          .collect(Collectors.toSet());
    } finally {
      commit.close();
    }
  }
Exemplo n.º 2
0
  /** Gets a resource. */
  protected long getResource(Commit<? extends GetResource> commit) {
    String key = commit.operation().key();
    ResourceType type = registry.lookup(commit.operation().type());

    // If the resource type is not known, fail the get.
    if (type == null) {
      commit.close();
      throw new IllegalArgumentException("unknown resource type: " + commit.operation().type());
    }

    // Lookup the resource ID for the resource key.
    Long resourceId = keys.get(key);

    // If no resource ID was found, create the resource.
    if (resourceId == null) {

      // The first time a resource is created, the resource ID is the index of the commit that
      // created it.
      resourceId = commit.index();
      keys.put(key, resourceId);

      try {
        // For the new resource, construct a state machine and store the resource info.
        ResourceStateMachine stateMachine = type.stateMachine().newInstance();
        ResourceManagerStateMachineExecutor executor =
            new ResourceManagerStateMachineExecutor(resourceId, this.executor);

        // Store the resource to be referenced by its resource ID.
        ResourceHolder resourceHolder =
            new ResourceHolder(resourceId, key, type, commit, stateMachine, executor);
        resources.put(resourceId, resourceHolder);

        // Initialize the resource state machine.
        stateMachine.init(executor);

        // Create a resource session for the client resource instance.
        ManagedResourceSession session = new ManagedResourceSession(resourceId, commit.session());
        SessionHolder holder = new SessionHolder(commit, session);
        resourceHolder.sessions.put(commit.session().id(), holder);

        // Register the newly created session with the resource state machine.
        stateMachine.register(session);

        // Returns the session ID for the resource client session.
        return resourceId;
      } catch (InstantiationException | IllegalAccessException e) {
        throw new ResourceManagerException("failed to instantiate state machine", e);
      }
    } else {
      // If a resource was found, validate that the resource type matches.
      ResourceHolder resourceHolder = resources.get(resourceId);
      if (resourceHolder == null || !resourceHolder.type.equals(type)) {
        throw new ResourceManagerException(
            "inconsistent resource type: " + commit.operation().type());
      }

      // If the session is not already associated with the resource, attach the session to the
      // resource.
      // Otherwise, if the session already has a multiton instance of the resource open, clean the
      // commit.
      SessionHolder holder = resourceHolder.sessions.get(commit.session().id());
      if (holder == null) {
        // Crete a resource session for the client resource instance.
        ManagedResourceSession session = new ManagedResourceSession(resourceId, commit.session());
        holder = new SessionHolder(commit, session);
        resourceHolder.sessions.put(commit.session().id(), holder);

        // Register the newly created session with the resource state machine.
        resourceHolder.stateMachine.register(session);

        return resourceId;
      } else {
        // Return the resource client session ID and clean the commit since no new resource or
        // session was created.
        commit.close();
        return holder.session.id();
      }
    }
  }