001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package taskdefs;
038:
039: import java.io.BufferedReader;
040: import java.io.BufferedWriter;
041: import java.io.File;
042: import java.io.FileNotFoundException;
043: import java.io.FileReader;
044: import java.io.FileWriter;
045: import java.io.IOException;
046:
047: import org.apache.tools.ant.BuildException;
048: import org.apache.tools.ant.DirectoryScanner;
049: import org.apache.tools.ant.Project;
050: import org.apache.tools.ant.taskdefs.Delete;
051: import org.apache.tools.ant.taskdefs.Expand;
052: import org.apache.tools.ant.taskdefs.Taskdef;
053: import org.apache.tools.ant.types.FileSet;
054:
055: /**
056: * This Ant task concats manifest information from multiple jar files.
057: *
058: * @author Arun Gupta
059: */
060: public final class ConcatManifest extends Taskdef {
061: private File tempdir;
062: private File manifest;
063: private FileSet fileset;
064:
065: public File getTempdir() {
066: return tempdir;
067: }
068:
069: public void setTempdir(File tempdir) {
070: this .tempdir = tempdir;
071: }
072:
073: public void addConfiguredFileset(FileSet fileset) {
074: this .fileset = fileset;
075: }
076:
077: public File getManifest() {
078: return manifest;
079: }
080:
081: public void setManifest(File manifest) {
082: this .manifest = manifest;
083: }
084:
085: @Override
086: public void execute() throws BuildException {
087: if (!getManifest().isFile())
088: throw new BuildException("manifest must be a file.");
089:
090: Expand expand = newExpand();
091: if (!getTempdir().isDirectory())
092: throw new BuildException("tempdir must be a directory.");
093: expand.setDest(tempdir);
094:
095: final File m = new File(tempdir + File.separator
096: + "META-INF/MANIFEST.MF");
097: Delete delete = newDelete();
098: delete.setFile(m);
099:
100: BufferedWriter bw;
101: try {
102: bw = new BufferedWriter(new FileWriter(getManifest(), true));
103: DirectoryScanner ds = fileset
104: .getDirectoryScanner(this .project);
105: for (String file : ds.getIncludedFiles()) {
106: delete.execute();
107: expand.setSrc(new File(ds.getBasedir() + File.separator
108: + file));
109: expand.execute();
110: writeManifest(bw, file, m);
111: }
112: bw.flush();
113: bw.close();
114: } catch (IOException e) {
115: throw new BuildException(e);
116: }
117: }
118:
119: private void writeManifest(BufferedWriter bw, String file, File m)
120: throws IOException {
121: bw.write("\nName: " + file + "\n");
122: BufferedReader br;
123: try {
124: br = new BufferedReader(new FileReader(m));
125: } catch (FileNotFoundException e) {
126: log("No manifest found for " + file, Project.MSG_WARN);
127:
128: // ignore this exception and continue
129: return;
130: }
131: String line = br.readLine();
132: log("\nManifest for " + file, Project.MSG_DEBUG);
133: while (line != null) {
134: log(line, Project.MSG_DEBUG);
135: bw.write(line + "\n");
136: line = br.readLine();
137: }
138: bw.write("\n");
139: }
140:
141: private Delete newDelete() {
142: Delete delete = new Delete();
143: delete.setProject(this .project);
144: delete.setFailOnError(false);
145: delete.setQuiet(true);
146:
147: return delete;
148: }
149:
150: private Expand newExpand() {
151: Expand expand = new Expand();
152: expand.setProject(this.project);
153:
154: return expand;
155: }
156: }
|