View Javadoc

1   /* ===================================================================
2    * Copyright 2007 Consultation Eric Giguere Inc. All rights reserved
3    *
4    * ===================================================================
5    */
6    
7    
8   package com.eginc.maven.maximo.plugin;
9   
10  import java.io.BufferedWriter;
11  import java.io.File;
12  import java.io.FileWriter;
13  import java.io.IOException;
14  import java.util.ArrayList;
15  import java.util.Iterator;
16  import java.util.List;
17  import java.util.StringTokenizer;
18  
19  /**
20   * @author fbeliveau
21   * @version = $Id
22   */
23  public class CreatePsdiScript {
24  
25  	/**
26  	 * 
27  	 */
28  	public CreatePsdiScript() {
29  	}
30  
31  	public int createPsdiScript(String strFiles, String strRootDir,
32  			String scriptDir, String strScriptFileName, String installDirPath)
33  			throws Exception {
34  
35  		if (strFiles == null)
36  			strFiles = "";
37  
38  		if (!strRootDir.endsWith(File.separator))
39  			strRootDir += File.separator;
40  		File dir = new File(strRootDir);
41  		List files = prepareFileList(strRootDir, strFiles, scriptDir);
42  		String script = createAntTask(files, dir, scriptDir);
43  
44  		if (script == null) {
45  			return 1;
46  		}
47  
48  		FileWriter outFile;
49  		outFile = new FileWriter(strScriptFileName);
50  		BufferedWriter writer = new BufferedWriter(outFile);
51  		writer.write(script);
52  		writer.close();
53  		return 0;
54  	}
55  
56  	/**
57  	 * @param files
58  	 * @param scriptDir
59  	 * @return
60  	 */
61  	private String createAntTask(List files, File rootDir, String scriptDir) {
62  		StringBuffer script = new StringBuffer(16384);
63  		StringBuffer files1 = new StringBuffer(8192);
64  
65  		Iterator it = files.iterator();
66  		while (it.hasNext()) {
67  			File aFile = (File) it.next();
68  			String filename = getFileName(aFile, scriptDir);
69  
70  			files1.append("<antcall target=\"runPsdiScript\">\n");
71  			files1.append("<param name=\"dir\" value=\"" + scriptDir + "\"/>");
72  			files1.append("<param name=\"filename\" value=\"" + filename
73  					+ "\"/>");
74  			files1.append("</antcall>\n");
75  
76  		}
77  
78  		script
79  				.append("<project name=\"psdi-scripts\" default=\"psdi\" basedir=\".\">\n");
80  		script
81  				.append("<target name=\"run-psdi-scripts\" description=\"Runs psdi scripts files\">\n");
82  
83  		if (files1.length() > 0) {
84  			script.append(files1.toString());
85  		} else {
86  			script.append("<echo message=\"No script to run\"/>");
87  		}
88  
89  		script.append("</target>\n");
90  		script.append(antTarget);
91  		script.append("</project>\n");
92  		return script.toString();
93  	}
94  
95  	/**
96  	 * @param file
97  	 * @param scriptDir
98  	 * @return
99  	 */
100 	private String getFileName(File file, String scriptDir) {
101 		String filename = file.getName();
102 		filename = filename.substring(0, filename.length() - 4);
103 		return filename;
104 	}
105 
106 	/**
107 	 * @param strFiles
108 	 * @param strFiles
109 	 * @param scriptDir
110 	 * @return
111 	 * @throws Exception
112 	 */
113 	private List prepareFileList(String strRootDir, String strFiles,
114 			String scriptDir) throws Exception {
115 		List result = new ArrayList();
116 		StringTokenizer tokens = new StringTokenizer(strFiles, ",");
117 		String token;
118 		while (tokens.hasMoreTokens()) {
119 			token = tokens.nextToken();
120 //			String filepath = strRootDir
121 //					+ "/tools/maximo/classes/cim/psdi/script/" + token;
122 			String filepath = strRootDir + token;
123 			File fileToAdd = new File(filepath);
124 			if (!fileToAdd.exists()) {
125 				throw new IOException("File " + fileToAdd + " does not exist");
126 			}
127 			result.add(fileToAdd);
128 		}
129 		return result;
130 	}
131 
132 	private String antTarget = "<target name=\"runPsdiScript\">\n<java classname=\"psdi.tools.RunScriptFile\" dir=\"${maximo.home}/tools/maximo/classes\" fork=\"true\">\n<arg value=\"-c${dir}\"/>\n<arg value=\"-f${filename}\"/>\n<classpath>\n<pathelement location=\"${maximo.home}/applications/maximo/lib/Opta.jar\"/>\n<pathelement location=\"${maximo.home}/applications/maximo/lib/oraclethin.jar\"/>\n<pathelement path=\"${maximo.home}/tools/maximo/classes\"/>\n<pathelement path=\"${maximo.home}/applications/maximo/businessobjects/classes\"/>\n</classpath>\n</java>\n</target>";
133 
134 }