1 /* 
2  * Copyright 2005 Paul Hinds
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
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/**
27 * usage: 
28 * <antinstaller-property name="property.name" value="myValue"/>
29 * <br />
30 * or: 
31 * &lt;antinstaller-property resource="/resources/my.props"/&gt;
32 * @author teknopaul
33 *
34 */
35public final class PropertyTask extends Task {
36    
37    private String name;
38    private String value;
39    private String resource;
40    /**
41     * Ant Tasks must have a noargs constructor
42     */
43    public PropertyTask(){
44    }
45
46    /**
47     * the "main" method
48     */
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    // FindBugs - this class should stay final since getResourceAsStream() may not work in subclasses
68    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