View Javadoc
1   package org.apache.maven.scm.provider.integrity.command.update;
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.ScmFile;
30  import org.apache.maven.scm.ScmFileSet;
31  import org.apache.maven.scm.ScmFileStatus;
32  import org.apache.maven.scm.ScmVersion;
33  import org.apache.maven.scm.command.changelog.ChangeLogCommand;
34  import org.apache.maven.scm.command.update.AbstractUpdateCommand;
35  import org.apache.maven.scm.command.update.UpdateScmResult;
36  import org.apache.maven.scm.provider.ScmProviderRepository;
37  import org.apache.maven.scm.provider.integrity.ExceptionHandler;
38  import org.apache.maven.scm.provider.integrity.Sandbox;
39  import org.apache.maven.scm.provider.integrity.command.changelog.IntegrityChangeLogCommand;
40  import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository;
41  
42  import java.util.ArrayList;
43  import java.util.List;
44  
45  /**
46   * MKS Integrity implementation of Maven's AbstractUpdateCommand
47   * <br>This command will execute a 'si resync' to synchronize the sandbox with the server revisions.
48   *
49   * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
50   * @since 1.6
51   */
52  public class IntegrityUpdateCommand
53      extends AbstractUpdateCommand
54  {
55      /**
56       * {@inheritDoc}
57       */
58      @Override
59      public UpdateScmResult executeUpdateCommand( ScmProviderRepository repository, ScmFileSet fileSet,
60                                                   ScmVersion scmVersion )
61          throws ScmException
62      {
63          getLogger().info( "Attempting to synchronize sandbox in " + fileSet.getBasedir().getAbsolutePath() );
64          List<ScmFile> updatedFiles = new ArrayList<ScmFile>();
65          IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
66          Sandbox siSandbox = iRepo.getSandbox();
67          try
68          {
69              // Make sure we've got a valid sandbox, otherwise create it...
70              if ( siSandbox.create() )
71              {
72                  Response res = siSandbox.resync();
73                  // Lets capture what we got from running this resync
74                  WorkItemIterator wit = res.getWorkItems();
75                  while ( wit.hasNext() )
76                  {
77                      WorkItem wi = wit.next();
78                      if ( wi.getModelType().equals( SIModelTypeName.MEMBER ) )
79                      {
80                          Result message = wi.getResult();
81                          getLogger().debug( wi.getDisplayId() + " " + ( null != message ? message.getMessage() : "" ) );
82                          if ( null != message && message.getMessage().length() > 0 )
83                          {
84                              updatedFiles.add( new ScmFile( wi.getDisplayId(),
85                                                             message.getMessage().equalsIgnoreCase( "removed" )
86                                                                 ? ScmFileStatus.DELETED
87                                                                 : ScmFileStatus.UPDATED ) );
88                          }
89                      }
90                  }
91                  return new UpdateScmResult( res.getCommandString(), updatedFiles );
92              }
93              else
94              {
95                  return new UpdateScmResult( "si resync", "Failed to synchronize workspace", "", false );
96              }
97          }
98          catch ( APIException aex )
99          {
100             ExceptionHandler eh = new ExceptionHandler( aex );
101             getLogger().error( "MKS API Exception: " + eh.getMessage() );
102             getLogger().info( eh.getCommand() + " exited with return code " + eh.getExitCode() );
103             return new UpdateScmResult( eh.getCommand(), eh.getMessage(), "Exit Code: " + eh.getExitCode(), false );
104         }
105     }
106 
107     /**
108      * {@inheritDoc}
109      */
110     @Override
111     protected ChangeLogCommand getChangeLogCommand()
112     {
113         IntegrityChangeLogCommand command = new IntegrityChangeLogCommand();
114         command.setLogger( getLogger() );
115         return command;
116     }
117 }