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: */
018:
019: /* $Id: CopyJavaSourcesTask.java 416059 2006-06-21 18:27:27Z andreas $ */
020:
021: package org.apache.lenya.cms.ant;
022:
023: import java.io.File;
024: import java.io.FileInputStream;
025: import java.io.FileNotFoundException;
026: import java.io.FileOutputStream;
027: import java.io.FilenameFilter;
028: import java.io.IOException;
029: import java.io.InputStream;
030: import java.io.OutputStream;
031: import java.io.PrintWriter;
032: import java.io.StringWriter;
033: import java.util.StringTokenizer;
034:
035: import org.apache.tools.ant.BuildException;
036: import org.apache.tools.ant.Project;
037: import org.apache.tools.ant.Task;
038: import org.apache.tools.ant.types.Path;
039:
040: /**
041: * Task to copy java sources.
042: */
043: public class CopyJavaSourcesTask extends Task {
044:
045: private Path pubsRootDirs;
046: private String javaDir;
047: private String buildDir;
048:
049: /**
050: * @see org.apache.tools.ant.Task#execute()
051: */
052: public void execute() throws BuildException {
053: int numberOfDirectoriesCreated = 0;
054: int numberOfFilesCopied = 0;
055: TwoTuple twoTuple = new TwoTuple(numberOfDirectoriesCreated,
056: numberOfFilesCopied);
057:
058: String translatedBuildDir = Project
059: .translatePath(this .buildDir);
060: File absoluteBuildDir = null;
061: if (translatedBuildDir != null
062: && translatedBuildDir.startsWith(File.separator)) {
063: absoluteBuildDir = new File(translatedBuildDir);
064: } else {
065: absoluteBuildDir = new File(getProject().getBaseDir(),
066: translatedBuildDir);
067: }
068:
069: StringTokenizer st = new StringTokenizer(this .pubsRootDirs
070: .toString(), File.pathSeparator);
071:
072: while (st.hasMoreTokens()) {
073: String pubsRootDir = st.nextToken();
074:
075: File path = new File(pubsRootDir);
076:
077: if (path.isDirectory()) {
078: // In the case of a publication
079: if (new File(path, "/config/publication.xml").isFile()) {
080: copyContentOfDir(new File(path, this .javaDir),
081: absoluteBuildDir, twoTuple,
082: new JavaFilenameFilter(), this );
083: // In the case of a module
084: } else if (new File(path, "/config/module.xml")
085: .isFile()) {
086: copyContentOfDir(new File(path, this .javaDir),
087: absoluteBuildDir, twoTuple,
088: new JavaFilenameFilter(), this );
089: } else {
090: // FIXME: Look for publications defined by the file "publication.xml" or modules
091: // defined by the file "module.xml"
092: String[] pubs = path.list();
093: if (pubs != null) {
094: for (int i = 0; i < pubs.length; i++) {
095: File pubJavaDir = new File(path, new File(
096: pubs[i], this .javaDir).toString());
097:
098: copyContentOfDir(pubJavaDir,
099: absoluteBuildDir, twoTuple,
100: new JavaFilenameFilter(), this );
101: }
102: }
103: }
104: } else {
105: throw new BuildException("No such directory: " + path);
106: }
107: }
108:
109: numberOfDirectoriesCreated = twoTuple.x;
110: numberOfFilesCopied = twoTuple.y;
111: log("Copying " + numberOfDirectoriesCreated
112: + " directories to " + absoluteBuildDir,
113: Project.MSG_INFO);
114: log("Copying " + numberOfFilesCopied + " files to "
115: + absoluteBuildDir, Project.MSG_INFO);
116: }
117:
118: /**
119: * Copies the directory "source" into the directory "destination"
120: * @param source The source directory
121: * @param destination The destination directory
122: * @param twoTuple The twoTuple to use
123: * @param filenameFilter The filename filter to apply
124: * @param client The client task
125: */
126: public static void copyDir(File source, File destination,
127: TwoTuple twoTuple, FilenameFilter filenameFilter,
128: Task client) {
129: File actualDestination = new File(destination, source.getName());
130: actualDestination.mkdirs();
131: copyContentOfDir(source, actualDestination, twoTuple,
132: filenameFilter, client);
133: }
134:
135: /**
136: * Copies the content of a directory into another directory
137: * @param source The source directory
138: * @param destination The destination directory
139: * @param twoTuple The twoTuple to use
140: * @param filenameFilter The filename filter to use
141: * @param client The client task
142: */
143: public static void copyContentOfDir(File source, File destination,
144: TwoTuple twoTuple, FilenameFilter filenameFilter,
145: Task client) {
146: if (source.isDirectory()) {
147: String[] files;
148:
149: if (filenameFilter != null) {
150: files = source.list(filenameFilter);
151: } else {
152: files = source.list();
153: }
154:
155: for (int i = 0; i < files.length; i++) {
156: File file = new File(source, files[i]);
157:
158: if (file.isFile()) {
159: copyFile(file, new File(destination, files[i]),
160: twoTuple, client, false);
161: } else if (file.isDirectory()) {
162: copyContentOfDir(file, new File(destination,
163: files[i]), twoTuple, filenameFilter, client);
164: } else {
165: client.log(
166: "CopyJavaSourcesTask.copyContentOfDir(): Neither file nor directory: "
167: + file, Project.MSG_ERR);
168: }
169: }
170: }
171: }
172:
173: /**
174: * Copies the content of a file into another file
175: * @param source The source file (not a directory!)
176: * @param destination File (not a directory!)
177: * @param twoTuple The twoTuple to use
178: * @param client The client task
179: */
180: public static void copyFile(File source, File destination,
181: TwoTuple twoTuple, Task client, boolean force) {
182: if (source.isFile()) {
183: File parentDest = new File(destination.getParent());
184:
185: if (!parentDest.exists()) {
186: parentDest.mkdirs();
187:
188: int numberOfDirectoriesCreated = twoTuple.x;
189: numberOfDirectoriesCreated++;
190: twoTuple.x = numberOfDirectoriesCreated;
191: }
192:
193: if (destination.isFile()) {
194: if (destination.lastModified() > source.lastModified()
195: && !force) {
196: return;
197: }
198: }
199:
200: byte[] buffer = new byte[1024];
201: int bytesRead = -1;
202: InputStream in = null;
203: OutputStream out = null;
204:
205: try {
206: in = new FileInputStream(source);
207: out = new FileOutputStream(destination);
208:
209: while ((bytesRead = in.read(buffer)) >= 0) {
210: out.write(buffer, 0, bytesRead);
211: }
212: } catch (final FileNotFoundException e) {
213: StringWriter writer = new StringWriter();
214: e.printStackTrace(new PrintWriter(writer));
215: client.log("Exception caught: " + writer.toString());
216: } catch (final IOException e) {
217: StringWriter writer = new StringWriter();
218: e.printStackTrace(new PrintWriter(writer));
219: client.log("Exception caught: " + writer.toString());
220: } finally {
221: try {
222: if (out != null) {
223: out.close();
224: }
225: if (in != null) {
226: in.close();
227: }
228: } catch (final IOException e1) {
229: StringWriter writer = new StringWriter();
230: e1.printStackTrace(new PrintWriter(writer));
231: client.log("Exception closing stream: "
232: + writer.toString());
233: }
234: }
235:
236: int numberOfFilesCopied = twoTuple.y;
237: numberOfFilesCopied++;
238: twoTuple.y = numberOfFilesCopied;
239:
240: } else {
241: client.log("No such file: " + source, Project.MSG_ERR);
242: }
243: }
244:
245: /**
246: * Set the root publication root directories
247: * @param _pubsRootDirs The root directories
248: */
249: public void setPubsRootDirs(Path _pubsRootDirs) {
250: this .pubsRootDirs = _pubsRootDirs;
251: }
252:
253: /**
254: * Set the java directory
255: * @param _javaDir The java directory
256: */
257: public void setJavaDir(String _javaDir) {
258: this .javaDir = _javaDir;
259: }
260:
261: /**
262: * Set the build directory
263: * @param _buildDir The build directory
264: */
265: public void setBuildDir(String _buildDir) {
266: this.buildDir = _buildDir;
267: }
268: }
|