001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.ant;
027:
028: import org.apache.tools.ant.*;
029:
030: import java.io.*;
031:
032: import java.util.*;
033:
034: public class ConditionalTasks extends Task implements TaskContainer {
035: private ArrayList tasks = new ArrayList();
036: private String dirs;
037: private String files;
038: private String name;
039: private String family;
040:
041: public ConditionalTasks() {
042: }
043:
044: public void setFamily(String family) {
045: this .family = family;
046: }
047:
048: public void setDirs(String dirs) {
049: this .dirs = dirs;
050: }
051:
052: public void setFiles(String files) {
053: this .files = files;
054: }
055:
056: public void setName(String name) {
057: this .name = name;
058: }
059:
060: public void addTask(Task task) {
061: tasks.add(tasks.size(), task);
062: }
063:
064: public void execute() {
065: if ((dirs == null) && (files == null)) {
066: throw new BuildException(
067: "ConditionalTasks: You must supply at least one of either the files or dirs properties");
068: }
069:
070: if (name == null) {
071: throw new BuildException(
072: "ConditionalTasks: You must supply a name for these conditional tasks!");
073: }
074:
075: log("Verifying conditions for " + name);
076:
077: if (family != null) {
078: StringTokenizer tokenizer = new StringTokenizer(dirs, ",");
079: boolean familyMatch = false;
080:
081: while (tokenizer.hasMoreElements() && !familyMatch) {
082: String condition = (String) tokenizer.nextElement();
083:
084: if (condition.equals(family)) {
085: familyMatch = true;
086: }
087: }
088:
089: if (!familyMatch) {
090: log("ConditionalTasks: OS Family '" + family
091: + "' does not match; " + name
092: + " will not be performed");
093:
094: return;
095: }
096: }
097:
098: File basedir = getProject().getBaseDir();
099:
100: if (dirs != null) {
101: StringTokenizer tokenizer = new StringTokenizer(dirs, ",");
102: File f;
103:
104: while (tokenizer.hasMoreElements()) {
105: String condition = (String) tokenizer.nextElement();
106: f = new File(basedir, condition);
107:
108: if (!f.exists()) {
109: f = new File(condition);
110:
111: if (!f.exists()) {
112: log("ConditionalTasks: Directory '" + condition
113: + "' does not exist; " + name
114: + " will not be performed");
115:
116: return;
117: }
118: }
119: }
120: }
121:
122: if (files != null) {
123: StringTokenizer tokenizer = new StringTokenizer(files, ",");
124: File f;
125:
126: while (tokenizer.hasMoreElements()) {
127: String condition = (String) tokenizer.nextElement();
128: f = new File(basedir, condition);
129:
130: if (!f.exists()) {
131: log("ConditionalTasks: File '" + condition
132: + "' does not exist; " + name
133: + " will not be performed");
134:
135: return;
136: }
137: }
138: }
139:
140: System.out.println("Executing Conditional Tasks");
141:
142: Iterator it = tasks.iterator();
143: Task task;
144:
145: while (it.hasNext()) {
146: task = (Task) it.next();
147: task.setProject(getProject());
148: task.setOwningTarget(getOwningTarget());
149: task.setLocation(getLocation());
150: task.perform();
151: }
152: }
153: }
|