001: package org.antmod.scm;
002:
003: import java.text.ParseException;
004: import java.util.Iterator;
005:
006: import org.antmod.conf.AntmodProperties;
007: import org.apache.commons.lang.StringUtils;
008:
009: /**
010: * Source Configuration Management manager class, for getting access to
011: * a configured source configuration system. This depends on the configured
012: * repository providers under the Antmod properties
013: * "antmod.scm.providers.[providername]".
014: *
015: * @author Klaas Waslander
016: */
017: public final class ScmSystemFactory {
018:
019: /**
020: * Never to be instantiated.
021: */
022: private ScmSystemFactory() {
023: }
024:
025: /**
026: * Get a handle to an SCM system of the given type.
027: * For this the property "antmod.scm.providers.<scmType>"
028: * is checked, which specifies the classname that implements the
029: * "org.antmod.scm.ScmSystem" interface for this scmType.
030: * @param scmType The name of the repository provider, such as "cvs" or "svn".
031: * @return The repository provider's implementation of the ScmSystem interface.
032: */
033: public static ScmSystem getScmSystemByType(String scmType)
034: throws IllegalArgumentException {
035: String providerPropStart = "antmod.scm.providers.";
036: Iterator scmProviders = AntmodProperties
037: .getPropertyNamesStartingWith(providerPropStart);
038: while (scmProviders.hasNext()) {
039: String providerPropName = (String) scmProviders.next();
040: if (providerPropName.substring(providerPropStart.length())
041: .equals(scmType)) {
042: String providerImpl = AntmodProperties
043: .getProperty(providerPropName);
044: try {
045: return (ScmSystem) Class.forName(providerImpl)
046: .newInstance();
047: } catch (InstantiationException e) {
048: e.printStackTrace();
049: } catch (IllegalAccessException e) {
050: e.printStackTrace();
051: } catch (ClassNotFoundException e) {
052: e.printStackTrace();
053: }
054: }
055: }
056: throw new IllegalArgumentException(
057: "Unknown scm system type: \"" + scmType + "\"");
058: }
059:
060: /**
061: * Get an SCM system by name, properly configured already
062: * based on the given repository name.
063: * It checks the Antmod property "antmod.properties.<reposName>"
064: * for this, which specifies a URL in the form of "scm:<scmType>:etc...".
065: * @param reposName The name of the configured repository.
066: */
067: public static ScmSystem getScmSystemByName(String reposName)
068: throws IllegalArgumentException {
069: if (StringUtils.isBlank(reposName)) {
070: reposName = "default";
071: }
072:
073: String reposUrlString = AntmodProperties
074: .getProperty("antmod.repositories." + reposName);
075: if (StringUtils.isBlank(reposUrlString)) {
076: throw new IllegalArgumentException(
077: "No such scm repository configured: \"" + reposName
078: + "\", property 'antmod.repositories."
079: + reposName + "' apparently not set.");
080: } else {
081: try {
082: return getScmSystemByUrl(reposUrlString);
083: } catch (ParseException pe) {
084: throw new IllegalArgumentException(pe.toString());
085: }
086: }
087: }
088:
089: /**
090: * Get an SCM system representing the given SCM url,
091: * which specifies an SCM repository in the form of "scm:<scmType>:etc...".
092: */
093: public static ScmSystem getScmSystemByUrl(String scmUrl)
094: throws IllegalArgumentException, ParseException {
095: if (StringUtils.isBlank(scmUrl)) {
096: throw new IllegalArgumentException(
097: "Cannot return scm system with an empty or null url.");
098: }
099: return getScmSystemByUrl(new ScmUrl(scmUrl));
100: }
101:
102: /**
103: * Get an SCM system representing the given SCM url.
104: */
105: public static ScmSystem getScmSystemByUrl(ScmUrl scmUrl)
106: throws IllegalArgumentException {
107: ScmSystem scm = getScmSystemByType(scmUrl.getType());
108: scm.setUrl(scmUrl);
109: return scm;
110: }
111: }
|