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.scm;
20  
21  import java.io.File;
22  
23  /***
24   * <p>Describes a dependency for a <code>Module</code>. This class is used by a Digester reading in a file called
25   * <code>dependencies.xml</code> which is located in the root for each module that need dependencies. Dependencies can
26   * be defined in three ways:
27   *
28   * <ul>
29   *   <li/><code>&lt;dependency module="&lt;module-name&gt;"/&gt;</code> defines a dependency to another module that is
30   *        part of the same manifest. Those modules should be of the correct type (<code>Java - Source Module</code>).
31   *   <li/><code>&lt;dependency groupId="" artifactId="" version=""/&gt;</code> defines a dependency Maven-style. This
32   *        means that the actual <code>jar</code>-file is found on a local disk in a Maven repository. Karma imposes a
33   *        stronger definition of Maven dependencies than Maven does itself. Karma does not allow the following
34   *        structure : <code>id="" jar=""</code>.
35   *   <li/><code>&lt;dependency libmodule="" artifactId="" version=""/&gt;</code> defines that the
36   *        <code>&lt;artifactId&gt;-&lt;version&gt;.jar</code> package is 'loaded' from the given lib module.
37   * </ul>
38   * <p>
39   * As an optional attribute <code>package</code> can be defined, with possible values <code>true</code> or <code>false</code>.
40   * <code>false</code> is the default. This defines whether or not the dependency should be packaged in the current module's package.
41   * </p>
42   *
43   * @see nl.toolforge.karma.core.cmd.util.DependencyException
44   *
45   * @author D.A. Smedes
46   * @version $Id: ModuleDependency.java,v 1.14 2004/10/19 19:57:00 hippe Exp $
47   */
48  public final class ModuleDependency {
49  
50    private String id = null;
51    private String jar = null;
52  
53    private String groupId = null;
54    private String libModule = null;
55    private String artifactId = null;
56    private String version = null;
57  
58    private String module = null;
59  
60    private boolean doPackage = false;
61  
62  
63    public String getId() {
64      return id;
65    }
66  
67    public void setId(String id) {
68      this.id = id;
69    }
70  
71    public String getGroupId() {
72      return groupId;
73    }
74  
75    public void setGroupId(String groupId) {
76      this.groupId = groupId;
77    }
78  
79    public String getArtifactId() {
80      return artifactId;
81    }
82  
83    public void setArtifactId(String artifactId) {
84      this.artifactId = artifactId;
85    }
86  
87    public String getVersion() {
88      return version;
89    }
90  
91    public void setVersion(String version) {
92      this.version = version;
93    }
94  
95    public String getModule() {
96      return module;
97    }
98  
99    public void setModule(String module) {
100     this.module = module;
101   }
102 
103   public String getLibModule() {
104     return libModule;
105   }
106 
107   public void setLibModule(String libModule) {
108     this.libModule = libModule;
109   }
110 
111   public String getJar() {
112     return jar;
113   }
114 
115   public void setJar(String jar) {
116     this.jar = jar;
117   }
118 
119   public void setPackage(boolean b) {
120     this.doPackage = b;
121   }
122 
123   public boolean doPackage() {
124     return this.doPackage;
125   }
126 
127   public String getJarDependency() {
128 
129     String dep = null;
130 
131     if (groupId != null) {
132       // <dependency groupId="" artifactId="" version=""/>
133       //
134       dep = groupId + File.separator + "jars" + File.separator + artifactId + "-" + version;
135       dep += ".jar";
136     } else if (libModule != null) {
137       // <dependency libModule="" artifactId="" version=""/>
138       //
139       dep = libModule + File.separator + "lib" + File.separator + artifactId + "-" + version;
140       dep += ".jar";
141     } else if (id != null) {
142       // <dependency id="" jar=""/>
143       //
144       dep = id + File.separator + "jars" + File.separator + jar;
145     }
146 
147     return dep;
148   }
149 
150   /***
151    * <code>true</code> if the dependency identifies a module in the same
152    * manifest, otherwise false.
153    */
154   public boolean isModuleDependency() {
155 
156     // <dependency module=""/>
157     //
158     return module != null;
159   }
160 
161   /***
162    * <code>true</code> if the dependency identifies a jar in a module in the
163    * same manifest, otherwise false.
164    */
165   public boolean isLibModuleDependency() {
166 
167     // <dependency module=""/>
168     //
169     return libModule != null;
170   }
171 
172   /***
173    * Returns the hash code for this instance. The hash code is either <code>module.hashCode()</code> or
174    * <code>artifactId.hashCode()</code>; this follows the general structure
175    *
176    * @return
177    */
178   public int hashCode() {
179     if (isModuleDependency()) {
180       return module.hashCode();
181     } else {
182       if (groupId != null || libModule != null) {
183         return artifactId.hashCode();
184       } else {
185         return id.hashCode();
186       }
187     }
188   }
189 
190   /***
191    * Checks two <code>ModuleDependency</code> instances for equality. If the dependency is a module dependency, their
192    * module names are checked for equality. Otherwise the <code>artifactId</code> attribute is used to determine
193    * equality.
194    *
195    * @param obj Another <code>ModuleDependency</code>.
196    */
197   public boolean equals(Object obj) {
198 
199     if (!(obj instanceof ModuleDependency)) {
200       return false;
201     } else {
202 
203       if (isModuleDependency()) {
204         return module.equals(((ModuleDependency) obj).module);
205       } else if (groupId != null || libModule != null){
206         return artifactId.equals(((ModuleDependency) obj).artifactId);
207       } else if (id != null) {
208         return id.equals(((ModuleDependency) obj).id) &&
209                 jar.equals(((ModuleDependency) obj).jar);
210       } else {
211         return false;
212       }
213     }
214   }
215 
216   public String toString() {
217     String result = "<dependency ";
218 
219     if (module != null) {
220       result += "module=\""+module+"\" ";
221     } else if (groupId != null) {
222       result += "groupId=\""+groupId+"\" artifactId=\""+artifactId+"\" version=\""+version+"\" ";
223     } else if (id != null) {
224       result += "id=\""+id+"\" jar=\""+jar+"\" ";
225     } else {
226       result += "libModule=\""+libModule+"\" artifactId=\""+artifactId+"\" version=\""+version+"\" ";
227     }
228     if (doPackage) {
229       result += "package=\"true\" ";
230     } else {
231       result += "package=\"false\" ";
232     }
233     result += "/>";
234     return result;
235   }
236 }