Fix: Update Sonar needed parameters
[oam/tr069-adapter.git] / common / src / main / java / org / commscope / tr069adapter / common / scheduler / impl / QuartzSchedulerProducer.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 package org.commscope.tr069adapter.common.scheduler.impl;\r
20 \r
21 import java.io.BufferedInputStream;\r
22 import java.io.FileInputStream;\r
23 import java.io.IOException;\r
24 import java.io.InputStream;\r
25 import java.util.Properties;\r
26 \r
27 import org.apache.commons.logging.Log;\r
28 import org.apache.commons.logging.LogFactory;\r
29 import org.quartz.Scheduler;\r
30 import org.quartz.SchedulerException;\r
31 import org.quartz.impl.StdSchedulerFactory;\r
32 import org.springframework.stereotype.Component;\r
33 \r
34 @Component\r
35 public class QuartzSchedulerProducer {\r
36 \r
37   private static final Log logger = LogFactory.getLog(QuartzSchedulerProducer.class);\r
38 \r
39   public Scheduler getStdScheduler() {\r
40     StdSchedulerFactory schedulerFactory = new StdSchedulerFactory();\r
41     try {\r
42       schedulerFactory.initialize("config/quartz.properties");\r
43       Properties props = new Properties();\r
44       props.setProperty("org.quartz.scheduler.instanceName",\r
45           props.getProperty("org.quartz.scheduler.instanceName") + "_standby");\r
46       return schedulerFactory.getScheduler();\r
47     } catch (SchedulerException e) {\r
48       logger.error("SchedulerException : {}" + e.getMessage());\r
49     }\r
50 \r
51     return null;\r
52   }\r
53 \r
54   public Properties initialize(String filename) throws SchedulerException {\r
55 \r
56     InputStream is = null;\r
57     Properties props = new Properties();\r
58 \r
59     is = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);\r
60 \r
61     try {\r
62       if (is != null) {\r
63         is = new BufferedInputStream(is);\r
64       } else {\r
65         is = new BufferedInputStream(new FileInputStream(filename));\r
66       }\r
67       props.load(is);\r
68     } catch (IOException ioe) {\r
69       throw new SchedulerException("Properties file: '" + filename + "' could not be read.", ioe);\r
70     } finally {\r
71       if (is != null)\r
72         try {\r
73           is.close();\r
74         } catch (IOException ignore) {\r
75           logger.error("while initialize : {}" + ignore.getMessage());\r
76         }\r
77     }\r
78 \r
79     return props;\r
80   }\r
81 }\r