View Javadoc

1   /*
2   Karma core - Core of the Karma application
3   Copyright (C) 2004  Toolforge <www.toolforge.nl>
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19  package nl.toolforge.karma.core.vc.cvsimpl;
20  
21  import nl.toolforge.karma.core.KarmaRuntimeException;
22  import nl.toolforge.karma.core.Patch;
23  import nl.toolforge.karma.core.Version;
24  import nl.toolforge.karma.core.module.Module;
25  import nl.toolforge.karma.core.vc.AuthenticationException;
26  import nl.toolforge.karma.core.vc.DevelopmentLine;
27  import nl.toolforge.karma.core.vc.ModuleStatus;
28  import nl.toolforge.karma.core.vc.PatchLine;
29  import nl.toolforge.karma.core.vc.Runner;
30  import nl.toolforge.karma.core.vc.RunnerFactory;
31  import nl.toolforge.karma.core.vc.SymbolicName;
32  import nl.toolforge.karma.core.vc.VersionControlException;
33  import nl.toolforge.karma.core.vc.model.MainLine;
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  public final class Utils {
38  
39    private static final Log logger = LogFactory.getLog(Utils.class);
40  
41    /***
42     * <p>Creates a symbolic name for <code>module</code>, based on <code>version</code> and whether a module has an
43     * associated <code>DevelopmentLine</code>.</p>
44     *
45     * <p>Right now, symbolic name stuff is supported for <code>SourceModule</code>s, so if this method is supplied with
46     * another <code>module</code> type, the method returns an empty <code>CVSTag</code>.
47     *
48     * @param module The module at hand.
49     * @param developmentLine The development line to which the version applies.
50     * @param version The version (within the modules' development line. <code>version</code> may be <code>null</code> in
51     *   which case it is ignored when creating a <code>SymbolicName</code>.
52     *
53     * @return A symbolic name (tag) as they are used in a CVS repository. An empty CVSTag is returned when another
54     *  instance than a {@link nl.toolforge.karma.core.module.SourceModule} is passed as the <code>module</code> parameter.
55     */
56    public static SymbolicName createSymbolicName(Module module, DevelopmentLine developmentLine, Version version) {
57  
58      if (version == null) {
59        new CVSTag("");
60        if (developmentLine != null) {
61          return new CVSTag(developmentLine.getName());
62        } else {
63          // We are using the TRUNK.
64          //
65          new CVSTag("");
66        }
67      } else {
68        if (developmentLine != null) {
69  
70          if (developmentLine instanceof PatchLine) {
71            if (!(version instanceof Patch)) {
72              throw new KarmaRuntimeException(
73                  "The provided developmentLine is a PatchLine instance. This implies " +
74                  "that the provided version should be a Patch instance.");
75            }
76            return createSymbolicName((Patch)version);
77          } else {
78            return new CVSTag(developmentLine.getName() + "_" + version.getVersionNumber());
79          }
80        } else if (version instanceof Patch) {
81  
82          // If the version argument is in fact a Patch instance, we have to create something like : PATCHLINE|p_0-0-0-
83          //
84          return new CVSTag(PatchLine.NAME_PREFIX + PatchLine.PATCH_SEPARATOR + version.getVersionNumber());
85  
86        } else {
87          return new CVSTag(MainLine.NAME_PREFIX + "_" + version.getVersionNumber());
88        }
89  
90      }
91      return new CVSTag("");
92    }
93  
94    public static SymbolicName createSymbolicName(Module module,  Version version) {
95      return Utils.createSymbolicName(module, null, version);
96    }
97  
98    public static SymbolicName createSymbolicName(Patch patch) {
99      return new CVSTag(PatchLine.NAME_PREFIX + PatchLine.PATCH_SEPARATOR + patch.getVersionNumber());
100   }
101 
102   public static Version getLastVersion(Module module) throws VersionControlException {
103 
104     logger.debug("Getting last version for module : " + module.getName());
105 
106     Runner runner = null;
107     try {
108       runner = RunnerFactory.getRunner(module.getLocation());
109     } catch (AuthenticationException e) {
110       throw new CVSException(e.getErrorCode(), e.getMessageArguments());
111     }
112 
113     ModuleStatus status = new CVSModuleStatus(module, ((CVSRunner) runner).log(module));
114     return status.getLastVersion();
115   }
116 
117   public static Version getLocalVersion(Module module) throws VersionControlException {
118     logger.debug("Getting local version for module : " + module.getName());
119 
120     ModuleStatus status = new CVSModuleStatus(module);
121     logger.debug("returning: " + status.getLocalVersion());
122     return status.getLocalVersion();
123   }
124 
125   public static boolean existsInRepository(Module module) {
126 
127     Runner runner = null;
128     try {
129       runner = RunnerFactory.getRunner(module.getLocation());
130       return runner.existsInRepository(module);
131 
132     } catch (AuthenticationException e) {
133       return false;
134     } catch (VersionControlException c) {
135       return false;
136     }
137   }
138 }