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   * @version $Id: IntegrityTagCommand.java 1.4 2011/08/22 13:06:38EDT Cletus D'Souza (dsouza) Exp  $
46   * @since 1.6
47   */
48  public class IntegrityTagCommand
49      extends AbstractTagCommand
50  {
51      /**
52       * {@inheritDoc}
53       */
54      @Override
55      public TagScmResult executeTagCommand( ScmProviderRepository repository, ScmFileSet fileSet, String tagName,
56                                             ScmTagParameters scmTagParameters )
57          throws ScmException
58      {
59          getLogger().info(
60              "Attempting to checkpoint project associated with sandbox " + fileSet.getBasedir().getAbsolutePath() );
61          TagScmResult result;
62          String message = scmTagParameters.getMessage();
63          IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
64  
65          try
66          {
67              // First validate the checkpoint label string by evaluating the groovy script
68              String chkptLabel = evalGroovyExpression( tagName );
69              Project.validateTag( chkptLabel );
70              String msg = ( ( null == message || message.length() == 0 ) ? System.getProperty( "message" ) : message );
71              // Get information about the Project
72              Project siProject = iRepo.getProject();
73              // Ensure this is not a build project configuration
74              if ( !siProject.isBuild() )
75              {
76                  Response res = siProject.checkpoint( msg, chkptLabel );
77                  int exitCode = res.getExitCode();
78                  boolean success = ( exitCode == 0 ? true : false );
79                  WorkItem wi = res.getWorkItem( siProject.getConfigurationPath() );
80                  String chkpt = wi.getResult().getField( "resultant" ).getItem().getId();
81                  getLogger().info(
82                      "Successfully checkpointed project " + siProject.getConfigurationPath() + " with label '"
83                          + chkptLabel + "', new revision is " + chkpt );
84                  result =
85                      new TagScmResult( res.getCommandString(), wi.getResult().getMessage(), "Exit Code: " + exitCode,
86                                        success );
87              }
88              else
89              {
90                  getLogger().error(
91                      "Cannot checkpoint a build project configuration: " + siProject.getConfigurationPath() + "!" );
92                  result =
93                      new TagScmResult( "si checkpoint", "Cannot checkpoint a build project configuration!", "", false );
94              }
95          }
96          catch ( CompilationFailedException cfe )
97          {
98              getLogger().error( "Groovy Compilation Exception: " + cfe.getMessage() );
99              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 }