Coverage Report - fr.paris.lutece.plugins.releaser.service.SvnService
 
Classes in this File Line Coverage Branch Coverage Complexity
SvnService
2 %
2/87
0 %
0/14
2,5
SvnService$1
0 %
0/5
0 %
0/2
2,5
 
 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.service;
 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.commons.lang.StringUtils;
 43  
 import org.tmatesoft.svn.core.SVNCancelException;
 44  
 import org.tmatesoft.svn.core.SVNException;
 45  
 import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
 46  
 import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
 47  
 import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
 48  
 import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
 49  
 import org.tmatesoft.svn.core.wc.ISVNEventHandler;
 50  
 import org.tmatesoft.svn.core.wc.SVNCommitPacket;
 51  
 import org.tmatesoft.svn.core.wc.SVNEvent;
 52  
 import org.tmatesoft.svn.core.wc.SVNEventAction;
 53  
 import org.tmatesoft.svn.core.wc.SVNWCUtil;
 54  
 
 55  
 import fr.paris.lutece.plugins.releaser.business.Component;
 56  
 import fr.paris.lutece.plugins.releaser.business.Site;
 57  
 import fr.paris.lutece.plugins.releaser.util.CommandResult;
 58  
 import fr.paris.lutece.plugins.releaser.util.ConstanteUtils;
 59  
 import fr.paris.lutece.plugins.releaser.util.ReleaserUtils;
 60  
 import fr.paris.lutece.plugins.releaser.util.pom.PomUpdater;
 61  
 import fr.paris.lutece.plugins.releaser.util.svn.ReleaseSvnCheckoutClient;
 62  
 import fr.paris.lutece.plugins.releaser.util.svn.ReleaseSvnCommitClient;
 63  
 import fr.paris.lutece.plugins.releaser.util.svn.ReleaseSvnCopyClient;
 64  
 import fr.paris.lutece.plugins.releaser.util.svn.SvnUtils;
 65  
 import fr.paris.lutece.plugins.releaser.util.svn.SvnUser;
 66  
 import fr.paris.lutece.portal.service.spring.SpringContextService;
 67  
 import fr.paris.lutece.portal.service.util.AppLogService;
 68  
 
 69  
 
 70  
 public class SvnService implements ISvnService
 71  
 {
 72  
     
 73  
     private static ISvnService _instance;
 74  
     // private static ISvnService _singleton;
 75  
   
 76  
     //    public static ISvnService getInstance(  )
 77  
     //    {
 78  
     //        if ( _singleton == null )
 79  
     //        {
 80  
     //            _singleton = new SvnService(  );
 81  
     //        }
 82  
     //
 83  
     //        return _singleton;
 84  
     //    }
 85  
     private SvnService(  )
 86  1
     {
 87  
   
 88  1
     }
 89  
     
 90  
     
 91  
     public static ISvnService getService()
 92  
     {
 93  0
         if(_instance==null)
 94  
         {
 95  0
             _instance=SpringContextService.getBean(ConstanteUtils.BEAN_SVN_SERVICE );
 96  0
             _instance.init( );
 97  
         }
 98  
             
 99  0
       return _instance;
 100  
         
 101  
     }
 102  
 
 103  
     /* (non-Javadoc)
 104  
          * @see fr.paris.lutece.plugins.deployment.service.ISvnService#init()
 105  
          */
 106  
     public void init(  )
 107  
     {
 108  
         /*
 109  
              * For using over http:// and https:/
 110  
              */
 111  0
         DAVRepositoryFactory.setup(  );
 112  
         /*
 113  
          * For using over svn:// and svn+xxx:/
 114  
          */
 115  0
         SVNRepositoryFactoryImpl.setup(  );
 116  
 
 117  
         /*
 118  
          * For using over file://
 119  
          */
 120  0
         FSRepositoryFactory.setup(  );
 121  0
     }
 122  
 
 123  
   
 124  
 
 125  
     public String doSvnCheckoutSite(  Site site, String strSvnLogin,String strSvnPassword, CommandResult commandResult)
 126  
     {
 127  0
         ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager( strSvnLogin,
 128  
                 strSvnPassword );
 129  
        
 130  0
         String strSiteLocalBasePath = ReleaserUtils.getLocalSitePath( site.getArtifactId( ) );
 131  
 
 132  0
         if ( StringUtils.isNotBlank( strSiteLocalBasePath ) && ( strSvnLogin != null ) )
 133  
         {
 134  0
             ReleaseSvnCheckoutClient updateClient = new ReleaseSvnCheckoutClient( authManager,
 135  0
                     SVNWCUtil.createDefaultOptions( false ) );
 136  
 
 137  0
             String strError = null;
 138  
 
 139  
             try
 140  
             {
 141  0
                 strError = SvnUtils.doSvnCheckout(  site.getScmUrl( ), strSiteLocalBasePath, updateClient,
 142  
                         commandResult );
 143  
             }
 144  0
             catch ( Exception e )
 145  
             {
 146  0
                     ReleaserUtils.addTechnicalError(commandResult,"errreur lors du checkout du site "+ e.getMessage(),e);
 147  0
             }
 148  
         }
 149  
         
 150  
 
 151  0
         return ConstanteUtils.CONSTANTE_EMPTY_STRING;
 152  
     }
 153  
     
 154  
     public String doSvnCheckoutComponent(  Component component, String strSvnLogin,String strSvnPassword, CommandResult commandResult)
 155  
     {
 156  0
         ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager( strSvnLogin,
 157  
                 strSvnPassword );
 158  
        
 159  0
         String strLocalBasePath = ReleaserUtils.getLocalComponentPath(component.getArtifactId( ) );
 160  
 
 161  0
         if ( StringUtils.isNotBlank( strLocalBasePath ) && ( strSvnLogin != null ) )
 162  
         {
 163  0
             ReleaseSvnCheckoutClient updateClient = new ReleaseSvnCheckoutClient( authManager,
 164  0
                     SVNWCUtil.createDefaultOptions( false ) );
 165  
 
 166  0
             String strError = null;
 167  
 
 168  
             try
 169  
             {
 170  0
                 strError = SvnUtils.doSvnCheckout( SvnUtils.getRepoUrl( component.getScmDeveloperConnection( )), strLocalBasePath, updateClient,
 171  
                         commandResult );
 172  
             }
 173  0
             catch ( Exception e )
 174  
             {
 175  0
                 ReleaserUtils.addTechnicalError(commandResult,"errreur lors du checkout du composant"+ e.getMessage(),e);
 176  0
             }
 177  
         }
 178  
         
 179  
 
 180  0
         return ConstanteUtils.CONSTANTE_EMPTY_STRING;
 181  
     }
 182  
 
 183  
     /* (non-Javadoc)
 184  
          * @see fr.paris.lutece.plugins.deployment.service.ISvnService#doSvnTagSite(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, fr.paris.lutece.plugins.deployment.business.User)
 185  
          */
 186  
     public String doReleaseSite( Site site, String strSvnLogin,String strSvnPassword, CommandResult commandResult )
 187  
     {
 188  
          
 189  0
         String strSitePomLocalBasePath = ReleaserUtils.getLocalSitePomPath( site.getArtifactId( ) );
 190  
         
 191  0
         ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(strSvnLogin,
 192  
                 strSvnPassword);
 193  
 
 194  0
         String strSrcURL = site.getScmUrl( );
 195  0
         String strDstURL = SvnUtils.getSvnUrlTagSite( site.getScmUrl( ), ReleaserUtils.getSiteTagName( site ) );
 196  
 
 197  0
         ReleaseSvnCommitClient commitClient = new ReleaseSvnCommitClient( authManager,
 198  0
                 SVNWCUtil.createDefaultOptions( false ) );
 199  0
         ReleaseSvnCopyClient copyClient = new ReleaseSvnCopyClient( authManager, SVNWCUtil.createDefaultOptions( false ) );
 200  
 
 201  
         try
 202  
         {
 203  0
             final StringBuffer sbLog = commandResult.getLog(  );
 204  
             
 205  
 
 206  0
             copyClient.setEventHandler( new ISVNEventHandler(  )
 207  0
                 {
 208  
                     public void checkCancelled(  ) throws SVNCancelException
 209  
                     {
 210  
                         // Do nothing
 211  0
                     }
 212  
 
 213  
                     public void handleEvent( SVNEvent event, double progress )
 214  
                         throws SVNException
 215  
                     {
 216  0
                         sbLog.append( ( ( event.getAction(  ) == SVNEventAction.COPY ) ? "Tag " : event.getAction(  ) ) +
 217  0
                             " " + ObjectUtils.toString( event.getFile(  ) ) + "\n" );
 218  0
                     }
 219  
                 } );
 220  
 
 221  0
             sbLog.append( "Preparing release site\n" );
 222  0
             sbLog.append( "Updating pom version to " + site.getNextReleaseVersion( ) + "...\n" );
 223  0
             sbLog.append( "Updating dependency version ...\n" );
 224  0
             PomUpdater.updateSiteBeforeTag( site );
 225  
             
 226  0
             SvnUtils.doCommit( strSitePomLocalBasePath, "[site-release] update pom before tag", commitClient );
 227  
             // PROGRESS 60%
 228  0
             commandResult.setProgressValue( commandResult.getProgressValue( ) + 30 );
 229  
 
 230  
             
 231  
            // sbLog.append( ReleaserUtils.updateReleaseVersion( strSiteLocalBasePath, strVersion,
 232  
             //        "[site-release] Prepare tag for " + strSiteName, commitClient ) );
 233  0
             sbLog.append( "Pom updated\n" );
 234  
             
 235  0
             if(!site.isTheme( ) )
 236  
             {
 237  
 
 238  0
                 sbLog.append( "Tagging site to " + site.getNextReleaseVersion( ) + "...\n" );
 239  
             
 240  0
                 String strErrorDuringTag=SvnUtils.doTagSite( site.getArtifactId( ), site.getArtifactId( )+"-"+site.getNextReleaseVersion( ), strSrcURL, strDstURL, copyClient );
 241  
            
 242  
                 // PROGRESS 80%
 243  0
                 commandResult.setProgressValue( commandResult.getProgressValue( ) + 20 );
 244  
 
 245  
             
 246  0
                 if(StringUtils.isEmpty(strErrorDuringTag))
 247  
                 {
 248  0
                         sbLog.append( "Tag done\n" );
 249  
             
 250  0
                         sbLog.append( "Updating pom to next development " + site.getNextSnapshotVersion( ) + "\n" );
 251  
                         
 252  0
                         PomUpdater.updateSiteAfterTag( site );
 253  
                         
 254  0
                         SvnUtils.doCommit( strSitePomLocalBasePath, "[site-release] update Updating pom to next development ", commitClient );
 255  
     
 256  0
                         sbLog.append( "Pom updated\n" );
 257  
                 }
 258  
                 else
 259  
                 {
 260  0
                     ReleaserUtils.addTechnicalError(commandResult, strErrorDuringTag);
 261  
                 }
 262  0
             }
 263  
             else
 264  
             {
 265  0
                 sbLog.append( "Release prepare theme " + site.getNextReleaseVersion( ) + "...\n" );
 266  
                 
 267  0
                 MavenService.getService( ).mvnReleasePrepare( strSitePomLocalBasePath, site.getNextReleaseVersion( ),  site.getArtifactId( )+"-"+site.getNextReleaseVersion( ),
 268  0
                         site.getNextSnapshotVersion( ), strSvnLogin, strSvnPassword, commandResult );
 269  0
                 sbLog.append( "End Release prepare theme " + site.getNextReleaseVersion( ) + "...\n" );
 270  0
                 sbLog.append( "Release perform theme " + site.getNextReleaseVersion( ) + "...\n" );
 271  0
                 MavenService.getService( ).mvnReleasePerform( strSitePomLocalBasePath, strSvnLogin, strSvnPassword, commandResult );
 272  0
                 sbLog.append( "End Release perform theme " + site.getNextReleaseVersion( ) + "...\n" );
 273  0
                 sbLog.append( "Updating pom to next development " + site.getNextSnapshotVersion( ) + "\n" );
 274  
                 
 275  0
                 PomUpdater.updateSiteAfterTag( site );
 276  
                 
 277  0
                 SvnUtils.doCommit( strSitePomLocalBasePath, "[site-release] update Updating pom to next development ", commitClient );
 278  
 
 279  0
                 sbLog.append( "Pom updated\n" );
 280  
             
 281  
             }
 282  
             
 283  
         }
 284  0
         catch ( Exception e )
 285  
         {
 286  
             //commandResult.setStatus( ICommandThread.STATUS_EXCEPTION );
 287  0
             StringWriter sw = new StringWriter(  );
 288  0
             PrintWriter pw = new PrintWriter( sw );
 289  0
             e.printStackTrace( pw );
 290  
 
 291  0
             String errorLog = sw.toString(  );
 292  0
             pw.flush(  );
 293  0
             pw.close(  );
 294  
 
 295  
             try
 296  
             {
 297  0
                 sw.flush(  );
 298  0
                 sw.close(  );
 299  
             }
 300  0
             catch ( IOException e1 )
 301  
             {
 302  
                 // do nothing
 303  0
                     AppLogService.error(e1);
 304  0
             }
 305  
 
 306  0
             commandResult.getLog(  ).append( errorLog );
 307  
 
 308  
             /**
 309  
             _result.setIdError( ReleaseLogger.logError(
 310  
                                                         _result.getLog(  ).toString(  ),
 311  
                                                         e ) );
 312  
                                                         **/
 313  0
             ReleaserUtils.addTechnicalError(commandResult, errorLog,e);
 314  0
         }
 315  
 
 316  0
         return ConstanteUtils.CONSTANTE_EMPTY_STRING;
 317  
     }
 318  
 
 319  
         
 320  
 }