Coverage Report - org.apache.maven.scm.provider.synergy.command.update.SynergyUpdateCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
SynergyUpdateCommand
0 %
0/46
0 %
0/22
6,333
 
 1  
 package org.apache.maven.scm.provider.synergy.command.update;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  * http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.apache.maven.scm.ScmException;
 23  
 import org.apache.maven.scm.ScmFile;
 24  
 import org.apache.maven.scm.ScmFileSet;
 25  
 import org.apache.maven.scm.ScmFileStatus;
 26  
 import org.apache.maven.scm.ScmVersion;
 27  
 import org.apache.maven.scm.command.changelog.ChangeLogCommand;
 28  
 import org.apache.maven.scm.command.update.AbstractUpdateCommand;
 29  
 import org.apache.maven.scm.command.update.UpdateScmResult;
 30  
 import org.apache.maven.scm.provider.ScmProviderRepository;
 31  
 import org.apache.maven.scm.provider.synergy.command.SynergyCommand;
 32  
 import org.apache.maven.scm.provider.synergy.command.changelog.SynergyChangeLogCommand;
 33  
 import org.apache.maven.scm.provider.synergy.repository.SynergyScmProviderRepository;
 34  
 import org.apache.maven.scm.provider.synergy.util.SynergyUtil;
 35  
 import org.codehaus.plexus.util.FileUtils;
 36  
 
 37  
 import java.io.File;
 38  
 import java.io.IOException;
 39  
 import java.util.ArrayList;
 40  
 import java.util.List;
 41  
 
 42  
 /**
 43  
  * @author <a href="mailto:julien.henry@capgemini.com">Julien Henry</a>
 44  
  * @version $Id: SynergyUpdateCommand.java 1056980 2011-01-09 17:23:55Z olamy $
 45  
  */
 46  0
 public class SynergyUpdateCommand
 47  
     extends AbstractUpdateCommand
 48  
     implements SynergyCommand
 49  
 {
 50  
     /** {@inheritDoc} */
 51  
     protected UpdateScmResult executeUpdateCommand( ScmProviderRepository repository, ScmFileSet fileSet,
 52  
                                                     ScmVersion version )
 53  
         throws ScmException
 54  
     {
 55  0
         if ( getLogger().isDebugEnabled() )
 56  
         {
 57  0
             getLogger().debug( "executing update command..." );
 58  
         }
 59  0
         SynergyScmProviderRepository repo = (SynergyScmProviderRepository) repository;
 60  
 
 61  0
         if ( getLogger().isDebugEnabled() )
 62  
         {
 63  0
             getLogger().debug( "basedir: " + fileSet.getBasedir() );
 64  
         }
 65  
 
 66  0
         String ccmAddr = SynergyUtil.start( getLogger(), repo.getUser(), repo.getPassword(), null );
 67  
 
 68  
         File waPath;
 69  
         try
 70  
         {
 71  0
             String projectSpec =
 72  
                 SynergyUtil.getWorkingProject( getLogger(), repo.getProjectSpec(), repo.getUser(), ccmAddr );
 73  0
             SynergyUtil.reconfigureProperties( getLogger(), projectSpec, ccmAddr );
 74  0
             SynergyUtil.reconfigure( getLogger(), projectSpec, ccmAddr );
 75  
             // We need to get WA path
 76  0
             waPath = SynergyUtil.getWorkArea( getLogger(), projectSpec, ccmAddr );
 77  
         }
 78  
         finally
 79  
         {
 80  0
             SynergyUtil.stop( getLogger(), ccmAddr );
 81  0
         }
 82  
 
 83  0
         File source = new File( waPath, repo.getProjectName() );
 84  
 
 85  
         // Move file from work area to expected dir if not the same
 86  0
         List<ScmFile> modifications = new ArrayList<ScmFile>();
 87  0
         if ( !source.equals( fileSet.getBasedir() ) )
 88  
         {
 89  0
             if ( getLogger().isInfoEnabled() )
 90  
             {
 91  0
                 getLogger().info( "We will copy modified files from Synergy Work Area [" + source
 92  
                                   + "] to expected folder [" + fileSet.getBasedir() + "]" );
 93  
             }
 94  
             try
 95  
             {
 96  0
                 copyDirectoryStructure( source, fileSet.getBasedir(), modifications );
 97  
             }
 98  0
             catch ( IOException e1 )
 99  
             {
 100  0
                 throw new ScmException( "Unable to copy directory structure", e1 );
 101  0
             }
 102  
         }
 103  
 
 104  0
         return new UpdateScmResult( "ccm reconcile -uwa ...", modifications );
 105  
     }
 106  
 
 107  
     protected ChangeLogCommand getChangeLogCommand()
 108  
     {
 109  0
         SynergyChangeLogCommand changeLogCmd = new SynergyChangeLogCommand();
 110  
 
 111  0
         changeLogCmd.setLogger( getLogger() );
 112  
 
 113  0
         return changeLogCmd;
 114  
     }
 115  
 
 116  
     /**
 117  
      * Copies a entire directory structure and collect modifications.
 118  
      * <p/>
 119  
      * Note:
 120  
      * <ul>
 121  
      * <li>It will include empty directories.
 122  
      * <li>The <code>sourceDirectory</code> must exists.
 123  
      * </ul>
 124  
      *
 125  
      * @param sourceDirectory
 126  
      * @param destinationDirectory
 127  
      * @throws IOException
 128  
      */
 129  
     public static void copyDirectoryStructure( File sourceDirectory, File destinationDirectory, List<ScmFile> modifications )
 130  
         throws IOException
 131  
     {
 132  0
         if ( !sourceDirectory.exists() )
 133  
         {
 134  0
             throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." );
 135  
         }
 136  
 
 137  0
         File[] files = sourceDirectory.listFiles();
 138  
 
 139  0
         String sourcePath = sourceDirectory.getAbsolutePath();
 140  
 
 141  0
         for ( int i = 0; i < files.length; i++ )
 142  
         {
 143  0
             File file = files[i];
 144  
 
 145  0
             String dest = file.getAbsolutePath();
 146  
 
 147  0
             dest = dest.substring( sourcePath.length() + 1 );
 148  
 
 149  0
             File destination = new File( destinationDirectory, dest );
 150  
 
 151  0
             if ( file.isFile() )
 152  
             {
 153  
 
 154  0
                 if ( file.lastModified() != destination.lastModified() )
 155  
                 {
 156  
 
 157  0
                     destination = destination.getParentFile();
 158  
 
 159  0
                     FileUtils.copyFileToDirectory( file, destination );
 160  
 
 161  0
                     modifications.add( new ScmFile( file.getAbsolutePath(), ScmFileStatus.UPDATED ) );
 162  
 
 163  
                 }
 164  
             }
 165  0
             else if ( file.isDirectory() )
 166  
             {
 167  0
                 if ( !destination.exists() && !destination.mkdirs() )
 168  
                 {
 169  0
                     throw new IOException(
 170  
                         "Could not create destination directory '" + destination.getAbsolutePath() + "'." );
 171  
                 }
 172  
 
 173  0
                 copyDirectoryStructure( file, destination, modifications );
 174  
             }
 175  
             else
 176  
             {
 177  0
                 throw new IOException( "Unknown file type: " + file.getAbsolutePath() );
 178  
             }
 179  
         }
 180  0
     }
 181  
 
 182  
 }