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
17
18
19
20
21
22
23
24
25
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
48
49 private String pkg;
50 private String name;
51 private String version;
52
53
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
72
73
74
75
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
83
84
85
86
87
88
89
90
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
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
239
240
241
242 public void writeContent(Writer writer) throws IOException
243 {
244
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
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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324 }