1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.sys.context;
17
18 import java.io.File;
19 import java.util.Arrays;
20 import java.util.Date;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.log4j.Appender;
24 import org.apache.log4j.FileAppender;
25 import org.apache.log4j.Logger;
26 import org.apache.log4j.NDC;
27 import org.kuali.ole.sys.OLEConstants;
28 import org.kuali.ole.sys.batch.BatchSpringContext;
29 import org.kuali.ole.sys.batch.Job;
30 import org.kuali.ole.sys.batch.Step;
31 import org.kuali.rice.core.api.config.property.ConfigurationService;
32 import org.kuali.rice.core.api.datetime.DateTimeService;
33 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
34 import org.kuali.rice.krad.service.KualiModuleService;
35 import org.kuali.rice.krad.service.ModuleService;
36
37 public class BatchStepRunner {
38 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BatchStepRunner.class);
39
40 public static void main(String[] args) {
41 if (args.length < 1) {
42 System.err.println("ERROR: You must pass the name of the step to run on the command line.");
43 System.exit(8);
44 }
45 try {
46 Log4jConfigurer.configureLogging(false);
47 SpringContextForBatchRunner.initializeKfs();
48 String[] stepNames;
49 if (args[0].indexOf(",") > 0) {
50 stepNames = StringUtils.split(args[0], ",");
51 }
52 else {
53 stepNames = new String[] { args[0] };
54 }
55 ParameterService parameterService = SpringContext.getBean(ParameterService.class);
56 DateTimeService dateTimeService = SpringContext.getBean(DateTimeService.class);
57
58 String jobName = args.length >= 2 ? args[1] : OLEConstants.BATCH_STEP_RUNNER_JOB_NAME;
59 Date jobRunDate = dateTimeService.getCurrentDate();
60 LOG.info("Executing job: " + jobName + " steps: " + Arrays.toString(stepNames));
61 for (int i = 0; i < stepNames.length; ++i) {
62 Step step = BatchSpringContext.getStep(stepNames[i]);
63 if ( step != null ) {
64 Step unProxiedStep = (Step) ProxyUtils.getTargetIfProxied(step);
65 Class<?> stepClass = unProxiedStep.getClass();
66
67 ModuleService module = SpringContext.getBean(KualiModuleService.class).getResponsibleModuleService( stepClass );
68 Appender ndcAppender = null;
69 String nestedDiagnosticContext =
70 StringUtils.substringAfter( module.getModuleConfiguration().getNamespaceCode(), "-").toLowerCase()
71 + File.separator + step.getName()
72 + "-" + dateTimeService.toDateTimeStringForFilename(dateTimeService.getCurrentDate());
73 boolean ndcSet = false;
74 try {
75 ndcAppender = new FileAppender(Logger.getRootLogger().getAppender("LogFile").getLayout(), getLogFileName(nestedDiagnosticContext));
76 ndcAppender.addFilter(new NDCFilter(nestedDiagnosticContext));
77 Logger.getRootLogger().addAppender(ndcAppender);
78 NDC.push(nestedDiagnosticContext);
79 ndcSet = true;
80 } catch (Exception ex) {
81 LOG.warn("Could not initialize custom logging for step: " + step.getName(), ex);
82 }
83 try {
84 if (!Job.runStep(parameterService, jobName, i, step, jobRunDate) ) {
85 System.exit(4);
86 }
87 } catch ( RuntimeException ex ) {
88 throw ex;
89 } finally {
90 if ( ndcSet ) {
91 if (ndcAppender != null) {
92 ndcAppender.close();
93 }
94 Logger.getRootLogger().removeAppender(ndcAppender);
95 NDC.pop();
96 }
97 }
98 } else {
99 throw new IllegalArgumentException( "Unable to find step: " + stepNames[i] );
100 }
101 }
102 LOG.info("Finished executing job: " + jobName + " steps: " + Arrays.toString(stepNames));
103 System.exit(0);
104 }
105 catch (Throwable t) {
106 System.err.println("ERROR: Exception caught: ");
107 t.printStackTrace(System.err);
108 System.exit(8);
109 }
110 }
111
112 protected static String getLogFileName(String nestedDiagnosticContext) {
113 return SpringContext.getBean( ConfigurationService.class ).getPropertyValueAsString(OLEConstants.REPORTS_DIRECTORY_KEY)
114 + File.separator
115 + nestedDiagnosticContext + ".log";
116 }
117
118 }