001: /*
002: * Copyright 2005 Paul Hinds
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.tp23.antinstaller.antmod.taskdefs;
017:
018: import java.io.IOException;
019: import java.io.InputStream;
020: import java.util.Properties;
021:
022: import org.apache.tools.ant.BuildException;
023: import org.apache.tools.ant.Task;
024: import org.tp23.antinstaller.InstallException;
025: import org.tp23.antinstaller.InstallerContext;
026: import org.tp23.antinstaller.antmod.RuntimeLauncher;
027: import org.tp23.antinstaller.runtime.IfPropertyHelper;
028:
029: /**
030: * usage:
031: * <antinstaller-property name="property.name" value="myValue"/>
032: * <br />
033: * or:
034: * <antinstaller-property resource="/resources/my.props"/>
035: * @author teknopaul
036: *
037: */
038: public final class PropertyTask extends Task {
039:
040: private String name;
041: private String value;
042: private String resource;
043: private String ifProperty;
044:
045: /**
046: * Ant Tasks must have a noargs constructor
047: */
048: public PropertyTask() {
049: }
050:
051: /**
052: * the "main" method
053: */
054: public void execute() throws BuildException {
055: if (resource != null) {
056: executeResource();
057: } else {
058: if (name == null || value == null) {
059: throw new BuildException(
060: "either resource or (name and value) can not be null for antinstaller-property task");
061: }
062: executePropVal();
063: }
064: }
065:
066: private void executePropVal() throws BuildException {
067: InstallerContext ctx = (InstallerContext) getProject()
068: .getReference(RuntimeLauncher.CONTEXT_REFERENCE);
069: IfPropertyHelper helper = new IfPropertyHelper(ctx);
070: try {
071: if (helper.ifProperty(ifProperty, ctx.getInstaller()
072: .getResultContainer())) {
073: ctx.log("setting property: name=" + name + ", value="
074: + value);
075: ctx.getInstaller().getResultContainer().setProperty(
076: name, value);
077: }
078: } catch (InstallException e) {
079: ctx.log(ctx.getInstaller().isVerbose(), e);
080: throw new BuildException(
081: "Error parsing ifProperty condition for antinstaller-property "
082: + ifProperty);
083: }
084: }
085:
086: // FindBugs - this class should stay final since getResourceAsStream() may not work in subclasses
087: private void executeResource() throws BuildException {
088: InstallerContext ctx = (InstallerContext) getProject()
089: .getReference(RuntimeLauncher.CONTEXT_REFERENCE);
090: InputStream is = this .getClass().getResourceAsStream(resource);
091: if (is == null) {
092: ctx.log("Can not find resource: " + resource);
093: throw new BuildException("Can not find resource: "
094: + resource);
095: }
096: try {
097: Properties props = new Properties();
098: props.load(is);
099: is.close();
100: if (ctx.getInstaller().isVerbose()) {
101: ctx.log("loaded properties: " + props.size());
102: }
103:
104: ctx.getInstaller().getResultContainer().getAllProperties()
105: .putAll(props);
106: } catch (IOException e) {
107: ctx.log("Can not load resource: " + resource);
108: throw new BuildException("Can not load resource: "
109: + resource);
110: }
111: }
112:
113: public String getName() {
114: return name;
115: }
116:
117: public void setName(String name) {
118: this .name = name;
119: }
120:
121: public String getValue() {
122: return value;
123: }
124:
125: public void setValue(String value) {
126: this .value = value;
127: }
128:
129: public String getResource() {
130: return resource;
131: }
132:
133: public void setResource(String resource) {
134: this .resource = resource;
135: }
136:
137: public String getIfProperty() {
138: return ifProperty;
139: }
140:
141: public void setIfProperty(String ifProperty) {
142: this.ifProperty = ifProperty;
143: }
144:
145: }
|