001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.pluto.ant;
018:
019: import java.io.File;
020: import java.util.Collection;
021: import java.util.Iterator;
022: import java.util.LinkedList;
023:
024: import org.apache.pluto.util.UtilityException;
025: import org.apache.pluto.util.assemble.Assembler;
026: import org.apache.pluto.util.assemble.AssemblerConfig;
027: import org.apache.pluto.util.assemble.AssemblerFactory;
028: import org.apache.tools.ant.BuildException;
029: import org.apache.tools.ant.DirectoryScanner;
030: import org.apache.tools.ant.Task;
031: import org.apache.tools.ant.types.FileSet;
032:
033: /**
034: * TODO JavaDoc
035: *
036: * @version 1.0
037: * @since Nov 23, 2005
038: */
039: public class AssembleTask extends Task {
040:
041: /**
042: * Path to the portlet descriptor
043: * (normally <code>WEB-INF/portlet.xml</code>)
044: * <p/>
045: * If <code>webapp</code> is specified, this File will
046: * be resolved relative to <code>webapp</code>
047: */
048: private File portletxml;
049:
050: /**
051: * Path to the unassembled servlet descriptor
052: * (normally <code>WEB-INF/web.xml</code>)
053: * <p/>
054: * If <code>webapp</code> is specified, this File will
055: * be resolved relative to <code>webapp</code>
056: */
057: private File webxml;
058:
059: /**
060: * Path the assembled servlet descriptor will
061: * be written to.
062: */
063: private File destfile;
064:
065: /**
066: * The base directory of the exploded web application to assemble.
067: * If set, <code>webXml</code> and <code>portletXml</code>
068: * will be resolved relative to this directory.
069: */
070: private File webapp;
071:
072: /**
073: * Path to the archive to assemble. EAR and WAR
074: * packaging formats are supported.
075: */
076: private File archive;
077:
078: /**
079: * Destination directory the assembled archives
080: * are written out to.
081: */
082: private File destdir;
083:
084: private final Collection archiveFileSets = new LinkedList();
085:
086: public File getPortletxml() {
087: if (webapp != null)
088: return new File(webapp, "WEB-INF/portlet.xml");
089: return portletxml;
090: }
091:
092: public void setPortletxml(File portletxml) {
093: this .portletxml = portletxml;
094: }
095:
096: public File getWebxml() {
097: if (webapp != null)
098: return new File(webapp, "WEB-INF/web.xml");
099: return webxml;
100: }
101:
102: public void setWebxml(File webxml) {
103: this .webxml = webxml;
104: }
105:
106: public File getDestfile() {
107: if (destfile != null)
108: return destfile;
109: return getWebxml();
110: }
111:
112: public void setDestfile(File destfile) {
113: this .destfile = destfile;
114: }
115:
116: public File getWebapp() {
117: return webapp;
118: }
119:
120: public void setWebapp(File webapp) {
121: this .webapp = webapp;
122: }
123:
124: /**
125: * Note this methods remains to support
126: * backwards compatiblity.
127: *
128: * @deprecated see <code>getArchive()</code>
129: */
130: public File getWar() {
131: return this .archive;
132: }
133:
134: /**
135: * Note this methods remains to support
136: * backwards compatiblity.
137: *
138: * @param war
139: * @deprecated see <code>setArchive(File)</code>
140: */
141: public void setWar(File war) {
142: this .archive = war;
143: }
144:
145: public File getArchive() {
146: return this .archive;
147: }
148:
149: public void setArchive(File archive) {
150: this .archive = archive;
151: }
152:
153: public File getDestdir() {
154: if (destdir == null) {
155: return (archive != null ? archive.getParentFile() : null);
156: }
157: return this .destdir;
158: }
159:
160: public void setDestdir(File destDir) {
161: this .destdir = destDir;
162: }
163:
164: /**
165: * Note this method remains to support
166: * backwards compatiblity.
167: *
168: * @param fileSet
169: * @deprecated use addArchives instead
170: */
171: public void addWars(FileSet fileSet) {
172: this .archiveFileSets.add(fileSet);
173: }
174:
175: public void addArchives(FileSet fileSet) {
176: this .archiveFileSets.add(fileSet);
177: }
178:
179: public void execute() throws BuildException {
180:
181: validateArgs();
182:
183: try {
184: if (this .archiveFileSets.size() > 0) {
185: for (final Iterator fileSetItr = this .archiveFileSets
186: .iterator(); fileSetItr.hasNext();) {
187: final FileSet fileSet = (FileSet) fileSetItr.next();
188: final DirectoryScanner directoryScanner = fileSet
189: .getDirectoryScanner(this .getProject());
190:
191: final File basedir = directoryScanner.getBasedir();
192: final String[] includedFiles = directoryScanner
193: .getIncludedFiles();
194:
195: for (int index = 0; index < includedFiles.length; index++) {
196: AssemblerConfig config = new AssemblerConfig();
197:
198: final File archiveSource = new File(basedir,
199: includedFiles[index]);
200: config.setSource(archiveSource);
201: config.setDestination(getDestdir());
202:
203: this .log("Assembling '" + archiveSource
204: + "' to '" + getDestdir() + "'");
205: Assembler assembler = AssemblerFactory
206: .getFactory().createAssembler(config);
207: assembler.assemble(config);
208: }
209: }
210: } else {
211: AssemblerConfig config = new AssemblerConfig();
212:
213: final File archiveSource = getArchive();
214: if (archiveSource != null) {
215: config.setSource(archiveSource);
216: config.setDestination(getDestdir());
217: this .log("Assembling '" + archiveSource + "' to '"
218: + getDestdir() + "'");
219: } else {
220: config.setPortletDescriptor(getPortletxml());
221: config.setWebappDescriptor(getWebxml());
222: config.setDestination(getDestfile());
223: this .log("Assembling '" + getWebxml() + "' to '"
224: + getDestfile() + "'");
225: }
226:
227: Assembler assembler = AssemblerFactory.getFactory()
228: .createAssembler(config);
229: assembler.assemble(config);
230: }
231: }
232:
233: catch (UtilityException ue) {
234: throw new BuildException(ue);
235: }
236: }
237:
238: private void validateArgs() throws BuildException {
239: //Check if running with webapp arg
240: if (webapp != null) {
241: if (!webapp.exists()) {
242: throw new BuildException("webapp "
243: + webapp.getAbsolutePath() + " does not exist");
244: }
245:
246: if (archive != null) {
247: throw new BuildException(
248: "archive (or war) should not be specified if webapp is specified");
249: }
250: if (this .archiveFileSets.size() > 0) {
251: throw new BuildException(
252: "archive (or wars) should not be specified if webapp is specified");
253: }
254: // TODO check this
255: if (destdir != null) {
256: throw new BuildException(
257: "destfile should not be specified if webapp is specified");
258: }
259:
260: return;
261: }
262:
263: //Check if running with war arg
264: if (archive != null) {
265: if (!archive.exists()) {
266: throw new BuildException("Archive file "
267: + archive.getAbsolutePath() + " does not exist");
268: }
269:
270: if (this .archiveFileSets.size() > 0) {
271: throw new BuildException(
272: "archives (or wars) should not be specified if archive (or war) is specified");
273: }
274: if (webapp != null) {
275: throw new BuildException(
276: "webapp should not be specified if archive (or war) is specified");
277: }
278: if (destfile != null) {
279: throw new BuildException(
280: "destfile should not be specified if archive (or war) is specified");
281: }
282: if (portletxml != null) {
283: throw new BuildException(
284: "portletxml should not be specified if archive (or war) is specified");
285: }
286: if (webxml != null) {
287: throw new BuildException(
288: "webxml should not be specified if archive (or war) is specified");
289: }
290:
291: return;
292: }
293:
294: //Check if running with archives or wars arg
295: if (this .archiveFileSets.size() > 0) {
296: if (archive != null) {
297: throw new BuildException(
298: "archives (or wars) should not be specified if archive (or war) is specified");
299: }
300: if (webapp != null) {
301: throw new BuildException(
302: "webapp should not be specified if archives (or wars) is specified");
303: }
304: if (destfile != null) {
305: throw new BuildException(
306: "destfile should not be specified if archives (or wars) is specified");
307: }
308: if (portletxml != null) {
309: throw new BuildException(
310: "portletxml should not be specified if archive (or wars) is specified");
311: }
312: if (webxml != null) {
313: throw new BuildException(
314: "webxml should not be specified if archives (or wars) is specified");
315: }
316:
317: return;
318: }
319:
320: //Check if running with portletxml && webxml args
321: if (portletxml == null || !portletxml.exists()) {
322: throw new BuildException("portletxml " + portletxml
323: + " does not exist");
324: }
325: if (webxml == null || !webxml.exists()) {
326: throw new BuildException("webxml " + webxml
327: + " does not exist");
328: }
329: if (archive != null) {
330: throw new BuildException(
331: "archive (or war) should not be specified if portletxml and webxml are specified");
332: }
333: if (this .archiveFileSets.size() > 0) {
334: throw new BuildException(
335: "archives (or wars) should not be specified if portletxml and webxml are specified");
336: }
337: if (destdir != null) {
338: // TODO check this
339: throw new BuildException(
340: "destfile should not be specified if portletxml and webxml are aspecified");
341: }
342: }
343: }
|