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   * @version $Id: IntegrityUpdateCommand.java 1.3 2011/08/22 13:06:42EDT Cletus D'Souza (dsouza) Exp  $
51   * @since 1.6
52   */
53  public class IntegrityUpdateCommand
54      extends AbstractUpdateCommand
55  {
56      /**
57       * {@inheritDoc}
58       */
59      @Override
60      public UpdateScmResult executeUpdateCommand( ScmProviderRepository repository, ScmFileSet fileSet,
61                                                   ScmVersion scmVersion )
62          throws ScmException
63      {
64          getLogger().info( "Attempting to synchronize sandbox in " + fileSet.getBasedir().getAbsolutePath() );
65          List<ScmFile> updatedFiles = new ArrayList<ScmFile>();
66          IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
67          Sandbox siSandbox = iRepo.getSandbox();
68          try
69          {
70              // Make sure we've got a valid sandbox, otherwise create it...
71              if ( siSandbox.create() )
72              {
73                  Response res = siSandbox.resync();
74                  // Lets capture what we got from running this resync
75                  WorkItemIterator wit = res.getWorkItems();
76                  while ( wit.hasNext() )
77                  {
78                      WorkItem wi = wit.next();
79                      if ( wi.getModelType().equals( SIModelTypeName.MEMBER ) )
80                      {
81                          Result message = wi.getResult();
82                          getLogger().debug( wi.getDisplayId() + " " + ( null != message ? message.getMessage() : "" ) );
83                          if ( null != message && message.getMessage().length() > 0 )
84                          {
85                              updatedFiles.add( new ScmFile( wi.getDisplayId(),
86                                                             message.getMessage().equalsIgnoreCase( "removed" )
87                                                                 ? ScmFileStatus.DELETED
88                                                                 : ScmFileStatus.UPDATED ) );
89                          }
90                      }
91                  }
92                  return new UpdateScmResult( res.getCommandString(), updatedFiles );
93              }
94              else
95              {
96                  return new UpdateScmResult( "si resync", "Failed to synchronize workspace", "", false );
97              }
98          }
99          catch ( APIException aex )
100         {
101             ExceptionHandler eh = new ExceptionHandler( aex );
102             getLogger().error( "MKS API Exception: " + eh.getMessage() );
103             getLogger().info( eh.getCommand() + " exited with return code " + eh.getExitCode() );
104             return new UpdateScmResult( eh.getCommand(), eh.getMessage(), "Exit Code: " + eh.getExitCode(), false );
105         }
106     }
107 
108     /**
109      * {@inheritDoc}
110      */
111     @Override
112     protected ChangeLogCommand getChangeLogCommand()
113     {
114         IntegrityChangeLogCommand command = new IntegrityChangeLogCommand();
115         command.setLogger( getLogger() );
116         return command;
117     }
118 }