/** * @param clusterId * @param hostName */ @Transactional public void removeHost(final long clusterId, String hostName) { populateCache(); Set<HostConfigMapping> set = hostConfigMappingByHost.get(hostName); // Remove from cache items with clusterId CollectionUtils.filter( set, new Predicate() { @Override public boolean evaluate(Object arg0) { return !((HostConfigMapping) arg0).getClusterId().equals(clusterId); } }); // delete from db TypedQuery<HostConfigMappingEntity> query = entityManagerProvider .get() .createQuery( "SELECT entity FROM HostConfigMappingEntity entity " + "WHERE entity.clusterId = ?1 AND entity.hostName = ?2", HostConfigMappingEntity.class); List<HostConfigMappingEntity> list = daoUtils.selectList(query, Long.valueOf(clusterId), hostName); for (HostConfigMappingEntity entity : list) { entityManagerProvider.get().remove(entity); } }
/** * Gets all alert definitions stored in the database. * * @return all alert definitions or an empty list if none exist (never {@code null}). */ public List<AlertDefinitionEntity> findAll() { TypedQuery<AlertDefinitionEntity> query = entityManagerProvider .get() .createNamedQuery("AlertDefinitionEntity.findAll", AlertDefinitionEntity.class); return daoUtils.selectList(query); }
/** * Gets all alert definitions stored in the database. * * @return all alert definitions or empty list if none exist (never {@code null}). */ public List<AlertDefinitionEntity> findAll(long clusterId) { TypedQuery<AlertDefinitionEntity> query = entityManagerProvider .get() .createNamedQuery( "AlertDefinitionEntity.findAllInCluster", AlertDefinitionEntity.class); query.setParameter("clusterId", clusterId); return daoUtils.selectList(query); }
private void populateCache() { if (!cacheLoaded) { gl.writeLock().lock(); try { gl.writeLock().lock(); try { if (hostConfigMappingByHost == null) { hostConfigMappingByHost = new WeakHashMap<String, Set<HostConfigMapping>>(); TypedQuery<HostConfigMappingEntity> query = entityManagerProvider .get() .createQuery( "SELECT entity FROM HostConfigMappingEntity entity", HostConfigMappingEntity.class); List<HostConfigMappingEntity> hostConfigMappingEntities = daoUtils.selectList(query); for (HostConfigMappingEntity hostConfigMappingEntity : hostConfigMappingEntities) { Set<HostConfigMapping> setByHost = hostConfigMappingByHost.get((hostConfigMappingEntity.getHostName())); if (setByHost == null) { setByHost = new HashSet<HostConfigMapping>(); hostConfigMappingByHost.put(hostConfigMappingEntity.getHostName(), setByHost); } HostConfigMapping hostConfigMapping = buildHostConfigMapping(hostConfigMappingEntity); setByHost.add(hostConfigMapping); } } } finally { gl.writeLock().unlock(); } } finally { gl.writeLock().unlock(); } cacheLoaded = true; } }