// No package - this is deliberate
import org.apache.commons.lang.Validate;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.sventon.RepositoryConnectionFactory;
import org.sventon.appl.ConfigDirectory;
import org.sventon.model.Credentials;
import org.sventon.model.RepositoryName;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNWCUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.IOException;
/**
* Factory class responsible for creating subversion repository connections.
* Based on the default sventon 2.0.4 RepositoryConnectionFactoryImpl.
*/
public class CowRepositoryConnectionFactory implements RepositoryConnectionFactory {
private final Log logger = LogFactory.getLog(getClass());
/**
* Root directory where to place the svn config files.
*/
private final ConfigDirectory configurationDirectory;
/**
* Subversion configuration directory, where we will look for
* <code>config</code> and <code>servers</code> files.
*/
private final File svnconfigDirectory;
/**
* Constructor.
*
* @param configDirectory Root directory where to place the svn config files.
* If the directory does not exist, it will be created.
* @param svnconfigDirectory Directory containing the subversion
* configuration data (the config and servers files).
*/
public CowRepositoryConnectionFactory(final ConfigDirectory configDirectory,
final File svnconfigDirectory) {
Validate.notNull(configDirectory, "Configuration directory cannot be null!");
this.configurationDirectory = configDirectory;
this.svnconfigDirectory = svnconfigDirectory;
}
/**
* {@inheritDoc}
* <p/>
* Note: Be sure to call <code>repository.closeSession()</code> when connection is not needed anymore.
*/
public SVNRepository createConnection(final RepositoryName repositoryName, final SVNURL svnUrl,
final Credentials credentials) throws SVNException {
if (svnUrl == null) {
return null;
}
final SVNRepository repository = SVNRepositoryFactory.create(svnUrl);
final File configDirectory = new File(configurationDirectory.getRepositoriesDirectory(), repositoryName.toString());
AuthenticationData ad = null;
ISVNAuthenticationManager manager = null;
try {
File authFile = new File(configDirectory, "auth");
FileInputStream fis = new FileInputStream(authFile);
ObjectInputStream ois = new ObjectInputStream(fis);
try {
ad = (AuthenticationData)ois.readObject();
}
finally {
ois.close();
}
}
catch(Exception e) {
logger.warn("Could not find Cow-specific authentication data for "
+ "repository " + repositoryName, e);
// do nothing
}
if(ad != null) {
manager = new CowAuthenticationManager(svnconfigDirectory);
((CowAuthenticationManager)manager).setAuthenticationData(ad);
}
else {
// fall back on sventon default behaviour
manager = SVNWCUtil.createDefaultAuthenticationManager(
svnconfigDirectory, credentials.getUsername(), credentials.getPassword(), false);
}
repository.setAuthenticationManager(manager);
// fetch tunnel definitions from svnconfig/config file
repository.setTunnelProvider(
SVNWCUtil.createDefaultOptions(svnconfigDirectory, true));
return repository;
}
}