View Javadoc
1   package org.apache.maven.scm.provider.integrity.command.tag;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import com.mks.api.response.APIException;
23  import com.mks.api.response.Response;
24  import com.mks.api.response.WorkItem;
25  import groovy.lang.Binding;
26  import groovy.lang.GroovyShell;
27  import org.apache.maven.scm.ScmException;
28  import org.apache.maven.scm.ScmFileSet;
29  import org.apache.maven.scm.ScmTagParameters;
30  import org.apache.maven.scm.command.tag.AbstractTagCommand;
31  import org.apache.maven.scm.command.tag.TagScmResult;
32  import org.apache.maven.scm.provider.ScmProviderRepository;
33  import org.apache.maven.scm.provider.integrity.ExceptionHandler;
34  import org.apache.maven.scm.provider.integrity.Project;
35  import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository;
36  import org.codehaus.groovy.control.CompilationFailedException;
37  import org.codehaus.groovy.control.CompilerConfiguration;
38  
39  /**
40   * MKS Integrity implementation of Maven's AbstractTagCommand
41   * <br>This command will execute a 'si checkpoint' command using a groovy
42   * script for evaluating the tag (label) name
43   *
44   * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
45   * @since 1.6
46   */
47  public class IntegrityTagCommand
48      extends AbstractTagCommand
49  {
50      /**
51       * {@inheritDoc}
52       */
53      @Override
54      public TagScmResult executeTagCommand( ScmProviderRepository repository, ScmFileSet fileSet, String tagName,
55                                             ScmTagParameters scmTagParameters )
56          throws ScmException
57      {
58          getLogger().info(
59              "Attempting to checkpoint project associated with sandbox " + fileSet.getBasedir().getAbsolutePath() );
60          TagScmResult result;
61          String message = scmTagParameters.getMessage();
62          IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
63  
64          try
65          {
66              // First validate the checkpoint label string by evaluating the groovy script
67              String chkptLabel = evalGroovyExpression( tagName );
68              Project.validateTag( chkptLabel );
69              String msg = ( ( null == message || message.length() == 0 ) ? System.getProperty( "message" ) : message );
70              // Get information about the Project
71              Project siProject = iRepo.getProject();
72              // Ensure this is not a build project configuration
73              if ( !siProject.isBuild() )
74              {
75                  Response res = siProject.checkpoint( msg, chkptLabel );
76                  int exitCode = res.getExitCode();
77                  boolean success = ( exitCode == 0 ? true : false );
78                  WorkItem wi = res.getWorkItem( siProject.getConfigurationPath() );
79                  String chkpt = wi.getResult().getField( "resultant" ).getItem().getId();
80                  getLogger().info(
81                      "Successfully checkpointed project " + siProject.getConfigurationPath() + " with label '"
82                          + chkptLabel + "', new revision is " + chkpt );
83                  result =
84                      new TagScmResult( res.getCommandString(), wi.getResult().getMessage(), "Exit Code: " + exitCode,
85                                        success );
86              }
87              else
88              {
89                  getLogger().error(
90                      "Cannot checkpoint a build project configuration: " + siProject.getConfigurationPath() + "!" );
91                  result =
92                      new TagScmResult( "si checkpoint", "Cannot checkpoint a build project configuration!", "", false );
93              }
94          }
95          catch ( CompilationFailedException cfe )
96          {
97              getLogger().error( "Groovy Compilation Exception: " + cfe.getMessage() );
98              result = new TagScmResult( "si checkpoint", cfe.getMessage(), "", false );
99          }
100         catch ( APIException aex )
101         {
102             ExceptionHandler eh = new ExceptionHandler( aex );
103             getLogger().error( "MKS API Exception: " + eh.getMessage() );
104             getLogger().info( eh.getCommand() + " exited with return code " + eh.getExitCode() );
105             result = new TagScmResult( eh.getCommand(), eh.getMessage(), "Exit Code: " + eh.getExitCode(), false );
106         }
107         catch ( Exception e )
108         {
109             getLogger().error( "Failed to checkpoint project! " + e.getMessage() );
110             result = new TagScmResult( "si checkpoint", e.getMessage(), "", false );
111         }
112         return result;
113     }
114 
115     public String evalGroovyExpression( String expression )
116     {
117         Binding binding = new Binding();
118         binding.setVariable( "env", System.getenv() );
119         binding.setVariable( "sys", System.getProperties() );
120         CompilerConfiguration config = new CompilerConfiguration();
121         GroovyShell shell = new GroovyShell( binding, config );
122         Object result = shell.evaluate( "return \"" + expression + "\"" );
123         if ( result == null )
124         {
125             return "";
126         }
127         else
128         {
129             return result.toString().trim();
130         }
131     }
132 }