gt: 		if (repo != null) {
context: /*
   * Tigase Jabber/XMPP Server
   * Copyright (C) 2004-2012 "Artur Hefczyc" <artur.hefczyc@tigase.org>
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU Affero General Public License as published by
   * the Free Software Foundation, version 3 of the License.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU Affero General Public License for more details.
   *
   * You should have received a copy of the GNU Affero General Public License
   * along with this program. Look for COPYING file in the top folder.
   * If not, see http://www.gnu.org/licenses/.
   *
   * $Rev$
   * Last modified by $Author$
   * $Date$
   */
  
  package tigase.db;
  
  //~--- non-JDK imports --------------------------------------------------------
  
  import tigase.util.SimpleCache;
  
  import tigase.xmpp.BareJID;
  
  //~--- JDK imports ------------------------------------------------------------
  
  import java.util.Collections;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Map;
  import java.util.concurrent.LinkedBlockingQueue;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  //~--- classes ----------------------------------------------------------------
  
  /**
   * Created: Jan 28, 2009 8:46:53 PM
   *
   * @author <a href="mailto:artur.hefczyc@tigase.org">Artur Hefczyc</a>
   * @version $Rev$
   */
  public class UserRepositoryPool implements UserRepository {
  	private static final Logger log = Logger.getLogger(UserRepositoryPool.class.getName());
  
  	//~--- fields ---------------------------------------------------------------
  
  	private Map<String, Object> cache = null;
  	private LinkedBlockingQueue<UserRepository> repoPool =
  		new LinkedBlockingQueue<UserRepository>();
  
  	//~--- methods --------------------------------------------------------------
  
  	/**
  	 * Method description
  	 *
  	 *
  	 * @param user
  	 * @param subnode
  	 * @param key
  	 * @param list
  	 *
  	 * @throws TigaseDBException
  	 * @throws UserNotFoundException
  	 */
  	@Override
  	public void addDataList(BareJID user, String subnode, String key, String[] list)
  			throws UserNotFoundException, TigaseDBException {
  		UserRepository repo = takeRepo();
  
  		if (repo != null) {
  			try {
  				repo.addDataList(user, subnode, key, list);
  			} finally {
  				addRepo(repo);
  			}
  		} else {
  			log.log(Level.WARNING, "repo is NULL, pool empty? - {0}", repoPool.size());
  		}
  	}
  
  	/**
  	 * Method description
  	 *
  	 *
  	 * @param repo
  	 */
  	public void addRepo(UserRepository repo) {
  		repoPool.offer(repo);
  	}
  
  	/**
  	 * Method description
  	 *
  	 *
  	 * @param user
  	 *
  	 * @throws TigaseDBException
  	 * @throws UserExistsException
  	 */
  	@Override
  	public void addUser(BareJID user) throws UserExistsException, TigaseDBException {
  		UserRepository repo = takeRepo();
  
  		if (repo != null) {
  			try {
  				repo.addUser(user);
  			} finally {
  				addRepo(repo);
  			}
  		} else {
  			log.log(Level.WARNING, "repo is NULL, pool empty? - {0}", repoPool.size());
  		}
  	}
  
  	//~--- get methods ----------------------------------------------------------
  
  	/**
  	 * Method description
  	 *
  	 *
  	 * @param user
  	 * @param subnode
  	 * @param key
  	 * @param def
  	 *
  	 * @return
  	 *
  	 * @throws TigaseDBException
  	 * @throws UserNotFoundException
  	 */
  	@Override
  	public String getData(BareJID user, String subnode, String key, String def)
  			throws UserNotFoundException, TigaseDBException {
  		String data = (String) cache.get(user + "/" + subnode + "/" + key);
  
  		if (data != null) {
  			return data;
  		}
  
  		UserRepository repo = takeRepo();
  
  		if (repo != null) {
  			try {
  				return repo.getData(user, subnode, key, def);
  			} finally {
  				addRepo(repo);
  			}
  		} else {
  			log.log(Level.WARNING, "repo is NULL, pool empty? - {0}", repoPool.size());
  		}
  
  		return null;
  	}
  
  	/**
  	 * Method description
  	 *
  	 *
  	 * @param user
  	 * @param subnode
  	 * @param key
  	 *
  	 * @return
  	 *
  	 * @throws TigaseDBException
  	 * @throws UserNotFoundException
  	 */
  	@Override
  	public String getData(BareJID user, String subnode, String key)
  			throws UserNotFoundException, TigaseDBException {
  		String data = (String) cache.get(user + "/" + subnode + "/" + key);
  
  		if (data != null) {
  			return data;
  		}
  
  		UserRepository repo = takeRepo();
  
  		if (repo != null) {
  			try {
  				return repo.getData(user, subnode, key);
  			} finally {
  				addRepo(repo);
  			}
  		} else {
  			log.log(Level.WARNING, "repo is NULL, pool empty? - {0}", repoPool.size());
  		}
  
  		return null;
  	}
  
  	/**
  	 * Method description
  	 *
  	 *
  	 * @param user
  	 * @param key
  	 *
  	 * @return
  	 *
  	 * @throws TigaseDBException
  	 * @throws UserNotFoundException
  	 */
  	@Override
  	public String getData(BareJID user, String key)
  			throws UserNotFoundException, TigaseDBException {
  		String data = (String) cache.get(user + "/" + key);
  
  		if (data != null) {
  			return data;
  		}
  
  		UserRepository repo = takeRepo();
  
  		if (repo != null) {
  			try {
  				return repo.getData(user, key);
  			} finally {
  				addRepo(repo);
  			}
  		} else {
  			log.log(Level.WARNING, "repo is NULL, pool empty? - {0}", repoPool.size());
  		}
  
  		return null;
  	}
  
  	/**
  	 * Method description
  	 *
  	 *
  	 * @param user
  	 * @param subnode
  	 * @param key
  	 *
  	 * @return
  	 *
  	 * @throws TigaseDBException
  	 * @throws UserNotFoundException
  	 */
  	@Override
  	public String[] getDataList(BareJID user, String subnode, String key)
  			throws UserNotFoundException, TigaseDBException {
  		UserRepository repo = takeRepo();
  
  		if (repo != null) {
  			try {
  				return repo.getDataList(user, subnode, key);
  			} finally {
  				addRepo(repo);
  			}
  		} else {
  			log.log(Level.WARNING, "repo is NULL, pool empty? - {0}", repoPool.size());
  		}
  
  		return null;
  	}
  
  	/**
  	 * Method description
  	 *
  	 *
  	 * @param user
  	 * @param subnode
  	 *
  	 * @return
  	 *
  	 * @throws TigaseDBException
  	 * @throws UserNotFoundException
  	 */
  	@Override
  	public String[] getKeys(BareJID user, String subnode)
  			throws UserNotFoundException, TigaseDBException {
  		UserRepository repo = takeRepo();
  
  		if (repo != null) {
  			try {
  				return repo.getKeys(user, subnode);
  			} finally {
  				addRepo(repo);
  			}
  		} else {
  			log.log(Level.WARNING, "repo is NULL, pool empty? - {0}", repoPool.size());
  		}
  
  		return null;
  	}
  
  	/**
  	 * Method description
  	 *
  	 *
  	 * @param user
  	 *
  	 * @return
  	 *
  	 * @throws TigaseDBException
  	 * @throws UserNotFoundException
  	 */
  	@Override
  	public String[] getKeys(BareJID user) throws UserNotFoundException, TigaseDBException {
  		UserRepository repo = takeRepo();
  
  		if (repo != null) {
  			try {
  				return repo.getKeys(user);
  			} finally {
  				addRepo(repo);
  			}
  		} else {
  			log.log(Level.WARNING, "repo is NULL, pool empty? - {0}", repoPool.size());
  		}
  
  		return null;
  	}
  
  	/**
  	 * Method description
  	 *
  	 *
  	 * @return
  	 */
  	@Override
  	public String getResourceUri() {
  		return null;
  	}
  
  	/**
  	 * Method description
  	 *
  	 *
  	 * @param user
  	 * @param subnode
  	 *
  	 * @return
  	 *
  	 * @throws TigaseDBException
  	 * @throws UserNotFoundException
  	 */
  	@Override
  	public String[] getSubnodes(BareJID user, String subnode)
  			throws UserNotFoundException, TigaseDBException {
  		UserRepository repo = takeRepo();
  
  		if (repo != null) {
  			try {
  				return repo.getSubnodes(user, subnode);
  			} finally {
  				addRepo(repo);
  			}
  		} else {
  			log.log(Level.WARNING, "repo is NULL, pool empty? - {0}", repoPool.size());
  		}
  
  		return null;
  	}
  
  	/**
  	 * Method description
  	 *
  	 *
  	 * @param user
  	 *
  	 * @return
  	 *
  	 * @throws TigaseDBException
  	 * @throws UserNotFoundException
  	 */
  	@Override
  	public String[] getSubnodes(BareJID user) throws UserNotFoundException, TigaseDBException {
  		UserRepository repo = takeRepo();
  
  		if (repo != null) {
  			try {
  				return repo.getSubnodes(user);
  			} finally {
  				addRepo(repo);
  			}
  		} else {
  			log.log(Level.WARNING, "repo is NULL, pool empty? - {0}", repoPool.size());
  		}
  
  		return null;
  	}
  
  	/**
  	 * Method description
  	 *
  	 *
  	 * @param user
  	 *
  	 * @return
  	 *
  	 * @throws TigaseDBException
  	 */
  	@Override
  	public long getUserUID(BareJID user) throws TigaseDBException {
  		UserRepository repo = takeRepo();
  
  		if (repo != null) {
  			try {
  				return repo.getUserUID(user);
  			} finally {
  				addRepo(repo);
  			}
  		} else {
  			log.log(Level.WARNING, "repo is NULL, pool empty? - {0}", repoPool.size());
  		}
  
  		return -1;
  	}
  
  	/**
  	 * Method description
  	 *
  	 *
  	 * @return
  	 *
  	 * @throws TigaseDBException
  	 */
  	@Override
  	public List<BareJID> getUsers() throws TigaseDBException {
  		UserRepository repo = takeRepo();
  
  		if (repo != null) {
  			try {
  				return repo.getUsers();
  			} finally {
  				addRepo(repo);
  			}
  		} else {
  			log.log(Level.WARNING, "repo is NULL, pool empty? - {0}", repoPool.size());
  		}
  
  		return null;
  	}
  
  	/**
  	 * Method description
  	 *
  	 *
  	 * @return
  	 */
  	@Override
  	public long getUsersCount() {
  		UserRepository repo = takeRepo();
  
  		if (repo != null) {
  			try {
  				return repo.getUsersCount();
  			} finally {
  				addRepo(repo);
  			}
  		} else {
  			log.log(Level.WARNING, "repo is NULL, pool empty? - {0}", repoPool.size());
  		}
  
  		return 0;
  	}
  
  	/**
  	 * Method description
  	 *
  	 *
  	 * @param domain
  	 *
  	 * @return
  	 */
  	@Override
  	public long getUsersCount(String domain) {
  		UserRepository repo = takeRepo();
  
  		if (repo != null) {
  			try {
  				return repo.getUsersCount(domain);
  			} finally {
  				addRepo(repo);
  			}
  		} else {
  			log.log(Level.WARNING, "repo is NULL, pool empty? - {0}", repoPool.size());
  		}
  
  		return 0;
  	}
  
  	//~--- methods --------------------------------------------------------------
  
  	/**
  	 * Method description
  	 *
  	 *
  	 * @param resource_uri
  	 * @param params
  	 *
  	 * @throws DBInitException
  	 */
  	@Override
  	public void initRepository(String resource_uri, Map<String, String> params)
  			throws DBInitException {
  		if (resource_uri.contains("cacheRepo=off")) {
  			log.fine("Disabling cache.");
  			cache = Collections.synchronizedMap(new RepoCache(0, -1000));
  		} else {
  			cache = Collections.synchronizedMap(new RepoCache(10000, 60 * 1000));
  		}
  	}
  
  	/**
  	 * Method description
  	 *
  	 *
  	 * @param user
  	 * @param subnode
  	 * @param key
  	 *
  	 * @throws TigaseDBException
  	 * @throws UserNotFoundException
  	 */
  	@Override
  	public void removeData(BareJID user, String subnode, String key)
  			throws UserNotFoundException, TigaseDBException {
  		cache.remove(user + "/" + subnode + "/" + key);
  
  		UserRepository repo = takeRepo();
  
  		if (repo != null) {
  			try {
  				repo.removeData(user, subnode, key);
  			} finally {
  				addRepo(repo);
  			}
  		} else {
  			log.log(Level.WARNING, "repo is NULL, pool empty? - {0}", repoPool.size());
  		}
  	}
  
  	/**
  	 * Method description
  	 *
  	 *
  	 * @param user
  	 * @param key
  	 *
  	 * @throws TigaseDBException
  	 * @throws UserNotFoundException
  	 */
  	@Override
  	public void removeData(BareJID user, String key)
  			throws UserNotFoundException, TigaseDBException {
  		cache.remove(user + "/" + key);
  
  		UserRepository repo = takeRepo();
  
  