View Javadoc
1   package org.apache.maven.scm.provider.integrity.command.status;
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.Item;
24  import com.mks.api.response.WorkItem;
25  import org.apache.maven.scm.ScmException;
26  import org.apache.maven.scm.ScmFile;
27  import org.apache.maven.scm.ScmFileSet;
28  import org.apache.maven.scm.ScmFileStatus;
29  import org.apache.maven.scm.ScmResult;
30  import org.apache.maven.scm.command.status.AbstractStatusCommand;
31  import org.apache.maven.scm.command.status.StatusScmResult;
32  import org.apache.maven.scm.provider.ScmProviderRepository;
33  import org.apache.maven.scm.provider.integrity.ExceptionHandler;
34  import org.apache.maven.scm.provider.integrity.Sandbox;
35  import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository;
36  
37  import java.io.File;
38  import java.util.ArrayList;
39  import java.util.Iterator;
40  import java.util.List;
41  
42  /**
43   * MKS Integrity implementation for Maven's AbstractStatusCommand
44   * <br>This command will execute a 'si viewsandbox' and report on all
45   * changed and dropped working files.  Additionally, this command
46   * will also run a 'si viewnonmembers' command to report on all new
47   * files added in the sandbox directory
48   *
49   * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
50   * @version $Id: IntegrityStatusCommand.java 1.6 2011/08/22 13:06:36EDT Cletus D'Souza (dsouza) Exp  $
51   * @since 1.6
52   */
53  public class IntegrityStatusCommand
54      extends AbstractStatusCommand
55  {
56      /**
57       * {@inheritDoc}
58       */
59      @Override
60      public StatusScmResult executeStatusCommand( ScmProviderRepository repository, ScmFileSet fileSet )
61          throws ScmException
62      {
63          StatusScmResult result;
64          IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
65          getLogger().info( "Status of files changed in sandbox " + fileSet.getBasedir().getAbsolutePath() );
66          try
67          {
68              // Initialize the list of ScmFile objects for the StatusScmResult
69              List<ScmFile> scmFileList = new ArrayList<ScmFile>();
70  
71              // Get a listing for all the changes in the sandbox
72              Sandbox siSandbox = iRepo.getSandbox();
73              // Get the excludes and includes list from the configuration
74              String excludes = Sandbox.formatFilePatterns( fileSet.getExcludes() );
75              String includes = Sandbox.formatFilePatterns( fileSet.getIncludes() );
76  
77              // Get the new members found in the sandbox
78              List<ScmFile> newMemberList = siSandbox.getNewMembers( excludes, includes );
79              // Update the scmFileList with our updates
80              scmFileList.addAll( newMemberList );
81  
82              // Get the changed/dropped members from the sandbox
83              List<WorkItem> changeList = siSandbox.getChangeList();
84              for ( Iterator<WorkItem> wit = changeList.iterator(); wit.hasNext(); )
85              {
86                  WorkItem wi = wit.next();
87                  File memberFile = new File( wi.getField( "name" ).getValueAsString() );
88                  // Separate the changes into files that have been updated and deleted files
89                  if ( siSandbox.hasWorkingFile( (Item) wi.getField( "wfdelta" ).getValue() ) )
90                  {
91                      scmFileList.add( new ScmFile( memberFile.getAbsolutePath(), ScmFileStatus.UPDATED ) );
92                  }
93                  else
94                  {
95                      scmFileList.add( new ScmFile( memberFile.getAbsolutePath(), ScmFileStatus.DELETED ) );
96                  }
97              }
98  
99              if ( scmFileList.size() == 0 )
100             {
101                 getLogger().info( "No local changes found!" );
102             }
103             result = new StatusScmResult( scmFileList, new ScmResult( "si viewsandbox", "", "", true ) );
104         }
105         catch ( APIException aex )
106         {
107             ExceptionHandler eh = new ExceptionHandler( aex );
108             getLogger().error( "MKS API Exception: " + eh.getMessage() );
109             getLogger().debug( eh.getCommand() + " exited with return code " + eh.getExitCode() );
110             result = new StatusScmResult( eh.getCommand(), eh.getMessage(), "Exit Code: " + eh.getExitCode(), false );
111         }
112 
113         return result;
114     }
115 
116 }