001    package org.apache.maven.scm.provider.integrity.command.tag;
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 com.mks.api.response.WorkItem;
025    import groovy.lang.Binding;
026    import groovy.lang.GroovyShell;
027    import org.apache.maven.scm.ScmException;
028    import org.apache.maven.scm.ScmFileSet;
029    import org.apache.maven.scm.ScmTagParameters;
030    import org.apache.maven.scm.command.tag.AbstractTagCommand;
031    import org.apache.maven.scm.command.tag.TagScmResult;
032    import org.apache.maven.scm.provider.ScmProviderRepository;
033    import org.apache.maven.scm.provider.integrity.ExceptionHandler;
034    import org.apache.maven.scm.provider.integrity.Project;
035    import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository;
036    import org.codehaus.groovy.control.CompilationFailedException;
037    import org.codehaus.groovy.control.CompilerConfiguration;
038    
039    /**
040     * MKS Integrity implementation of Maven's AbstractTagCommand
041     * <br>This command will execute a 'si checkpoint' command using a groovy
042     * script for evaluating the tag (label) name
043     *
044     * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
045     * @version $Id: IntegrityTagCommand.java 1.4 2011/08/22 13:06:38EDT Cletus D'Souza (dsouza) Exp  $
046     * @since 1.6
047     */
048    public class IntegrityTagCommand
049        extends AbstractTagCommand
050    {
051        /**
052         * {@inheritDoc}
053         */
054        @Override
055        public TagScmResult executeTagCommand( ScmProviderRepository repository, ScmFileSet fileSet, String tagName,
056                                               ScmTagParameters scmTagParameters )
057            throws ScmException
058        {
059            getLogger().info(
060                "Attempting to checkpoint project associated with sandbox " + fileSet.getBasedir().getAbsolutePath() );
061            TagScmResult result;
062            String message = scmTagParameters.getMessage();
063            IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
064    
065            try
066            {
067                // First validate the checkpoint label string by evaluating the groovy script
068                String chkptLabel = evalGroovyExpression( tagName );
069                Project.validateTag( chkptLabel );
070                String msg = ( ( null == message || message.length() == 0 ) ? System.getProperty( "message" ) : message );
071                // Get information about the Project
072                Project siProject = iRepo.getProject();
073                // Ensure this is not a build project configuration
074                if ( !siProject.isBuild() )
075                {
076                    Response res = siProject.checkpoint( msg, chkptLabel );
077                    int exitCode = res.getExitCode();
078                    boolean success = ( exitCode == 0 ? true : false );
079                    WorkItem wi = res.getWorkItem( siProject.getConfigurationPath() );
080                    String chkpt = wi.getResult().getField( "resultant" ).getItem().getId();
081                    getLogger().info(
082                        "Successfully checkpointed project " + siProject.getConfigurationPath() + " with label '"
083                            + chkptLabel + "', new revision is " + chkpt );
084                    result =
085                        new TagScmResult( res.getCommandString(), wi.getResult().getMessage(), "Exit Code: " + exitCode,
086                                          success );
087                }
088                else
089                {
090                    getLogger().error(
091                        "Cannot checkpoint a build project configuration: " + siProject.getConfigurationPath() + "!" );
092                    result =
093                        new TagScmResult( "si checkpoint", "Cannot checkpoint a build project configuration!", "", false );
094                }
095            }
096            catch ( CompilationFailedException cfe )
097            {
098                getLogger().error( "Groovy Compilation Exception: " + cfe.getMessage() );
099                result = new TagScmResult( "si checkpoint", cfe.getMessage(), "", false );
100            }
101            catch ( APIException aex )
102            {
103                ExceptionHandler eh = new ExceptionHandler( aex );
104                getLogger().error( "MKS API Exception: " + eh.getMessage() );
105                getLogger().info( eh.getCommand() + " exited with return code " + eh.getExitCode() );
106                result = new TagScmResult( eh.getCommand(), eh.getMessage(), "Exit Code: " + eh.getExitCode(), false );
107            }
108            catch ( Exception e )
109            {
110                getLogger().error( "Failed to checkpoint project! " + e.getMessage() );
111                result = new TagScmResult( "si checkpoint", e.getMessage(), "", false );
112            }
113            return result;
114        }
115    
116        public String evalGroovyExpression( String expression )
117        {
118            Binding binding = new Binding();
119            binding.setVariable( "env", System.getenv() );
120            binding.setVariable( "sys", System.getProperties() );
121            CompilerConfiguration config = new CompilerConfiguration();
122            GroovyShell shell = new GroovyShell( binding, config );
123            Object result = shell.evaluate( "return \"" + expression + "\"" );
124            if ( result == null )
125            {
126                return "";
127            }
128            else
129            {
130                return result.toString().trim();
131            }
132        }
133    }