View Javadoc

1   package net.sf.jpkgmk.pkgmap;
2   
3   import java.io.File;
4   import java.io.IOException;
5   
6   import net.sf.jpkgmk.FileHandler;
7   import net.sf.jpkgmk.prototype.Prototype;
8   
9   import org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  
12  /**
13   * Utility that helps creating a PkgMap file from a given prototype.
14   * 
15   * @author gommma (gommma AT users.sourceforge.net)
16   * @author Last changed by: $Author: gommma $
17   * @version $Revision: 2 $ $Date: 2008-08-20 21:14:19 +0200 (Mi, 20 Aug 2008) $
18   * @since 1.0
19   */
20  public class PkgMapBuilder 
21  {
22  	private Log log = LogFactory.getLog(PkgMapBuilder.class);
23  
24  	private PkgMap pkgMap;
25  	private File targetDir;
26  	private Prototype prototype;
27  	
28  
29  	/**
30  	 * Setup a new instance of the builder that helps creating a pkgmap.
31  	 * @param targetDir The target directory in which all artifacts are created
32  	 * @param prototype The prototype that contains information about all artifacts to be included in the package
33  	 */
34  	public PkgMapBuilder(File targetDir, Prototype prototype) 
35  	{
36  		if (targetDir == null) {
37  			throw new NullPointerException(
38  					"The parameter 'targetDir' must not be null");
39  		}
40  		if (prototype == null) {
41  			throw new NullPointerException(
42  					"The parameter 'prototype' must not be null");
43  		}
44  		
45  		this.targetDir = targetDir;
46  		this.prototype = prototype;
47  	}
48  
49  	public PkgMap getPkgMap()
50  	{
51  		return this.pkgMap;
52  	}
53  	
54  	public File getFile(){
55  		FileHandler fileHandler = getPkgMap().getFileHandler(this.targetDir);
56  		return fileHandler.getFile();
57  	}
58  
59  	public File clean() {
60  		getPkgMap().clean(this.targetDir);
61  		return getFile();
62  	}
63  
64  	/**
65  	 * Creates all files to be included in the package by copying them from the original location to the
66  	 * new pkgdir given as <code>targetDir</code> in the constructor. Finally generates a pkgmap file.
67  	 * @return The pkgmap file reference
68  	 * @throws IOException
69  	 */
70  	public File create() throws IOException {
71  		log.debug("<entering> create()");
72  		
73  		if(!this.targetDir.isDirectory()) {
74  			throw new IllegalStateException("The root target dir '" + this.targetDir + "' does not exist.");
75  		}
76  		
77  		if(this.prototype == null) {
78  			throw new IllegalStateException("The 'prototype' must not be null at this stage");
79  		}
80  		
81  		this.pkgMap = this.prototype.createPackage(this.targetDir);
82  		// Finally create the pkgmap file
83  		pkgMap.create(this.targetDir);
84  		return getFile();
85  	}
86  	
87  
88  }