001: /*
002: * To change this template, choose Tools | Templates
003: * and open the template in the editor.
004: */
005: package org.netbeans.modules.etlbulkloader.packager;
006:
007: import org.netbeans.modules.etlbulkloader.tools.ETLBLUtils;
008: import org.netbeans.modules.etlbulkloader.tools.CopyFile;
009: import java.io.BufferedWriter;
010: import java.io.File;
011: import java.io.FileWriter;
012: import java.io.IOException;
013: import java.util.ArrayList;
014: import java.util.HashMap;
015: import java.util.Iterator;
016: import net.java.hulp.i18n.Logger;
017: import org.netbeans.modules.etl.logger.Localizer;
018: import org.netbeans.modules.etl.logger.LogUtil;
019:
020: /**
021: *
022: * @author Manish
023: */
024: public class ETLBLPackager {
025:
026: String projecthome = null;
027: HashMap<String, String> modelAndEngineFilesMap = new HashMap();
028: ArrayList triggerStrings = new ArrayList();
029: private static transient final Logger mLogger = LogUtil
030: .getLogger(ETLBLPackager.class.getName());
031: private static transient final Localizer mLoc = Localizer.get();
032:
033: public ETLBLPackager() {
034: mLogger.infoNoloc(mLoc.t("Init ETL Bulk Loader Packaging ..."));
035: }
036:
037: public ETLBLPackager(String projecthome) {
038: this ();
039: this .projecthome = projecthome;
040: createETLExecutableDir();
041: }
042:
043: private void createETLExecutableDir() {
044: String packageContainer = this .projecthome
045: + ETLBLPkgConstants.fs + ETLBLPkgConstants.toplevelpkg;
046: boolean statusL = ETLBLUtils.checkIfPackageExists(
047: packageContainer, true);
048: if (!statusL) {
049: mLogger
050: .infoNoloc(mLoc
051: .t("ERROR: Unable To Create ETLLoader Package in the project home : "
052: + packageContainer));
053: System.exit(0);
054: }
055: String eTLProcessContainer = packageContainer
056: + ETLBLPkgConstants.fs + ETLBLPkgConstants.toplevelrt;
057: boolean statusP = ETLBLUtils.checkIfPackageExists(
058: eTLProcessContainer, true);
059: if (!statusP) {
060: mLogger
061: .infoNoloc(mLoc
062: .t("ERROR: Unable To Create ETLProcess Package in the project home : "
063: + eTLProcessContainer));
064: System.exit(0);
065: }
066: }
067:
068: public void createExecutablePackage() {
069: String pathToeTLDefinitionFiles = this .projecthome
070: + ETLBLPkgConstants.fs
071: + ETLBLPkgConstants.collabFolderName;
072: String pathToeTLEngineFiles = this .projecthome
073: + ETLBLPkgConstants.fs
074: + ETLBLPkgConstants.buildFolderName;
075: mLogger
076: .infoNoloc(mLoc
077: .t("Building package for artifacts under package :: \n"
078: + "ETL Definition file :: "
079: + pathToeTLDefinitionFiles
080: + "\n"
081: + "ETL Engine file :: "
082: + pathToeTLEngineFiles));
083:
084: //Validate if each model file has an engine file generated, build a list
085: correlateModelAndEngineFiles(pathToeTLDefinitionFiles,
086: pathToeTLEngineFiles);
087:
088: //Copy Model and Engine files in respective packages
089: if (this .modelAndEngineFilesMap != null) {
090: Iterator i = this .modelAndEngineFilesMap.keySet()
091: .iterator();
092: while (i.hasNext()) {
093: String modelfilename = (String) i.next();
094: String enginefilename = (String) this .modelAndEngineFilesMap
095: .get(modelfilename);
096: copyETLArtifactsToPackage(pathToeTLDefinitionFiles
097: + ETLBLPkgConstants.fs + modelfilename,
098: pathToeTLEngineFiles + ETLBLPkgConstants.fs
099: + enginefilename);
100: }
101:
102: //Copy Libs
103: copyRequiredLibsToPackage();
104:
105: //Copy and Create Triggers
106: copyAndAppendTriggers();
107: }
108: }
109:
110: private void correlateModelAndEngineFiles(String modelp,
111: String enginep) {
112: boolean statusModelp = ETLBLUtils.checkIfPackageExists(modelp,
113: false);
114: boolean statusEnginep = ETLBLUtils.checkIfPackageExists(
115: enginep, false);
116: if (!statusModelp) {
117: mLogger
118: .infoNoloc(mLoc
119: .t("ERROR: Missing Source package from eTL Project : "
120: + modelp));
121: System.exit(0);
122: } else if (!statusEnginep) {
123: mLogger
124: .infoNoloc(mLoc
125: .t("ERROR: Missing Source package from eTL Project : "
126: + enginep));
127: System.exit(0);
128: } else {
129: //Read All the Model Files available under model files path.
130: File models = new File(modelp);
131: File engines = new File(enginep);
132: String[] modelfiles = models.list();
133: String[] enginefiles = engines.list();
134:
135: for (int i = 0; i < modelfiles.length; i++) {
136: boolean matchfound = false;
137: if (modelfiles[i]
138: .indexOf(ETLBLPkgConstants.modelFileSuffix) != -1) {
139: String predictedEngineFileName = modelfiles[i]
140: .substring(
141: 0,
142: modelfiles[i]
143: .indexOf(ETLBLPkgConstants.modelFileSuffix))
144: + ETLBLPkgConstants.engineFileSuffix;
145: mLogger
146: .infoNoloc(mLoc
147: .t("Predicted engine file name for model [ "
148: + modelfiles[i]
149: + " ] is : "
150: + predictedEngineFileName));
151: // This model must have corresponding engine file built
152: for (int j = 0; j < enginefiles.length; j++) {
153: if (enginefiles[j]
154: .equals(predictedEngineFileName)) {
155: matchfound = true;
156: this .modelAndEngineFilesMap.put(
157: modelfiles[i],
158: predictedEngineFileName);
159: break;
160: }
161: }
162: if (!matchfound) {
163: mLogger
164: .warnNoloc(mLoc
165: .t("WARNING: Engine file does not exist for model : "
166: + modelfiles[i]
167: + ".\n"
168: + "This will be excluded from the eTL Bulk Loader Package"));
169: }
170: }
171: }
172: }
173: }
174:
175: private void copyETLArtifactsToPackage(String absmodelf,
176: String absenginef) {
177: File etlmodel = new File(absmodelf);
178: String sourcefilename = etlmodel.getName();
179: String sourcepackage = null;
180: if ((sourcefilename.indexOf(".")) != -1) {
181: sourcepackage = sourcefilename.substring(0,
182: sourcefilename.indexOf(".")).toUpperCase();
183: } else {
184: sourcepackage = sourcefilename.toUpperCase();
185: }
186:
187: //Output Directory
188: String outdir = this .projecthome + ETLBLPkgConstants.fs
189: + ETLBLPkgConstants.toplevelpkg + ETLBLPkgConstants.fs
190: + ETLBLPkgConstants.toplevelrt + ETLBLPkgConstants.fs
191: + sourcepackage;
192: boolean outpackageStatus = ETLBLUtils.checkIfPackageExists(
193: outdir, true);
194:
195: if (outpackageStatus) {
196: ETLBLUtils.copyFile(absmodelf, outdir);
197: ETLBLUtils.copyFile(absenginef, outdir);
198:
199: //Build an entry for inserting into the trigger
200: buildTriggerEntry(sourcepackage, new File(absenginef)
201: .getName());
202: } else {
203: mLogger
204: .infoNoloc(mLoc
205: .t("ERROR: Unable to create output package for eTL process : "
206: + outdir));
207: }
208: }
209:
210: private void buildTriggerEntry(String srcfoldername,
211: String enginefilename) {
212: StringBuilder sb = new StringBuilder();
213: sb
214: .append("java -cp %CP% -Xms256M -Xmx1024M %JAVA_OPTS% ETLEngineInvoker .");
215: sb.append(ETLBLPkgConstants.fs + "ETLProcess"
216: + ETLBLPkgConstants.fs);
217: sb.append(srcfoldername + ETLBLPkgConstants.fs);
218: sb.append(enginefilename);
219: this .triggerStrings.add(sb.toString());
220: }
221:
222: private void copyRequiredLibsToPackage() {
223: //Create a lib folder if not exists
224: String trgtLibdir = this .projecthome + ETLBLPkgConstants.fs
225: + ETLBLPkgConstants.pkglibs;
226: String srcLibPath = ETLBLPkgConstants.srclibs;
227: mLogger.infoNoloc(mLoc.t("copyRequiredLibsToPackage****** "
228: + srcLibPath));
229: boolean status = ETLBLUtils.checkIfPackageExists(trgtLibdir,
230: true);
231: if (status) {
232: //NOTE : ***** Check if some of these libs can be copied from project system directly *****
233: File srclibs = new File(srcLibPath);
234: String[] libnames = srclibs.list();
235: for (int i = 0; i < libnames.length; i++) {
236: mLogger.infoNoloc(mLoc.t("Copying Lib [ " + libnames[i]
237: + " ] ..."));
238: //eTLBLUtils.copyFile(srcLibPath + eTLBLPkgConstants.fs + libnames[i], trgtLibdir);
239: try {
240: CopyFile.copyFile(new File(srcLibPath
241: + ETLBLPkgConstants.fs + libnames[i]),
242: new File(trgtLibdir + ETLBLPkgConstants.fs
243: + libnames[i]));
244: } catch (IOException e) {
245: e.printStackTrace();
246: }
247: }
248: } else {
249: mLogger.infoNoloc(mLoc
250: .t("ERROR: Unable to create lib package : "
251: + trgtLibdir));
252: }
253: }
254:
255: private void copyAndAppendTriggers() {
256: //Copy the triggers first to the root dir
257: File triggertempl = new File(ETLBLPkgConstants.srctriggertempl);
258: String[] triggerstemp = triggertempl.list();
259: for (int i = 0; i < triggerstemp.length; i++) {
260: ETLBLUtils.copyFile(ETLBLPkgConstants.srctriggertempl
261: + ETLBLPkgConstants.fs + triggerstemp[i],
262: this .projecthome + ETLBLPkgConstants.fs
263: + ETLBLPkgConstants.toplevelpkg);
264: }
265:
266: //Modicy trigger with invocation calls
267: String triggerpkg = this .projecthome + ETLBLPkgConstants.fs
268: + ETLBLPkgConstants.toplevelpkg;
269: File finaltrigfiles = new File(triggerpkg);
270: String[] triggers = finaltrigfiles.list();
271: for (int i = 0; i < triggers.length; i++) {
272: if ((triggers[i].indexOf(".bat") != -1)
273: || (triggers[i].indexOf(".sh") != -1)) {
274: BufferedWriter out = null;
275: try {
276: mLogger.infoNoloc(mLoc
277: .t("Creating eTL Invocation trigger : "
278: + triggers[i]));
279: out = new BufferedWriter(new FileWriter(triggerpkg
280: + ETLBLPkgConstants.fs + triggers[i], true));
281: for (int j = 0; j < this .triggerStrings.size(); j++) {
282: out
283: .write("\n"
284: + this .triggerStrings.get(j)
285: .toString());
286: }
287: } catch (IOException ex) {
288: mLogger.errorNoloc(
289: mLoc.t("PRSR023: Exception :{0}", ex
290: .getMessage()), ex);
291: } finally {
292: try {
293: out.close();
294: } catch (IOException ex) {
295: mLogger.errorNoloc(mLoc.t(
296: "PRSR024: Exception :{0}", ex
297: .getMessage()), ex);
298: }
299: }
300: }
301: }
302: }
303: }
|