View Javadoc
1   package org.apache.maven.scm.provider.integrity.command.mkdir;
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 org.apache.maven.scm.ScmException;
25  import org.apache.maven.scm.ScmFile;
26  import org.apache.maven.scm.ScmFileSet;
27  import org.apache.maven.scm.ScmFileStatus;
28  import org.apache.maven.scm.ScmResult;
29  import org.apache.maven.scm.command.mkdir.AbstractMkdirCommand;
30  import org.apache.maven.scm.command.mkdir.MkdirScmResult;
31  import org.apache.maven.scm.provider.ScmProviderRepository;
32  import org.apache.maven.scm.provider.integrity.ExceptionHandler;
33  import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository;
34  
35  import java.io.File;
36  import java.util.ArrayList;
37  import java.util.Iterator;
38  import java.util.List;
39  
40  /**
41   * MKS Integrity implementation of Maven's AbstractMkdirCommand
42   * <br>This command will execute an 'si createsubproject' for the relative path
43   * represented in the fileSet.getFileList().iterator().next() entry.
44   * <br>A single subproject is created required for every directory encountered
45   * in the relative path.
46   *
47   * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
48   * @since 1.6
49   */
50  public class IntegrityMkdirCommand
51      extends AbstractMkdirCommand
52  {
53      /**
54       * Creates a subproject in the Integrity repository.
55       * <br>However, since the subproject automatically creates a folder in
56       * the local sandbox, the createInLocal argument will be ignored
57       */
58      @Override
59      public MkdirScmResult executeMkdirCommand( ScmProviderRepository repository, ScmFileSet fileSet, String message,
60                                                 boolean createInLocal )
61          throws ScmException
62      {
63          String dirPath = "";
64          Iterator<File> fit = fileSet.getFileList().iterator();
65          if ( fit.hasNext() )
66          {
67              dirPath = fit.next().getPath().replace( '\\', '/' );
68          }
69          if ( null == dirPath || dirPath.length() == 0 )
70          {
71              throw new ScmException( "A relative directory path is required to execute this command!" );
72          }
73          getLogger().info( "Creating subprojects one per directory, as required for " + dirPath );
74          MkdirScmResult result;
75          IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
76          try
77          {
78              Response res = iRepo.getSandbox().createSubproject( dirPath );
79              String subProject = res.getWorkItems().next().getResult().getField( "resultant" ).getItem().getDisplayId();
80              List<ScmFile> createdDirs = new ArrayList<ScmFile>();
81              createdDirs.add( new ScmFile( subProject, ScmFileStatus.ADDED ) );
82              int exitCode = res.getExitCode();
83              boolean success = ( exitCode == 0 ? true : false );
84              getLogger().info( "Successfully created subproject " + subProject );
85              result = new MkdirScmResult( createdDirs,
86                                           new ScmResult( res.getCommandString(), "", "Exit Code: " + exitCode,
87                                                          success ) );
88          }
89          catch ( APIException aex )
90          {
91              ExceptionHandler eh = new ExceptionHandler( aex );
92              getLogger().error( "MKS API Exception: " + eh.getMessage() );
93              getLogger().info( eh.getCommand() + " exited with return code " + eh.getExitCode() );
94              result = new MkdirScmResult( eh.getCommand(), eh.getMessage(), "Exit Code: " + eh.getExitCode(), false );
95          }
96  
97          return result;
98      }
99  
100 }