Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * ====================================================================
   * Copyright (c) 2004-2006 TMate Software Ltd.  All rights reserved.
   *
   * This software is licensed as described in the file COPYING, which
   * you should have received as part of this distribution.  The terms
   * are also available at http://svnkit.com/license.html
   * If newer versions of this license are posted there, you may use a
   * newer version instead, at your option.
  * ====================================================================
  */
 package org.tmatesoft.svn.core.wc;
 
 import java.util.Map;
 
The DefaultSVNRepositoryPool class is a default implementation of the ISVNRepositoryPool interface.

It creates SVNRepository objects that may be stored in a common pool and reused later. The objects common pool may be shared by different threads, but each thread can retrieve only those objects, that have been created within that thread. So, DefaultSVNRepositoryPool is thread-safe. An objects pool may be global during runtime, or it may be private - one separate pool per one DefaultSVNRepositoryPool object. Also there's a possibility to have a DefaultSVNRepositoryPool object with the pool feature disabled (SVNRepository objects instantiated by such a creator are never cached).

DefaultSVNRepositoryPool caches one SVNRepository object per one url protocol (per one thread), that is the number of protocols used equals to the number of objects cached per one thread (if all objects are created as reusable).

Also DefaultSVNRepositoryPool is able to create SVNRepository objects that use a single socket connection (i.e. don't close a connection after every repository access operation but reuse a single one).

Author(s):
TMate Software Ltd.
Version:
1.1.0
 
 public class DefaultSVNRepositoryPool implements ISVNRepositoryPoolISVNSession {
    
Defines a common shared objects pool. All objects that will be created by different threads will be stored in this common pool.
 
     public static final int RUNTIME_POOL = 1;
    
Defines a private pool. All objects that will be created by different threads will be stored only within this pool object. This allows to have more than one separate pools.
 
     public static final int INSTANCE_POOL = 2;
    
Defines a without-pool configuration. Objects that are created by this DefaultSVNRepositoryPool object are not cached, the pool feature is disabled.
 
     public static final int NO_POOL = 4;
     
     private boolean myIsKeepConnections;
     private int myPoolMode;
     
     private Map myPool;
     private static Map ourPool;
     private static Object ourPoolMonitor = new Object();
     
     private static final boolean ourAllowPersistentConnections = "true".equalsIgnoreCase(
             System.getProperty("svnkit.http.keepAlive", System.getProperty("javasvn.http.keepAlive""true")));
    
    
Constructs a DefaultSVNRepositoryPool instance that represents RUNTIME_POOL objects pool. SVNRepository objects created by this instance will use a single socket connection.

This constructor is equivalent to DefaultSVNRepositoryPool(authManager, true, RUNTIME_POOL).

Parameters:
authManager an authentication driver
tunnelProvider a tunnel provider
 
     public DefaultSVNRepositoryPool(ISVNAuthenticationManager authManagerISVNTunnelProvider tunnelProvider) {
        this(authManagertunnelProvidertrue);
    }
    
    
Constructs a DefaultSVNRepositoryPool instance.

Parameters:
authManager an authentication driver
tunnelProvider a tunnel provider
keepConnections if true then SVNRepository objects will keep a single connection for accessing a repository, if false - open a new connection per each repository access operation
poolMode a mode of this object represented by one of the constant fields of DefaultSVNRepositoryPool
    public DefaultSVNRepositoryPool(ISVNAuthenticationManager authManagerISVNTunnelProvider tunnelProviderboolean keepConnectionsint poolMode) {
         = authManager;
         = keepConnections;
         = poolMode;
         = tunnelProvider;
    }
    
    
Creates a new SVNRepository driver object. if mayReuse is true and the mode of this DefaultSVNRepositoryPool object is not NO_POOL then first tries to find the SVNRepository object in the pool for the given protocol. If the object is not found, creates a new one for that protocol, caches it in the pool and returns back.

NOTE: be careful when simultaneously using several SVNRepository drivers for the same protocol - since there can be only one driver object in the pool per a protocol, creating two objects for the same protocol with mayReuse set to true, actually returns the same single object stored in the thread pool.

Parameters:
url a repository location for which a driver is to be created
mayReuse if true then SVNRepository object is reusable
Returns:
a new SVNRepository driver object
Throws:
org.tmatesoft.svn.core.SVNException
See also:
org.tmatesoft.svn.core.io.SVNRepository
    public synchronized SVNRepository createRepository(SVNURL urlboolean mayReusethrows SVNException {
        SVNRepository repos = null;
        Map pool = getPool();
        if (!mayReuse || pool == null) {            
            repos = SVNRepositoryFactory.create(urlthis);
            repos.setAuthenticationManager();
            repos.setTunnelProvider();
            return repos;
        }
        
        repos = retriveRepository(poolurl.getProtocol());
        if (repos != null) {
            repos.setLocation(urlfalse);
        } else {
            repos = SVNRepositoryFactory.create(urlthis);
            saveRepository(poolreposurl.getProtocol());
        }         
        repos.setTunnelProvider();
        
        return repos;
    }
    
Says if the given SVNRepository driver object should keep a connection opened. If this object was created with keepConnections set to true and if repository is not created for the "svn+ssh" protocol (since for this protocol there's no extra need to keep a connection opened - it remains opened), this method returns true.

Parameters:
repository an SVNRepository driver
Returns:
true if the driver should keep a connection
    public boolean keepConnection(SVNRepository repository) {
        if (!) {
            return false;
        }
        String protocol = repository.getLocation().getProtocol();
        return  && !"svn".equalsIgnoreCase(protocol) && !"svn+ssh".equalsIgnoreCase(protocol);
    }
    
    
Closes connections of cached SVNRepository objects.

Parameters:
shutdownAll if true - closes connections of all the cached objects, otherwise only connections of those cached objects which owner threads have already disposed
See also:
org.tmatesoft.svn.core.io.SVNRepository
    public synchronized void shutdownConnections(boolean shutdownAll) {
        Map pool = null;
        if ( == ) {
            pool = ;
        } else if ( == ){
            pool = ;
        }
        if (pool != null) {
            synchronized () {
                clearPool(poolshutdownAll);
            }
        }
    }
    
    private Map getPool() {
        switch () {
            case :
                if ( == null) {
                     = new HashMap();
                }
                return ;
            case :
                synchronized () {
                    if ( == null) {
                         = new HashMap();
                    }
                    return ;
                }
            default:
        }
        return null;
    }
    // no caching in this class
    
Does nothing.

Parameters:
repository an SVNRepository driver (to distinguish that repository for which this message is actual)
revision a revision number
message the commit message for revision
    public void saveCommitMessage(SVNRepository repositorylong revisionString message) {
    }
    
    
Returns null.

Parameters:
repository an SVNRepository driver (to distinguish that repository for which a commit message is requested)
revision a revision number
Returns:
the commit message for revision
    public String getCommitMessage(SVNRepository repositorylong revision) {
        return null;
    }
    
    
Returns false.

Parameters:
repository an SVNRepository driver (to distinguish that repository for which a commit message is requested)
revision a revision number
Returns:
true if the cache has got a message for the given repository and revision, false otherwise
    public boolean hasCommitMessage(SVNRepository repositorylong revision) {
        return false;
    }
    
    private static void clearPool(Map poolboolean force) {
        for (Iterator references = pool.keySet().iterator(); references.hasNext();) {
            WeakReference reference = (WeakReferencereferences.next();
            if (force || reference.get() == null) {
                Map repositoriesMap = (Mappool.get(reference);
                for (Iterator repos = repositoriesMap.values().iterator(); repos.hasNext();) {
                    SVNRepository repo = (SVNRepositoryrepos.next();
                    try {
                        repo.closeSession();
                    } catch (SVNException e) {
                    }
                    repos.remove();
                }
                references.remove();
            }
        }
    }
    
    private static SVNRepository retriveRepository(Map poolString protocol) {
        synchronized () {
            clearPool(poolfalse);
            for (Iterator references = pool.keySet().iterator(); references.hasNext();) {
                WeakReference reference = (WeakReferencereferences.next();
                if (reference.get() == Thread.currentThread()) {
                    Map repositoriesMap = (Mappool.get(reference);
                    if (repositoriesMap.containsKey(protocol)) {
                        return (SVNRepositoryrepositoriesMap.get(protocol);
                    }
                    return null;
                } 
            }
            return null;
        }
    }
    private static void saveRepository(Map poolSVNRepository repositoryString protocol) {
        synchronized () {
            clearPool(poolfalse);
            for (Iterator references = pool.keySet().iterator(); references.hasNext();) {
                WeakReference reference = (WeakReferencereferences.next();
                if (reference.get() == Thread.currentThread()) {
                    Map repositoriesMap = (Mappool.get(reference);
                    repositoriesMap.put(protocolrepository);
                    return;
                } 
            }
            Map map = new HashMap();
            map.put(protocolrepository);
            pool.put(new WeakReference(Thread.currentThread()), map);
        }
    }
New to GrepCode? Check out our FAQ X