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   * @since 1.6
50   */
51  public class IntegrityExportCommand
52      extends AbstractExportCommand
53  {
54      /**
55       * {@inheritDoc}
56       */
57      @Override
58      public ExportScmResult executeExportCommand( ScmProviderRepository repository, ScmFileSet fileSet,
59                                                   ScmVersion scmVersion, String outputDirectory )
60          throws ScmException
61      {
62          // First lets figure out where we need to export files to...
63          String exportDir = outputDirectory;
64          exportDir =
65              ( ( null != exportDir && exportDir.length() > 0 ) ? exportDir : fileSet.getBasedir().getAbsolutePath() );
66          // Let the user know where we're going to be exporting the files...
67          getLogger().info( "Attempting to export files to " + exportDir );
68          ExportScmResult result;
69          IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
70          try
71          {
72              // Lets set our overall export success flag
73              boolean exportSuccess = true;
74              // Perform a fresh checkout of each file in the member list...
75              List<Member> projectMembers = iRepo.getProject().listFiles( exportDir );
76              // Initialize the list of files we actually exported...
77              List<ScmFile> scmFileList = new ArrayList<ScmFile>();
78              for ( Iterator<Member> it = projectMembers.iterator(); it.hasNext(); )
79              {
80                  Member siMember = it.next();
81                  try
82                  {
83                      getLogger().info( "Attempting to export file: " + siMember.getTargetFilePath() + " at revision "
84                                            + siMember.getRevision() );
85                      siMember.checkout( iRepo.getAPISession() );
86                      scmFileList.add( new ScmFile( siMember.getTargetFilePath(), ScmFileStatus.UNKNOWN ) );
87                  }
88                  catch ( APIException ae )
89                  {
90                      exportSuccess = false;
91                      ExceptionHandler eh = new ExceptionHandler( ae );
92                      getLogger().error( "MKS API Exception: " + eh.getMessage() );
93                      getLogger().debug( eh.getCommand() + " exited with return code " + eh.getExitCode() );
94                  }
95              }
96              // Lets advice the user that we've checked out all the members
97              getLogger().info(
98                  "Exported " + scmFileList.size() + " files out of a total of " + projectMembers.size() + " files!" );
99              if ( exportSuccess )
100             {
101                 result = new ExportScmResult( "si co", scmFileList );
102             }
103             else
104             {
105                 result = new ExportScmResult( "si co", "Failed to export all files!", "", exportSuccess );
106             }
107         }
108         catch ( APIException aex )
109         {
110             ExceptionHandler eh = new ExceptionHandler( aex );
111             getLogger().error( "MKS API Exception: " + eh.getMessage() );
112             getLogger().debug( eh.getCommand() + " exited with return code " + eh.getExitCode() );
113             result = new ExportScmResult( eh.getCommand(), eh.getMessage(), "Exit Code: " + eh.getExitCode(), false );
114         }
115 
116         return result;
117     }
118 
119 }