Source code: org/objectstyle/woproject/ant/WOTask.java
1 /* ====================================================================
2 *
3 * The ObjectStyle Group Software License, Version 1.0
4 *
5 * Copyright (c) 2002 The ObjectStyle Group
6 * and individual authors of the software. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. The end-user documentation included with the redistribution, if
21 * any, must include the following acknowlegement:
22 * "This product includes software developed by the
23 * ObjectStyle Group (http://objectstyle.org/)."
24 * Alternately, this acknowlegement may appear in the software itself,
25 * if and wherever such third-party acknowlegements normally appear.
26 *
27 * 4. The names "ObjectStyle Group" and "Cayenne"
28 * must not be used to endorse or promote products derived
29 * from this software without prior written permission. For written
30 * permission, please contact andrus@objectstyle.org.
31 *
32 * 5. Products derived from this software may not be called "ObjectStyle"
33 * nor may "ObjectStyle" appear in their names without prior written
34 * permission of the ObjectStyle Group.
35 *
36 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39 * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This software consists of voluntary contributions made by many
51 * individuals on behalf of the ObjectStyle Group. For more
52 * information on the ObjectStyle Group, please see
53 * <http://objectstyle.org/>.
54 *
55 */
56 package org.objectstyle.woproject.ant;
57
58 import java.io.File;
59 import java.util.ArrayList;
60 import java.util.Enumeration;
61 import java.util.Iterator;
62 import java.util.Vector;
63
64 import org.apache.tools.ant.BuildException;
65 import org.apache.tools.ant.DirectoryScanner;
66 import org.apache.tools.ant.Task;
67 import org.apache.tools.ant.taskdefs.Copy;
68 import org.apache.tools.ant.taskdefs.Jar;
69 import org.apache.tools.ant.taskdefs.Mkdir;
70 import org.apache.tools.ant.types.FileSet;
71
72 /**
73 * A <b>WOTask</b> is a common superclass of WOApplication and WOFramework that
74 * implements common build functionality.
75 *
76 * @author Emily Bache
77 * @author Andrei Adamchik
78 */
79 public abstract class WOTask extends Task {
80
81 protected Vector classes = new Vector();
82 protected String name;
83 protected String destDir;
84 protected String principalClass;
85 protected String jarName;
86 protected Vector resources = new Vector();
87 protected Vector wsresources = new Vector();
88 protected Vector lib = new Vector();
89 protected SubtaskFactory subtaskFactory = new SubtaskFactory(this);
90
91 public void setName(String name) {
92 this.name = name;
93 }
94
95 public String getName() {
96 return name;
97 }
98
99 public void setJarName(String jarName) {
100 this.jarName = jarName;
101 }
102
103 public String getJarName() {
104 if(jarName == null)
105 jarName = getName().toLowerCase();
106 return jarName;
107 }
108
109 public void setPrincipalClass(String principalClass) {
110 this.principalClass = principalClass;
111 }
112
113 public String getPrincipalClass() {
114 return principalClass;
115 }
116
117 public void setDestDir(String destDir) {
118 this.destDir = destDir;
119 }
120
121 public void addClasses(FileSet set) {
122 classes.addElement(set);
123 }
124
125 public void addResources(FileSet set) {
126 resources.addElement(set);
127 }
128
129 public void addLib(FileSet set) {
130 lib.addElement(set);
131 }
132
133
134 public void addWsresources(FileSet set) {
135 wsresources.addElement(set);
136 }
137
138 /**
139 * Returns a location where WOTask is being built up.
140 * For instance the <code>.woa</code> dir or the </code>.framework</code> dir.
141 */
142 protected abstract File taskDir();
143
144 /**
145 * Returns a location where resources should be put.
146 * For instance this can be WOComponents, EOModels etc.
147 */
148 protected abstract File resourcesDir();
149
150
151 /**
152 * Returns a location where web server resources should be put.
153 * WebServerResources are normally images, JavaScript files,
154 * stylesheets, etc.
155 */
156 protected abstract File wsresourcesDir();
157
158 /**
159 * Ensure we have a consistent and legal set of attributes, and set any
160 * internal flags necessary based on different combinations of attributes.
161 *
162 * @throws BuildException if task attributes are inconsistent or missing.
163 */
164 protected void validateAttributes() throws BuildException {
165 if (name == null) {
166 throw new BuildException("'name' attribute is missing.");
167 }
168
169 if (destDir == null) {
170 throw new BuildException("'destDir' attribute is missing.");
171 }
172 }
173
174 protected void createDirectories() throws BuildException {
175 Mkdir mkdir = subtaskFactory.getMkdir();
176
177 File taskDir = taskDir();
178
179 mkdir.setDir(taskDir);
180 mkdir.execute();
181
182 File resourceDir = resourcesDir();
183 mkdir.setDir(resourceDir);
184 mkdir.execute();
185
186 mkdir.setDir(new File(resourceDir, "Java"));
187 mkdir.execute();
188
189 if (hasWs()) {
190 mkdir.setDir(wsresourcesDir());
191 mkdir.execute();
192 }
193 }
194
195 public boolean hasWs() {
196 return wsresources.size() > 0;
197 }
198
199 public boolean hasResources() {
200 return resources.size() > 0;
201 }
202
203
204 public boolean hasClasses() {
205 return classes.size() > 0;
206 }
207
208 protected void jarClasses() throws BuildException {
209 Jar jar = subtaskFactory.getJar();
210 File taskJar =
211 new File(resourcesDir(), "Java" + File.separator + getJarName() + ".jar");
212 jar.setJarfile(taskJar);
213
214 if (hasClasses()) {
215 Enumeration en = classes.elements();
216 while (en.hasMoreElements()) {
217 jar.addFileset((FileSet) en.nextElement());
218 }
219 }
220
221 jar.execute();
222 }
223
224 protected void copyResources() throws BuildException {
225 Copy cp = subtaskFactory.getResourceCopy();
226
227 cp.setTodir(resourcesDir());
228 Enumeration en = resources.elements();
229 while (en.hasMoreElements()) {
230 cp.addFileset((FileSet) en.nextElement());
231 }
232 cp.execute();
233 }
234
235 protected void copyWsresources() throws BuildException {
236 Copy cp = subtaskFactory.getResourceCopy();
237 cp.setTodir(wsresourcesDir());
238
239 Enumeration en = wsresources.elements();
240 while (en.hasMoreElements()) {
241 cp.addFileset((FileSet) en.nextElement());
242 }
243 cp.execute();
244 }
245
246 protected void copyLibs() throws BuildException {
247 Copy cp = subtaskFactory.getResourceCopy();
248 cp.setTodir(new File(resourcesDir(), "Java"));
249
250 Enumeration en = lib.elements();
251 while (en.hasMoreElements()) {
252 cp.addFileset((FileSet) en.nextElement());
253 }
254 cp.execute();
255 }
256
257 protected boolean hasLib() {
258 return lib.size() > 0;
259 }
260
261
262 protected boolean hasJava() {
263 return classes.size() > 0 || lib.size() > 0;
264 }
265
266
267 /**
268 * Returns an Iterator over the file names of the library files
269 * included in the lib nested element.
270 */
271 public Iterator getLibNames() {
272 ArrayList libNames = new ArrayList();
273 Enumeration en = lib.elements();
274 while (en.hasMoreElements()) {
275 FileSet fs = (FileSet) en.nextElement();
276 DirectoryScanner scanner = fs.getDirectoryScanner(project);
277 String[] libs = scanner.getIncludedFiles();
278 for (int i = 0; i < libs.length; i++) {
279 File libFile = new File(libs[i]);
280 libNames.add(libFile.getName());
281 }
282 }
283 return libNames.iterator();
284 }
285
286
287 }