KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antmod > scm > ScmSystemFactory


1 package org.antmod.scm;
2
3 import java.text.ParseException JavaDoc;
4 import java.util.Iterator JavaDoc;
5
6 import org.antmod.conf.AntmodProperties;
7 import org.apache.commons.lang.StringUtils;
8
9
10 /**
11  * Source Configuration Management manager class, for getting access to
12  * a configured source configuration system. This depends on the configured
13  * repository providers under the Antmod properties
14  * "antmod.scm.providers.[providername]".
15  *
16  * @author Klaas Waslander
17  */

18 public final class ScmSystemFactory {
19     
20     /**
21      * Never to be instantiated.
22      */

23     private ScmSystemFactory() {
24     }
25
26     /**
27      * Get a handle to an SCM system of the given type.
28      * For this the property "antmod.scm.providers.<scmType>"
29      * is checked, which specifies the classname that implements the
30      * "org.antmod.scm.ScmSystem" interface for this scmType.
31      * @param scmType The name of the repository provider, such as "cvs" or "svn".
32      * @return The repository provider's implementation of the ScmSystem interface.
33      */

34     public static ScmSystem getScmSystemByType(String JavaDoc scmType) throws IllegalArgumentException JavaDoc {
35         String JavaDoc providerPropStart = "antmod.scm.providers.";
36         Iterator JavaDoc scmProviders = AntmodProperties.getPropertyNamesStartingWith(providerPropStart);
37         while (scmProviders.hasNext()) {
38             String JavaDoc providerPropName = (String JavaDoc)scmProviders.next();
39             if (providerPropName.substring(providerPropStart.length()).equals(scmType)) {
40                 String JavaDoc providerImpl = AntmodProperties.getProperty(providerPropName);
41                 try {
42                     return (ScmSystem)Class.forName(providerImpl).newInstance();
43                 } catch (InstantiationException JavaDoc e) {
44                     e.printStackTrace();
45                 } catch (IllegalAccessException JavaDoc e) {
46                     e.printStackTrace();
47                 } catch (ClassNotFoundException JavaDoc e) {
48                     e.printStackTrace();
49                 }
50             }
51         }
52         throw new IllegalArgumentException JavaDoc("Unknown scm system type: \"" + scmType + "\"");
53     }
54
55     /**
56      * Get an SCM system by name, properly configured already
57      * based on the given repository name.
58      * It checks the Antmod property "antmod.properties.<reposName>"
59      * for this, which specifies a URL in the form of "scm:<scmType>:etc...".
60      * @param reposName The name of the configured repository.
61      */

62     public static ScmSystem getScmSystemByName(String JavaDoc reposName) throws IllegalArgumentException JavaDoc {
63         if (StringUtils.isBlank(reposName)) {
64             reposName = "default";
65         }
66
67         String JavaDoc reposUrlString = AntmodProperties.getProperty("antmod.repositories." + reposName);
68         if (StringUtils.isBlank(reposUrlString)) {
69             throw new IllegalArgumentException JavaDoc("No such scm repository configured: \"" + reposName + "\", property 'antmod.repositories." + reposName + "' apparently not set.");
70         } else {
71             try {
72                 return getScmSystemByUrl(reposUrlString);
73             } catch (ParseException JavaDoc pe) {
74                 throw new IllegalArgumentException JavaDoc(pe.toString());
75             }
76         }
77     }
78
79     /**
80      * Get an SCM system representing the given SCM url,
81      * which specifies an SCM repository in the form of "scm:<scmType>:etc...".
82      */

83     public static ScmSystem getScmSystemByUrl(String JavaDoc scmUrl) throws IllegalArgumentException JavaDoc, ParseException JavaDoc {
84         if (StringUtils.isBlank(scmUrl)) {
85             throw new IllegalArgumentException JavaDoc("Cannot return scm system with an empty or null url.");
86         }
87         return getScmSystemByUrl(new ScmUrl(scmUrl));
88     }
89
90     /**
91      * Get an SCM system representing the given SCM url.
92      */

93     public static ScmSystem getScmSystemByUrl(ScmUrl scmUrl) throws IllegalArgumentException JavaDoc {
94         ScmSystem scm = getScmSystemByType(scmUrl.getType());
95         scm.setUrl(scmUrl);
96         return scm;
97     }
98 }
99
Popular Tags