content: /*
   * Copyright (c) 2013 3 Round Stones Inc., Some Rights Reserved
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   *
   */
  package org.callimachusproject.auth;
  
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.Map;
  import java.util.Set;
  
  import org.openrdf.repository.Repository;
  import org.openrdf.repository.RepositoryConnection;
  import org.openrdf.repository.RepositoryException;
  import org.openrdf.repository.object.ObjectRepository;
  
  public class GroupManager {
  	private static volatile int cache = 0;
  
  	public static void reset() {
  		cache++;
  	}
  
  	private int revision = cache;
  	private final Repository repo;
  	private final Map<String, Group> groups = new HashMap<String, Group>();
  
  	public GroupManager(ObjectRepository repository) {
  		this.repo = repository;
  	}
  
  	public synchronized void resetCache() {
  		groups.clear();
  	}
  
  	public synchronized Set<Group> getGroups(Set<String> uris) throws RepositoryException {
  		if (revision != cache) {
  			resetCache();
  			revision = cache;
  		}
  		Set<Group> groups = new HashSet<Group>(uris.size());
  		for (String uri : uris) {
  			groups.add(getGroup(uri));
  		}
  		return groups;
  	}
  
  	private synchronized Group getGroup(String uri) throws RepositoryException {
  		if (groups.containsKey(uri))
  			return groups.get(uri);
  		RepositoryConnection con = repo.getConnection();
  		try {
  			Group group = new Group(uri, con);
  			groups.put(uri, group);
  			return group;
  		} finally {
  			con.close();
  		}
  	}
  
  }
  