View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Ensure Quartz database initialization occurs only if Quartz tables are not already present.
33   *
34   * @see org.springframework.jdbc.datasource.init.DataSourceInitializer
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  }