/** * @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); } }
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; } }
@RequiresSession public List<HostConfigMappingEntity> findAll() { return daoUtils.selectAll(entityManagerProvider.get(), HostConfigMappingEntity.class); }