View Javadoc
1   package org.apache.maven.scm.provider.integrity.command.export;
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 com.mks.api.response.APIException;
23  import org.apache.maven.scm.ScmException;
24  import org.apache.maven.scm.ScmFile;
25  import org.apache.maven.scm.ScmFileSet;
26  import org.apache.maven.scm.ScmFileStatus;
27  import org.apache.maven.scm.ScmVersion;
28  import org.apache.maven.scm.command.export.AbstractExportCommand;
29  import org.apache.maven.scm.command.export.ExportScmResult;
30  import org.apache.maven.scm.provider.ScmProviderRepository;
31  import org.apache.maven.scm.provider.integrity.ExceptionHandler;
32  import org.apache.maven.scm.provider.integrity.Member;
33  import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository;
34  
35  import java.util.ArrayList;
36  import java.util.Iterator;
37  import java.util.List;
38  
39  /**
40   * MKS Integrity implementation for Maven's AbstractExportCommand
41   * <br>Since the IntegrityCheckoutCommand creates a fresh Sandbox in the checkoutDirectory,
42   * it does not make sense for the IntegrityExportCommand to essentially do the same thing.
43   * <br>Hence, this command does not create a Sandbox, instead the entire project contents
44   * are exported to the exportDirectory using the 'si projectco' command.
45   * <br>This gives the user the option of exporting a fresh copy of the repository void of
46   * any project.pj files
47   *
48   * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
49   * @version $Id: IntegrityExportCommand.java 1.3 2011/08/22 13:06:26EDT Cletus D'Souza (dsouza) Exp  $
50   * @since 1.6
51   */
52  public class IntegrityExportCommand
53      extends AbstractExportCommand
54  {
55      /**
56       * {@inheritDoc}
57       */
58      @Override
59      public ExportScmResult executeExportCommand( ScmProviderRepository repository, ScmFileSet fileSet,
60                                                   ScmVersion scmVersion, String outputDirectory )
61          throws ScmException
62      {
63          // First lets figure out where we need to export files to...
64          String exportDir = outputDirectory;
65          exportDir =
66              ( ( null != exportDir && exportDir.length() > 0 ) ? exportDir : fileSet.getBasedir().getAbsolutePath() );
67          // Let the user know where we're going to be exporting the files...
68          getLogger().info( "Attempting to export files to " + exportDir );
69          ExportScmResult result;
70          IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
71          try
72          {
73              // Lets set our overall export success flag
74              boolean exportSuccess = true;
75              // Perform a fresh checkout of each file in the member list...
76              List<Member> projectMembers = iRepo.getProject().listFiles( exportDir );
77              // Initialize the list of files we actually exported...
78              List<ScmFile> scmFileList = new ArrayList<ScmFile>();
79              for ( Iterator<Member> it = projectMembers.iterator(); it.hasNext(); )
80              {
81                  Member siMember = it.next();
82                  try
83                  {
84                      getLogger().info( "Attempting to export file: " + siMember.getTargetFilePath() + " at revision "
85                                            + siMember.getRevision() );
86                      siMember.checkout( iRepo.getAPISession() );
87                      scmFileList.add( new ScmFile( siMember.getTargetFilePath(), ScmFileStatus.UNKNOWN ) );
88                  }
89                  catch ( APIException ae )
90                  {
91                      exportSuccess = false;
92                      ExceptionHandler eh = new ExceptionHandler( ae );
93                      getLogger().error( "MKS API Exception: " + eh.getMessage() );
94                      getLogger().debug( eh.getCommand() + " exited with return code " + eh.getExitCode() );
95                  }
96              }
97              // Lets advice the user that we've checked out all the members
98              getLogger().info(
99                  "Exported " + scmFileList.size() + " files out of a total of " + projectMembers.size() + " files!" );
100             if ( exportSuccess )
101             {
102                 result = new ExportScmResult( "si co", scmFileList );
103             }
104             else
105             {
106                 result = new ExportScmResult( "si co", "Failed to export all files!", "", exportSuccess );
107             }
108         }
109         catch ( APIException aex )
110         {
111             ExceptionHandler eh = new ExceptionHandler( aex );
112             getLogger().error( "MKS API Exception: " + eh.getMessage() );
113             getLogger().debug( eh.getCommand() + " exited with return code " + eh.getExitCode() );
114             result = new ExportScmResult( eh.getCommand(), eh.getMessage(), "Exit Code: " + eh.getExitCode(), false );
115         }
116 
117         return result;
118     }
119 
120 }