1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.core.provisioning.java.job;
20
21 import javax.sql.DataSource;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.springframework.beans.factory.InitializingBean;
25 import org.springframework.jdbc.BadSqlGrammarException;
26 import org.springframework.jdbc.core.JdbcTemplate;
27 import org.springframework.jdbc.datasource.init.DatabasePopulator;
28 import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
29 import org.springframework.util.Assert;
30
31
32
33
34
35
36 public class SchedulerDBInit implements InitializingBean {
37
38 private static final Logger LOG = LoggerFactory.getLogger(SchedulerDBInit.class);
39
40 private DataSource dataSource;
41
42 private DatabasePopulator databasePopulator;
43
44 public void setDataSource(final DataSource dataSource) {
45 this.dataSource = dataSource;
46 }
47
48 public void setDatabasePopulator(final DatabasePopulator databasePopulator) {
49 this.databasePopulator = databasePopulator;
50 }
51
52 @Override
53 public void afterPropertiesSet() throws Exception {
54 Assert.state(this.dataSource != null, "DataSource must be set");
55 Assert.state(this.databasePopulator != null, "DatabasePopulator must be set");
56
57 JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource);
58 boolean existingData;
59 try {
60 existingData = jdbcTemplate.queryForObject("SELECT COUNT(0) FROM QRTZ_SCHEDULER_STATE", Integer.class) >= 0;
61 } catch (BadSqlGrammarException e) {
62 LOG.debug("Could not access to table QRTZ_SCHEDULER_STATE", e);
63 existingData = false;
64 }
65
66 if (existingData) {
67 LOG.info("Quartz tables found in the database, leaving untouched");
68 } else {
69 LOG.info("No Quartz tables found, creating");
70
71 DatabasePopulatorUtils.execute(databasePopulator, this.dataSource);
72 }
73 }
74 }