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 * @version $Id: IntegrityLockCommand.java 1.3 2011/08/22 13:06:31EDT Cletus D'Souza (dsouza) Exp  $
044 * @since 1.6
045 */
046public class IntegrityLockCommand
047    extends AbstractLockCommand
048{
049    /**
050     * {@inheritDoc}
051     */
052    @Override
053    public ScmResult executeLockCommand( ScmProviderRepository repository, File workingDirectory, String filename )
054        throws ScmException
055    {
056        getLogger().info( "Attempting to lock file: " + filename );
057        if ( null == filename || filename.length() == 0 )
058        {
059            throw new ScmException( "A single filename is required to execute the lock command!" );
060        }
061
062        ScmResult result;
063        IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
064        try
065        {
066            Sandbox siSandbox = iRepo.getSandbox();
067            File memberFile = new File( workingDirectory.getAbsoluteFile() + File.separator + filename );
068            Response res = siSandbox.lock( memberFile, filename );
069            int exitCode = res.getExitCode();
070            boolean success = ( exitCode == 0 ? true : false );
071            result = new ScmResult( res.getCommandString(), "", "Exit Code: " + exitCode, success );
072        }
073        catch ( APIException aex )
074        {
075            ExceptionHandler eh = new ExceptionHandler( aex );
076            getLogger().error( "MKS API Exception: " + eh.getMessage() );
077            getLogger().info( eh.getCommand() + " exited with return code " + eh.getExitCode() );
078            result = new ScmResult( eh.getCommand(), eh.getMessage(), "Exit Code: " + eh.getExitCode(), false );
079        }
080
081        return result;
082    }
083
084    /**
085     * {@inheritDoc}
086     */
087    @Override
088    protected ScmResult executeCommand( ScmProviderRepository repository, ScmFileSet fileSet,
089                                        CommandParameters parameters )
090        throws ScmException
091    {
092        return executeLockCommand( repository, fileSet.getBasedir(), parameters.getString( CommandParameter.FILE ) );
093    }
094
095}