public Collection<IdentityObject> findIdentityObject(
      IdentityStoreInvocationContext invocationCtx,
      IdentityObject identity,
      IdentityObjectRelationshipType relationshipType,
      boolean parent,
      IdentityObjectSearchCriteria criteria)
      throws IdentityException {

    IdentityObjectSearchImpl search = new IdentityObjectSearchImpl();
    search.setIdentityObjectSearchCriteria(criteria);
    search.setParent(parent);
    if (relationshipType != null) {
      search.setRelationshipType(relationshipType.getName());
    }
    if (identity != null) {
      search.setName(identity.getName());
      search.setType(identity.getIdentityType().getName());
    }

    Collection<IdentityObject> results =
        cacheSupport.getIdentityObjectSearch(getCacheNS(invocationCtx), search);

    if (results != null) {
      // TODO
      return results;
    }

    results =
        identityStore.findIdentityObject(
            invocationCtx, identity, relationshipType, parent, criteria);

    cacheSupport.putIdentityObjectSearch(getCacheNS(invocationCtx), search, results);

    return results;
  }
  public Set<IdentityObjectRelationship> resolveRelationships(
      IdentityStoreInvocationContext invocationCxt,
      IdentityObject fromIdentity,
      IdentityObject toIdentity,
      IdentityObjectRelationshipType relationshipType)
      throws IdentityException {

    IdentityObjectRelationshipSearchImpl search = new IdentityObjectRelationshipSearchImpl();
    if (fromIdentity != null) {
      search.setFromIOName(fromIdentity.getName());
      search.setFromIOType(fromIdentity.getIdentityType().getName());
    }
    if (toIdentity != null) {
      search.setToIOName(toIdentity.getName());
      search.setToIOType(toIdentity.getIdentityType().getName());
    }
    if (relationshipType != null) {
      search.setRelationshipType(relationshipType.getName());
    }

    Set<IdentityObjectRelationship> results =
        cacheSupport.getIdentityObjectRelationshipSearch(getCacheNS(invocationCxt), search);

    if (results != null) {
      return results;
    }

    results =
        identityStore.resolveRelationships(
            invocationCxt, fromIdentity, toIdentity, relationshipType);

    cacheSupport.putIdentityObjectRelationshipSearch(getCacheNS(invocationCxt), search, results);

    return results;
  }
  public Collection<IdentityObject> findIdentityObject(
      IdentityStoreInvocationContext invocationCtx,
      IdentityObjectType identityType,
      IdentityObjectSearchCriteria criteria)
      throws IdentityException {

    // TODO: fix

    //      IdentityObjectSearchImpl search = new IdentityObjectSearchImpl();
    //      search.setIdentityObjectSearchCriteria(criteria);
    //      if (identityType != null)
    //      {
    //         search.setType(identityType.getName());
    //      }

    //      Collection<IdentityObject> results =
    // cacheSupport.getIdentityObjectSearch(getCacheNS(invocationCtx), search);

    //      if (results != null)
    //      {
    //         return results;
    //      }

    Collection<IdentityObject> results =
        identityStore.findIdentityObject(invocationCtx, identityType, criteria);

    //      cacheSupport.putIdentityObjectSearch(getCacheNS(invocationCtx), search, results);

    return results;
  }
  public IdentityObject findIdentityObject(
      IdentityStoreInvocationContext invocationContext,
      String name,
      IdentityObjectType identityObjectType)
      throws IdentityException {
    IdentityObjectSearchImpl search = new IdentityObjectSearchImpl();
    search.setName(name);
    if (identityObjectType != null) {
      search.setType(identityObjectType.getName());
    }
    Collection<IdentityObject> results =
        cacheSupport.getIdentityObjectSearch(getCacheNS(invocationContext), search);

    if (results != null && results.size() == 1) {
      return results.iterator().next();
    }

    IdentityObject io =
        identityStore.findIdentityObject(invocationContext, name, identityObjectType);

    if (io != null) {
      Set<IdentityObject> temp = new HashSet<IdentityObject>();
      temp.add(io);
      cacheSupport.putIdentityObjectSearch(getCacheNS(invocationContext), search, temp);
    }

    return io;
  }
  public Set<String> getRelationshipNames(
      IdentityStoreInvocationContext ctx,
      IdentityObject identity,
      IdentityObjectSearchCriteria criteria)
      throws IdentityException, OperationNotSupportedException {

    IdentityObjectRelationshipNameSearchImpl search =
        new IdentityObjectRelationshipNameSearchImpl();
    if (identity != null) {
      search.setIoName(identity.getName());
      search.setIoType(identity.getIdentityType().getName());
    }
    search.setIdentityObjectSearchCriteria(criteria);

    Set<String> results =
        cacheSupport.getIdentityObjectRelationshipNameSearch(getCacheNS(ctx), search);

    if (results != null) {
      return results;
    }

    results = identityStore.getRelationshipNames(ctx, identity, criteria);

    cacheSupport.putIdentityObjectRelationshipNameSearch(getCacheNS(ctx), search, results);

    return results;
  }
  public void removeRelationshipNameProperties(
      IdentityStoreInvocationContext ctx, String name, Set<String> properties)
      throws IdentityException, OperationNotSupportedException {
    identityStore.removeRelationshipNameProperties(ctx, name, properties);

    cacheSupport.invalidateRelationshipNameProperties(getCacheNS(ctx), name);
    cacheSupport.putProperties(getCacheNS(ctx), name, getRelationshipNameProperties(ctx, name));
  }
 public boolean validateCredential(
     IdentityStoreInvocationContext ctx,
     IdentityObject identityObject,
     IdentityObjectCredential credential)
     throws IdentityException {
   // Should not be cached
   return identityStore.validateCredential(ctx, identityObject, credential);
 }
 public void updateCredential(
     IdentityStoreInvocationContext ctx,
     IdentityObject identityObject,
     IdentityObjectCredential credential)
     throws IdentityException {
   // Should not be cached
   identityStore.updateCredential(ctx, identityObject, credential);
 }
  public String removeRelationshipName(IdentityStoreInvocationContext ctx, String name)
      throws IdentityException, OperationNotSupportedException {
    cacheSupport.invalidateIdentityObjectRelationshipNameSearches(getCacheNS(ctx));
    cacheSupport.invalidateIdentityObjectRelationshipSearches(getCacheNS(ctx));
    cacheSupport.invalidateIdentityObjectSearches(getCacheNS(ctx));
    cacheSupport.invalidateRelationshipNameProperties(getCacheNS(ctx), name);

    return identityStore.removeRelationshipName(ctx, name);
  }
  public void removeRelationshipProperties(
      IdentityStoreInvocationContext ctx,
      IdentityObjectRelationship relationship,
      Set<String> properties)
      throws IdentityException, OperationNotSupportedException {
    identityStore.removeRelationshipProperties(ctx, relationship, properties);

    cacheSupport.invalidateRelationshipProperties(getCacheNS(ctx), relationship);
    cacheSupport.putProperties(
        getCacheNS(ctx), relationship, getRelationshipProperties(ctx, relationship));
  }
  public void removeIdentityObject(
      IdentityStoreInvocationContext invocationCtx, IdentityObject identity)
      throws IdentityException {
    identityStore.removeIdentityObject(invocationCtx, identity);

    cacheSupport.invalidateIdentityObjectRelationshipNameSearches(getCacheNS(invocationCtx));
    cacheSupport.invalidateIdentityObjectRelationshipSearches(getCacheNS(invocationCtx));
    cacheSupport.invalidateIdentityObjectSearches(getCacheNS(invocationCtx));
    cacheSupport.invalidateIdentityObjectAttriubtes(getCacheNS(invocationCtx), identity);
    cacheSupport.invalidateIdentityObjectCount(
        getCacheNS(invocationCtx), identity.getIdentityType().getName());
  }
  public void removeRelationships(
      IdentityStoreInvocationContext invocationCtx,
      IdentityObject identity1,
      IdentityObject identity2,
      boolean named)
      throws IdentityException {
    identityStore.removeRelationships(invocationCtx, identity1, identity2, named);

    cacheSupport.invalidateIdentityObjectRelationshipNameSearches(getCacheNS(invocationCtx));
    cacheSupport.invalidateIdentityObjectRelationshipSearches(getCacheNS(invocationCtx));
    cacheSupport.invalidateIdentityObjectSearches(getCacheNS(invocationCtx));
    cacheSupport.invalidateRelationshipProperties(getCacheNS(invocationCtx));
  }
  public IdentityObjectRelationship createRelationship(
      IdentityStoreInvocationContext invocationCxt,
      IdentityObject fromIdentity,
      IdentityObject toIdentity,
      IdentityObjectRelationshipType relationshipType,
      String relationshipName,
      boolean createNames)
      throws IdentityException {
    cacheSupport.invalidateIdentityObjectRelationshipNameSearches(getCacheNS(invocationCxt));
    cacheSupport.invalidateIdentityObjectRelationshipSearches(getCacheNS(invocationCxt));
    cacheSupport.invalidateIdentityObjectSearches(getCacheNS(invocationCxt));

    return identityStore.createRelationship(
        invocationCxt, fromIdentity, toIdentity, relationshipType, relationshipName, createNames);
  }
  public Map<String, String> getRelationshipProperties(
      IdentityStoreInvocationContext ctx, IdentityObjectRelationship relationship)
      throws IdentityException, OperationNotSupportedException {
    Map<String, String> properties = cacheSupport.getProperties(getCacheNS(ctx), relationship);

    if (properties != null) {
      return properties;
    }

    properties = identityStore.getRelationshipProperties(ctx, relationship);

    cacheSupport.putProperties(getCacheNS(ctx), relationship, properties);

    return properties;
  }
  public int getIdentityObjectsCount(
      IdentityStoreInvocationContext invocationCtx, IdentityObjectType identityType)
      throws IdentityException {
    int count =
        cacheSupport.getIdentityObjectCount(getCacheNS(invocationCtx), identityType.getName());

    if (count != -1) {
      return count;
    }
    count = identityStore.getIdentityObjectsCount(invocationCtx, identityType);

    cacheSupport.putIdentityObjectCount(getCacheNS(invocationCtx), identityType.getName(), count);

    return count;
  }
  public IdentityObject createIdentityObject(
      IdentityStoreInvocationContext invocationCtx,
      String name,
      IdentityObjectType identityObjectType)
      throws IdentityException {

    IdentityObject io = identityStore.createIdentityObject(invocationCtx, name, identityObjectType);

    if (io != null) {
      cacheSupport.invalidateIdentityObjectRelationshipNameSearches(getCacheNS(invocationCtx));
      cacheSupport.invalidateIdentityObjectRelationshipSearches(getCacheNS(invocationCtx));
      cacheSupport.invalidateIdentityObjectSearches(getCacheNS(invocationCtx));
      cacheSupport.invalidateIdentityObjectCount(
          getCacheNS(invocationCtx), identityObjectType.getName());
    }
    return io;
  }
  public JBossCacheIdentityStoreWrapper(
      IdentityStore identityStore, IdentityStoreCacheProvider cacheSupport, String cacheScope)
      throws IdentityException {
    super(identityStore, cacheSupport, cacheScope);

    this.identityStore = identityStore;

    log.fine("------------------------------------------------------");
    log.fine(
        "JBossCacheIdentityStoreWrapper created ....."
            + "(IdentityStore: "
            + identityStore.getId()
            + "; cache scope: "
            + cacheScope
            + ")");
    log.fine("------------------------------------------------------");
  }
  public void removeRelationship(
      IdentityStoreInvocationContext invocationCxt,
      IdentityObject fromIdentity,
      IdentityObject toIdentity,
      IdentityObjectRelationshipType relationshipType,
      String relationshipName)
      throws IdentityException {
    identityStore.removeRelationship(
        invocationCxt, fromIdentity, toIdentity, relationshipType, relationshipName);

    cacheSupport.invalidateIdentityObjectRelationshipNameSearches(getCacheNS(invocationCxt));
    cacheSupport.invalidateIdentityObjectRelationshipSearches(getCacheNS(invocationCxt));
    cacheSupport.invalidateIdentityObjectSearches(getCacheNS(invocationCxt));
    IdentityObjectRelationship relationship =
        new SimpleIdentityObjectRelationship(
            fromIdentity, toIdentity, relationshipName, relationshipType);
    cacheSupport.invalidateRelationshipProperties(getCacheNS(invocationCxt), relationship);
  }
  public Set<IdentityObjectRelationship> resolveRelationships(
      IdentityStoreInvocationContext invocationCtx,
      IdentityObject identity,
      IdentityObjectRelationshipType relationshipType,
      boolean parent,
      boolean named,
      String name)
      throws IdentityException {

    IdentityObjectRelationshipSearchImpl search = new IdentityObjectRelationshipSearchImpl();
    if (identity != null) {
      search.setIoName(identity.getName());
      search.setIoType(identity.getIdentityType().getName());
    }
    if (relationshipType != null) {
      search.setRelationshipType(relationshipType.getName());
    }
    search.setParent(parent);
    search.setNamed(named);
    search.setName(name);

    Set<IdentityObjectRelationship> results =
        cacheSupport.getIdentityObjectRelationshipSearch(getCacheNS(invocationCtx), search);

    if (results != null) {
      return results;
    }

    results =
        identityStore.resolveRelationships(
            invocationCtx, identity, relationshipType, parent, named, name);

    cacheSupport.putIdentityObjectRelationshipSearch(getCacheNS(invocationCtx), search, results);

    return results;
  }
 public FeaturesMetaData getSupportedFeatures() {
   return identityStore.getSupportedFeatures();
 }
 public void bootstrap(IdentityStoreConfigurationContext configurationContext)
     throws IdentityException {
   identityStore.bootstrap(configurationContext);
 }
 @Override
 public String toString() {
   return "JBossCacheIdentityStoreWrapper (IdentityStore=" + identityStore.getId() + ")";
 }