Coverage Report - fr.paris.lutece.plugins.releaser.util.svn.SvnUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
SvnUtils
0 %
0/66
0 %
0/14
2,455
SvnUtils$1
0 %
0/6
0 %
0/2
2,455
SvnUtils$2
0 %
0/5
0 %
0/4
2,455
 
 1  
 /*
 2  
  * Copyright (c) 2002-2013, Mairie de Paris
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met:
 8  
  *
 9  
  *  1. Redistributions of source code must retain the above copyright notice
 10  
  *     and the following disclaimer.
 11  
  *
 12  
  *  2. Redistributions in binary form must reproduce the above copyright notice
 13  
  *     and the following disclaimer in the documentation and/or other materials
 14  
  *     provided with the distribution.
 15  
  *
 16  
  *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
 17  
  *     contributors may be used to endorse or promote products derived from
 18  
  *     this software without specific prior written permission.
 19  
  *
 20  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 21  
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 22  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 23  
  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 24  
  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 25  
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 26  
  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 27  
  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 28  
  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 29  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 30  
  * POSSIBILITY OF SUCH DAMAGE.
 31  
  *
 32  
  * License 1.0
 33  
  */
 34  
 package fr.paris.lutece.plugins.releaser.util.svn;
 35  
 
 36  
 import java.io.File;
 37  
 import java.io.IOException;
 38  
 import java.io.PrintWriter;
 39  
 import java.io.StringWriter;
 40  
 
 41  
 import org.apache.commons.lang.ObjectUtils;
 42  
 import org.apache.log4j.Logger;
 43  
 import org.tmatesoft.svn.core.ISVNDirEntryHandler;
 44  
 import org.tmatesoft.svn.core.SVNAuthenticationException;
 45  
 import org.tmatesoft.svn.core.SVNCancelException;
 46  
 import org.tmatesoft.svn.core.SVNCommitInfo;
 47  
 import org.tmatesoft.svn.core.SVNDirEntry;
 48  
 import org.tmatesoft.svn.core.SVNException;
 49  
 import org.tmatesoft.svn.core.SVNNodeKind;
 50  
 import org.tmatesoft.svn.core.SVNURL;
 51  
 import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
 52  
 import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
 53  
 import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
 54  
 import org.tmatesoft.svn.core.io.SVNRepository;
 55  
 import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
 56  
 import org.tmatesoft.svn.core.wc.ISVNEventHandler;
 57  
 import org.tmatesoft.svn.core.wc.SVNClientManager;
 58  
 import org.tmatesoft.svn.core.wc.SVNCommitPacket;
 59  
 import org.tmatesoft.svn.core.wc.SVNCopyClient;
 60  
 import org.tmatesoft.svn.core.wc.SVNCopySource;
 61  
 import org.tmatesoft.svn.core.wc.SVNEvent;
 62  
 import org.tmatesoft.svn.core.wc.SVNEventAction;
 63  
 import org.tmatesoft.svn.core.wc.SVNRevision;
 64  
 
 65  
 import fr.paris.lutece.plugins.releaser.util.CommandResult;
 66  
 import fr.paris.lutece.plugins.releaser.util.ConstanteUtils;
 67  
 import fr.paris.lutece.plugins.releaser.util.ReleaserUtils;
 68  
 import fr.paris.lutece.plugins.releaser.util.file.FileUtils;
 69  
 import fr.paris.lutece.util.ReferenceItem;
 70  
 import fr.paris.lutece.util.ReferenceList;
 71  
 
 72  
 
 73  
 
 74  
 public final class SvnUtils
 75  
 {
 76  
     private static final String MESSAGE_ERROR_SVN = "Impossible de se connecter au SVN. Veuillez verifier vos identifiants";
 77  
     private static final String CONSTANTE_SLASH = "/";
 78  0
     private static final Logger logger = Logger.getLogger( SvnUtils.class );
 79  
 
 80  
     /**
 81  
      * Constructeur vide
 82  
      */
 83  
     private SvnUtils(  )
 84  0
     {
 85  
         // nothing
 86  0
     }
 87  
 
 88  
     /**
 89  
      * Initialise les diff�rentes factory pour le svn
 90  
      */
 91  
     public static void init(  )
 92  
     {
 93  
         /*
 94  
         * For using over http:// and https:
 95  
         */
 96  0
         DAVRepositoryFactory.setup(  );
 97  
         /*
 98  
          * For using over svn:// and svn+xxx:
 99  
          */
 100  0
         SVNRepositoryFactoryImpl.setup(  );
 101  
 
 102  
         /*
 103  
          * For using over file:/
 104  
          */
 105  0
         FSRepositoryFactory.setup(  );
 106  0
     }
 107  
     
 108  
 
 109  
     
 110  
     /**
 111  
      * Commit
 112  
      * @param strSiteName le nom du site
 113  
      * @param strTagName le nom du tag
 114  
      * @param copyClient le client svn permettant la copie
 115  
      * @throws SVNException
 116  
      */
 117  
     public static void  doCommit(  String strPathFile,String strCommitMessage,
 118  
             ReleaseSvnCommitClient commitClient  ) throws SVNException
 119  
     {
 120  0
         SVNCommitPacket commitPacket = commitClient.doCollectCommitItems( new File[] { new File( strPathFile ) },
 121  
                 false, false, false );
 122  
 
 123  0
         if ( !SVNCommitPacket.EMPTY.equals( commitPacket ) )
 124  
         {
 125  0
             commitClient.doCommit( commitPacket, false, strCommitMessage );
 126  
         }
 127  0
     }
 128  
 
 129  
 
 130  
     /**
 131  
      * Tag un site
 132  
      * @param strSiteName le nom du site
 133  
      * @param strTagName le nom du tag
 134  
      * @param copyClient le client svn permettant la copie
 135  
      * @throws SVNException
 136  
      */
 137  
     public static String doTagSite( String strSiteName, String strTagName, String strSrcURL, String strDstURL,
 138  
         SVNCopyClient copyClient ) throws SVNException
 139  
     {
 140  
         // COPY from trunk to tags/tagName
 141  0
         SVNURL srcURL = SVNURL.parseURIEncoded( strSrcURL );
 142  0
         SVNURL dstURL = SVNURL.parseURIEncoded( strDstURL );
 143  0
         SVNCopySource svnCopySource = new SVNCopySource( SVNRevision.HEAD, SVNRevision.HEAD, srcURL );
 144  0
         SVNCopySource[] tabSVNCopy = new SVNCopySource[1];
 145  0
         tabSVNCopy[0] = svnCopySource;
 146  
 
 147  0
         SVNCommitInfo info = copyClient.doCopy( tabSVNCopy, dstURL, false, false, false,
 148  
                 "[site-release] Tag site " + strSiteName + " to " + strTagName, null );
 149  
 
 150  0
         if ( info.getErrorMessage(  ) != null )
 151  
         {
 152  
                 
 153  0
             return info.getErrorMessage(  ).getMessage(  );
 154  
         }
 155  
 
 156  0
         return null;
 157  
     }
 158  
 
 159  
     public static String doSvnCheckout( String strUrl, String strCheckoutBaseSitePath,
 160  
         ReleaseSvnCheckoutClient updateClient, CommandResult result )
 161  
         throws SVNException
 162  
     {
 163  0
         SVNURL url = SVNURL.parseURIEncoded( strUrl );
 164  0
         File file = new File( strCheckoutBaseSitePath );
 165  
 
 166  0
         if ( file.exists(  ) )
 167  
         {
 168  0
             if ( !FileUtils.delete( file, result.getLog(  ) ) )
 169  
             {
 170  0
                 result.setError( result.getLog(  ).toString(  ) );
 171  
 
 172  0
                 return result.getLog(  ).toString(  );
 173  
             }
 174  
         }
 175  
 
 176  0
         SVNRepository repository = SVNRepositoryFactory.create( url, null );
 177  0
         final StringBuffer logBuffer = result.getLog(  );
 178  
 
 179  
         try
 180  
         {
 181  0
             updateClient.setEventHandler( new ISVNEventHandler(  )
 182  0
                 {
 183  
                     public void checkCancelled(  ) throws SVNCancelException
 184  
                     {
 185  
                         // Do nothing
 186  0
                     }
 187  
 
 188  
                     public void handleEvent( SVNEvent event, double progress )
 189  
                         throws SVNException
 190  
                     {
 191  0
                         logBuffer.append( ( ( event.getAction(  ) == SVNEventAction.UPDATE_ADD ) ? "ADDED "
 192  0
                                                                                                  : event.getAction(  ) ) +
 193  0
                             " " + event.getFile(  ) + "\n" );
 194  0
                     }
 195  
                 } );
 196  
 
 197  
             // SVNDepth.INFINITY + dernier param�tre � FALSE pour la version 1.3.2
 198  0
             updateClient.doCheckout( repository.getLocation(  ), file, SVNRevision.HEAD, SVNRevision.HEAD, true );
 199  
         }
 200  0
         catch ( SVNAuthenticationException e )
 201  
         {
 202  
             //            _result.getLog(  ).append( CONSTANTE_NO_LOGIN_PASSWORD );
 203  
             //            _result.setStatus( ICommandThread.STATUS_EXCEPTION );
 204  
             //            _result.setRunning( false );
 205  
 
 206  
 
 207  0
                 ReleaserUtils.addTechnicalError(result,"Une erreur est survenue lors de la tentative d'authentification avec le svn"+e,e);
 208  
                       
 209  0
             StringWriter sw = new StringWriter(  );
 210  0
             PrintWriter pw = new PrintWriter( sw );
 211  0
             e.printStackTrace( pw );
 212  
 
 213  0
             String errorLog = sw.toString(  );
 214  0
             pw.flush(  );
 215  0
             pw.close(  );
 216  
 
 217  
             try
 218  
             {
 219  0
                 sw.flush(  );
 220  0
                 sw.close(  );
 221  
             }
 222  0
             catch ( IOException e1 )
 223  
             {
 224  
                 // do nothing
 225  
                 //  _logger.error( e1 );
 226  0
             }
 227  
 
 228  
             //            _result.setLog( _result.getLog(  ).append( errorLog ) );
 229  
             //            _logger.error( e );
 230  
 
 231  
             //_result.setIdError( ReleaseLogger.logError( _result.getLog(  ).toString(  ), e ) );
 232  
         }
 233  0
         catch ( Exception e )
 234  
         {
 235  
             //            _result.setStatus( ICommandThread.STATUS_EXCEPTION );
 236  
             //            _result.setRunning( false );
 237  0
             StringWriter sw = new StringWriter(  );
 238  0
             PrintWriter pw = new PrintWriter( sw );
 239  0
             e.printStackTrace( pw );
 240  
 
 241  0
             String errorLog = sw.toString(  );
 242  0
             pw.flush(  );
 243  0
             pw.close(  );
 244  
 
 245  
             try
 246  
             {
 247  0
                 sw.flush(  );
 248  0
                 sw.close(  );
 249  
             }
 250  0
             catch ( IOException e1 )
 251  
             {
 252  
                 // do nothing
 253  
                 //                _logger.error( e1 );
 254  0
             }
 255  
 
 256  0
                 ReleaserUtils.addTechnicalError(result,"Une erreur svn est survenue:"+e,e);
 257  
                     
 258  0
         }
 259  
 
 260  0
         return null;
 261  
     }
 262  
 
 263  
     public static ReferenceList getSvnSites( String strUrlSite, SVNClientManager clientManager )
 264  
         throws SVNException
 265  
     {
 266  0
         final ReferenceList listSites = new ReferenceList(  );
 267  
         final SVNURL url;
 268  
 
 269  0
         url = SVNURL.parseURIEncoded( strUrlSite );
 270  
 
 271  0
         SVNRepository repository = SVNRepositoryFactory.create( url, null );
 272  
 
 273  0
         clientManager.getLogClient(  ).doList( repository.getLocation(  ), SVNRevision.HEAD, SVNRevision.HEAD, false,
 274  
             false,
 275  
             new ISVNDirEntryHandler(  )
 276  0
             {
 277  
                 public void handleDirEntry( SVNDirEntry entry )
 278  
                     throws SVNException
 279  
                 {
 280  
                   
 281  0
                     if ( !url.equals( entry.getURL(  ) ) )
 282  
                     {
 283  0
                         if ( entry.getKind(  ) == SVNNodeKind.DIR )
 284  
                         {
 285  0
                             listSites.addItem( entry.getName(  ), entry.getName(  ) );
 286  
                         }
 287  
                     }
 288  0
                 }
 289  
             } );
 290  
 
 291  0
         return listSites;
 292  
     }
 293  
 
 294  
  
 295  
     public static String getSvnUrlTagSite( String strScmUrl, String strTagName )
 296  
     {
 297  
         
 298  0
         String strUrl=strScmUrl.contains( ConstanteUtils.CONSTANTE_TRUNK )?strScmUrl.replace( ConstanteUtils.CONSTANTE_TRUNK, ConstanteUtils.CONSTANTE_TAGS ):strScmUrl;
 299  0
         return strUrl+ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + strTagName;
 300  
     }
 301  
     
 302  
     
 303  
     public static String getRepoUrl(String strRepoUrl)
 304  
     {
 305  
         
 306  0
         if(strRepoUrl!=null && strRepoUrl.startsWith( "scm:svn:" ))
 307  
          {
 308  0
             strRepoUrl=strRepoUrl.substring( 8 );
 309  
                    
 310  
                    
 311  
               }
 312  
         
 313  0
         return strRepoUrl;
 314  
         
 315  
         
 316  
     }
 317  
     
 318  
 
 319  
    
 320  
 }