001package org.apache.maven.scm.provider.integrity.command.unlock;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import com.mks.api.response.APIException;
023import com.mks.api.response.Response;
024import org.apache.maven.scm.CommandParameter;
025import org.apache.maven.scm.CommandParameters;
026import org.apache.maven.scm.ScmException;
027import org.apache.maven.scm.ScmFileSet;
028import org.apache.maven.scm.ScmResult;
029import org.apache.maven.scm.command.unlock.AbstractUnlockCommand;
030import org.apache.maven.scm.provider.ScmProviderRepository;
031import org.apache.maven.scm.provider.integrity.ExceptionHandler;
032import org.apache.maven.scm.provider.integrity.Sandbox;
033import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository;
034
035import java.io.File;
036
037/**
038 * MKS Integrity implementation of Maven's AbstractUnlockCommand
039 * <br>This command will run a 'si unlock' command for the specified filename
040 *
041 * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
042 * @since 1.6
043 */
044public class IntegrityUnlockCommand
045    extends AbstractUnlockCommand
046{
047    // This command will require the filename argument to be supplied for its execution
048    private String filename;
049
050    /**
051     * IntegrityUnlockCommand constructor requires a filename argument to be supplied for its execution
052     * <br>This avoids having to run the unlock command across the entire Sandbox.
053     *
054     * @param filename Relative path of the file needed to be unlocked
055     */
056    public IntegrityUnlockCommand( String filename )
057    {
058        this.filename = filename;
059    }
060
061    /**
062     * {@inheritDoc}
063     */
064    @Override
065    public ScmResult executeUnlockCommand( ScmProviderRepository repository, File workingDirectory )
066        throws ScmException
067    {
068        getLogger().info( "Attempting to unlock file: " + filename );
069        if ( null == filename || filename.length() == 0 )
070        {
071            throw new ScmException( "A single filename is required to execute the unlock command!" );
072        }
073
074        ScmResult result;
075        IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
076        try
077        {
078            Sandbox siSandbox = iRepo.getSandbox();
079            File memberFile = new File( workingDirectory.getAbsoluteFile() + File.separator + filename );
080            Response res = siSandbox.unlock( memberFile, filename );
081            int exitCode = res.getExitCode();
082            boolean success = ( exitCode == 0 ? true : false );
083            result = new ScmResult( res.getCommandString(), "", "Exit Code: " + exitCode, success );
084        }
085        catch ( APIException aex )
086        {
087            ExceptionHandler eh = new ExceptionHandler( aex );
088            getLogger().error( "MKS API Exception: " + eh.getMessage() );
089            getLogger().info( eh.getCommand() + " exited with return code " + eh.getExitCode() );
090            result = new ScmResult( eh.getCommand(), eh.getMessage(), "Exit Code: " + eh.getExitCode(), false );
091        }
092
093        return result;
094    }
095
096    /**
097     * {@inheritDoc}
098     */
099    @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}