001    package 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    
022    import com.mks.api.response.APIException;
023    import com.mks.api.response.Response;
024    import org.apache.maven.scm.CommandParameter;
025    import org.apache.maven.scm.CommandParameters;
026    import org.apache.maven.scm.ScmException;
027    import org.apache.maven.scm.ScmFileSet;
028    import org.apache.maven.scm.ScmResult;
029    import org.apache.maven.scm.command.unlock.AbstractUnlockCommand;
030    import org.apache.maven.scm.provider.ScmProviderRepository;
031    import org.apache.maven.scm.provider.integrity.ExceptionHandler;
032    import org.apache.maven.scm.provider.integrity.Sandbox;
033    import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository;
034    
035    import 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     * @version $Id: IntegrityUnlockCommand.java 1.3 2011/08/22 13:06:40EDT Cletus D'Souza (dsouza) Exp  $
043     * @since 1.6
044     */
045    public class IntegrityUnlockCommand
046        extends AbstractUnlockCommand
047    {
048        // This command will require the filename argument to be supplied for its execution
049        private String filename;
050    
051        /**
052         * IntegrityUnlockCommand constructor requires a filename argument to be supplied for its execution
053         * <br>This avoids having to run the unlock command across the entire Sandbox.
054         *
055         * @param filename Relative path of the file needed to be unlocked
056         */
057        public IntegrityUnlockCommand( String filename )
058        {
059            this.filename = filename;
060        }
061    
062        /**
063         * {@inheritDoc}
064         */
065        @Override
066        public ScmResult executeUnlockCommand( ScmProviderRepository repository, File workingDirectory )
067            throws ScmException
068        {
069            getLogger().info( "Attempting to unlock file: " + filename );
070            if ( null == filename || filename.length() == 0 )
071            {
072                throw new ScmException( "A single filename is required to execute the unlock command!" );
073            }
074    
075            ScmResult result;
076            IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
077            try
078            {
079                Sandbox siSandbox = iRepo.getSandbox();
080                File memberFile = new File( workingDirectory.getAbsoluteFile() + File.separator + filename );
081                Response res = siSandbox.unlock( memberFile, filename );
082                int exitCode = res.getExitCode();
083                boolean success = ( exitCode == 0 ? true : false );
084                result = new ScmResult( res.getCommandString(), "", "Exit Code: " + exitCode, success );
085            }
086            catch ( APIException aex )
087            {
088                ExceptionHandler eh = new ExceptionHandler( aex );
089                getLogger().error( "MKS API Exception: " + eh.getMessage() );
090                getLogger().info( eh.getCommand() + " exited with return code " + eh.getExitCode() );
091                result = new ScmResult( eh.getCommand(), eh.getMessage(), "Exit Code: " + eh.getExitCode(), false );
092            }
093    
094            return result;
095        }
096    
097        /**
098         * {@inheritDoc}
099         */
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    }