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   * @version $Id: IntegrityMkdirCommand.java 1.3 2011/08/22 13:06:34EDT Cletus D'Souza (dsouza) Exp  $
49   * @since 1.6
50   */
51  public class IntegrityMkdirCommand
52      extends AbstractMkdirCommand
53  {
54      /**
55       * Creates a subproject in the Integrity repository.
56       * <br>However, since the subproject automatically creates a folder in
57       * the local sandbox, the createInLocal argument will be ignored
58       */
59      @Override
60      public MkdirScmResult executeMkdirCommand( ScmProviderRepository repository, ScmFileSet fileSet, String message,
61                                                 boolean createInLocal )
62          throws ScmException
63      {
64          String dirPath = "";
65          Iterator<File> fit = fileSet.getFileList().iterator();
66          if ( fit.hasNext() )
67          {
68              dirPath = fit.next().getPath().replace( '\\', '/' );
69          }
70          if ( null == dirPath || dirPath.length() == 0 )
71          {
72              throw new ScmException( "A relative directory path is required to execute this command!" );
73          }
74          getLogger().info( "Creating subprojects one per directory, as required for " + dirPath );
75          MkdirScmResult result;
76          IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
77          try
78          {
79              Response res = iRepo.getSandbox().createSubproject( dirPath );
80              String subProject = res.getWorkItems().next().getResult().getField( "resultant" ).getItem().getDisplayId();
81              List<ScmFile> createdDirs = new ArrayList<ScmFile>();
82              createdDirs.add( new ScmFile( subProject, ScmFileStatus.ADDED ) );
83              int exitCode = res.getExitCode();
84              boolean success = ( exitCode == 0 ? true : false );
85              getLogger().info( "Successfully created subproject " + subProject );
86              result = new MkdirScmResult( createdDirs,
87                                           new ScmResult( res.getCommandString(), "", "Exit Code: " + exitCode,
88                                                          success ) );
89          }
90          catch ( APIException aex )
91          {
92              ExceptionHandler eh = new ExceptionHandler( aex );
93              getLogger().error( "MKS API Exception: " + eh.getMessage() );
94              getLogger().info( eh.getCommand() + " exited with return code " + eh.getExitCode() );
95              result = new MkdirScmResult( eh.getCommand(), eh.getMessage(), "Exit Code: " + eh.getExitCode(), false );
96          }
97  
98          return result;
99      }
100 
101 }