View Javadoc

1   package net.sf.jpkgmk.pkgmap;
2   
3   import net.sf.jpkgmk.AbstractLineProvider;
4   import net.sf.jpkgmk.util.ObjectUtils;
5   import net.sf.jpkgmk.util.StringUtil;
6   import net.sf.jpkgmk.util.StringUtil.KeyValuePair;
7   
8   import org.apache.commons.logging.Log;
9   import org.apache.commons.logging.LogFactory;
10  
11  /**
12   * Abstract class that can be used for the most of the pkgmap entries, for example files, directories, links ...
13   * @author gommma (gommma AT users.sourceforge.net)
14   * @author Last changed by: $Author: gommma $
15   * @version $Revision: 2 $ $Date: 2008-08-20 21:14:19 +0200 (Mi, 20 Aug 2008) $
16   * @since 1.0
17   */
18  public abstract class AbstractPkgMapEntry extends AbstractLineProvider implements PkgMapEntry, Comparable<AbstractPkgMapEntry>
19  {
20  	private Log log = LogFactory.getLog(AbstractPkgMapEntry.class);
21  	
22  	/**
23  	 * An optional field designating the part number in which the object resides. 
24  	 * A part is a collection of files and is the atomic unit by which a package is processed. 
25  	 * A developer can choose criteria for grouping files into a part (for example, based on class). 
26  	 * If this field is not used, part 1 is assumed. (field is omitted in prototype file)
27  	 */
28  	private Integer part;
29  	private PkgMapEntryType type;
30  	private String entryPath;
31  	private String entryPathSource;
32  	private String entryClass;
33  	private Integer major;
34  	private Integer minor;
35  	private String mode;
36  	private String owner;
37  	private String group;
38  
39  	// Only for files
40  	private Long modtime;
41  	private Long checksum;
42  	private Long filesize;
43  	
44  	/**
45  	 * Constructor taking the mandatory arguments to create a pkgmap entry
46  	 * @param fileType
47  	 * @param value
48  	 */
49  	public AbstractPkgMapEntry(PkgMapEntryType fileType, String entryPath)
50  	{
51  		this(fileType, null, entryPath, null, null, null, null);
52  	}
53  
54  	/**
55  	 * @param fileType
56  	 * @param entryClass
57  	 * @param entryPath the target path in that this file/directory should have in the package
58  	 * @param mode
59  	 * @param owner
60  	 * @param group
61  	 */
62  	public AbstractPkgMapEntry(PkgMapEntryType fileType, String entryPath, String mode, String owner, String group)
63  	{
64  		this(fileType, null, entryPath, null, mode, owner, group);
65  	}
66  	
67  	/**
68  	 * @param fileType
69  	 * @param fileClass
70  	 * @param entryPath the target path in that this file/directory should have in the package
71  	 * @param entryPathSource path to the local source file
72  	 * @param mode
73  	 * @param owner
74  	 * @param group
75  	 */
76  	public AbstractPkgMapEntry(PkgMapEntryType fileType, String fileClass, String entryPath, String entryPathSource, String mode, String owner, String group)
77  	{
78  		this(null, fileType, fileClass, entryPath, entryPathSource, null, null, mode, owner, group);
79  	}
80  
81  	
82  	/**
83  	 * Full constructor taking all possible arguments
84  	 * @param part Optional part number
85  	 * @param type
86  	 * @param entryClass
87  	 * @param entryPath the target path in that this file/directory should have in the package
88  	 * @param entryPathSource
89  	 * @param major The major device number. The field is only specified for block or character special devices.
90  	 * @param minor The minor device number. The field is only specified for block or character special devices.
91  	 * @param mode
92  	 * @param owner
93  	 * @param group
94  	 */
95  	public AbstractPkgMapEntry(Integer part, PkgMapEntryType type, String entryClass, String entryPath, String entryPathSource, Integer major, Integer minor, String mode, String owner, String group)
96  	{
97  		// Mandatory parameter check
98  		if(type == null) {
99  			throw new NullPointerException("The parameter 'fileType' must not be null");
100 		}
101 		if(entryPath == null) {
102 			throw new NullPointerException("The parameter 'entryPath' must not be null");
103 		}
104 
105 		
106 		int maxClassLength = 12;
107 		if(entryClass != null) {
108 			if(entryClass.length() > maxClassLength) {
109 				throw new IllegalArgumentException("The given class '" + entryClass + "' exceeds the maximum allowed length of " + maxClassLength);
110 			}
111 			// Check if class is alphanumeric
112 			if(!StringUtil.isAlphaNumeric(entryClass)) {
113 				throw new IllegalArgumentException("The given class '" + entryClass + "' is not alphanumeric.");
114 			}
115 		}
116 		
117 		if(mode != null) {
118 			StringUtil.validateMode(mode);
119 		}
120 		int maxOwnerLength = 14;
121 		if(owner != null && owner.length() > maxOwnerLength) {
122 			throw new IllegalArgumentException("The given owner '" + owner + "' exceeds the maximum allowed length of " + maxOwnerLength);
123 		}
124 		
125 		int maxGroupLength = 14;
126 		if(group != null && group.length() > maxGroupLength) {
127 			throw new IllegalArgumentException("The given group '" + group + "' exceeds the maximum allowed length of " + maxGroupLength);
128 		}
129 		
130 		this.part = part;
131 		this.type = type;
132 		this.entryClass = entryClass;
133 		this.entryPath = entryPath;
134 		this.entryPathSource = entryPathSource;
135 		this.mode = mode;
136 		this.owner = owner;
137 		this.group = group;
138 		this.major = major;
139 		this.minor = minor;
140 	}
141 
142 	/**
143 	 * @return The pkgmap line
144 	 */
145 	public String createLine()
146 	{
147 		log.debug("<entering> createLine()");
148 		
149 		StringBuffer sb = new StringBuffer();
150 		
151 		if(this.part != null) {
152 			sb.append(this.part);
153 			sb.append(" "); // add separator
154 		}
155 		
156 		sb.append(this.type.getKey());
157 		
158 		if(this.entryClass != null) {
159 			sb.append(" ").append(this.entryClass);
160 		}
161 
162 		sb.append(" ").append(this.entryPath);
163 		if(!StringUtil.isNullOrEmpty(entryPathSource)) {
164 			sb.append(KeyValuePair.ASSIGNMENT_CHAR).append(entryPathSource);
165 		}
166 		
167 		if(!StringUtil.isNullOrEmpty(mode)) {
168 			sb.append(" ").append(mode);
169 		}
170 		if(!StringUtil.isNullOrEmpty(owner)) {
171 			sb.append(" ").append(owner);
172 		}
173 		if(!StringUtil.isNullOrEmpty(group)) {
174 			sb.append(" ").append(group);
175 		}
176 	
177 		if(filesize!=null) {
178 			sb.append(" ").append(filesize);
179 		}
180 		if(checksum!=null) {
181 			sb.append(" ").append(checksum);
182 		}
183 		if(modtime!=null) {
184 			sb.append(" ").append(modtime);
185 		}
186 
187 		return sb.toString();
188 	}
189 
190 	public String getLine()
191 	{
192 		return this.createLine();
193 	}
194 	
195 	public Integer getPart() {
196 		return part;
197 	}
198 
199 	/**
200 	 * @return Returns the type of this entry
201 	 */
202 	public PkgMapEntryType getType()
203 	{
204 		return this.type;
205 	}
206 	
207 	public String getEntryPath() {
208 		return entryPath;
209 	}
210 
211 	public String getEntryPathSource() {
212 		return entryPathSource;
213 	}
214 
215 	public String getEntryClass() {
216 		return entryClass;
217 	}
218 
219 	public String getMode() {
220 		return mode;
221 	}
222 
223 	public String getOwner() {
224 		return owner;
225 	}
226 
227 	public String getGroup() {
228 		return group;
229 	}
230 
231 	
232 	public Integer getMajor() {
233 		return major;
234 	}
235 
236 	public Integer getMinor() {
237 		return minor;
238 	}
239 
240 	public Long getModtime() {
241 		return modtime;
242 	}
243 
244 	public void setModtime(Long modtime) {
245 		this.modtime = modtime;
246 	}
247 
248 	public Long getChecksum() {
249 		return checksum;
250 	}
251 
252 	public void setChecksum(Long checksum) {
253 		this.checksum = checksum;
254 	}
255 
256 	public Long getFilesize() {
257 		return filesize;
258 	}
259 
260 	public void setFilesize(Long filesize) {
261 		this.filesize = filesize;
262 	}
263 
264 	/**
265 	 * Compares this object with the given {@link AbstractPkgMapEntry} using the {@ink #entryClass}
266 	 * or the {@link #entryPath} if the classes are equal. Needed for sorting the pkgmap entries.
267 	 * <p>
268 	 * As defined in the sun docs:
269 	 * &quot;The package objects in pkgmap are listed in alphabetical order by class and by path name to
270 	 * reduce the time it takes to install the package.&quot;
271 	 * 
272 	 * @see java.lang.Comparable#compareTo(java.lang.Object)
273 	 */
274 	public int compareTo(AbstractPkgMapEntry o) {
275 		int classCompare = StringUtil.compare(this.entryClass, o.getEntryClass());
276 		if(classCompare != 0)
277 			return classCompare;
278 		
279 		int pathCompare = StringUtil.compare(this.entryPath, o.getEntryPath());
280 		return pathCompare;
281 	}
282 
283 	@Override
284 	public int hashCode() {
285 		final int prime = 31;
286 		int result = 1;
287 		result = prime * result
288 				+ ((checksum == null) ? 0 : checksum.hashCode());
289 		result = prime * result
290 				+ ((entryClass == null) ? 0 : entryClass.hashCode());
291 		result = prime * result
292 				+ ((entryPath == null) ? 0 : entryPath.hashCode());
293 		result = prime * result
294 				+ ((entryPathSource == null) ? 0 : entryPathSource.hashCode());
295 		result = prime * result
296 				+ ((filesize == null) ? 0 : filesize.hashCode());
297 		result = prime * result + ((group == null) ? 0 : group.hashCode());
298 		result = prime * result + ((major == null) ? 0 : major.hashCode());
299 		result = prime * result + ((minor == null) ? 0 : minor.hashCode());
300 		result = prime * result + ((mode == null) ? 0 : mode.hashCode());
301 		result = prime * result + ((modtime == null) ? 0 : modtime.hashCode());
302 		result = prime * result + ((owner == null) ? 0 : owner.hashCode());
303 		result = prime * result + ((part == null) ? 0 : part.hashCode());
304 		result = prime * result + ((type == null) ? 0 : type.hashCode());
305 		return result;
306 	}
307 
308 	@Override
309 	public boolean equals(Object obj) {
310 		return this.equals(obj, false);
311 	}
312 
313 	public boolean equals(Object obj, boolean ignoreLastModified)
314 	{
315 		if (this == obj)
316 			return true;
317 		if (obj == null)
318 			return false;
319 		if (getClass() != obj.getClass())
320 			return false;
321 		final AbstractPkgMapEntry other = (AbstractPkgMapEntry) obj;
322 
323 		if(!ObjectUtils.areObjectsEqual(this.checksum, other.checksum))
324 			return false;
325 
326 		if(!ObjectUtils.areObjectsEqual(this.entryClass, other.entryClass))
327 			return false;
328 
329 		if(!ObjectUtils.areObjectsEqual(this.entryPath, other.entryPath))
330 			return false;
331 
332 		if(!ObjectUtils.areObjectsEqual(this.entryPathSource, other.entryPathSource))
333 			return false;
334 		
335 		if(!ObjectUtils.areObjectsEqual(this.filesize, other.filesize))
336 			return false;
337 		
338 		if(!ObjectUtils.areObjectsEqual(this.group, other.group))
339 			return false;
340 		
341 		if(!ObjectUtils.areObjectsEqual(this.major, other.major))
342 			return false;
343 		
344 		if(!ObjectUtils.areObjectsEqual(this.minor, other.minor))
345 			return false;
346 		
347 		if(!ObjectUtils.areObjectsEqual(this.mode, other.mode))
348 			return false;
349 		
350 		if(!ObjectUtils.areObjectsEqual(this.entryPath, other.entryPath))
351 			return false;
352 		
353 		if(!ignoreLastModified) {
354 			if(!ObjectUtils.areObjectsEqual(this.modtime, other.modtime))
355 				return false;
356 		}
357 		
358 		if(!ObjectUtils.areObjectsEqual(this.owner, other.owner))
359 			return false;
360 		
361 		if(!ObjectUtils.areObjectsEqual(this.part, other.part))
362 			return false;
363 		
364 		if(!ObjectUtils.areObjectsEqual(this.type, other.type))
365 			return false;
366 
367 		return true;
368 	}
369 
370 	
371 	@Override
372 	public String toString()
373 	{
374 		StringBuffer sb = new StringBuffer();
375 		sb.append("part=").append(part);
376 		sb.append(",type=").append(type);
377 		sb.append(",entryPath=").append(entryPath);
378 		sb.append(",entryPathSource=").append(entryPathSource);
379 		sb.append(",fileClass=").append(entryClass);
380 		sb.append(",major=").append(major);
381 		sb.append(",minor=").append(minor);
382 		sb.append(",mode=").append(mode);
383 		sb.append(",owner=").append(owner);
384 		sb.append(",group=").append(group);
385 		return sb.toString();
386 	}
387 	
388 }