001package org.apache.maven.scm.provider.integrity.command.lock;
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.lock.AbstractLockCommand;
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 AbstractLockCommand
039 * <br>This command will execute a 'si lock' on the specified member,
040 * which assumes the user has a valid Sandbox to work with.
041 *
042 * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
043 * @since 1.6
044 */
045public class IntegrityLockCommand
046    extends AbstractLockCommand
047{
048    /**
049     * {@inheritDoc}
050     */
051    @Override
052    public ScmResult executeLockCommand( ScmProviderRepository repository, File workingDirectory, String filename )
053        throws ScmException
054    {
055        getLogger().info( "Attempting to lock file: " + filename );
056        if ( null == filename || filename.length() == 0 )
057        {
058            throw new ScmException( "A single filename is required to execute the lock command!" );
059        }
060
061        ScmResult result;
062        IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
063        try
064        {
065            Sandbox siSandbox = iRepo.getSandbox();
066            File memberFile = new File( workingDirectory.getAbsoluteFile() + File.separator + filename );
067            Response res = siSandbox.lock( memberFile, filename );
068            int exitCode = res.getExitCode();
069            boolean success = ( exitCode == 0 ? true : false );
070            result = new ScmResult( res.getCommandString(), "", "Exit Code: " + exitCode, success );
071        }
072        catch ( APIException aex )
073        {
074            ExceptionHandler eh = new ExceptionHandler( aex );
075            getLogger().error( "MKS API Exception: " + eh.getMessage() );
076            getLogger().info( eh.getCommand() + " exited with return code " + eh.getExitCode() );
077            result = new ScmResult( eh.getCommand(), eh.getMessage(), "Exit Code: " + eh.getExitCode(), false );
078        }
079
080        return result;
081    }
082
083    /**
084     * {@inheritDoc}
085     */
086    @Override
087    protected ScmResult executeCommand( ScmProviderRepository repository, ScmFileSet fileSet,
088                                        CommandParameters parameters )
089        throws ScmException
090    {
091        return executeLockCommand( repository, fileSet.getBasedir(), parameters.getString( CommandParameter.FILE ) );
092    }
093
094}