View Javadoc
1   package org.apache.maven.scm.provider.integrity.command.unlock;
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.CommandParameter;
25  import org.apache.maven.scm.CommandParameters;
26  import org.apache.maven.scm.ScmException;
27  import org.apache.maven.scm.ScmFileSet;
28  import org.apache.maven.scm.ScmResult;
29  import org.apache.maven.scm.command.unlock.AbstractUnlockCommand;
30  import org.apache.maven.scm.provider.ScmProviderRepository;
31  import org.apache.maven.scm.provider.integrity.ExceptionHandler;
32  import org.apache.maven.scm.provider.integrity.Sandbox;
33  import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository;
34  
35  import java.io.File;
36  
37  /**
38   * MKS Integrity implementation of Maven's AbstractUnlockCommand
39   * <br>This command will run a 'si unlock' command for the specified filename
40   *
41   * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
42   * @version $Id: IntegrityUnlockCommand.java 1.3 2011/08/22 13:06:40EDT Cletus D'Souza (dsouza) Exp  $
43   * @since 1.6
44   */
45  public class IntegrityUnlockCommand
46      extends AbstractUnlockCommand
47  {
48      // This command will require the filename argument to be supplied for its execution
49      private String filename;
50  
51      /**
52       * IntegrityUnlockCommand constructor requires a filename argument to be supplied for its execution
53       * <br>This avoids having to run the unlock command across the entire Sandbox.
54       *
55       * @param filename Relative path of the file needed to be unlocked
56       */
57      public IntegrityUnlockCommand( String filename )
58      {
59          this.filename = filename;
60      }
61  
62      /**
63       * {@inheritDoc}
64       */
65      @Override
66      public ScmResult executeUnlockCommand( ScmProviderRepository repository, File workingDirectory )
67          throws ScmException
68      {
69          getLogger().info( "Attempting to unlock file: " + filename );
70          if ( null == filename || filename.length() == 0 )
71          {
72              throw new ScmException( "A single filename is required to execute the unlock command!" );
73          }
74  
75          ScmResult result;
76          IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
77          try
78          {
79              Sandbox siSandbox = iRepo.getSandbox();
80              File memberFile = new File( workingDirectory.getAbsoluteFile() + File.separator + filename );
81              Response res = siSandbox.unlock( memberFile, filename );
82              int exitCode = res.getExitCode();
83              boolean success = ( exitCode == 0 ? true : false );
84              result = new ScmResult( res.getCommandString(), "", "Exit Code: " + exitCode, success );
85          }
86          catch ( APIException aex )
87          {
88              ExceptionHandler eh = new ExceptionHandler( aex );
89              getLogger().error( "MKS API Exception: " + eh.getMessage() );
90              getLogger().info( eh.getCommand() + " exited with return code " + eh.getExitCode() );
91              result = new ScmResult( eh.getCommand(), eh.getMessage(), "Exit Code: " + eh.getExitCode(), false );
92          }
93  
94          return result;
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     protected ScmResult executeCommand( ScmProviderRepository repository, ScmFileSet fileSet,
102                                         CommandParameters parameters )
103         throws ScmException
104     {
105         filename = parameters.getString( CommandParameter.FILE );
106         return executeUnlockCommand( repository, fileSet.getBasedir() );
107     }
108 
109 }