KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > kettle > KettleComponent


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Jan 2, 2006
14  * @author Matt Casters
15  */

16
17 package org.pentaho.plugin.kettle;
18
19 import java.io.File JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.pentaho.core.connection.memory.MemoryMetaData;
26 import org.pentaho.core.connection.memory.MemoryResultSet;
27 import org.pentaho.core.solution.IActionResource;
28 import org.pentaho.core.system.PentahoSystem;
29 import org.pentaho.core.util.XmlHelper;
30 import org.pentaho.core.util.XmlW3CHelper;
31 import org.pentaho.messages.Messages;
32 import org.pentaho.plugin.ComponentBase;
33
34 import be.ibridge.kettle.core.LocalVariables;
35 import be.ibridge.kettle.core.LogWriter;
36 import be.ibridge.kettle.core.Result;
37 import be.ibridge.kettle.core.Row;
38 import be.ibridge.kettle.core.exception.KettleException;
39 import be.ibridge.kettle.core.logging.Log4jStringAppender;
40 import be.ibridge.kettle.core.value.Value;
41 import be.ibridge.kettle.job.Job;
42 import be.ibridge.kettle.job.JobMeta;
43 import be.ibridge.kettle.repository.RepositoriesMeta;
44 import be.ibridge.kettle.repository.Repository;
45 import be.ibridge.kettle.repository.RepositoryDirectory;
46 import be.ibridge.kettle.repository.RepositoryMeta;
47 import be.ibridge.kettle.repository.UserInfo;
48 import be.ibridge.kettle.trans.StepLoader;
49 import be.ibridge.kettle.trans.Trans;
50 import be.ibridge.kettle.trans.TransMeta;
51 import be.ibridge.kettle.trans.step.RowListener;
52 import be.ibridge.kettle.trans.step.StepMetaDataCombi;
53
54 /**
55  * KettleComponent shows a list of available transformations in the root of the choosen repository.
56  *
57  * @author Matt
58  *
59  */

60 public class KettleComponent extends ComponentBase implements RowListener {
61
62     private static final long serialVersionUID = 8217343898202366129L;
63
64     private static final String JavaDoc DIRECTORY = "directory"; //$NON-NLS-1$
65

66     private static final String JavaDoc TRANSFORMATION = "transformation"; //$NON-NLS-1$
67

68     private static final String JavaDoc JOB = "job"; //$NON-NLS-1$
69

70     private static final String JavaDoc TRANSFORMFILE = "transformation-file"; //$NON-NLS-1$
71

72     private static final String JavaDoc JOBFILE = "job-file"; //$NON-NLS-1$
73

74     private static final String JavaDoc IMPORTSTEP = "importstep"; //$NON-NLS-1$
75

76     /**
77      * The repositories.xml file location, if empty take the default $HOME/.kettle/repositories.xml
78      */

79     private String JavaDoc repositoriesXMLFile;
80
81     /** The name of the repository to use */
82     private String JavaDoc repositoryName;
83
84     /** The username to login with */
85     private String JavaDoc username;
86
87     MemoryResultSet results;
88
89     /** The password to login with */
90     private String JavaDoc password;
91   
92   private Log4jStringAppender kettleUserAppender;
93
94     public Log getLogger() {
95         return LogFactory.getLog(KettleComponent.class);
96     }
97
98     protected boolean validateSystemSettings() {
99         // set pentaho.solutionpath so that it can be used in file paths
100
boolean useRepository = PentahoSystem.getSystemSetting("kettle/settings.xml", "repository.type", "files").equals("rdbms"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
101
if (useRepository) {
102             repositoriesXMLFile = PentahoSystem.getSystemSetting("kettle/settings.xml", "repositories.xml.file", null); //$NON-NLS-1$ //$NON-NLS-2$
103
repositoryName = PentahoSystem.getSystemSetting("kettle/settings.xml", "repository.name", null); //$NON-NLS-1$ //$NON-NLS-2$
104
username = PentahoSystem.getSystemSetting("kettle/settings.xml", "repository.userid", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
105
password = PentahoSystem.getSystemSetting("kettle/settings.xml", "repository.password", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
106
// Check the Kettle settings...
107
if ("".equals(repositoryName) || username.equals("")) { //$NON-NLS-1$ //$NON-NLS-2$
108
// looks like the Kettle stuff is not configured yet...
109
// see if we can provide feedback to the user...
110

111                 error(Messages.getErrorString("Kettle.ERROR_0001_SERVER_SETTINGS_NOT_SET")); //$NON-NLS-1$
112
return false;
113             }
114
115             boolean ok = (repositoryName != null && repositoryName.length() > 0);
116             ok = ok || (username != null && username.length() > 0);
117
118             return ok;
119         }
120         return true;
121     }
122
123     public boolean init() {
124         return true;
125
126     }
127
128     public boolean validateAction() {
129
130         if (isDefinedResource(TRANSFORMFILE) || isDefinedResource(JOBFILE)) {
131             return true;
132         }
133
134         boolean useRepository = PentahoSystem.getSystemSetting("kettle/settings.xml", "repository.type", "files").equals("rdbms"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
135

136         if (!useRepository) {
137             error(Messages.getErrorString("Kettle.ERROR_0019_REPOSITORY_TYPE_FILES")); //$NON-NLS-1$
138
return false;
139         }
140
141         if (isDefinedInput(DIRECTORY) && (isDefinedInput(TRANSFORMATION) || isDefinedInput(JOB))) {
142             return true;
143         }
144
145         if (!isDefinedInput(DIRECTORY)) {
146             error(Messages.getErrorString("Kettle.ERROR_0002_DIR_OR_FILE__NOT_DEFINED", getActionName())); //$NON-NLS-1$
147
return false;
148         } else {
149             if (!isDefinedInput(TRANSFORMATION)) {
150                 error(Messages.getErrorString("Kettle.ERROR_0003_TRANS_NOT_DEFINED", getActionName())); //$NON-NLS-1$
151
return false;
152             }
153         }
154
155         return false;
156
157     }
158
159     /**
160      * Execute the specified transformation in the choosen repository.
161      */

162     public boolean executeAction() {
163
164         if (debug)
165             debug(Messages.getString("Kettle.DEBUG_START")); //$NON-NLS-1$
166

167         TransMeta transMeta = null;
168         JobMeta jobMeta = null;
169         LogWriter logWriter = null;
170
171     kettleUserAppender = LogWriter.createStringAppender();
172         try {
173             logWriter = LogWriter.getInstance("Kettle-pentaho", false, LogWriter.LOG_LEVEL_BASIC); //$NON-NLS-1$
174
} catch (Throwable JavaDoc t) {
175
176         }
177
178   
179         // this use is now considered obsolete, as we prefer the action-sequence inputs since they
180
// now maintain order
181
boolean running = true;
182         int index = 1;
183         ArrayList JavaDoc parameterList = new ArrayList JavaDoc();
184         while (running) {
185             if (isDefinedInput("parameter" + index)) { //$NON-NLS-1$
186
String JavaDoc value = null;
187                 String JavaDoc inputName = getInputStringValue("parameter" + index); //$NON-NLS-1$
188
// see if we have an input with this name
189
if (isDefinedInput(inputName)) {
190                     value = getInputStringValue(inputName);
191                 }
192                 parameterList.add(value);
193             } else {
194                 running = false;
195             }
196             index++;
197         }
198
199         // this is the preferred way to provide inputs to the KetteComponent, the order of inputs is now preserved
200
Iterator JavaDoc inputNamesIter = getInputNames().iterator();
201         while (inputNamesIter.hasNext()) {
202             String JavaDoc name = (String JavaDoc) inputNamesIter.next();
203             String JavaDoc value = null;
204             if (isDefinedInput(name)) {
205                 value = getInputStringValue(name);
206             }
207             if (!parameterList.contains(value)) {
208                 parameterList.add(value);
209             }
210         }
211
212         String JavaDoc parameters[] = (String JavaDoc[]) parameterList.toArray(new String JavaDoc[parameterList.size()]);
213
214         Repository repository = connectToRepository(logWriter);
215         boolean result = false;
216     logWriter.addAppender(kettleUserAppender);
217         try {
218             if (isDefinedInput(DIRECTORY)) {
219                 String JavaDoc directoryName = getInputStringValue(DIRECTORY);
220
221                 if (repository == null) {
222                     return false;
223                 }
224
225                 if (isDefinedInput(TRANSFORMATION)) {
226                     String JavaDoc transformationName = getInputStringValue(TRANSFORMATION);
227                     transMeta = loadTransformFromRepository(directoryName, transformationName, repository, logWriter);
228                     if (transMeta != null) {
229                         transMeta.setArguments(parameters);
230                     } else {
231                         return false;
232                     }
233                 } else if (isDefinedInput(JOB)) {
234                     String JavaDoc jobName = getInputStringValue(JOB);
235                     jobMeta = loadJobFromRepository(directoryName, jobName, repository, logWriter);
236                     if (jobMeta != null) {
237                         jobMeta.setArguments(parameters);
238                     } else {
239                         return false;
240                     }
241                 }
242             } else if (isDefinedResource(TRANSFORMFILE)) {
243                 IActionResource transformResource = getResource(TRANSFORMFILE);
244                 String JavaDoc fileAddress = getActualFileName(transformResource);
245
246                 try {
247                     if (fileAddress != null) { // We have an actual loadable filesystem and file
248
transMeta = new TransMeta(fileAddress, repository, true);
249                     } else {
250                         String JavaDoc jobXmlStr = getResourceAsString(getResource(TRANSFORMFILE));
251                         jobXmlStr = jobXmlStr.replaceAll("\\$\\{pentaho.solutionpath\\}", PentahoSystem.getApplicationContext().getFileOutputPath("")); //$NON-NLS-1$ //$NON-NLS-2$
252
jobXmlStr = jobXmlStr.replaceAll("\\%\\%pentaho.solutionpath\\%\\%", PentahoSystem.getApplicationContext().getFileOutputPath("")); //$NON-NLS-1$ //$NON-NLS-2$
253
org.w3c.dom.Document JavaDoc doc = XmlW3CHelper.getDomFromString(jobXmlStr);
254                         // create a tranformation from the document
255
transMeta = new TransMeta(doc.getFirstChild());
256                     }
257                 } catch (Exception JavaDoc e) {
258                     error(Messages.getErrorString("Kettle.ERROR_0015_BAD_RESOURCE", TRANSFORMFILE, fileAddress), e); //$NON-NLS-1$
259
return false;
260                 }
261
262                 if (transMeta == null) {
263                     error(Messages.getErrorString("Kettle.ERROR_0015_BAD_RESOURCE", TRANSFORMFILE, fileAddress)); //$NON-NLS-1$
264
info(kettleUserAppender.getBuffer().toString());
265                     return false;
266                 } else { // Don't forget to set the parameters here as well...
267
transMeta.setArguments(parameters);
268                 }
269             } else if (isDefinedResource(JOBFILE)) {
270                 String JavaDoc fileAddress = ""; //$NON-NLS-1$
271
try {
272                     fileAddress = getResource(JOBFILE).getAddress();
273                     String JavaDoc jobXmlStr = XmlHelper.getContentFromSolutionResource(fileAddress);
274                     String JavaDoc solutionPath = PentahoSystem.getApplicationContext().getFileOutputPath( "" );//$NON-NLS-1$
275
solutionPath = solutionPath.replaceAll( "\\\\", "\\\\\\\\" ); //$NON-NLS-1$ //$NON-NLS-2$
276
solutionPath = solutionPath.replaceAll( "\\$", "\\\\\\$" ); //$NON-NLS-1$ //$NON-NLS-2$
277
jobXmlStr = jobXmlStr.replaceAll( "\\$\\{pentaho.solutionpath\\}", solutionPath ); //$NON-NLS-1$
278
jobXmlStr = jobXmlStr.replaceAll( "\\%\\%pentaho.solutionpath\\%\\%", solutionPath ); //$NON-NLS-1$
279
org.w3c.dom.Document JavaDoc doc = XmlW3CHelper.getDomFromString(jobXmlStr);
280                     if (doc == null) {
281                         error(Messages.getErrorString("Kettle.ERROR_0015_BAD_RESOURCE", JOBFILE, fileAddress)); //$NON-NLS-1$
282
info(kettleUserAppender.getBuffer().toString());
283                         return false;
284                     }
285                     // create a job from the document
286
try {
287                         repository = connectToRepository(logWriter);
288                         // if we get a valid repository its great, if not try it without
289
jobMeta = new JobMeta(logWriter, doc.getFirstChild(), repository);
290                     } catch (Exception JavaDoc e) {
291                         error(Messages.getString("Kettle.ERROR_0023_NO_META"), e); //$NON-NLS-1$
292
} finally {
293                         if (repository != null) {
294                             if (debug)
295                                 debug(Messages.getString("Kettle.DEBUG_DISCONNECTING")); //$NON-NLS-1$
296
repository.disconnect();
297                         }
298                     }
299                 } catch (Exception JavaDoc e) {
300                     error(Messages.getErrorString("Kettle.ERROR_0015_BAD_RESOURCE", JOBFILE, fileAddress), e); //$NON-NLS-1$
301
return false;
302                 }
303                 if (jobMeta == null) {
304                     error(Messages.getErrorString("Kettle.ERROR_0015_BAD_RESOURCE", JOBFILE, fileAddress)); //$NON-NLS-1$
305
info(kettleUserAppender.getBuffer().toString());
306                     return false;
307                 } else {
308                     jobMeta.setArguments(parameters);
309                 }
310
311             }
312
313             // OK, we have the information, let's load and execute the
314
// transformation or job
315

316             if (transMeta != null) {
317                 result = executeTransformation(transMeta, logWriter);
318             }
319             if (jobMeta != null) {
320                 result = executeJob(jobMeta, repository, logWriter);
321             }
322
323         } finally {
324       logWriter.removeAppender(kettleUserAppender);
325             if (repository != null) {
326                 if (debug)
327                     debug(Messages.getString("Kettle.DEBUG_DISCONNECTING")); //$NON-NLS-1$
328
repository.disconnect();
329             }
330         }
331         return result;
332
333     }
334
335     private String JavaDoc getActualFileName(IActionResource resource) {
336         String JavaDoc fileAddress = null;
337
338         // Is it a hardcoded path?
339
if ((resource.getSourceType() == IActionResource.FILE_RESOURCE)) {
340             fileAddress = resource.getAddress();
341         }
342         // Is it a solution relative path?
343
else if (resource.getSourceType() == IActionResource.SOLUTION_FILE_RESOURCE) {
344             fileAddress = PentahoSystem.getApplicationContext().getSolutionPath(resource.getAddress());
345         }
346
347         // Can it be loaded? this may not be true if using the DB Based repos
348
if (fileAddress != null) {
349             File JavaDoc file = new File JavaDoc(fileAddress);
350             if (!file.exists() || !file.isFile()) {
351                 fileAddress = null;
352             }
353         }
354         return (fileAddress);
355     }
356
357     private boolean executeTransformation(TransMeta transMeta, LogWriter logWriter) {
358         boolean success = true;
359         Trans trans = null;
360         try {
361             if (transMeta != null) {
362                 try {
363                     trans = new Trans(logWriter, transMeta);
364                 } catch (Throwable JavaDoc t) {
365                     error(Messages.getErrorString("Kettle.ERROR_0010_BAD_TRANSFORMATION_METADATA"), t); //$NON-NLS-1$
366
return false;
367                 }
368
369             }
370             if (trans == null) {
371                 error(Messages.getErrorString("Kettle.ERROR_0010_BAD_TRANSFORMATION_METADATA")); //$NON-NLS-1$
372
error(kettleUserAppender.getBuffer().toString());
373                 return false;
374             }
375             if (trans != null) {
376                 // OK, we have the transformation, now run it!
377
if (debug)
378                     debug(Messages.getString("Kettle.DEBUG_PREPARING_TRANSFORMATION")); //$NON-NLS-1$
379
try {
380                     KettleSystemListener.environmentInit();
381                     if (!trans.prepareExecution(transMeta.getArguments())) {
382                         error(Messages.getErrorString("Kettle.ERROR_0011_TRANSFORMATION_PREPARATION_FAILED")); //$NON-NLS-1$
383
error(kettleUserAppender.getBuffer().toString());
384                         return false;
385                     }
386                 } catch (Throwable JavaDoc t) {
387                     error(Messages.getErrorString("Kettle.ERROR_0011_TRANSFORMATION_PREPARATION_FAILED"), t); //$NON-NLS-1$
388
return false;
389                 }
390
391                 String JavaDoc stepName = null;
392                 String JavaDoc outputName = null;
393                 if (debug)
394                     debug(Messages.getString("Kettle.DEBUG_FINDING_STEP_IMPORTER")); //$NON-NLS-1$
395
if (isDefinedInput(IMPORTSTEP)) {
396                     try {
397                         // get the name of the step that we are going to listen
398
// to
399
stepName = getInputStringValue(IMPORTSTEP);
400                         if (getOutputNames().size() == 1) {
401                             outputName = (String JavaDoc) getOutputNames().iterator().next();
402                         }
403                         boolean foundStep = false;
404                         if (stepName != null && outputName != null) {
405                             ArrayList JavaDoc stepList = trans.getSteps();
406                             // find the specified step
407
for (int stepNo = 0; stepNo < stepList.size(); stepNo++) {
408                                 // get the next step
409
StepMetaDataCombi step = (StepMetaDataCombi) stepList.get(stepNo);
410                                 if (step.stepname.equals(stepName)) {
411                                     if (debug)
412                                         debug(Messages.getString("Kettle.DEBUG_FOUND_STEP_IMPORTER")); //$NON-NLS-1$
413
// this is the step we are looking for
414
if (debug)
415                                         debug(Messages.getString("Kettle.DEBUG_GETTING_STEP_METADATA")); //$NON-NLS-1$
416
Row row = transMeta.getStepFields(stepName);
417                                     // create the metadata that the Pentaho
418
// result set needs
419
String JavaDoc fieldNames[] = row.getFieldNames();
420                                     String JavaDoc columns[][] = new String JavaDoc[1][fieldNames.length];
421                                     for (int column = 0; column < fieldNames.length; column++) {
422                                         columns[0][column] = fieldNames[column];
423                                     }
424                                     if (debug)
425                                         debug(Messages.getString("Kettle.DEBUG_CREATING_RESULTSET_METADATA")); //$NON-NLS-1$
426

427                                     MemoryMetaData metaData = new MemoryMetaData(columns, null);
428                                     results = new MemoryResultSet(metaData);
429                                     // add ourself as a row listener
430
step.step.addRowListener(this);
431                                     foundStep = true;
432                                     break;
433                                 }
434                             }
435
436                         }
437                         if (!foundStep) {
438                             error(Messages.getErrorString("Kettle.ERROR_0012_ROW_LISTENER_CREATE_FAILED")); //$NON-NLS-1$
439
}
440                     } catch (Exception JavaDoc e) {
441                         error(Messages.getErrorString("Kettle.ERROR_0012_ROW_LISTENER_CREATE_FAILED"), e); //$NON-NLS-1$
442
return false;
443                     }
444                 }
445
446                 try {
447                     if (debug)
448                         debug(Messages.getString("Kettle.DEBUG_STARTING_TRANSFORMATION")); //$NON-NLS-1$
449
trans.startThreads();
450                 } catch (Throwable JavaDoc t) {
451                     error(Messages.getErrorString("Kettle.ERROR_0013_TRANSFORMATION_START_FAILED"), t); //$NON-NLS-1$
452
return false;
453                 }
454
455                 try {
456                     // It's running in a separate tread to allow monitoring,
457
// etc.
458
if (debug)
459                         debug(Messages.getString("Kettle.DEBUG_TRANSFORMATION_RUNNING")); //$NON-NLS-1$
460
trans.waitUntilFinished();
461                 } catch (Throwable JavaDoc t) {
462                     error(Messages.getErrorString("Kettle.ERROR_0014_ERROR_DURING_EXECUTE")); //$NON-NLS-1$
463
return false;
464                 }
465                 // Dump the Kettle log...
466
info(kettleUserAppender.getBuffer().toString());
467                 if (outputName != null && results != null) {
468                     if (debug)
469                         debug(Messages.getString("Kettle.DEBUG_SETTING_OUTPUT")); //$NON-NLS-1$
470
setOutputValue(outputName, results);
471                 }
472             }
473         } catch (Exception JavaDoc e) {
474             error(Messages.getErrorString("Kettle.ERROR_0008_ERROR_RUNNING", e.toString()), e); //$NON-NLS-1$
475
success = false;
476         } finally {
477             if (trans != null) {
478                 LocalVariables.getInstance().removeKettleVariables(Thread.currentThread().getName());
479             }
480         }
481         return success;
482
483     }
484
485     private boolean executeJob(JobMeta jobMeta, Repository repository, LogWriter logWriter) {
486         boolean success = true;
487         Job job = null;
488         try {
489             if (jobMeta != null) {
490                 try {
491                     job = new Job(logWriter, StepLoader.getInstance(), repository, jobMeta);
492                 } catch (Throwable JavaDoc t) {
493                     error(Messages.getErrorString("Kettle.ERROR_0021_BAD_JOB_METADATA"), t); //$NON-NLS-1$
494
return false;
495                 }
496
497             }
498             if (job == null) {
499                 error(Messages.getErrorString("Kettle.ERROR_0021_BAD_JOB_METADATA")); //$NON-NLS-1$
500
info(kettleUserAppender.getBuffer().toString());
501                 return false;
502             }
503             if (job != null) {
504                 // OK, we have the job, now run it!
505
Result result = null;
506                 try {
507                     if (debug)
508                         debug(Messages.getString("Kettle.DEBUG_STARTING_JOB")); //$NON-NLS-1$
509
KettleSystemListener.environmentInit();
510                     result = job.execute();
511                 } catch (Throwable JavaDoc t) {
512                     error(Messages.getErrorString("Kettle.ERROR_002_JOB_START_FAILED"), t); //$NON-NLS-1$
513
return false;
514                 }
515
516                 try {
517                     // It's running in a separate tread to allow monitoring,
518
// etc.
519
if (debug)
520                         debug(Messages.getString("Kettle.DEBUG_JOB_RUNNING")); //$NON-NLS-1$
521
job.waitUntilFinished(5000000);
522                     if (!result.getResult()) {
523                         error(Messages.getErrorString("Kettle.ERROR_0014_ERROR_DURING_EXECUTE")); //$NON-NLS-1$
524
info(kettleUserAppender.getBuffer().toString());
525                         return false;
526                     }
527                 } catch (Throwable JavaDoc t) {
528                     error(Messages.getErrorString("Kettle.ERROR_0014_ERROR_DURING_EXECUTE"), t); //$NON-NLS-1$
529
return false;
530                 }
531                 // Dump the Kettle log...
532
info(kettleUserAppender.getBuffer().toString());
533             }
534         } catch (Exception JavaDoc e) {
535             error(Messages.getErrorString("Kettle.ERROR_0008_ERROR_RUNNING", e.toString()), e); //$NON-NLS-1$
536
success = false;
537         } finally {
538             if (job != null) {
539                 LocalVariables.getInstance().removeKettleVariables(Thread.currentThread().getName());
540             }
541         }
542         return success;
543
544     }
545
546     private TransMeta loadTransformFromRepository(String JavaDoc directoryName, String JavaDoc transformationName, Repository repository, LogWriter logWriter) {
547         if (debug)
548             debug(Messages.getString("Kettle.DEBUG_DIRECTORY", directoryName)); //$NON-NLS-1$
549
if (debug)
550             debug(Messages.getString("Kettle.DEBUG_TRANSFORMATION", transformationName)); //$NON-NLS-1$
551
TransMeta transMeta = null;
552         try {
553
554             if (debug)
555                 debug(Messages.getString("Kettle.DEBUG_FINDING_DIRECTORY")); //$NON-NLS-1$
556

557             // Find the directory specified.
558
RepositoryDirectory repositoryDirectory = null;
559             try {
560                 repositoryDirectory = repository.getDirectoryTree().findDirectory(directoryName);
561             } catch (Throwable JavaDoc t) {
562                 error(Messages.getErrorString("Kettle.ERROR_0006_DIRECTORY_NOT_FOUND", directoryName), t); //$NON-NLS-1$
563
return null;
564             }
565             if (repositoryDirectory == null) {
566                 error(Messages.getErrorString("Kettle.ERROR_0006_DIRECTORY_NOT_FOUND", directoryName)); //$NON-NLS-1$
567
return null;
568             }
569
570             if (repositoryDirectory != null) {
571                 if (debug)
572                     debug(Messages.getString("Kettle.DEBUG_GETTING_TRANSFORMATION_METADATA")); //$NON-NLS-1$
573

574                 try {
575                     // Load the transformation from the repository
576
transMeta = new TransMeta(repository, transformationName, repositoryDirectory);
577                 } catch (Throwable JavaDoc t) {
578                     error(Messages.getErrorString("Kettle.ERROR_0009_TRANSFROMATION_METADATA_NOT_FOUND", repositoryDirectory + "/" + transformationName), t); //$NON-NLS-1$ //$NON-NLS-2$
579
return null;
580                 }
581                 if (transMeta == null) {
582                     error(Messages.getErrorString("Kettle.ERROR_0009_TRANSFROMATION_METADATA_NOT_FOUND", repositoryDirectory + "/" + transformationName)); //$NON-NLS-1$ //$NON-NLS-2$
583
info(kettleUserAppender.getBuffer().toString());
584                     return null;
585                 } else {
586                     return transMeta;
587                 }
588             }
589
590             if (debug)
591                 debug(kettleUserAppender.getBuffer().toString());
592             // OK, close shop!
593

594         } catch (Throwable JavaDoc e) {
595             error(Messages.getErrorString("Kettle.ERROR_0008_ERROR_RUNNING", e.toString()), e); //$NON-NLS-1$
596
}
597         return null;
598     }
599
600     private JobMeta loadJobFromRepository(String JavaDoc directoryName, String JavaDoc jobName, Repository repository, LogWriter logWriter) {
601         if (debug)
602             debug(Messages.getString("Kettle.DEBUG_DIRECTORY", directoryName)); //$NON-NLS-1$
603
if (debug)
604             debug(Messages.getString("Kettle.DEBUG_JOB", jobName)); //$NON-NLS-1$
605
JobMeta jobMeta = null;
606         try {
607
608             if (debug)
609                 debug(Messages.getString("Kettle.DEBUG_FINDING_DIRECTORY")); //$NON-NLS-1$
610

611             // Find the directory specified.
612
RepositoryDirectory repositoryDirectory = null;
613             try {
614                 repositoryDirectory = repository.getDirectoryTree().findDirectory(directoryName);
615             } catch (Throwable JavaDoc t) {
616                 error(Messages.getErrorString("Kettle.ERROR_0006_DIRECTORY_NOT_FOUND", directoryName), t); //$NON-NLS-1$
617
return null;
618             }
619             if (repositoryDirectory == null) {
620                 error(Messages.getErrorString("Kettle.ERROR_0006_DIRECTORY_NOT_FOUND", directoryName)); //$NON-NLS-1$
621
return null;
622             }
623
624             if (repositoryDirectory != null) {
625                 if (debug)
626                     debug(Messages.getString("Kettle.DEBUG_GETTING_JOB_METADATA")); //$NON-NLS-1$
627

628                 try {
629                     // Load the transformation from the repository
630
jobMeta = new JobMeta(logWriter, repository, jobName, repositoryDirectory);
631                 } catch (Throwable JavaDoc t) {
632                     error(Messages.getErrorString("Kettle.ERROR_0020_JOB_METADATA_NOT_FOUND", repositoryDirectory + "/" + jobName), t); //$NON-NLS-1$ //$NON-NLS-2$
633
return null;
634                 }
635                 if (jobMeta == null) {
636                     error(Messages.getErrorString("Kettle.ERROR_0020_JOB_METADATA_NOT_FOUND", repositoryDirectory + "/" + jobName)); //$NON-NLS-1$ //$NON-NLS-2$
637
info(kettleUserAppender.getBuffer().toString());
638                     return null;
639                 } else {
640                     return jobMeta;
641                 }
642             }
643
644             if (debug)
645                 debug(kettleUserAppender.getBuffer().toString());
646             // OK, close shop!
647

648         } catch (Throwable JavaDoc e) {
649             error(Messages.getErrorString("Kettle.ERROR_0008_ERROR_RUNNING", e.toString()), e); //$NON-NLS-1$
650
}
651         return null;
652     }
653
654     private Repository connectToRepository(LogWriter logWriter) {
655         boolean useRepository = PentahoSystem.getSystemSetting("kettle/settings.xml", "repository.type", "files").equals("rdbms"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
656

657         if (!useRepository) {
658             return null;
659         }
660
661         try {
662             if (debug)
663                 debug(Messages.getString("Kettle.DEBUG_META_REPOSITORY")); //$NON-NLS-1$
664

665             RepositoriesMeta repositoriesMeta = null;
666             try {
667                 repositoriesMeta = new RepositoriesMeta(logWriter);
668             } catch (Throwable JavaDoc t) {
669                 error(Messages.getErrorString("Kettle.ERROR_0007_BAD_META_REPOSITORY"), t); //$NON-NLS-1$
670
return null;
671             }
672             if (repositoriesMeta == null) {
673                 error(Messages.getErrorString("Kettle.ERROR_0007_BAD_META_REPOSITORY")); //$NON-NLS-1$
674
info(kettleUserAppender.getBuffer().toString());
675                 return null;
676             }
677
678             if (debug)
679                 debug(Messages.getString("Kettle.DEBUG_POPULATING_META")); //$NON-NLS-1$
680
try {
681                 // TODO: add support for specified repositories.xml files...
682
repositoriesMeta.readData(); // Read from the default $HOME/.kettle/repositories.xml file.
683
} catch (Throwable JavaDoc t) {
684                 error(Messages.getErrorString("Kettle.ERROR_0018_META_REPOSITORY_NOT_POPULATED"), t); //$NON-NLS-1$
685
return null;
686             }
687             if (repositoriesXMLFile != null && !"".equals(repositoriesXMLFile)) //$NON-NLS-1$
688
{
689                 error(Messages.getErrorString("Kettle.ERROR_0017_XML_REPOSITORY_NOT_SUPPORTED")); //$NON-NLS-1$
690
info(kettleUserAppender.getBuffer().toString());
691                 return null;
692             }
693
694             if (debug)
695                 debug(Messages.getString("Kettle.DEBUG_FINDING_REPOSITORY")); //$NON-NLS-1$
696
// Find the specified repository.
697
RepositoryMeta repositoryMeta = null;
698             try {
699                 repositoryMeta = repositoriesMeta.findRepository(repositoryName);
700             } catch (Throwable JavaDoc t) {
701                 error(Messages.getErrorString("Kettle.ERROR_0004_REPOSITORY_NOT_FOUND", repositoryName), t); //$NON-NLS-1$
702
return null;
703             }
704
705             if (repositoryMeta == null) {
706                 error(Messages.getErrorString("Kettle.ERROR_0004_REPOSITORY_NOT_FOUND", repositoryName)); //$NON-NLS-1$
707
info(kettleUserAppender.getBuffer().toString());
708                 return null;
709             }
710
711             if (debug)
712                 debug(Messages.getString("Kettle.DEBUG_GETTING_REPOSITORY")); //$NON-NLS-1$
713
Repository repository = null;
714             UserInfo userInfo = null;
715             try {
716                 repository = new Repository(logWriter, repositoryMeta, userInfo);
717             } catch (Throwable JavaDoc t) {
718                 error(Messages.getErrorString("Kettle.ERROR_0016_COULD_NOT_GET_REPOSITORY_INSTANCE"), t); //$NON-NLS-1$
719
return null;
720             }
721
722             // OK, now try the username and password
723
if (debug)
724                 debug(Messages.getString("Kettle.DEBUG_CONNECTING")); //$NON-NLS-1$
725
if (repository.connect(getClass().getName())) {
726                 try {
727                     userInfo = new UserInfo(repository, username, password);
728                 } catch (KettleException e) {
729                     userInfo = null;
730                 } finally {
731                 }
732             } else {
733                 error(Messages.getErrorString("Kettle.ERROR_0005_LOGIN_FAILED")); //$NON-NLS-1$
734
info(kettleUserAppender.getBuffer().toString());
735                 return null;
736             }
737
738             // OK, the repository is open and ready to use.
739
if (debug)
740                 debug(Messages.getString("Kettle.DEBUG_FINDING_DIRECTORY")); //$NON-NLS-1$
741

742             return repository;
743
744         } catch (Throwable JavaDoc e) {
745             error(Messages.getErrorString("Kettle.ERROR_0008_ERROR_RUNNING", e.toString()), e); //$NON-NLS-1$
746
}
747         return null;
748     }
749
750     public void done() {
751
752     }
753
754     public void rowReadEvent(Row row) {
755     }
756
757     public void rowWrittenEvent(Row row) {
758
759         if (results == null) {
760             return;
761         }
762         Object JavaDoc pentahoRow[] = new Object JavaDoc[results.getColumnCount()];
763         for (int columnNo = 0; columnNo < results.getColumnCount(); columnNo++) {
764             Value value = row.getValue(columnNo);
765             switch (value.getType()) {
766             case Value.VALUE_TYPE_BIGNUMBER:
767                 pentahoRow[columnNo] = value.getBigNumber();
768                 break;
769             case Value.VALUE_TYPE_BOOLEAN:
770                 pentahoRow[columnNo] = new Boolean JavaDoc(value.getBoolean());
771                 break;
772             case Value.VALUE_TYPE_DATE:
773                 pentahoRow[columnNo] = value.getDate();
774                 break;
775             case Value.VALUE_TYPE_INTEGER:
776                 pentahoRow[columnNo] = new Long JavaDoc(value.getInteger());
777                 break;
778             case Value.VALUE_TYPE_NONE:
779                 pentahoRow[columnNo] = value.getString();
780                 break;
781             case Value.VALUE_TYPE_NUMBER:
782                 pentahoRow[columnNo] = new Double JavaDoc(value.getNumber());
783                 break;
784             case Value.VALUE_TYPE_STRING:
785                 pentahoRow[columnNo] = value.getString();
786                 break;
787             default:
788                 pentahoRow[columnNo] = value.getString();
789             }
790         }
791         results.addRow(pentahoRow);
792
793     }
794
795 }
796
Popular Tags