View Javadoc

1   package net.sf.jpkgmk;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.io.Writer;
6   import java.util.ArrayList;
7   import java.util.Iterator;
8   import java.util.List;
9   
10  import net.sf.jpkgmk.util.StringUtil;
11  
12  import org.apache.commons.logging.Log;
13  import org.apache.commons.logging.LogFactory;
14  
15  /**
16   * Object representation for a pkginfo file.
17   * <p>
18   * For details refer to <a href="http://docs.sun.com/app/docs/doc/817-0683/6mgff29br?a=view">
19   * http://docs.sun.com/app/docs/doc/817-0683/6mgff29br?a=view</a>.
20   * </p>
21   * 
22   * @author gommma (gommma AT users.sourceforge.net)
23   * @author Last changed by: $Author: gommma $
24   * @version $Revision: 2 $ $Date: 2008-08-20 21:14:19 +0200 (Mi, 20 Aug 2008) $
25   * @since 1.0
26   */
27  public class PkgInfo extends AbstractFileCreatorAdapter
28  {
29  
30  	public static final String PKG_INFO = "pkginfo";
31  
32  	public static final String CATEGORY_APPLICATION = "application";
33  	public static final String CATEGORY_SYSTEM = "system";
34  	
35  	public static final String LINE_SEPARATOR = System.getProperty("line.separator");
36  
37  	static final String VAR_VENDOR = "VENDOR";
38  	static final String VAR_CATEGORY = "CATEGORY";
39  	static final String VAR_BASEDIR = "BASEDIR";
40  	static final String VAR_DESC = "DESC";
41  	static final String VAR_ARCH = "ARCH";
42  	static final String VAR_VERSION = "VERSION";
43  	static final String VAR_NAME = "NAME";
44  	static final String VAR_PKG = "PKG";
45  
46  	/**
47  	 * Package name (up to 10 characters) - is required
48  	 */
49  	private String pkg;
50  	private String name;
51  	private String version;
52  	/**
53  	 * Solaris architecture (sparc/x86) - is required
54  	 */
55  	private String arch;
56  	private String desc;
57  	private String pkgInfoBasedir = "/";
58  	private String category;
59  	private String vendor = "unknown";
60  	private List<String> optionList;
61  	
62  	private Log log = LogFactory.getLog(PkgInfo.class);
63  	
64  	
65  	public PkgInfo()
66  	{
67  		
68  	}
69  	
70  	/**
71  	 * Constructor taking the mandatory parameters for this class
72  	 * @param pkg
73  	 * @param name
74  	 * @param version
75  	 * @param arch
76  	 */
77  	public PkgInfo(String pkg, String name, String version, String arch, String category) {
78  		this(pkg, name, version, arch, category, null, null, null, null);
79  	}
80  	
81  	/**
82  	 * @param pkg
83  	 * @param name
84  	 * @param version
85  	 * @param arch
86  	 * @param category
87  	 * @param desc
88  	 * @param basedir
89  	 * @param vendor
90  	 * @param optionList addition options, for example "SUMW_PRODNAME=XyOS", "SUMW_PRODVERS=5.10"
91  	 */
92  	public PkgInfo(String pkg, String name, String version, String arch, String category, 
93  			String desc, String basedir, String vendor, List<String> optionList) {
94  		super();
95  		
96  		// Set the values
97  		setPkgInternal(pkg);
98  		setNameInternal(name);
99  		this.version = version;
100 		this.arch = arch;
101 		this.category = category;
102 
103 		
104 		if(!StringUtil.isNullOrEmpty(desc)) {
105 			this.desc = desc;
106 		}
107 		if(!StringUtil.isNullOrEmpty(basedir)) {
108 			this.pkgInfoBasedir = basedir;
109 		}
110 		if(!StringUtil.isNullOrEmpty(vendor)) {
111 			this.vendor = vendor;
112 		}
113 		this.optionList = optionList;
114 	}
115 
116 
117 	public void validate()
118 	{
119 		if(pkg == null) {
120 			throw new NullPointerException("The parameter 'pkg' must not be null");
121 		}
122 		if(name == null) {
123 			throw new NullPointerException("The parameter 'name' must not be null");
124 		}
125 		if(version == null) {
126 			throw new NullPointerException("The parameter 'version' must not be null");
127 		}
128 		if(arch == null) {
129 			throw new NullPointerException("The parameter 'arch' must not be null");
130 		}
131 		if (category == null) {
132 			throw new NullPointerException("The parameter 'category' must not be null");
133 		}
134 	}
135 
136 	
137 	public String getPkg() {
138 		return pkg;
139 	}
140 
141 	public void setPkg(String pkg) {
142 		setPkgInternal(pkg);
143 	}
144 	
145 	private void setPkgInternal(String pkg) {
146 		if(pkg.length() > 32) {
147 			throw new IllegalArgumentException("The given package abbreviation '" + pkg + "' exceeds the maximum allowed length of 32.");
148 		}
149 		this.pkg = pkg;
150 	}
151 	
152 	
153 	public String getVersion() {
154 		return version;
155 	}
156 
157 	
158 	public String getName() {
159 		return name;
160 	}
161 
162 	public void setName(String name) {
163 		this.setNameInternal(name);
164 	}
165 
166 	private void setNameInternal(String name) {
167 		if(name.length() > 256) {
168 			throw new IllegalArgumentException("The given package name '" + name + "' exceeds the maximum allowed length of 256.");
169 		}
170 		this.name = name;
171 	}
172 
173 	public String getArch() {
174 		return arch;
175 	}
176 
177 	
178 	public String getDesc() {
179 		return desc;
180 	}
181 
182 	public String getPkgInfoBasedir() {
183 		return pkgInfoBasedir;
184 	}
185 
186 	public String getCategory() {
187 		return category;
188 	}
189 
190 	public String getVendor() {
191 		return vendor;
192 	}
193 
194 	public List<String> getOptionList() {
195 		return optionList;
196 	}
197 	public void setOptionList(List<String> optionList) {
198 		this.optionList = optionList;
199 	}
200 	public void addOption(String option) {
201 		if(this.optionList == null) {
202 			this.optionList = new ArrayList<String>();
203 		}
204 		this.optionList.add(option);
205 	}
206 
207 	public void setVersion(String version) {
208 		this.version = version;
209 	}
210 
211 	public void setArch(String arch) {
212 		this.arch = arch;
213 	}
214 
215 	public void setDesc(String desc) {
216 		this.desc = desc;
217 	}
218 
219 	public void setPkgInfoBasedir(String pkgInfoBasedir) {
220 		this.pkgInfoBasedir = pkgInfoBasedir;
221 	}
222 
223 	public void setCategory(String category) {
224 		this.category = category;
225 	}
226 
227 	public void setVendor(String vendor) {
228 		this.vendor = vendor;
229 	}
230 
231 	@Override
232 	public FileHandler getFileHandler(File targetDir) {
233 		DefaultFileHandler fileCreator = new DefaultFileHandler(targetDir, PKG_INFO);
234 		return fileCreator;
235 	}
236 
237 	/**
238 	 * write pkginfo
239 	 * @param target
240 	 * @throws IOException 
241 	 */
242 	public void writeContent(Writer writer) throws IOException 
243 	{
244 		// Info message
245 		log.info("creating pkginfo: \t" + this.toString());
246 		String pkgInfo = createPkgInfo();
247 		writer.write(pkgInfo);
248 		writer.flush();
249 		log.info("done.");
250 	}
251 
252 	private String createPkgInfo() 
253 	{
254 		StringBuffer target = new StringBuffer();
255 		// creating pkginfo
256 		target.append(VAR_PKG).append("=").append(pkg).append(LINE_SEPARATOR);
257 		target.append(VAR_NAME).append("=").append(name).append(LINE_SEPARATOR);
258 		target.append(VAR_VERSION).append("=").append(version).append(LINE_SEPARATOR);
259 		target.append(VAR_ARCH).append("=").append(arch).append(LINE_SEPARATOR);
260 		if(this.desc != null) {
261 			target.append(VAR_DESC).append("=").append(desc).append(LINE_SEPARATOR);
262 		}
263 		if(this.pkgInfoBasedir != null) {
264 			target.append(VAR_BASEDIR).append("=").append(pkgInfoBasedir).append(LINE_SEPARATOR);
265 		}
266 		if(this.category != null) {
267 			target.append(VAR_CATEGORY).append("=").append(category).append(LINE_SEPARATOR);
268 		}
269 		if(this.vendor != null) {
270 			target.append(VAR_VENDOR).append("=").append(vendor).append(LINE_SEPARATOR);
271 		}
272 		
273 		// add options
274 		if(this.optionList != null) {
275 			for(Iterator<String> optionIter = this.optionList.iterator(); optionIter.hasNext() ;) {
276 				String option = optionIter.next();
277 				target.append(option).append(LINE_SEPARATOR);
278 			}
279 		}
280 		return target.toString();
281 	}
282 
283 	public String generateFilename() {
284 		String name = this.getPkg() + "_" + this.getVersion();
285 		return name;
286 	}
287 
288 	@Override
289 	public String toString()
290 	{
291 		StringBuffer sb = new StringBuffer();
292 		sb.append("pkg=").append(pkg);
293 		sb.append(",name=").append(name);
294 		sb.append(",version=").append(version);
295 		sb.append(",arch=").append(arch);
296 		sb.append(",desc=").append(desc);
297 		sb.append(",basedir=").append(pkgInfoBasedir);
298 		sb.append(",category=").append(category);
299 		sb.append(",vendor=").append(vendor);
300 		return sb.toString();
301 	}
302 	
303 	
304 //	private void printUsage()
305 //	{
306 //		StringBuffer sb = new StringBuffer();
307 //		sb.append("Usage of $0");
308 //		sb.append("> $0 -p <pkg> -n <name> -v <version> -a <arch> [-c <category>]");
309 //		sb.append("             [-b <basedir>] [-d <description>] [-vd <vendor>] [-o <flag>...]");
310 //		sb.append("[Details of Option]");
311 //		sb.append(  "-p  <pkg>          : package name (ex. SUMWgcc)");
312 //		sb.append(  "-n  <name>         : application name (ex. gcc)");
313 //		sb.append(  "-v  <version>      : application version (ex. 4.2.0)");
314 //		sb.append(  "-a  <arch>         : architecuture, i86pc,sum4u...");
315 //		sb.append(  "-c  <category>     : category of the application (ex. system, application, GNOME2)");
316 //		sb.append(  "                     (default = appilcation)");
317 //		sb.append(  "-b  <basedir>      : base directory of the application (default = /)");
318 //		sb.append(  "-d  <description>  : package description");
319 //		sb.append(  "-vd <vendor>       : vendor name (default = unknown)");
320 //		sb.append(  "-o  <flag>         : if you want to add other flags, please use it");
321 //	    sb.append(  "                     (ex. -o \"SUMW_PRODNAME=XyOS\" -o \"SUMW_PRODVERS=5.10\"");
322 //	}
323 
324 }