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.util.configmigration;
020:
021: import java.io.File;
022: import java.io.FileInputStream;
023: import java.io.FileOutputStream;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.OutputStream;
027: import java.util.ArrayList;
028: import java.util.Iterator;
029:
030: import javax.xml.transform.Result;
031: import javax.xml.transform.Source;
032: import javax.xml.transform.Templates;
033: import javax.xml.transform.Transformer;
034: import javax.xml.transform.TransformerConfigurationException;
035: import javax.xml.transform.TransformerException;
036: import javax.xml.transform.TransformerFactory;
037: import javax.xml.transform.TransformerFactoryConfigurationError;
038: import javax.xml.transform.stream.StreamResult;
039: import javax.xml.transform.stream.StreamSource;
040:
041: public class MigrationTool {
042:
043: public static void main(String[] args) {
044: // Use Saxon 6.5.x
045: System.setProperty("javax.xml.transform.TransformerFactory",
046: "com.icl.saxon.TransformerFactoryImpl");
047:
048: // Check whether current working path is a Pustefix environment
049: File centralProjectsFile = new File(
050: "projects/servletconf/projects.xml.in");
051: File projectsDir = new File("./projects");
052: if (!centralProjectsFile.exists()) {
053: centralProjectsFile = new File(
054: "example/servletconf/projects.xml.in");
055: projectsDir = new File("./example");
056: }
057: if (!centralProjectsFile.exists()) {
058: System.err
059: .println("Neither projects/servletconf/projects.xml.in"
060: + " nor example/servletconf/projects.xml.in"
061: + " could be found, exiting...");
062: return;
063: }
064:
065: // Talk to user
066: System.out
067: .print("WARNING this tool will overwrite existing files!\n");
068: System.out
069: .print("Make a backup of your environment before using this tool!\n\n");
070: try {
071: System.out.print("Process will start in 3");
072: Thread.sleep(1000);
073: System.out.print(", 2");
074: Thread.sleep(1000);
075: System.out.print(", 1");
076: Thread.sleep(1000);
077: System.out.print(", now!\n\n");
078: } catch (InterruptedException e) {
079: System.out.print("\nAborted!\n");
080: return;
081: }
082: System.out.print("Creating file list...");
083:
084: // List of configuration files that have to be processed
085: ArrayList<String> filenamesToProcess = new ArrayList<String>();
086: ArrayList<String> filenamesThatFailed = new ArrayList<String>();
087:
088: File tempFile;
089:
090: // Add files, that will not be found automatically
091: filenamesToProcess.add("servletconf/projects.xml.in");
092: tempFile = new File(projectsDir, "common/conf/pfixlog.xml.in");
093: if (tempFile.exists()) {
094: filenamesToProcess.add("common/conf/pfixlog.xml.in");
095: }
096: tempFile = new File(projectsDir,
097: "common/conf/pfixlog.xml.in.in");
098: if (tempFile.exists()) {
099: filenamesToProcess.add("common/conf/pfixlog.xml.in.in");
100: }
101: tempFile = new File(projectsDir, "common/conf/pustefix.prop.in");
102: if (tempFile.exists()) {
103: filenamesToProcess.add("common/conf/pustefix.prop.in");
104: }
105: tempFile = new File(projectsDir, "common/conf/factory.prop.in");
106: if (tempFile.exists()) {
107: filenamesToProcess.add("common/conf/factory.prop.in");
108: }
109: tempFile = new File(projectsDir,
110: "common/conf/exceptionhandler.prop.in");
111: if (tempFile.exists()) {
112: filenamesToProcess
113: .add("common/conf/exceptionhandler.prop.in");
114: }
115: tempFile = new File(projectsDir,
116: "common/conf/apploader.prop.in");
117: if (tempFile.exists()) {
118: filenamesToProcess.add("common/conf/apploader.prop.in");
119: }
120:
121: // Iterate over all project directories
122: File[] projectDirs = projectsDir.listFiles();
123: for (int i = 0; i < projectDirs.length; i++) {
124: // Ignore special directories "core" and "servletconf", and
125: // files that are no directories
126: // Files in "common" directory have already been manually added
127: String projectName = projectDirs[i].getName();
128: if (!projectDirs[i].isDirectory()
129: || projectDirs[i].isHidden()
130: || projectName.equals("servletconf")
131: || projectName.equals("core")
132: || projectName.equals("common")) {
133: continue;
134: }
135:
136: File confDir = new File(projectDirs[i], "conf");
137:
138: // If there is no "conf" subdirectory, this is not a project
139: if (!confDir.exists()) {
140: continue;
141: }
142:
143: // Iterate over all files in directory
144: File[] confFiles = confDir.listFiles();
145: for (int j = 0; j < confFiles.length; j++) {
146: String filename = confFiles[j].getName();
147: if (filename.equals("depend.xml.in")
148: || filename.equals("webservice.conf.in")
149: || filename.equals("project.xml.in")
150: || filename.endsWith(".prop.in")) {
151: filenamesToProcess.add(projectName + "/conf/"
152: + filename);
153: }
154: }
155: }
156:
157: // Talk to user
158: System.out.print(" DONE\n\n");
159:
160: // Process all files
161: for (Iterator i = filenamesToProcess.iterator(); i.hasNext();) {
162: String filename = (String) i.next();
163: File sourceFile = new File(projectsDir, filename);
164:
165: // Talk to user
166: System.out.print("Processing " + filename + "...");
167:
168: String targetFilename = null;
169: boolean success = false;
170: String errorString = "Unknown error";
171:
172: try {
173: if (filename.endsWith("/depend.xml.in")) {
174: targetFilename = filename.substring(0, filename
175: .length() - 3);
176: File targetFile = new File(projectsDir,
177: targetFilename);
178: transform(sourceFile, targetFile,
179: getDependTemplates());
180: if (sourceFile.delete())
181: success = true;
182: } else if (filename.endsWith("/pfixlog.xml.in")
183: || filename.endsWith("/pfixlog.xml.in.in")) {
184: if (filename.endsWith("/pfixlog.xml.in")) {
185: targetFilename = filename.substring(0, filename
186: .length() - 3);
187: } else {
188: targetFilename = filename;
189: }
190: File targetFile = new File(projectsDir,
191: targetFilename);
192: transform(sourceFile, targetFile,
193: getPfixlogTemplates());
194: if (!targetFilename.equals(filename)) {
195: if (sourceFile.delete())
196: success = true;
197: } else {
198: success = true;
199: }
200: } else if (filename.endsWith("/webservice.conf.in")) {
201: targetFilename = filename.substring(0, filename
202: .length() - 3)
203: + ".xml";
204: File targetFile = new File(projectsDir,
205: targetFilename);
206: transform(sourceFile, targetFile,
207: getWebserviceTemplates());
208: if (sourceFile.delete())
209: success = true;
210: } else if (filename.endsWith("/project.xml.in")
211: || filename.endsWith("/projects.xml.in")) {
212: targetFilename = filename;
213: File targetFile = sourceFile;
214: transform(sourceFile, targetFile,
215: getProjectTmeplates());
216: success = true;
217: } else if (filename
218: .equals("common/conf/pustefix.prop.in")
219: || filename
220: .equals("common/conf/factory.prop.in")) {
221: targetFilename = filename.substring(0, filename
222: .length() - 8)
223: + ".xml";
224: File targetFile = new File(projectsDir,
225: targetFilename);
226: transform(sourceFile, targetFile,
227: getPropertiesTemplates());
228: if (sourceFile.delete())
229: success = true;
230: } else if (filename.endsWith(".prop.in")
231: && filename.startsWith("common/conf/")) {
232: targetFilename = filename.substring(0, filename
233: .length() - 8)
234: + ".xml";
235: File targetFile = new File(projectsDir,
236: targetFilename);
237: transform(sourceFile, targetFile,
238: getPropertiesTemplates());
239: if (sourceFile.delete()) {
240: success = true;
241: }
242: } else if (filename.endsWith(".prop.in")) {
243: targetFilename = filename.substring(0, filename
244: .length() - 8)
245: + ".conf.xml";
246: File targetFile = new File(projectsDir,
247: targetFilename);
248: transform(sourceFile, targetFile,
249: getPropertiesTemplates());
250: if (sourceFile.delete())
251: success = true;
252: }
253: } catch (Exception e) {
254: errorString = "\"" + e.getMessage() + "\"";
255: success = false;
256: }
257: if (success) {
258: System.out.print(" OK\n");
259: System.out.print(" ==> " + targetFilename + "\n");
260: } else {
261: System.out.print(" FAILED\n");
262: System.out.print(" xx> " + errorString + "\n");
263: filenamesThatFailed.add(filename);
264: }
265: i.remove();
266: }
267:
268: // Talk to user
269: System.out.print("\nProcessing completed.\n");
270: if (filenamesThatFailed.size() == 0) {
271: System.out
272: .print("\nAll files have been successfully processed!\n");
273: } else {
274: System.out.print("\nThe following files have failed:\n");
275: for (Iterator i = filenamesThatFailed.iterator(); i
276: .hasNext();) {
277: String filename = (String) i.next();
278: System.out.print(" " + filename + "\n");
279: }
280: }
281: }
282:
283: private static Templates getTemplates(String tmplName)
284: throws TransformerConfigurationException,
285: TransformerFactoryConfigurationError {
286: Source s = new StreamSource(MigrationTool.class
287: .getClassLoader().getResource(
288: "xsl/" + tmplName + ".xsl").toExternalForm());
289: Templates tmpl = TransformerFactory.newInstance().newTemplates(
290: s);
291: return tmpl;
292: }
293:
294: private static Templates getPropertiesTemplates()
295: throws TransformerConfigurationException,
296: TransformerFactoryConfigurationError {
297: return getTemplates("properties");
298: }
299:
300: private static Templates getProjectTmeplates()
301: throws TransformerConfigurationException,
302: TransformerFactoryConfigurationError {
303: return getTemplates("project");
304: }
305:
306: private static Templates getWebserviceTemplates()
307: throws TransformerConfigurationException,
308: TransformerFactoryConfigurationError {
309: return getTemplates("webservice");
310: }
311:
312: private static Templates getPfixlogTemplates()
313: throws TransformerConfigurationException,
314: TransformerFactoryConfigurationError {
315: return getTemplates("pfixlog");
316: }
317:
318: private static Templates getDependTemplates()
319: throws TransformerConfigurationException,
320: TransformerFactoryConfigurationError {
321: return getTemplates("depend");
322: }
323:
324: private static void transform(File sourceFile, File targetFile,
325: Templates templates) throws TransformerException,
326: IOException {
327: Transformer tr = templates.newTransformer();
328: Source source = new StreamSource(sourceFile);
329: File tempFile = File.createTempFile("transform", ".xml");
330: tempFile.deleteOnExit();
331: Result result = new StreamResult(tempFile);
332: tr.transform(source, result);
333: copy(tempFile, targetFile);
334: }
335:
336: private static void copy(File source, File target)
337: throws IOException {
338: InputStream in = new FileInputStream(source);
339: OutputStream out = new FileOutputStream(target);
340: int b;
341: while ((b = in.read()) != -1) {
342: out.write(b);
343: }
344: in.close();
345: out.close();
346: }
347:
348: }
|