View Javadoc
1   package fr.ifremer.quadrige2.synchro.server.meta;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 Synchro server
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  
27  import fr.ifremer.common.synchro.config.SynchroConfiguration;
28  import fr.ifremer.common.synchro.meta.SynchroDatabaseMetadata;
29  import fr.ifremer.common.synchro.service.SynchroDatabaseConfiguration;
30  import fr.ifremer.quadrige2.synchro.meta.data.DataSynchroTables;
31  import fr.ifremer.quadrige2.synchro.service.data.DataSynchroContext;
32  import fr.ifremer.quadrige2.synchro.service.data.DataSynchroDatabaseConfiguration;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.nuiton.i18n.I18n;
36  import org.springframework.beans.factory.InitializingBean;
37  import org.springframework.beans.factory.annotation.Autowired;
38  import org.springframework.context.annotation.Lazy;
39  import org.springframework.jdbc.datasource.DataSourceUtils;
40  import org.springframework.stereotype.Component;
41  
42  import javax.sql.DataSource;
43  import java.sql.Connection;
44  import java.sql.SQLException;
45  
46  @Component("dataSynchroDatabaseMetadata")
47  @Lazy
48  public class DataSynchroDatabaseMetadata extends SynchroDatabaseMetadata implements InitializingBean {
49  
50      /** Logger. */
51      private static final Log log =
52              LogFactory.getLog(DataSynchroDatabaseMetadata.class);
53  
54      private DataSource dataSource;
55      
56      public DataSynchroDatabaseMetadata(Connection connection, SynchroDatabaseConfiguration config) throws SQLException {
57          super(connection, config);
58      }
59  
60      @Autowired
61      public DataSynchroDatabaseMetadata(DataSource dataSource, SynchroConfiguration config) throws SQLException {
62          super(getConnection(dataSource), createDatabaseConfiguration(config));
63          this.dataSource = dataSource;
64      }
65      
66  
67      @Override
68      public void afterPropertiesSet() throws Exception {
69  
70          // Prepare data tables
71          log.info(I18n.t("quadrige2.synchro.server.meta.data.load",
72                  config.getJdbcUrl(),
73                  config.getJdbcUser()));
74          prepare(DataSynchroTables.getImportTablesIncludes());
75          
76          // Close the connection
77          if (dataSource != null) {
78              DataSourceUtils.releaseConnection(getConnection(), dataSource);
79          }
80          
81          // Close (to free memory)
82          close();    
83       }
84  
85      /* -- internal methods -- */
86  
87      private static Connection getConnection(DataSource dataSource) {
88          return DataSourceUtils.getConnection(dataSource);
89      }
90      
91      public static DataSynchroDatabaseConfiguration createDatabaseConfiguration(SynchroConfiguration config) {
92          DataSynchroDatabaseConfiguration result = new DataSynchroDatabaseConfiguration(new DataSynchroContext(), config.getImportConnectionProperties(), false);
93          result.setFullMetadataEnable(false);
94          result.setReadOnly(true);
95  
96          return result;
97      }
98  }