Initial source code
[oam/tr069-adapter.git] / common / src / main / java / org / commscope / tr069adapter / common / scheduler / impl / QuartzJob.java
1 /*\r
2  * ============LICENSE_START========================================================================\r
3  * ONAP : tr-069-adapter\r
4  * =================================================================================================\r
5  * Copyright (C) 2020 CommScope Inc Intellectual Property.\r
6  * =================================================================================================\r
7  * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,\r
8  * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You\r
9  * may obtain a copy of the License at\r
10  *\r
11  * http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,\r
14  * either express or implied. See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  * ===============LICENSE_END=======================================================================\r
17  */\r
18 \r
19 \r
20 package org.commscope.tr069adapter.common.scheduler.impl;\r
21 \r
22 import static org.commscope.tr069adapter.common.scheduler.impl.QuartzSchedulerConstants.JOB_NAME;\r
23 import static org.commscope.tr069adapter.common.scheduler.impl.QuartzSchedulerConstants.SUB_SYSTEM;\r
24 \r
25 import org.apache.commons.logging.Log;\r
26 import org.apache.commons.logging.LogFactory;\r
27 import org.commscope.tr069adapter.common.scheduler.ExecutionContext;\r
28 import org.commscope.tr069adapter.common.scheduler.Job;\r
29 import org.quartz.DisallowConcurrentExecution;\r
30 import org.quartz.JobDataMap;\r
31 import org.quartz.JobExecutionContext;\r
32 import org.quartz.JobExecutionException;\r
33 import org.quartz.PersistJobDataAfterExecution;\r
34 import org.springframework.context.ApplicationContext;\r
35 \r
36 @PersistJobDataAfterExecution\r
37 @DisallowConcurrentExecution\r
38 public class QuartzJob implements org.quartz.Job {\r
39 \r
40   private static ApplicationContext appctx;\r
41 \r
42   private static final Log logger = LogFactory.getLog(QuartzJob.class);\r
43 \r
44   protected void executeInternal(JobExecutionContext context) throws JobExecutionException {\r
45     JobDataMap dataMap = null;\r
46     try {\r
47       dataMap = context.getJobDetail().getJobDataMap();\r
48       ExecutionContext jobContext = new ExecutionContext(context.getJobDetail().getKey().getName(),\r
49           dataMap.getString(SUB_SYSTEM), dataMap.getString(JOB_NAME));\r
50       for (String key : dataMap.getKeys()) {\r
51         jobContext.addJobData(key, dataMap.get(key));\r
52       }\r
53       // add scheduling information\r
54       jobContext.addJobData("SCHEDULED_TIME", context.getScheduledFireTime());\r
55       String beanName = jobContext.getJobName();\r
56       Job job = (Job) appctx.getBean(beanName);\r
57       job.execute(jobContext);\r
58 \r
59     } catch (Exception exception) {\r
60       if (dataMap != null) {\r
61         logger.error("executeInternal : Executing job " + dataMap.getString(JOB_NAME)\r
62             + ", SubSystem " + dataMap.getString(SUB_SYSTEM));\r
63       }\r
64       logger.error("executeInternal : Could not execute job. Exception while executing the job",\r
65           exception);\r
66       throw new JobExecutionException(exception);\r
67     }\r
68   }\r
69 \r
70   public static String getJndiName(String subSystem, String jobName) {\r
71     String jndiName = "csadapter/";\r
72     jndiName += subSystem + "/" + jobName + "#" + Job.class.getName();\r
73     return jndiName;\r
74   }\r
75 \r
76   public void execute(JobExecutionContext context) {\r
77     try {\r
78       executeInternal(context);\r
79     } catch (JobExecutionException e) {\r
80       logger.error("Exception : " + e.getMessage());\r
81     }\r
82   }\r
83 \r
84   public static void setApplicationContext(ApplicationContext ctx) {\r
85     appctx = ctx;\r
86   }\r
87 \r
88   public static ApplicationContext getApplicationContext() {\r
89     return appctx;\r
90   }\r
91 }\r