Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2006-2013 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;
 
 
Implementation on the Maven SCM implementation to fetch the latest revision number.

Author(s):
Robert Reiner
Version:
$Revision:591 $
 
 public final class MavenScmRevisionNumberFetcher implements RevisionNumberFetcher
 {
   // ********************************* Fields *********************************
 
   // --- constants ------------------------------------------------------------
 
  
Reference to the logger for this class.
 
   private static final Log LOG = LogFactory
 
   // --- 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 *****************************
  // ********************************* Methods ********************************
  // --- init -----------------------------------------------------------------
  // --- get&set --------------------------------------------------------------
  // --- business -------------------------------------------------------------

  
  {
    if (.isDebugEnabled())
    {
      .debug("  Fetching latest revision number.\n    "
                + this. + "\n    " + this.);
    }
    final ScmRepository repository =
    final ScmProvider provider = createScmProvider(repository);
    final ChangeLogScmResult result =
        .fetchChangeLog(repositoryprovider);
    if (result != null)
    {
      final ChangeLogSet changeLogSet = result.getChangeLog();
      final Revision revision = findEndVersion(changeLogSet);
      if (.isDebugEnabled())
      {
        .debug("  Found revision '" + revision + "'.");
      }
      return revision;
    }
    else if (.isDebugEnabled())
    {
      .debug("  No revision information found.");
    }
    return null;
  }

  
    throws ScmException
  {
    if (.isDebugEnabled())
    {
      .debug("  Fetching modification information.\n    "
                + this. + "\n    " + this.);
    }
    try
    {
      final ScmRepository repository =
      final ScmProvider provider = createScmProvider(repository);
      final StatusScmResult result = provider.status(repositoryfileSet);
      if (result.isSuccess())
      {
        return createLocallyModifiedInfo(result);
      }
      else
      {
        final String message =
            result.getProviderMessage() + ": " + result.getCommandOutput();
        if (.isDebugEnabled())
        {
          .debug(message);
        }
        throw new ScmException(message);
      }
    }
    catch (final org.apache.maven.scm.ScmException e)
    {
      throw new ScmException(e);
    }
  }
  @SuppressWarnings("unchecked")
      final StatusScmResult result)
  {
    final List<ScmFilechangedFiles = filter(result.getChangedFiles());
    final boolean locallyModified = !changedFiles.isEmpty();
    if (.isDebugEnabled())
    {
      .debug("  Modifications have" + (locallyModified ? "" : " not")
                + " been found.");
    }
    return new LocallyModifiedInfo(locallyModifiedlocallyModified
        ? toString(changedFiles) : null);
  }
  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 List<?> items)
  {
    final StringBuilder buffer = new StringBuilder(512);
    for (Object item : items)
    {
      buffer.append(item).append(' ');
    }
    return StringUtils.chomp(buffer.toString());
  }

  
Finds the largest revision number.

Parameters:
changeLogSet the set of change log entries to compare the revision numbers to find the largest.
Returns:
the largest revision number from the set or null if no end version can be found.
Impl:
Currently we assume the the largest revision is provided by the last entry of the set.
  private Revision findEndVersion(final ChangeLogSet changeLogSet)
  {
    if (changeLogSet != null)
    {
      final ScmVersion endVersion = changeLogSet.getEndVersion();
      if (endVersion != null)
      {
        if(.isDebugEnabled())
        {
          .debug("End version found.");
        }
        return new MavenRevision(endVersionchangeLogSet.getEndDate());
      }
      final List<ChangeSetchangeSets = changeLogSet.getChangeSets();
      if (!changeSets.isEmpty())
      {
        final int lastIndex = changeSets.size() - 1;
        for (int index = lastIndexindex >= 0; index--)
        {
          final ChangeSet set = changeSets.get(lastIndex);
          final List<ChangeFilechangeFiles = set.getFiles();
          if (!changeFiles.isEmpty())
          {
            final ChangeFile file = changeFiles.get(0);
            final String revision = file.getRevision();
            if (revision != null)
            {
              return new StringRevision(revisionset.getDate());
            }
          }
          else
          {
            if(.isDebugEnabled())
            {
              .debug("No change files found.");
            }
          }
        }
      }
      else
      {
        if(.isDebugEnabled())
        {
          .debug("No change set found.");
        }
      }
    }
    else
    {
      if(.isDebugEnabled())
      {
        .debug("No change log set found.");
      }
    }
    return null;
  }

  
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