1
2
3
4
5
6 package com.eginc.maven.maximo.plugin;
7
8 import java.io.BufferedWriter;
9 import java.io.File;
10 import java.io.FileNotFoundException;
11 import java.io.FileWriter;
12 import java.util.Iterator;
13 import java.util.List;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17
18 /**
19 * @author egiguere
20 * @version = $Id
21 */
22 public class AntFilesetUtil {
23 private static Log log = LogFactory.getLog(AntFilesetUtil.class);
24
25 public List scanDirectory(File dir) {
26 if (!dir.isDirectory()) {
27 log.error(dir + " is NOT a directory");
28 return null;
29 }
30
31 try {
32 FileListing listing = new FileListing();
33 List files = listing.getFileListing(dir, false, true);
34 Iterator it = files.iterator();
35 while (it.hasNext()) {
36 File obj = (File) it.next();
37 log.info("AntFilesetUtil file = " + obj);
38 }
39 return files;
40 } catch (FileNotFoundException e) {
41 e.printStackTrace();
42 }
43 return null;
44 }
45
46 public String createAntTask(List files, File rootDir) {
47 StringBuffer script = new StringBuffer(16384);
48 StringBuffer files1 = new StringBuffer(8192);
49 StringBuffer files2 = new StringBuffer(8192);
50 StringBuffer files3 = new StringBuffer(8192);
51
52
53 Iterator it = files.iterator();
54 while (it.hasNext()) {
55 File aFile = (File) it.next();
56 String filePath = aFile.getAbsolutePath().substring(rootDir.getAbsolutePath().length());
57 String fileRelPath = aFile.getAbsolutePath().substring(rootDir.getAbsolutePath().length(),
58 aFile.getAbsolutePath().length() - aFile.getName().length());
59 filePath = filePath.replace('\\', '/').trim();
60 fileRelPath = fileRelPath.replace('\\', '/').trim();
61 files1.append("<copy preservelastmodified=\"true\" failonerror=\"false\" todir=\"${backup.dir}" + fileRelPath
62 + "\" file=\"${maximo.home}" + filePath + "\"/>\n");
63 files2.append("<delete failonerror=\"false\" file=\"${maximo.home}" + filePath + "\"/>\n");
64 files3.append("<copy preservelastmodified=\"true\" failonerror=\"false\" todir=\"${maximo.home}" + fileRelPath
65 + "\" file=\"${backup.dir}" + filePath + "\"/>\n");
66 }
67
68
69
70 script.append("<project name=\"mx-backup\" default=\"backup\" basedir=\".\">\n");
71 script.append("<target name=\"backup-files\" description=\"Performs backup of listed file\">\n");
72 script.append("<echo message=\"mx-backup : backup-files on: ${maximo.home} to: ${backup.dir}\"/>\n");
73 if (files1.length() > 0) {
74 script.append(files1);
75 } else {
76 script.append("<echo message=\"mx-backup : no files to backup\"/>\n");
77 }
78 script.append("</target>\n");
79 script.append("<target name=\"remove-files\" description=\"Remove file listed from the Maximo installation\">\n");
80 script.append("<echo message=\"mx-backup : remove-files on: ${maximo.home}\"/>\n");
81 if (files2.length() > 0) {
82 script.append(files2);
83 } else {
84 script.append("<echo message=\"mx-backup : no files to remove\"/>\n");
85 }
86
87 script.append("</target>\n");
88 script.append("<target name=\"restore-files\">\n");
89 script.append("<echo message=\"mx-backup : restore-files on: ${maximo.home} from:${backup.dir} \"/>\n");
90 if (files3.length() > 0) {
91 script.append(files3);
92 } else {
93 script.append("<echo message=\"mx-backup : no files to restore\"/>\n");
94 }
95
96 script.append("</target>\n");
97 script.append("</project>\n");
98
99 return script.toString();
100 }
101
102 public int createCopyScript(String outfile, String dirToCopy, String installDirPath) throws Exception {
103
104 File dir = new File(dirToCopy);
105 List files = scanDirectory(dir);
106 String script = createAntTask(files, dir);
107
108 if (script == null)
109 return 1;
110
111 FileWriter outFile;
112 outFile = new FileWriter(outfile);
113 BufferedWriter writer = new BufferedWriter(outFile);
114
115 writer.write(script);
116 writer.close();
117 return 0;
118 }
119
120 }