ScmSystemFactory.java :  » Build » antmod » org » antmod » scm » Java Open Source

Java Open Source » Build » antmod 
antmod » org » antmod » scm » ScmSystemFactory.java
package org.antmod.scm;

import java.text.ParseException;
import java.util.Iterator;

import org.antmod.conf.AntmodProperties;
import org.apache.commons.lang.StringUtils;


/**
 * Source Configuration Management manager class, for getting access to
 * a configured source configuration system. This depends on the configured
 * repository providers under the Antmod properties
 * "antmod.scm.providers.[providername]".
 * 
 * @author Klaas Waslander
 */
public final class ScmSystemFactory {
  
  /**
   * Never to be instantiated.
   */
  private ScmSystemFactory() {
  }

  /**
   * Get a handle to an SCM system of the given type.
   * For this the property "antmod.scm.providers.<scmType>"
   * is checked, which specifies the classname that implements the
   * "org.antmod.scm.ScmSystem" interface for this scmType.
   * @param scmType The name of the repository provider, such as "cvs" or "svn".
   * @return The repository provider's implementation of the ScmSystem interface.
   */
  public static ScmSystem getScmSystemByType(String scmType) throws IllegalArgumentException {
    String providerPropStart = "antmod.scm.providers.";
    Iterator scmProviders = AntmodProperties.getPropertyNamesStartingWith(providerPropStart);
    while (scmProviders.hasNext()) {
      String providerPropName = (String)scmProviders.next();
      if (providerPropName.substring(providerPropStart.length()).equals(scmType)) {
        String providerImpl = AntmodProperties.getProperty(providerPropName);
        try {
                    return (ScmSystem)Class.forName(providerImpl).newInstance();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
      }
    }
    throw new IllegalArgumentException("Unknown scm system type: \"" + scmType + "\"");
  }

  /**
   * Get an SCM system by name, properly configured already
   * based on the given repository name.
   * It checks the Antmod property "antmod.properties.<reposName>"
   * for this, which specifies a URL in the form of "scm:<scmType>:etc...".
   * @param reposName The name of the configured repository.
   */
  public static ScmSystem getScmSystemByName(String reposName) throws IllegalArgumentException {
    if (StringUtils.isBlank(reposName)) {
      reposName = "default";
    }

    String reposUrlString = AntmodProperties.getProperty("antmod.repositories." + reposName);
    if (StringUtils.isBlank(reposUrlString)) {
      throw new IllegalArgumentException("No such scm repository configured: \"" + reposName + "\", property 'antmod.repositories." + reposName + "' apparently not set.");
    } else {
      try {
        return getScmSystemByUrl(reposUrlString);
      } catch (ParseException pe) {
        throw new IllegalArgumentException(pe.toString());
      }
    }
  }

  /**
   * Get an SCM system representing the given SCM url,
   * which specifies an SCM repository in the form of "scm:<scmType>:etc...".
   */
  public static ScmSystem getScmSystemByUrl(String scmUrl) throws IllegalArgumentException, ParseException {
    if (StringUtils.isBlank(scmUrl)) {
      throw new IllegalArgumentException("Cannot return scm system with an empty or null url.");
    }
    return getScmSystemByUrl(new ScmUrl(scmUrl));
  }

  /**
   * Get an SCM system representing the given SCM url.
   */
  public static ScmSystem getScmSystemByUrl(ScmUrl scmUrl) throws IllegalArgumentException {
    ScmSystem scm = getScmSystemByType(scmUrl.getType());
    scm.setUrl(scmUrl);
    return scm;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.