net.sf.ehcache.search.Results Java Examples

The following examples show how to use net.sf.ehcache.search.Results. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DeviceCacheImpl.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public List<Device> getByDeviceClassId(Long deviceClassId) {
  List<Device> deviceCacheObjects = new ArrayList<>();

  Results results = null;

  try {
    Query query = getCache().createQuery();
    Attribute<Long> id = getCache().getSearchAttribute("deviceClassId");
    results = query.includeKeys().includeValues().addCriteria(id.eq(deviceClassId)).execute();

    if (results.size() == 0) {
      throw new CacheElementNotFoundException("Failed to get device ids from cache");
    }

    results.all().forEach((result) -> deviceCacheObjects.add((DeviceCacheObject) result.getValue()));
  } finally {
    if (results != null) {
      results.discard();
    }
  }

  return deviceCacheObjects;
}
 
Example #2
Source File: DomainAccessControlStoreEhCache.java    From joynr with Apache License 2.0 6 votes vote down vote up
private <T extends ControlEntry> List<T> getAces(String domain, String interfaceName, CacheId cacheId) {
    Cache cache = getCache(cacheId);
    List<T> aces = new ArrayList<T>();
    // here search on domain and interface take place
    Attribute<String> domainAttribute = cache.getSearchAttribute(UserDomainInterfaceOperationKey.DOMAIN);
    Attribute<String> interfaceAttribute = cache.getSearchAttribute(UserDomainInterfaceOperationKey.INTERFACE);
    // query is the fastest if you search for keys and if you need value then call Cache.get(key)
    Query queryDomainInterface = cache.createQuery()
                                      .addCriteria(domainAttribute.eq(domain)
                                                                  .and(interfaceAttribute.eq(interfaceName)))
                                      .includeKeys()
                                      .end();
    Results results = queryDomainInterface.execute();
    for (Result result : results.all()) {
        T ace = DomainAccessControlStoreEhCache.<T> getElementValue(cache.get(result.getKey()));
        aces.add(ace);
    }

    return aces;
}
 
Example #3
Source File: DomainAccessControlStoreEhCache.java    From joynr with Apache License 2.0 6 votes vote down vote up
private <T extends ControlEntry> List<T> getAces(String uid, CacheId cacheId) {
    Cache cache = getCache(cacheId);
    List<T> aces = new ArrayList<T>();
    // here search on uid take place
    Attribute<String> uidAttribute = cache.getSearchAttribute(UserDomainInterfaceOperationKey.USER_ID);
    // query is the fastest if you search for keys and if you need value then call Cache.get(key)
    Query queryRequestedUid = cache.createQuery()
                                   .addCriteria(uidAttribute.eq(uid).or(uidAttribute.eq(WILDCARD)))
                                   // have specific user ids appear before wildcards
                                   .addOrderBy(uidAttribute, Direction.DESCENDING)
                                   .includeKeys()
                                   .end();
    Results results = queryRequestedUid.execute();
    for (Result result : results.all()) {
        aces.add(DomainAccessControlStoreEhCache.<T> getElementValue(cache.get(result.getKey())));
    }

    return aces;
}
 
Example #4
Source File: DomainAccessControlStoreEhCache.java    From joynr with Apache License 2.0 6 votes vote down vote up
@Override
public DomainRoleEntry getDomainRole(String uid, Role role) {
    Cache cache = getCache(CacheId.DOMAIN_ROLES);
    Attribute<String> uidAttribute = cache.getSearchAttribute(UserRoleKey.USER_ID);
    Attribute<Role> roleAttribute = cache.getSearchAttribute(UserRoleKey.ROLE);
    // query is the fastest if you search for keys and if you need value then call Cache.get(key)
    Query queryRequestedUid = cache.createQuery()
                                   .addCriteria(uidAttribute.eq(uid))
                                   .addCriteria(roleAttribute.eq(role))
                                   .includeKeys()
                                   .end();
    Results results = queryRequestedUid.execute();
    DomainRoleEntry domainRole = null;
    if (!results.all().isEmpty()) {
        // Note: since (uid, role) is the primary key in domain role table
        // results is either empty or contains exactly one entry
        assert (results.all().size() == 1);
        domainRole = (DomainAccessControlStoreEhCache.<DomainRoleEntry> getElementValue(cache.get(results.all()
                                                                                                         .get(0)
                                                                                                         .getKey())));
    }

    return domainRole;
}
 
Example #5
Source File: DeviceClassCacheImpl.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Long getDeviceClassIdByName(String deviceClassName) {
  Long deviceClassId;

  if (deviceClassName == null || deviceClassName.equalsIgnoreCase("")) {
    throw new IllegalArgumentException("Attempting to retrieve a DeviceClass from the cache with a NULL or empty name parameter.");
  }

  Results results = null;
  try {
    Attribute<String> className = getCache().getSearchAttribute("deviceClassName");
    Query query = getCache().createQuery();
    results = query.includeKeys().addCriteria(className.eq(deviceClassName)).maxResults(1).execute();

    if (results.size() == 0) {
      throw new CacheElementNotFoundException("Failed to find a device class with name " + deviceClassName + " in the cache.");
    }

    deviceClassId = (long) results.all().get(0).getKey();
  } finally {
    if (results != null) {
      results.discard();
    }
  }

  return deviceClassId;
}
 
Example #6
Source File: DomainAccessControlStoreEhCache.java    From joynr with Apache License 2.0 5 votes vote down vote up
private <T extends ControlEntry> List<T> getEditableAces(String uid, CacheId cacheId, Role role) {
    List<T> aces = new ArrayList<T>();
    // find out first on which domains uid has specified role
    Cache drtCache = getCache(CacheId.DOMAIN_ROLES);
    UserRoleKey dreKey = new UserRoleKey(uid, role);
    String[] uidDomains = null;
    // read domains from DRE
    if (drtCache.isKeyInCache(dreKey)) {
        DomainRoleEntry dre = DomainAccessControlStoreEhCache.<DomainRoleEntry> getElementValue(drtCache.get(dreKey));
        uidDomains = dre.getDomains();
    }
    // if uid has no domains with specified role return empty list
    if (uidDomains == null || uidDomains.length == 0) {
        return aces;
    }

    Cache cache = getCache(cacheId);
    // here should search on uid and domain take place
    Attribute<String> uidAttribute = cache.getSearchAttribute(UserDomainInterfaceOperationKey.USER_ID);
    Attribute<String> domainAttribute = cache.getSearchAttribute(UserDomainInterfaceOperationKey.DOMAIN);
    for (String domain : uidDomains) {
        Query query = cache.createQuery()
                           .addCriteria(uidAttribute.eq(uid).and(domainAttribute.eq(domain)))
                           .includeKeys()
                           .end();
        Results results = query.execute();
        for (Result result : results.all()) {
            aces.add(DomainAccessControlStoreEhCache.<T> getElementValue(cache.get(result.getKey())));
        }

    }

    return aces;
}
 
Example #7
Source File: DomainAccessControlStoreEhCache.java    From joynr with Apache License 2.0 5 votes vote down vote up
private <T extends ControlEntry> List<T> getAces(CacheId cacheId, String uid, String domain, String interfaceName) {
    Cache cache = getCache(cacheId);
    Attribute<String> uidAttribute = cache.getSearchAttribute(UserDomainInterfaceOperationKey.USER_ID);
    Attribute<String> domainAttribute = cache.getSearchAttribute(UserDomainInterfaceOperationKey.DOMAIN);
    Attribute<String> interfaceAttribute = cache.getSearchAttribute(UserDomainInterfaceOperationKey.INTERFACE);
    Query queryAllOperations = cache.createQuery()
                                    .addCriteria(uidAttribute.eq(uid).or(uidAttribute.eq(WILDCARD)))
                                    .addCriteria(domainAttribute.eq(domain))
                                    .addCriteria(interfaceAttribute.eq(interfaceName))
                                    // have specific user ids appear before wildcards
                                    .addOrderBy(uidAttribute, Direction.DESCENDING)
                                    .includeKeys()
                                    .end();
    Results results = queryAllOperations.execute();
    List<T> aces = new ArrayList<T>();
    String currentUid = null;
    for (Result result : results.all()) {
        T ace = DomainAccessControlStoreEhCache.<T> getElementValue(cache.get(result.getKey()));

        // Don't add uid wildcards if a specific uid has been added to the results
        if (currentUid == null) {
            currentUid = ace.getUid();
        } else if (!currentUid.equals(ace.getUid())) {
            break;
        }

        aces.add(ace);
    }

    return aces;
}
 
Example #8
Source File: DomainAccessControlStoreEhCache.java    From joynr with Apache License 2.0 5 votes vote down vote up
private <T extends ControlEntry> T getAce(CacheId cacheId,
                                          String uid,
                                          String domain,
                                          String interfaceName,
                                          String operation) {
    Cache cache = getCache(cacheId);
    Attribute<String> uidAttribute = cache.getSearchAttribute(UserDomainInterfaceOperationKey.USER_ID);
    Attribute<String> domainAttribute = cache.getSearchAttribute(UserDomainInterfaceOperationKey.DOMAIN);
    Attribute<String> interfaceAttribute = cache.getSearchAttribute(UserDomainInterfaceOperationKey.INTERFACE);
    Attribute<String> operationAttribute = cache.getSearchAttribute(UserDomainInterfaceOperationKey.OPERATION);
    Query queryAllOperations = cache.createQuery()
                                    .addCriteria(uidAttribute.eq(uid).or(uidAttribute.eq(WILDCARD)))
                                    .addCriteria(domainAttribute.eq(domain))
                                    .addCriteria(interfaceAttribute.eq(interfaceName))
                                    .addCriteria(operationAttribute.eq(operation))
                                    // have specific user ids appear before wildcards
                                    .addOrderBy(uidAttribute, Direction.DESCENDING)
                                    .includeKeys()
                                    .end();
    Results results = queryAllOperations.execute();
    T ace = null;
    if (!results.all().isEmpty()) {
        ace = DomainAccessControlStoreEhCache.<T> getElementValue(cache.get(results.all().get(0).getKey()));
    }

    return ace;
}
 
Example #9
Source File: DomainAccessControlStoreEhCache.java    From joynr with Apache License 2.0 5 votes vote down vote up
@Override
public List<DomainRoleEntry> getDomainRoles(String uid) {
    Cache cache = getCache(CacheId.DOMAIN_ROLES);
    List<DomainRoleEntry> domainRoles = new ArrayList<DomainRoleEntry>();
    Attribute<String> uidAttribute = cache.getSearchAttribute(UserRoleKey.USER_ID);
    // query is the fastest if you search for keys and if you need value then call Cache.get(key)
    Query queryRequestedUid = cache.createQuery().addCriteria(uidAttribute.eq(uid)).includeKeys().end();
    Results results = queryRequestedUid.execute();
    for (Result result : results.all()) {
        domainRoles.add(DomainAccessControlStoreEhCache.<DomainRoleEntry> getElementValue(cache.get(result.getKey())));
    }

    return domainRoles;
}
 
Example #10
Source File: CacheSample.java    From directory-fortress-core with Apache License 2.0 5 votes vote down vote up
void runTests()
{
    loadCache();
    Attribute<String> member = cache.getSearchAttribute( "member" );
    Query query = cache.createQuery();
    query.includeKeys();
    query.includeValues();
    Set<String> roles = new HashSet<>();
    roles.add( "oamt17dsd1" );
    roles.add( "oamt17dsd4" );
    roles.add( "oamT13DSD6" );
    roles.add( "oamT16SDR7" );
    query.addCriteria( member.in( roles ) );
    Results results = query.execute();
    System.out.println( " Size: " + results.size() );
    System.out.println( "----Results-----\n" );
    Set<SDSet> resultSet = new HashSet<>();

    for ( Result result : results.all() )
    {
        DsdCacheEntry entry = ( DsdCacheEntry ) result.getValue();
        resultSet.add( entry.getSdSet() );
    }

    for ( SDSet sdSet : resultSet )
    {
        LOG.info( "Found SDSet: " + sdSet.getName() );
    }
}
 
Example #11
Source File: SDUtil.java    From directory-fortress-core with Apache License 2.0 5 votes vote down vote up
/**
 * Given a role name, return the set of DSD's that have a matching member.
 *
 * @param name contains name of authorized Role used to search the cache.
 * @param contextId maps to sub-tree in DIT, e.g. ou=contextId, dc=example, dc=com.
 * @return un-ordered set of matching DSD's.
 * @throws SecurityException in the event of system or rule violation.
 */
private Set<SDSet> getDsdCache(String name, String contextId)
    throws SecurityException
{
    contextId = getContextId(contextId);
    Set<SDSet> finalSet = new HashSet<>();
    Attribute<String> context = m_dsdCache.getSearchAttribute(CONTEXT_ID);
    Attribute<String> member = m_dsdCache.getSearchAttribute(SchemaConstants.MEMBER_AT);
    Query query = m_dsdCache.createQuery();
    query.includeKeys();
    query.includeValues();
    query.addCriteria(member.eq(name).and(context.eq(contextId)));
    Results results = query.execute();
    boolean empty = false;
    for (Result result : results.all())
    {
        DsdCacheEntry entry = (DsdCacheEntry) result.getValue();
        if (!entry.isEmpty())
        {
            finalSet.add(entry.getSdSet());
            finalSet = putDsdCache(name, contextId);
        }
        else
        {
            empty = true;
        }
        finalSet.add(entry.getSdSet());
    }
    // If nothing was found in the cache, determine if it needs to be seeded:
    if (finalSet.size() == 0 && !empty)
    {
        finalSet = putDsdCache(name, contextId);
    }
    return finalSet;
}
 
Example #12
Source File: SDUtil.java    From directory-fortress-core with Apache License 2.0 5 votes vote down vote up
/**
 * Given DSD entry name, clear its corresponding object values from the cache.
 *
 * @param name contains the name of object to be cleared.
 * @param contextId maps to sub-tree in DIT, e.g. ou=contextId, dc=example, dc=com.     *
 * @throws SecurityException in the event of system or rule violation.
 */
void clearDsdCacheEntry(String name, String contextId)
{
    Attribute<String> context = m_dsdCache.getSearchAttribute(CONTEXT_ID);
    Attribute<String> dsdName = m_dsdCache.getSearchAttribute(DSD_NAME);
    Query query = m_dsdCache.createQuery();
    query.includeKeys();
    query.includeValues();
    query.addCriteria(dsdName.eq(name).and(context.eq(contextId)));
    Results results = query.execute();
    for (Result result : results.all())
    {
        m_dsdCache.clear(result.getKey());
    }
}
 
Example #13
Source File: CommandTagCacheImpl.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Long getCommandTagId(final String name) {
  Long commandTagKey = null;
  Results results = null;

  if (name == null || name.equalsIgnoreCase("")) {
    throw new IllegalArgumentException("Attempting to retrieve a CommandTag from the cache with a NULL or empty name parameter.");
  }

  try {
    Attribute<String> commandTagName = getCache().getSearchAttribute("commandTagName");
    Query query = getCache().createQuery();
    results = query.includeKeys().addCriteria(commandTagName.eq(name)).maxResults(1).execute();

    // Find the number of results -- the number of hits.
    int size = results.size();
    if (size == 0) {
      log.info("Failed to find a command tag with name " + name + " in the cache.");
    }

    commandTagKey = results.all().size() > 0 ? (Long) results.all().get(0).getKey() : null;
  }
  finally {
    if (results != null) {
      // Discard the results when done to free up cache resources.
      results.discard();
    }
  }

  return commandTagKey;
}
 
Example #14
Source File: ProcessCacheImpl.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Long getProcessId(final String name) {
  Long processKey = null;
  Results results = null;

  if (name == null || name.equalsIgnoreCase("")) {
    throw new IllegalArgumentException("Attempting to retrieve a Process from the cache with a NULL or empty name parameter.");
  }

  try {
    Attribute<String> processName = getCache().getSearchAttribute("processName");
    // By limiting the query result list to 1 it is up to the administrator to
    // make
    // sure that the process name is unique. Otherwise this will result in an
    // unpredictable behaviour.
    Query query = getCache().createQuery();
    results = query.includeKeys().addCriteria(processName.eq(name)).maxResults(1).execute();

    // Find the number of results -- the number of hits.
    int size = results.size();
    if (size == 0) {
      throw new CacheElementNotFoundException("Failed to find a process with name " + name + " in the cache.");
    }

    processKey = (Long) results.all().get(0).getKey();
  }
  finally {
    if (results != null) {
      // Discard the results when done to free up cache resources.
      results.discard();
    }
  }

  return processKey;
}
 
Example #15
Source File: DataTagCacheImpl.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Receives a list of all DataTag ids which are attached to the given equipment or sub-equipment.
 * @param id The id of the (sub-)equipment
 * @param searchAttribute The ehcache search attribute, which is specified in the wrapper method
 * @return A list of all DataTag ids belonging to the given (sub-)equipment
 */
private List<Long> getDataTagIds(Long id, String searchAttribute) {
  List<Long> tagIds = new LinkedList<>();
  Results results = null;

  if (id == null) {
    throw new IllegalArgumentException("Attempting to retrieve a List of DataTag ids from the cache with a NULL " +
        "parameter.");
  }

  try {
    Attribute<Long> cacheEquipmentId = getCache().getSearchAttribute(searchAttribute);
    results = getCache().createQuery().includeKeys().addCriteria(cacheEquipmentId.eq(id)).execute();

    if (results == null) {
      throw new CacheElementNotFoundException("Failed to execute query with (sub)EquipmentId " + id + " : " +
          "Result is null.");
    }

    results.all().forEach(r -> tagIds.add((Long) r.getKey()));

  } finally {
    if (results != null) {
      // Discard the results when done to free up cache resources.
      results.discard();
    }
  }
  return tagIds;
}
 
Example #16
Source File: AbstractTagCache.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public boolean hasTagWithName(String name) {
  if (name == null || name.equalsIgnoreCase("")) {
    throw new IllegalArgumentException("Attempting to retrieve a Tag from the cache with a NULL or empty name parameter.");
  }

  // This will prevent wildcard searches
  if (name.contains("*")) {
    name = name.replace("*", "\\*");
  }
  if (name.contains("?")) {
    name = name.replace("?", "\\?");
  }

  Results results = null;

  try {
    Ehcache ehcache = getCache();
    Attribute<String> tagName = ehcache.getSearchAttribute("tagName");

    Query query = ehcache.createQuery();
    results = query.includeKeys().addCriteria(tagName.ilike(name)).maxResults(1).execute();

    return results.hasKeys();
  }
  finally {
    if (results != null) {
      // Discard the results when done to free up cache resources.
      results.discard();
    }
  }

}
 
Example #17
Source File: SDUtil.java    From directory-fortress-core with Apache License 2.0 4 votes vote down vote up
/**
 * Given a Set of authorized Roles, return the set of DSD's that have matching members.
 *
 * @param authorizedRoleSet contains an un-order Set of authorized Roles.
 * @param contextId maps to sub-tree in DIT, e.g. ou=contextId, dc=example, dc=com.
 * @return un-ordered set of matching DSD's.
 * @throws SecurityException in the event of system or rule violation.
 */
Set<SDSet> getDsdCache(Set<String> authorizedRoleSet, String contextId)
    throws SecurityException
{
    contextId = getContextId(contextId);
    Set<SDSet> dsdRetSets = new HashSet<>();
    // Need to proceed?
    if (!CollectionUtils.isNotEmpty( authorizedRoleSet ))
    {
        return dsdRetSets;
    }
    // Was the DSD Cache switched off?
    boolean isCacheDisabled = Config.getInstance().getBoolean(IS_DSD_CACHE_DISABLED_PARM, false);
    // If so, get DSD's from LDAP:
    if (isCacheDisabled)
    {
        SDSet sdSet = new SDSet();
        sdSet.setType(SDSet.SDType.DYNAMIC);
        sdSet.setContextId(contextId);
        dsdRetSets = sp.search(authorizedRoleSet, sdSet);
    }
    // Search the DSD cache for matching Role members:
    else
    {
        // Search on roleName attribute which maps to 'member' attr on the cache record:
        Attribute<String> member = m_dsdCache.getSearchAttribute(SchemaConstants.MEMBER_AT);
        Attribute<String> context = m_dsdCache.getSearchAttribute(CONTEXT_ID);
        Query query = m_dsdCache.createQuery();
        query.includeKeys();
        query.includeValues();
        // Add the passed in authorized Role names to this cache query:
        Set<String> roles = new HashSet<>(authorizedRoleSet);
        query.addCriteria(member.in(roles).and(context.eq(contextId)));
        // Return all DSD cache entries that match roleName to the 'member' attribute in cache entry:
        Results results = query.execute();
        for (Result result : results.all())
        {
            DsdCacheEntry entry = (DsdCacheEntry) result.getValue();
            // Do not add dummy DSD sets to the final list:
            if (!entry.isEmpty())
            {
                dsdRetSets.add(entry.getSdSet());
            }
            // Remove role member from authorizedRoleSet to preclude from upcoming DSD search:
            //authorizedRoleSet.remove(entry.getMember());
        }
        // Authorized roles remaining in this set correspond to missed cache hits from above:
        if (authorizedRoleSet.size() > 0)
        {
            dsdRetSets = putDsdCache(authorizedRoleSet, contextId);
        }
    }
    return dsdRetSets;
}