001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package de.schlund.pfixcore.util;
020:
021: import java.io.File;
022: import java.io.FileInputStream;
023: import java.io.FileOutputStream;
024: import java.io.IOException;
025: import java.util.HashSet;
026: import java.util.List;
027: import java.util.Set;
028:
029: import org.apache.tools.ant.BuildException;
030: import org.apache.tools.ant.Task;
031: import org.w3c.dom.Document;
032: import org.w3c.dom.Node;
033:
034: import de.schlund.pfixxml.util.XPath;
035: import de.schlund.pfixxml.util.Xml;
036:
037: /**
038: * Ant task copying the paths specified using the passthrough option
039: * in a projects.xml file from a source to a destination directory.
040: *
041: * @author Sebastian Marsching <sebastian.marsching@1und1.de>
042: */
043: public class WebappCopyPassthroughHtdocsTask extends Task {
044: private File srcdir;
045:
046: private File destdir;
047:
048: private File projectsxml;
049:
050: private String projectname;
051:
052: public void setSrcdir(File srcdir) {
053: this .srcdir = srcdir;
054: }
055:
056: public void setDestdir(File destdir) {
057: this .destdir = destdir;
058: }
059:
060: public void setProjectsxml(File projectsxml) {
061: this .projectsxml = projectsxml;
062: }
063:
064: public void setProjectname(String projectname) {
065: this .projectname = projectname;
066: }
067:
068: public void execute() throws BuildException {
069: checkParameters();
070:
071: String htdocspath = getHtdocs();
072: File htdocs = new File(htdocspath);
073: if (htdocs.exists() && htdocs.isDirectory()) {
074: // Change srcdir temporarily
075: File temp = srcdir;
076: srcdir = htdocs;
077: copyDirectory("/");
078: srcdir = temp;
079: }
080:
081: Set<String> paths = getPassthroughPaths();
082:
083: for (String path : paths) {
084: System.out.println("Processing passthrough " + path);
085: File pathelement = new File(srcdir, path);
086: if (pathelement.exists() && !pathelement.isHidden()) {
087: if (pathelement.isFile()) {
088: copyFile(path);
089: } else if (pathelement.isDirectory()) {
090: copyDirectory(path);
091: } else {
092: System.out.println("Ignoring " + path);
093: }
094: }
095: }
096: }
097:
098: private void checkParameters() throws BuildException {
099: if (projectsxml == null) {
100: throw new BuildException(
101: "Mandatory attribute \"projectsxml\" is not set!");
102: }
103: if (!projectsxml.exists() || !projectsxml.isFile()) {
104: throw new BuildException("File " + projectsxml
105: + " does not exist!");
106: }
107: if (srcdir == null) {
108: throw new BuildException(
109: "Mandatory attribute \"srcdir\" is not set!");
110: }
111: if (!srcdir.exists() || !srcdir.isDirectory()) {
112: throw new BuildException("Directory " + srcdir
113: + " does not exist!");
114: }
115: if (destdir == null) {
116: throw new BuildException(
117: "Mandatory attribute \"destdir\" is not set!");
118: }
119: if (!destdir.exists() || !destdir.isDirectory()) {
120: throw new BuildException("Directory " + destdir
121: + " does not exist!");
122: }
123: if (projectname == null || projectname.equals("")) {
124: throw new BuildException(
125: "Attribute \"projectname\" has to be set!");
126: }
127: }
128:
129: private Set<String> getPassthroughPaths() throws BuildException {
130: HashSet<String> paths = new HashSet<String>();
131:
132: try {
133: Document doc = Xml.parseMutable(projectsxml);
134: List<Node> nodes = XPath
135: .select(
136: doc,
137: "/projects/project/passthrough/text()|/projects/common/apache/passthrough/text()");
138: for (Node node : nodes) {
139: if (node.getNodeValue().length() > 0)
140: paths.add(node.getNodeValue());
141: }
142: } catch (Exception e) {
143: throw new BuildException("Cannot parse " + projectsxml, e);
144: }
145:
146: return paths;
147: }
148:
149: private String getHtdocs() throws BuildException {
150: try {
151: Document doc = Xml.parseMutable(projectsxml);
152: Node node = XPath.selectNode(doc,
153: "/projects/project[@name='" + projectname
154: + "']/documentroot/text()");
155: if (node != null) {
156: String val = node.getNodeValue();
157: if (val != null && val.length() > 0) {
158: return val;
159: } else {
160: return null;
161: }
162: }
163: return null;
164:
165: } catch (Exception e) {
166: throw new BuildException("Cannot parse " + projectsxml, e);
167: }
168: }
169:
170: private void copyFile(String path) {
171: try {
172: File srcfile = new File(srcdir, path);
173: File destfile = new File(destdir, path);
174:
175: File parent = destfile.getParentFile();
176: if (!parent.exists()) {
177: parent.mkdirs();
178: }
179:
180: FileInputStream fis = new FileInputStream(srcfile);
181: FileOutputStream fos = new FileOutputStream(destfile);
182:
183: int bytes_read = 0;
184: byte buffer[] = new byte[512];
185: while ((bytes_read = fis.read(buffer)) != -1) {
186: fos.write(buffer, 0, bytes_read);
187: }
188:
189: fis.close();
190: fos.close();
191:
192: } catch (IOException e) {
193: throw new BuildException("Error while copying file " + path);
194: }
195: }
196:
197: private void copyDirectory(String path) {
198: File dir = new File(srcdir, path);
199:
200: String subpaths[] = dir.list();
201: for (String subpath : subpaths) {
202: String completepath = path + File.separator + subpath;
203: File subfile = new File(srcdir, completepath);
204:
205: if (subfile.exists() && !subfile.isHidden()) {
206: if (subfile.isFile()) {
207: copyFile(completepath);
208: } else if (subfile.isDirectory()) {
209: copyDirectory(completepath);
210: } else {
211: System.out.println("Ignoring " + completepath);
212: }
213: }
214: }
215: }
216: }
|