View Javadoc
1   package org.apache.maven.scm.provider.integrity.command.checkout;
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 com.mks.api.response.Response;
24  import com.mks.api.response.Result;
25  import com.mks.api.response.WorkItem;
26  import com.mks.api.response.WorkItemIterator;
27  import com.mks.api.si.SIModelTypeName;
28  import org.apache.maven.scm.ScmException;
29  import org.apache.maven.scm.ScmFileSet;
30  import org.apache.maven.scm.ScmVersion;
31  import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand;
32  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
33  import org.apache.maven.scm.provider.ScmProviderRepository;
34  import org.apache.maven.scm.provider.integrity.ExceptionHandler;
35  import org.apache.maven.scm.provider.integrity.Sandbox;
36  import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository;
37  
38  /**
39   * MKS Integrity implementation for Maven's AbstractCheckOutCommand
40   * <br>The Checkout command will create a fresh sandbox in the checkoutDirectory
41   * <br>Since, Maven deletes the prior checkout folder, this command will check
42   * for a prior sandbox in the checkout directory and if a sandbox was found,
43   * then the command will resynchronize the sandbox.
44   *
45   * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
46   * @version $Id: IntegrityCheckOutCommand.java 1.3 2011/08/22 13:06:21EDT Cletus D'Souza (dsouza) Exp  $
47   * @since 1.6
48   */
49  public class IntegrityCheckOutCommand
50      extends AbstractCheckOutCommand
51  {
52      /**
53       * Overridden function that performs a checkout (resynchronize) operation against an MKS Source Project
54       * This function ignores the scmVerion and recursive arguments passed into this function as while there is
55       * a suitable equivalent to checkout/resynchronize by label/revision, it doesn't make sense for the way
56       * Maven seems to want to execute this command.  Hence we will create/resynchronize a sandbox, which will
57       * be recursive in nature.  If the user chooses to checkout a specific versioned configuration (checkpoint),
58       * then that information will be contained in the Configuration Path obtained from the
59       * IntegrityScmProviderRepository
60       */
61      @Override
62      public CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repository, ScmFileSet fileSet,
63                                                       ScmVersion scmVersion, boolean recursive )
64          throws ScmException
65      {
66          CheckOutScmResult result;
67          IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
68          try
69          {
70              getLogger().info(
71                  "Attempting to checkout source for project " + iRepo.getProject().getConfigurationPath() );
72              String checkoutDir = System.getProperty( "checkoutDirectory" );
73              // Override the sandbox definition, if a checkout directory is specified for this command
74              Sandbox siSandbox;
75              if ( null != checkoutDir && checkoutDir.length() > 0 )
76              {
77                  siSandbox = new Sandbox( iRepo.getAPISession(), iRepo.getProject(), checkoutDir );
78                  iRepo.setSandbox( siSandbox );
79              }
80              else
81              {
82                  siSandbox = iRepo.getSandbox();
83              }
84              getLogger().info( "Sandbox location is " + siSandbox.getSandboxDir() );
85              // Now attempt to create the sandbox, if it doesn't already exist
86              if ( siSandbox.create() )
87              {
88                  // Resynchronize the new or previously created sandbox
89                  Response res = siSandbox.resync();
90                  // Lets output what we got from running this command
91                  WorkItemIterator wit = res.getWorkItems();
92                  while ( wit.hasNext() )
93                  {
94                      WorkItem wi = wit.next();
95                      if ( wi.getModelType().equals( SIModelTypeName.MEMBER ) )
96                      {
97                          Result message = wi.getResult();
98                          getLogger().debug( wi.getDisplayId() + " " + ( null != message ? message.getMessage() : "" ) );
99                      }
100                 }
101                 int exitCode = res.getExitCode();
102                 boolean success = ( exitCode == 0 ? true : false );
103                 result = new CheckOutScmResult( res.getCommandString(), "", "Exit Code: " + exitCode, success );
104             }
105             else
106             {
107                 result = new CheckOutScmResult( "si createsandbox", "Failed to create sandbox!", "", false );
108             }
109         }
110         catch ( APIException aex )
111         {
112             ExceptionHandler eh = new ExceptionHandler( aex );
113             getLogger().error( "MKS API Exception: " + eh.getMessage() );
114             getLogger().info( eh.getCommand() + " exited with return code " + eh.getExitCode() );
115             result = new CheckOutScmResult( eh.getCommand(), eh.getMessage(), "Exit Code: " + eh.getExitCode(), false );
116         }
117 
118         return result;
119     }
120 
121 }