Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2006-2014 smartics, Kronseder & Reiner GmbH
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.redhat.rcm.maven.plugin.buildmetadata.scm.maven;
 
 import java.util.List;
 import java.util.Set;
Implementation on the Maven SCM implementation to fetch the latest revision number.

Author(s):
Robert Reiner
 
 public final class MavenScmRevisionNumberFetcher extends AbstractBuildMojo implements
 {
   // ********************************* Fields *********************************
 
   // --- constants ------------------------------------------------------------
 
   // --- members --------------------------------------------------------------
 
  
The SCM manager to access to SCM system.
 
   private final ScmManager scmManager;

  
The information to connect to the SCM system.
 
   private final ScmConnectionInfo scmConnectionInfo;

  
Information to retrieve the revision information from the SCM after the connection is established.
 
   private final ScmAccessInfo scmAccessInfo;
 
   // ****************************** Initializer *******************************
 
   // ****************************** Constructors ******************************
 
  
Default constructor.

Parameters:
scmManager the SCM manager to access to SCM system.
scmConnectionInfo the information to connect to the SCM system.
scmAccessInfo the value for scmAccessInfo.
 
   public MavenScmRevisionNumberFetcher(final ScmManager scmManager,
       final ScmConnectionInfo scmConnectionInfo,
       final ScmAccessInfo scmAccessInfo)
   {
     this. = scmManager;
     this. = scmConnectionInfo;
     this. = scmAccessInfo;
   }
 
   // ****************************** Inner Classes *****************************
 
  
Wrapper to access the changed files.
 
   private interface ScmResultWrapper
  {
    boolean isSuccess();
  }

  
Default base implementation.
  private abstract static class AbstractScmResultWrapper implements
  {
    
Reference to the result.
    private final ScmResult result;
    protected AbstractScmResultWrapper(final ScmResult result)
    {
      this. = result;
    }
    @Override
    public boolean isSuccess()
    {
      return .isSuccess();
    }
    @Override
    public String toString()
    {
      return .getProviderMessage() + ": " + .getCommandOutput();
    }
  }
  // ********************************* Methods ********************************
  // --- init -----------------------------------------------------------------
  // --- get&set --------------------------------------------------------------
  // --- business -------------------------------------------------------------

  
  {
      if (getLog().isDebugEnabled())
      {
          getLog().debug("  Fetching latest revision number.\n    "
                  + this. + "\n    " + this.);
      }
      final ScmRepository repository =
              .createRepository();
      final ScmProvider provider = createScmProvider(repository);
      final ChangeLogScmResult result =
              .fetchChangeLog(repositoryprovider);
      Revision revision = null;
      if (result != null && result.isSuccess())
      {
          final List<ChangeSetchangeLogSets = result.getChangeLog().getChangeSets();
          if (changeLogSets.size() > 0)
          {
              revision = new StringRevision (changeLogSets.get(0).getRevision(),
                      changeLogSets.get(0).getDate());
              if (getLog().isDebugEnabled())
              {
                  getLog().debug("Found revision '" + revision + "'.");
              }
          }
      }
      else if (getLog().isDebugEnabled())
      {
          getLog().debug("No revision information found.");
      }
      return revision;
  }

  
    throws ScmException
  {
    if (getLog().isDebugEnabled())
    {
      getLog().debug("  Fetching modification information.\n    "
                + this. + "\n    " + this.);
    }
    try
    {
      final ScmRepository repository =
      final ScmProvider provider = createScmProvider(repository);
      final ScmResultWrapper result = execute(fileSetrepositoryprovider);
      if (result.isSuccess())
      {
        return createLocallyModifiedInfo(result);
      }
      else
      {
        final String message = result.toString();
        if (getLog().isDebugEnabled())
        {
          getLog().debug(message);
        }
        throw new ScmException(message);
      }
    }
    catch (final org.apache.maven.scm.ScmException e)
    {
      throw new ScmException(e);
    }
  }
  private ScmResultWrapper execute(final ScmFileSet fileSet,
      final ScmRepository repositoryfinal ScmProvider provider)
  {
    final ScmVersion remoteVersion = .getRemoteVersion();
    final ScmResultWrapper result;
    if (remoteVersion != null)
    {
      final ScmVersion localVersion = null;
      result =
          createScmDiffResultWrapper(provider.diff(repositoryfileSet,
              remoteVersionlocalVersion));
    }
    else
    {
      result =
          createScmStatusResultWrapper(provider.status(repositoryfileSet));
    }
    return result;
  }
  {
    return new AbstractScmResultWrapper(diff)
    {
      @Override
    public List<ScmFilegetChangedFiles()
      {
        return diff.getChangedFiles();
      }
    };
  }
      final StatusScmResult diff)
  {
    return new AbstractScmResultWrapper(diff)
    {
      @Override
    public List<ScmFilegetChangedFiles()
      {
        return diff.getChangedFiles();
      }
    };
  }
      final ScmResultWrapper result)
  {
    final List<ScmFilechangedFiles = filter(result.getChangedFiles());
    final boolean locallyModified = !changedFiles.isEmpty();
    if (getLog().isDebugEnabled())
    {
      getLog().debug("  Modifications have" + (locallyModified ? "" : " not")
                + " been found.");
    }
    return new LocallyModifiedInfo(locallyModifiedlocallyModified
        ? toString(createSortedFiles(changedFiles)) : null);
  }
  private static Set<ScmFilecreateSortedFiles(final List<ScmFilechangedFiles)
  {
    final Set<ScmFileset = new TreeSet<ScmFile>(new Comparator<ScmFile>()
    {
      @Override
    public int compare(final ScmFile o1final ScmFile o2)
      {
        return o2.compareTo(o1);
      }
    });
    set.addAll(changedFiles);
    return set;
  }
  private List<ScmFilefilter(final List<ScmFilefiles)
  {
    {
      filterDotFiles(files);
    }
    return files;
  }
  private void filterDotFiles(final List<ScmFilefiles)
  {
    for (final Iterator<ScmFilei = files.iterator(); i.hasNext();)
    {
      final ScmFile file = i.next();
      final String path = file.getPath();
      if (path.length() > 0 && path.charAt(0) == '.')
      {
        i.remove();
      }
    }
  }

  
Renders the files to a blank separated list of file names.

Parameters:
items the file items.
Returns:
the string representation of the files.
  private String toString(final Collection<?> items)
  {
    final StringBuilder buffer = new StringBuilder(512);
    for (final Object item : items)
    {
      buffer.append(item).append(' ');
    }
    return StringUtils.chomp(buffer.toString());
  }


  
Creates the provider instance to access the given repository.

Parameters:
repository the repository to access with the provider to be created.
Returns:
the provider to access the given repository.
Throws:
com.redhat.rcm.maven.plugin.buildmetadata.scm.ScmException if the provider cannot be created.
  private ScmProvider createScmProvider(final ScmRepository repository)
    throws ScmException
  {
    try
    {
      final ScmProvider provider =
          .getProviderByRepository(repository);
      return provider;
    }
    catch (final NoSuchScmProviderException e)
    {
      throw new ScmException("Cannot create SCM provider."e);
    }
  }
  // --- object basics --------------------------------------------------------
New to GrepCode? Check out our FAQ X