001package org.apache.maven.scm.provider.integrity.command.checkout; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import com.mks.api.response.APIException; 023import com.mks.api.response.Response; 024import com.mks.api.response.Result; 025import com.mks.api.response.WorkItem; 026import com.mks.api.response.WorkItemIterator; 027import com.mks.api.si.SIModelTypeName; 028import org.apache.maven.scm.ScmException; 029import org.apache.maven.scm.ScmFileSet; 030import org.apache.maven.scm.ScmVersion; 031import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand; 032import org.apache.maven.scm.command.checkout.CheckOutScmResult; 033import org.apache.maven.scm.provider.ScmProviderRepository; 034import org.apache.maven.scm.provider.integrity.ExceptionHandler; 035import org.apache.maven.scm.provider.integrity.Sandbox; 036import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository; 037 038/** 039 * MKS Integrity implementation for Maven's AbstractCheckOutCommand 040 * <br>The Checkout command will create a fresh sandbox in the checkoutDirectory 041 * <br>Since, Maven deletes the prior checkout folder, this command will check 042 * for a prior sandbox in the checkout directory and if a sandbox was found, 043 * then the command will resynchronize the sandbox. 044 * 045 * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a> 046 * @since 1.6 047 */ 048public class IntegrityCheckOutCommand 049 extends AbstractCheckOutCommand 050{ 051 /** 052 * Overridden function that performs a checkout (resynchronize) operation against an MKS Source Project 053 * This function ignores the scmVerion and recursive arguments passed into this function as while there is 054 * a suitable equivalent to checkout/resynchronize by label/revision, it doesn't make sense for the way 055 * Maven seems to want to execute this command. Hence we will create/resynchronize a sandbox, which will 056 * be recursive in nature. If the user chooses to checkout a specific versioned configuration (checkpoint), 057 * then that information will be contained in the Configuration Path obtained from the 058 * IntegrityScmProviderRepository 059 */ 060 @Override 061 public CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repository, ScmFileSet fileSet, 062 ScmVersion scmVersion, boolean recursive, boolean shallow ) 063 throws ScmException 064 { 065 CheckOutScmResult result; 066 IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository; 067 try 068 { 069 getLogger().info( 070 "Attempting to checkout source for project " + iRepo.getProject().getConfigurationPath() ); 071 String checkoutDir = System.getProperty( "checkoutDirectory" ); 072 // Override the sandbox definition, if a checkout directory is specified for this command 073 Sandbox siSandbox; 074 if ( null != checkoutDir && checkoutDir.length() > 0 ) 075 { 076 siSandbox = new Sandbox( iRepo.getAPISession(), iRepo.getProject(), checkoutDir ); 077 iRepo.setSandbox( siSandbox ); 078 } 079 else 080 { 081 siSandbox = iRepo.getSandbox(); 082 } 083 getLogger().info( "Sandbox location is " + siSandbox.getSandboxDir() ); 084 // Now attempt to create the sandbox, if it doesn't already exist 085 if ( siSandbox.create() ) 086 { 087 // Resynchronize the new or previously created sandbox 088 Response res = siSandbox.resync(); 089 // Lets output what we got from running this command 090 WorkItemIterator wit = res.getWorkItems(); 091 while ( wit.hasNext() ) 092 { 093 WorkItem wi = wit.next(); 094 if ( wi.getModelType().equals( SIModelTypeName.MEMBER ) ) 095 { 096 Result message = wi.getResult(); 097 getLogger().debug( wi.getDisplayId() + " " + ( null != message ? message.getMessage() : "" ) ); 098 } 099 } 100 int exitCode = res.getExitCode(); 101 boolean success = ( exitCode == 0 ? true : false ); 102 result = new CheckOutScmResult( res.getCommandString(), "", "Exit Code: " + exitCode, success ); 103 } 104 else 105 { 106 result = new CheckOutScmResult( "si createsandbox", "Failed to create sandbox!", "", false ); 107 } 108 } 109 catch ( APIException aex ) 110 { 111 ExceptionHandler eh = new ExceptionHandler( aex ); 112 getLogger().error( "MKS API Exception: " + eh.getMessage() ); 113 getLogger().info( eh.getCommand() + " exited with return code " + eh.getExitCode() ); 114 result = new CheckOutScmResult( eh.getCommand(), eh.getMessage(), "Exit Code: " + eh.getExitCode(), false ); 115 } 116 117 return result; 118 } 119 120}