@Override
  public PermissionStore getStoreForPermissionOperation(IdentityContext context) {

    IdentityConfiguration identityConfiguration = null;

    if (this.partitionManagementConfig != null) {
      identityConfiguration = getConfigurationForPartition(context.getPartition());
    } else if (this.configurations.size() == 1) {
      identityConfiguration = this.configurations.iterator().next();
    }

    if (identityConfiguration == null) {
      for (IdentityConfiguration configuration : this.configurations) {
        for (IdentityStoreConfiguration storeConfig : configuration.getStoreConfiguration()) {
          if (storeConfig.supportsPermissions()) {
            return (PermissionStore)
                getIdentityStoreAndInitializeContext(context, configuration, storeConfig);
          }
        }
      }
    } else {
      for (IdentityStoreConfiguration storeConfig : identityConfiguration.getStoreConfiguration()) {
        if (storeConfig.supportsPermissions()) {
          return (PermissionStore)
              getIdentityStoreAndInitializeContext(context, identityConfiguration, storeConfig);
        }
      }
    }

    throw MESSAGES.permissionUnsupportedOperation();
  }
  @Override
  public Set<CredentialStore<?>> getStoresForCredentialStorage(
      final IdentityContext context, Class<? extends CredentialStorage> storageClass) {
    Map<IdentityStoreConfiguration, IdentityStore<?>> storesConfig =
        this.stores.get(getConfigurationForPartition(context.getPartition()));

    Set<CredentialStore<?>> credentialStores = new HashSet<CredentialStore<?>>();

    if (storesConfig != null) {
      for (IdentityStore identityStore : storesConfig.values()) {
        if (CredentialStore.class.isInstance(identityStore)
            && identityStore.getConfig().supportsCredential()) {
          CredentialStore<?> credentialStore = (CredentialStore<?>) identityStore;

          for (Class<? extends CredentialHandler> credentialHandler :
              credentialStore.getConfig().getCredentialHandlers()) {
            SupportsCredentials supportedCredentials =
                credentialHandler.getAnnotation(SupportsCredentials.class);

            if (supportedCredentials != null) {
              if (supportedCredentials.credentialStorage().equals(storageClass)) {
                credentialStores.add(credentialStore);
              }
            }
          }
        }
      }
    }

    return credentialStores;
  }
  @Override
  public <T extends CredentialStore<?>> T getStoreForCredentialOperation(
      IdentityContext context, Class<?> credentialClass) {
    T store = null;

    IdentityConfiguration identityConfiguration = null;

    if (this.partitionManagementConfig != null) {
      identityConfiguration = getConfigurationForPartition(context.getPartition());
    } else {
      for (IdentityConfiguration configuration : this.configurations) {
        for (IdentityStoreConfiguration storeConfig : configuration.getStoreConfiguration()) {
          if (storeConfig.supportsCredential()) {
            identityConfiguration = configuration;
          }
        }
      }
    }

    if (identityConfiguration != null && identityConfiguration.supportsCredential()) {
      for (IdentityStoreConfiguration storeConfig : identityConfiguration.getStoreConfiguration()) {
        if (storeConfig.supportsCredential()) {
          for (@SuppressWarnings("rawtypes")
          Class<? extends CredentialHandler> handlerClass : storeConfig.getCredentialHandlers()) {
            if (handlerClass.isAnnotationPresent(SupportsCredentials.class)) {
              for (Class<?> cls :
                  handlerClass.getAnnotation(SupportsCredentials.class).credentialClass()) {
                if (cls.isAssignableFrom(credentialClass)) {
                  IdentityStore<?> identityStore = null;
                  try {
                    store =
                        getIdentityStoreAndInitializeContext(
                            context, identityConfiguration, storeConfig);
                  } catch (ClassCastException cce) {
                    throw MESSAGES.storeUnexpectedType(
                        identityStore.getClass(), CredentialStore.class);
                  }

                  // if we found a specific handler for the credential, immediately return.
                  if (cls.equals(credentialClass)) {
                    return store;
                  }
                }
              }
            }
          }
        }
      }
    }

    if (store == null) {
      throw MESSAGES.credentialNoStoreForCredentials(credentialClass);
    }

    return store;
  }
  @Override
  public <T extends IdentityStore<?>> T getStoreForIdentityOperation(
      IdentityContext context,
      Class<T> storeType,
      Class<? extends AttributedType> type,
      IdentityOperation operation) {
    checkSupportedTypes(context.getPartition(), type);

    IdentityConfiguration identityConfiguration = null;

    if (this.partitionManagementConfig != null) {
      identityConfiguration = getConfigurationForPartition(context.getPartition());
    } else if (this.configurations.size() == 1) {
      identityConfiguration = this.configurations.iterator().next();
    }

    T identityStore = null;

    if (identityConfiguration == null) {
      for (IdentityConfiguration configuration : this.configurations) {
        identityStore = lookupStore(context, configuration, type, operation);

        if (identityStore != null) {
          break;
        }
      }
    } else {
      identityStore = lookupStore(context, identityConfiguration, type, operation);
    }

    if (identityStore == null) {
      throw MESSAGES.attributedTypeUnsupportedOperation(type, operation, type, operation);
    }

    return identityStore;
  }