1
16package org.tp23.antinstaller.antmod.taskdefs;
17
18import java.io.IOException;
19import java.io.InputStream;
20import java.util.Properties;
21
22import org.apache.tools.ant.BuildException;
23import org.apache.tools.ant.Task;
24import org.tp23.antinstaller.InstallerContext;
25import org.tp23.antinstaller.antmod.RuntimeLauncher;
26
35public final class PropertyTask extends Task {
36
37 private String name;
38 private String value;
39 private String resource;
40
43 public PropertyTask(){
44 }
45
46
49 public void execute() throws BuildException {
50 if(resource != null){
51 executeResource();
52 }
53 else {
54 if(name == null || value == null) {
55 throw new BuildException("either resource or (name and value) can not be null for antinstaller-property task");
56 }
57 executePropVal();
58 }
59 }
60
61 private void executePropVal(){
62 InstallerContext ctx = (InstallerContext)getProject().getReference(RuntimeLauncher.CONTEXT_REFERENCE);
63 ctx.log("setting property: name=" + name + ", value=" + value);
64 ctx.getInstaller().getResultContainer().setProperty(name, value);
65 }
66
67 private void executeResource() throws BuildException{
69 InstallerContext ctx = (InstallerContext)getProject().getReference(RuntimeLauncher.CONTEXT_REFERENCE);
70 InputStream is = this.getClass().getResourceAsStream(resource);
71 if(is == null){
72 ctx.log("Can not find resource: " + resource);
73 throw new BuildException("Can not find resource: " + resource);
74 }
75 try {
76 Properties props = new Properties();
77 props.load(is);
78 is.close();
79 if(ctx.getInstaller().isVerbose()){
80 ctx.log("loaded properties: " + props.size());
81 }
82
83 ctx.getInstaller().getResultContainer().getAllProperties().putAll(props);
84 } catch (IOException e) {
85 ctx.log("Can not load resource: " + resource);
86 throw new BuildException("Can not load resource: " + resource);
87 }
88 }
89
90 public String getName() {
91 return name;
92 }
93
94 public void setName(String name) {
95 this.name = name;
96 }
97
98 public String getValue() {
99 return value;
00 }
01
02 public void setValue(String value) {
03 this.value = value;
04 }
05
06 public String getResource() {
07 return resource;
08 }
09
10 public void setResource(String resource) {
11 this.resource = resource;
12 }
13
14}
15