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   * @since 1.6
43   */
44  public class IntegrityUnlockCommand
45      extends AbstractUnlockCommand
46  {
47      // This command will require the filename argument to be supplied for its execution
48      private String filename;
49  
50      /**
51       * IntegrityUnlockCommand constructor requires a filename argument to be supplied for its execution
52       * <br>This avoids having to run the unlock command across the entire Sandbox.
53       *
54       * @param filename Relative path of the file needed to be unlocked
55       */
56      public IntegrityUnlockCommand( String filename )
57      {
58          this.filename = filename;
59      }
60  
61      /**
62       * {@inheritDoc}
63       */
64      @Override
65      public ScmResult executeUnlockCommand( ScmProviderRepository repository, File workingDirectory )
66          throws ScmException
67      {
68          getLogger().info( "Attempting to unlock file: " + filename );
69          if ( null == filename || filename.length() == 0 )
70          {
71              throw new ScmException( "A single filename is required to execute the unlock command!" );
72          }
73  
74          ScmResult result;
75          IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
76          try
77          {
78              Sandbox siSandbox = iRepo.getSandbox();
79              File memberFile = new File( workingDirectory.getAbsoluteFile() + File.separator + filename );
80              Response res = siSandbox.unlock( memberFile, filename );
81              int exitCode = res.getExitCode();
82              boolean success = ( exitCode == 0 ? true : false );
83              result = new ScmResult( res.getCommandString(), "", "Exit Code: " + exitCode, success );
84          }
85          catch ( APIException aex )
86          {
87              ExceptionHandler eh = new ExceptionHandler( aex );
88              getLogger().error( "MKS API Exception: " + eh.getMessage() );
89              getLogger().info( eh.getCommand() + " exited with return code " + eh.getExitCode() );
90              result = new ScmResult( eh.getCommand(), eh.getMessage(), "Exit Code: " + eh.getExitCode(), false );
91          }
92  
93          return result;
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      @Override
100     protected ScmResult executeCommand( ScmProviderRepository repository, ScmFileSet fileSet,
101                                         CommandParameters parameters )
102         throws ScmException
103     {
104         filename = parameters.getString( CommandParameter.FILE );
105         return executeUnlockCommand( repository, fileSet.getBasedir() );
106     }
107 
108 }