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.io;
 
 import java.io.File;
 import java.util.Map;
 
SVNRepositoryFactory is an abstract factory that is responsible for creating an appropriate SVNRepository driver specific for the protocol to use.

Depending on what protocol a user exactly would like to use to access the repository he should first of all set up an appropriate extension of this factory. So, if the user is going to work with the repository via the custom svn-protocol (or svn+xxx) he initially calls:

 ...
 import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
 ...      
     //do it once in your application prior to using the library
     //enables working with a repository via the svn-protocol (over svn and svn+ssh)
     SVNRepositoryFactoryImpl.setup();
 ...

From this point the SVNRepositoryFactory knows how to create SVNRepository instances specific for the svn-protocol. And further on the user can create an SVNRepository instance:
     ...
     //creating a new SVNRepository instance
     String url = "svn://host/path";
     SVNRepository repository = SVNRepositoryFactory.create(SVNURL.parseURIDecoded(url));
     ...

Supported ProtocolsFactory to setup
svn://, svn+xxx://SVNRepositoryFactoryImpl (org.tmatesoft.svn.core.internal.io.svn)
http://, https://DAVRepositoryFactory (org.tmatesoft.svn.core.internal.io.dav)
file:/// (FSFS only)FSRepositoryFactory (org.tmatesoft.svn.core.internal.io.fs)

Also SVNRepositoryFactory may be used to create local FSFS-type repositories.

Author(s):
TMate Software Ltd.
Version:
1.1.0
See also:
SVNRepository
Examples
 
 public abstract class SVNRepositoryFactory {
     
     private static final Map myFactoriesMap = new HashMap();
     private static final String REPOSITORY_TEMPLATE_PATH = "org/tmatesoft/svn/core/io/repository/template.jar";
     
     protected static void registerRepositoryFactory(String protocolSVNRepositoryFactory factory) {
         if (protocol != null && factory != null) {
             .put(protocolfactory);
        }
    }
    
    protected static boolean hasRepositoryFactory(String protocol) {
        if (protocol != null) {
            return .get(protocol) != null;
        }
        return false;
    }
    
    
Creates an SVNRepository driver according to the protocol that is to be used to access a repository.

The protocol is defined as the beginning part of the URL schema. Currently SVNKit supports only svn:// (svn+ssh://) and http:// (https://) schemas.

The created SVNRepository driver can later be "reused" for another location - that is you can switch it to another repository url not to create yet one more SVNRepository object. Use the SVNRepository.setLocation() method for this purpose.

An SVNRepository driver created by this method uses a default session options driver (ISVNSession.DEFAULT) which does not allow to keep a single socket connection opened and commit log messages caching.

Parameters:
url a repository location URL
Returns:
a protocol specific SVNRepository driver
Throws:
org.tmatesoft.svn.core.SVNException if there's no implementation for the specified protocol (the user may have forgotten to register a specific factory that creates SVNRepository instances for that protocol or the SVNKit library does not support that protocol at all)
See also:
create(org.tmatesoft.svn.core.SVNURL,org.tmatesoft.svn.core.io.ISVNSession)
SVNRepository
    public static SVNRepository create(SVNURL urlthrows SVNException {
        return create(urlnull);
        
    }
    
    
Creates an SVNRepository driver according to the protocol that is to be used to access a repository.

The protocol is defined as the beginning part of the URL schema. Currently SVNKit supports only svn:// (svn+ssh://) and http:// (https://) schemas.

The created SVNRepository driver can later be "reused" for another location - that is you can switch it to another repository url not to create yet one more SVNRepository object. Use the SVNRepository.setLocation() method for this purpose.

This method allows to customize a session options driver for an SVNRepository driver. A session options driver must implement the ISVNSession interface. It manages socket connections - says whether an SVNRepository driver may use a single socket connection during the runtime, or it should open a new connection per each repository access operation. And also a session options driver may cache and provide commit log messages during the runtime.

Parameters:
url a repository location URL
options a session options driver
Returns:
a protocol specific SVNRepository driver
Throws:
org.tmatesoft.svn.core.SVNException if there's no implementation for the specified protocol (the user may have forgotten to register a specific factory that creates SVNRepository instances for that protocol or the SVNKit library does not support that protocol at all)
See also:
create(org.tmatesoft.svn.core.SVNURL)
SVNRepository
    public static SVNRepository create(SVNURL urlISVNSession optionsthrows SVNException {
        String urlString = url.toString();
    	for(Iterator keys = .keySet().iterator(); keys.hasNext();) {
    		String key = (Stringkeys.next();
    		if (Pattern.matches(keyurlString)) {
    			return ((SVNRepositoryFactory.get(key)).createRepositoryImpl(urloptions);
    		}
    	}
        if ("file".equalsIgnoreCase(url.getProtocol())) {
            SVNErrorMessage err = SVNErrorMessage.create(."Unable to open an ra_local session to URL");
            SVNErrorManager.error(err);
        }
        SVNErrorMessage err = SVNErrorMessage.create(."Unable to create SVNRepository object for ''{0}''"url);
        SVNErrorManager.error(err);
        return null;
    }

    
Creates a local blank FSFS-type repository. A call to this routine is equvalent to createLocalRepository(path, null, enableRevisionProperties, force).

Parameters:
path a repository root location
enableRevisionProperties enables or not revision property modifications
force forces operation to run
Returns:
a local URL (file:///) of a newly created repository
Throws:
org.tmatesoft.svn.core.SVNException
Since:
1.1
See also:
createLocalRepository(java.io.File,java.lang.String,boolean,boolean)
    public static SVNURL createLocalRepository(File pathboolean enableRevisionPropertiesboolean forcethrows SVNException {
        return createLocalRepository(pathnullenableRevisionPropertiesforce);
    }
    
    
Creates a local blank FSFS-type repository. This is just similar to the Subversion's command: svnadmin create --fs-type=fsfs REPOS_PATH. The resultant repository is absolutely format-compatible with Subversion.

If uuid is null or not 36 chars wide, the method generates a new UUID for the repository. This UUID would have the same format as if it's generated by Subversion itself.

If enableRevisionProperties is true then the method creates a pre-revprop-change executable file inside the "hooks" subdir of the repository tree. This executable file simply returns 0 thus allowing revision property modifications, which are not permitted, unless one puts such a hook into that very directory.

If force is true and path already exists, deletes that path and creates a repository in its place.

Parameters:
path a repository root location
uuid a repository's uuid
enableRevisionProperties enables or not revision property modifications
force forces operation to run
Returns:
a local URL (file:///) of a newly created repository
Throws:
org.tmatesoft.svn.core.SVNException
Since:
1.1
See also:
createLocalRepository(java.io.File,boolean,boolean)
    public static SVNURL createLocalRepository(File pathString uuidboolean enableRevisionPropertiesboolean forcethrows SVNException {
        SVNFileType fType = SVNFileType.getType(path);
        if (!force && fType != .) {
            SVNErrorMessage err = SVNErrorMessage.create(."''{0}'' already exists; use ''force'' to overwrite existing files"path);
            SVNErrorManager.error(err);
        }
        SVNFileUtil.deleteAll(pathtrue);
        if (!path.mkdirs()) {
            SVNErrorMessage err = SVNErrorMessage.create(."Can not create directory ''{0}''"path);
            SVNErrorManager.error(err);
        }
        if (is == null) {
            SVNErrorMessage err = SVNErrorMessage.create(."No repository template found; should be part of SVNKit library jar");
            SVNErrorManager.error(err);
        }
        File jarFile = SVNFileUtil.createUniqueFile(path"template."".jar");
        OutputStream uuidOS = null
        try {
            copyToFile(isjarFile);
            extract(jarFilepath);
            // translate eols.
            if (!.) {
                translateFiles(path);
                translateFiles(new File(path"conf"));
                translateFiles(new File(path"hooks"));
                translateFiles(new File(path"locks"));
            }
            // create pre-rev-prop.
            if (enableRevisionProperties) {
                if (.) {
                    SVNFileUtil.createEmptyFile(new File(path"hooks/pre-revprop-change.bat"));
                } else {
                    File hookFile = new File(path"hooks/pre-revprop-change");
                    OutputStream os = null;
                    try {
                        os = SVNFileUtil.openFileForWriting(hookFile);
                        os.write("#!/bin/sh\nexit 0".getBytes("US-ASCII"));                        
                    } catch (IOException e) {
                        SVNErrorMessage err = SVNErrorMessage.create(."Cannot create pre-rev-prop-change hook file at ''{0}'': {1}"
                                new Object[] {hookFilee.getLocalizedMessage()});
                        SVNErrorManager.error(err);
                    } finally {
                        SVNFileUtil.closeFile(os);
                    }
                    SVNFileUtil.setExecutable(hookFiletrue);
                }
            }
            // generate and write UUID.
            File uuidFile = new File(path"db/uuid");
            if (uuid == null || uuid.length() != 36) {
                byte[] uuidBytes = SVNUUIDGenerator.generateUUID();
                uuid = SVNUUIDGenerator.formatUUID(uuidBytes);
            } 
            uuid += '\n'
            try {
                uuidOS = SVNFileUtil.openFileForWriting(uuidFile);
                uuidOS.write(uuid.getBytes("US-ASCII"));
            } catch (IOException e) {
                SVNErrorMessage err = SVNErrorMessage.create(."Error writing repository UUID to ''{0}''"uuidFile);
                err.setChildErrorMessage(SVNErrorMessage.create(.e.getLocalizedMessage()));
                SVNErrorManager.error(err);
            }
        } finally {
            SVNFileUtil.closeFile(uuidOS);
            SVNFileUtil.deleteFile(jarFile);
        }
        return SVNURL.fromFile(path);
    }
    protected abstract SVNRepository createRepositoryImpl(SVNURL urlISVNSession session);
    
    private static void copyToFile(InputStream isFile dstFilethrows SVNException {
        OutputStream os = null
        byte[] buffer = new byte[16*1024];
        try {
            os = SVNFileUtil.openFileForWriting(dstFile);
            while(true) {
                int r = is.read(buffer);
                if (r <= 0) {
                    break;
                }
                os.write(buffer, 0, r);
            }
        } catch (IOException e) {
            SVNErrorMessage err = SVNErrorMessage.create(."Can not copy repository template file to ''{0}''"dstFile);
            err.setChildErrorMessage(SVNErrorMessage.create(.e.getLocalizedMessage()));
            SVNErrorManager.error(err);
        } finally {
            SVNFileUtil.closeFile(os);
            SVNFileUtil.closeFile(is);
        }
    }
    private static void extract(File srcFileFile dstthrows SVNException {
        JarInputStream jis = null;
        InputStream is = SVNFileUtil.openFileForReading(srcFile);
        byte[] buffer = new byte[16*1024];
        
        JarFile jarFile = null;
        try {
            jarFile = new JarFile(srcFile);
            jis = new JarInputStream(is);
            while(true) {
                JarEntry entry = jis.getNextJarEntry();
                if (entry == null) {
                    break;
                }
                String name = entry.getName();
                File entryFile = new File(dstname); 
                if (entry.isDirectory()) {
                    entryFile.mkdirs();
                } else {
                    InputStream fis = null;
                    OutputStream fos = null;
                    try {
                        fis = new BufferedInputStream(jarFile.getInputStream(entry));
                        fos = SVNFileUtil.openFileForWriting(entryFile);
                        while(true) {
                            int r = fis.read(buffer);
                            if (r <= 0) {
                                break;
                            }
                            fos.write(buffer, 0, r);
                        }
                    } finally {
                        SVNFileUtil.closeFile(fos);
                        SVNFileUtil.closeFile(fis);
                    }
                }
                jis.closeEntry();
            }
        } catch (IOException e) {
            SVNErrorMessage err = SVNErrorMessage.create(."Can not extract repository files from ''{0}'' to ''{1}''"
                    new Object[] {srcFiledst});
            err.setChildErrorMessage(SVNErrorMessage.create(.e.getLocalizedMessage()));
            SVNErrorManager.error(err);
        } finally {
            SVNFileUtil.closeFile(jis);
            SVNFileUtil.closeFile(is);
            if (jarFile != null) {
                try {
                    jarFile.close();
                } catch (IOException e) {
                }
            }
        }
    }
    
    private static void translateFiles(File directorythrows SVNException {
        File[] children = directory.listFiles();
        byte[] eol = new byte[] {'\n'};
        for (int i = 0; children != null && i < children.lengthi++) {
            File child = children[i];
            File tmpChild = null;
            try {
                tmpChild = SVNFileUtil.createUniqueFile(directorychild.getName() + "."".tmp");
                if (child.isFile()) {
                    SVNTranslator.translate(childtmpChildeolnullfalsetrue);
                }
                SVNFileUtil.deleteFile(child);
                SVNFileUtil.rename(tmpChildchild);
            } finally {
                SVNFileUtil.deleteFile(tmpChild);
            }
        }
    }
New to GrepCode? Check out our FAQ X