Back to addression SessionFactory

This commit is contained in:
Cyrus 2022-07-21 16:18:33 -04:00
parent ba8c2d80ab
commit 509907472a
2 changed files with 198 additions and 205 deletions

View File

@ -6,6 +6,11 @@ import hirs.utils.LogConfigurationUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
@ -18,7 +23,6 @@ import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@ -34,6 +38,8 @@ import java.security.KeyStoreException;
import java.security.PrivateKey;
import java.security.Security;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
@ -93,14 +99,31 @@ public class AttestationCertificateAuthorityConfiguration implements WebMvcConfi
* @return session factory
*/
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan("hirs");
sessionFactory.setHibernateProperties(hibernateProperties());
public SessionFactory sessionFactory() {
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(getSettings()).build();
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
Metadata metadata = metadataSources.buildMetadata();
// LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build();
// sessionFactory.setDataSource(dataSource());
// sessionFactory.setPackagesToScan("hirs");
// sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
private Map<String, String> getSettings() {
Map<String, String> settings = new HashMap<>();
settings.put("connection.driver_class", environment.getRequiredProperty("persistence.db.driverClass"));
settings.put("dialect", environment.getRequiredProperty("persistence.hibernate.dialect"));
settings.put("hibernate.connection.url", environment.getRequiredProperty("persistence.db.url"));
settings.put("hibernate.connection.username", environment.getRequiredProperty("persistence.db.username"));
settings.put("hibernate.connection.password", environment.getRequiredProperty("persistence.db.password"));
// settings.put("hibernate.current_session_context_class", );
settings.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
settings.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
return settings;
}
/**
* Configures the data source to be used by the hibernate session factory.
*
@ -134,12 +157,12 @@ public class AttestationCertificateAuthorityConfiguration implements WebMvcConfi
@Bean
public Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect",
environment.getRequiredProperty("persistence.hibernate.dialect"));
properties.put("hibernate.show_sql",
environment.getRequiredProperty("hibernate.show_sql"));
properties.put("hibernate.format_sql",
environment.getRequiredProperty("hibernate.format_sql"));
// properties.put("hibernate.dialect",
// environment.getRequiredProperty("persistence.hibernate.dialect"));
// properties.put("hibernate.show_sql",
// environment.getRequiredProperty("hibernate.show_sql"));
// properties.put("hibernate.format_sql",
// environment.getRequiredProperty("hibernate.format_sql"));
properties.put("hibernate.hbm2ddl.auto",
environment.getRequiredProperty("persistence.hibernate.ddl"));
// properties.put("hibernate.current_session_context_class", "thread");
@ -214,7 +237,7 @@ public class AttestationCertificateAuthorityConfiguration implements WebMvcConfi
*/
@Bean
public HibernateTransactionManager getTransactionManager() {
return new HibernateTransactionManager(sessionFactory().getObject());
return new HibernateTransactionManager(sessionFactory());
}
/**

View File

@ -1,35 +1,5 @@
package hirs.attestationca.configuration;
import hirs.attestationca.AttestationCertificateAuthorityConfiguration;
import hirs.attestationca.persist.DBCertificateManager;
import hirs.attestationca.persist.DBDeviceGroupManager;
import hirs.attestationca.persist.DBDeviceManager;
import hirs.attestationca.persist.DBManager;
import hirs.attestationca.persist.DBPolicyManager;
import hirs.attestationca.persist.DBPortalInfoManager;
import hirs.attestationca.persist.DBReferenceEventManager;
import hirs.attestationca.persist.DBReferenceManifestManager;
import hirs.attestationca.persist.DBReportManager;
import hirs.attestationca.persist.DBReportRequestStateManager;
import hirs.attestationca.persist.DBReportSummaryManager;
import hirs.data.persist.SupplyChainValidationSummary;
import hirs.persist.CertificateManager;
import hirs.persist.CrudManager;
import hirs.persist.DeviceGroupManager;
import hirs.persist.DeviceManager;
import hirs.persist.PolicyManager;
import hirs.persist.PortalInfoManager;
import hirs.persist.ReferenceEventManager;
import hirs.persist.ReferenceManifestManager;
import hirs.persist.ReportManager;
import hirs.persist.ReportRequestStateManager;
import hirs.persist.ReportSummaryManager;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* Persistence Configuration for Spring enabled applications. Constructs a Hibernate SessionFactory
* backed powered by a HikariCP connection pooled data source. Module-specific settings
@ -37,168 +7,168 @@ import org.springframework.context.annotation.Import;
* such as the HIRS_Portal uses this class and doesn't have a persistence-extended.properties
* file, the default persistence file will be used instead.
*/
@Configuration
@Import(AttestationCertificateAuthorityConfiguration.class)
//@Configuration
//@Import(AttestationCertificateAuthorityConfiguration.class)
public class PersistenceConfiguration {
/**
* The bean name to retrieve the default/general implementation of {@link }.
*/
public static final String DEVICE_STATE_MANAGER_BEAN_NAME = "general_db_man_bean";
@Autowired
private SessionFactory sessionFactory;
@Autowired
private long retryWaitTimeMilliseconds;
@Autowired
private int maxTransactionRetryAttempts;
/**
* Creates a {@link hirs.persist.PolicyManager} ready to use.
*
* @return {@link hirs.persist.PolicyManager}
*/
@Bean
public PolicyManager policyManager() {
DBPolicyManager manager = new DBPolicyManager(sessionFactory);
setDbManagerRetrySettings(manager);
return manager;
}
/**
* Creates a {@link hirs.persist.ReportManager} ready to use.
*
* @return {@link hirs.persist.ReportManager}
*/
@Bean
public ReportManager reportManager() {
DBReportManager manager = new DBReportManager(sessionFactory);
setDbManagerRetrySettings(manager);
return manager;
}
/**
* Creates a {@link hirs.persist.DeviceManager} ready to use.
*
* @return {@link hirs.persist.DeviceManager}
*/
@Bean
public DeviceManager deviceManager() {
DBDeviceManager manager = new DBDeviceManager(sessionFactory);
setDbManagerRetrySettings(manager);
return manager;
}
/**
* Creates a {@link hirs.persist.ReportSummaryManager} ready to use.
*
* @return {@link hirs.persist.ReportSummaryManager}
*/
@Bean
public ReportSummaryManager reportSummaryManager() {
DBReportSummaryManager manager = new DBReportSummaryManager(sessionFactory);
setDbManagerRetrySettings(manager);
return manager;
}
/**
* Creates a {@link hirs.persist.DeviceGroupManager} ready to use.
*
* @return {@link hirs.persist.DeviceGroupManager}
*/
@Bean
public DeviceGroupManager deviceGroupManager() {
DBDeviceGroupManager manager = new DBDeviceGroupManager(sessionFactory);
setDbManagerRetrySettings(manager);
return manager;
}
/**
* Creates a {@link hirs.persist.CertificateManager} ready to use.
*
* @return {@link hirs.persist.CertificateManager}
*/
@Bean
public CertificateManager certificateManager() {
DBCertificateManager manager = new DBCertificateManager(sessionFactory);
manager.setRetryTemplate(maxTransactionRetryAttempts, retryWaitTimeMilliseconds);
return manager;
}
/**
* Creates a {@link hirs.persist.ReferenceManifestManager} ready to use.
*
* @return {@link hirs.persist.ReferenceManifestManager}
*/
@Bean
public ReferenceManifestManager referenceManifestManager() {
DBReferenceManifestManager manager
= new DBReferenceManifestManager(sessionFactory);
setDbManagerRetrySettings(manager);
return manager;
}
/**
* Creates a {@link hirs.persist.ReferenceEventManager} ready to use.
*
* @return {@link hirs.persist.ReferenceEventManager}
*/
@Bean
public ReferenceEventManager referenceEventManager() {
DBReferenceEventManager manager
= new DBReferenceEventManager(sessionFactory);
setDbManagerRetrySettings(manager);
return manager;
}
/**
* Creates a {@link hirs.persist.ReportRequestStateManager} ready to use.
*
* @return {@link hirs.persist.ReportRequestStateManager}
*/
@Bean
public ReportRequestStateManager reportRequestStateManager() {
DBReportRequestStateManager manager
= new DBReportRequestStateManager(sessionFactory);
setDbManagerRetrySettings(manager);
return manager;
}
/**
* Creates a {@link hirs.persist.PortalInfoManager} ready to use.
*
* @return {@link hirs.persist.PortalInfoManager}
*/
@Bean
public PortalInfoManager portalInfoManager() {
DBPortalInfoManager manager = new DBPortalInfoManager(sessionFactory);
setDbManagerRetrySettings(manager);
return manager;
}
/**
* Creates a {@link hirs.attestationca.persist.DBManager} for SupplyChainValidationSummary persistence, ready for use.
*
* @return {@link hirs.attestationca.persist.DBManager}
*/
@Bean
public CrudManager<SupplyChainValidationSummary> supplyChainValidationSummaryManager() {
DBManager<SupplyChainValidationSummary> manager
= new DBManager<SupplyChainValidationSummary>(
SupplyChainValidationSummary.class,
sessionFactory
);
setDbManagerRetrySettings(manager);
return manager;
}
/**
* Apply the spring-wired retry template settings to the db manager.
* @param dbManager the manager to apply the retry settings to
*/
private void setDbManagerRetrySettings(final DBManager dbManager) {
dbManager.setRetryTemplate(maxTransactionRetryAttempts, retryWaitTimeMilliseconds);
}
// /**
// * The bean name to retrieve the default/general implementation of {@link }.
// */
// public static final String DEVICE_STATE_MANAGER_BEAN_NAME = "general_db_man_bean";
//
// @Autowired
// private SessionFactory sessionFactory;
//
// @Autowired
// private long retryWaitTimeMilliseconds;
//
// @Autowired
// private int maxTransactionRetryAttempts;
//
// /**
// * Creates a {@link hirs.persist.PolicyManager} ready to use.
// *
// * @return {@link hirs.persist.PolicyManager}
// */
// @Bean
// public PolicyManager policyManager() {
// DBPolicyManager manager = new DBPolicyManager(sessionFactory);
// setDbManagerRetrySettings(manager);
// return manager;
// }
//
// /**
// * Creates a {@link hirs.persist.ReportManager} ready to use.
// *
// * @return {@link hirs.persist.ReportManager}
// */
// @Bean
// public ReportManager reportManager() {
// DBReportManager manager = new DBReportManager(sessionFactory);
// setDbManagerRetrySettings(manager);
// return manager;
// }
//
// /**
// * Creates a {@link hirs.persist.DeviceManager} ready to use.
// *
// * @return {@link hirs.persist.DeviceManager}
// */
// @Bean
// public DeviceManager deviceManager() {
// DBDeviceManager manager = new DBDeviceManager(sessionFactory);
// setDbManagerRetrySettings(manager);
// return manager;
// }
//
// /**
// * Creates a {@link hirs.persist.ReportSummaryManager} ready to use.
// *
// * @return {@link hirs.persist.ReportSummaryManager}
// */
// @Bean
// public ReportSummaryManager reportSummaryManager() {
// DBReportSummaryManager manager = new DBReportSummaryManager(sessionFactory);
// setDbManagerRetrySettings(manager);
// return manager;
// }
//
// /**
// * Creates a {@link hirs.persist.DeviceGroupManager} ready to use.
// *
// * @return {@link hirs.persist.DeviceGroupManager}
// */
// @Bean
// public DeviceGroupManager deviceGroupManager() {
// DBDeviceGroupManager manager = new DBDeviceGroupManager(sessionFactory);
// setDbManagerRetrySettings(manager);
// return manager;
// }
//
// /**
// * Creates a {@link hirs.persist.CertificateManager} ready to use.
// *
// * @return {@link hirs.persist.CertificateManager}
// */
// @Bean
// public CertificateManager certificateManager() {
// DBCertificateManager manager = new DBCertificateManager(sessionFactory);
// manager.setRetryTemplate(maxTransactionRetryAttempts, retryWaitTimeMilliseconds);
// return manager;
// }
//
// /**
// * Creates a {@link hirs.persist.ReferenceManifestManager} ready to use.
// *
// * @return {@link hirs.persist.ReferenceManifestManager}
// */
// @Bean
// public ReferenceManifestManager referenceManifestManager() {
// DBReferenceManifestManager manager
// = new DBReferenceManifestManager(sessionFactory);
// setDbManagerRetrySettings(manager);
// return manager;
// }
//
// /**
// * Creates a {@link hirs.persist.ReferenceEventManager} ready to use.
// *
// * @return {@link hirs.persist.ReferenceEventManager}
// */
// @Bean
// public ReferenceEventManager referenceEventManager() {
// DBReferenceEventManager manager
// = new DBReferenceEventManager(sessionFactory);
// setDbManagerRetrySettings(manager);
// return manager;
// }
//
// /**
// * Creates a {@link hirs.persist.ReportRequestStateManager} ready to use.
// *
// * @return {@link hirs.persist.ReportRequestStateManager}
// */
// @Bean
// public ReportRequestStateManager reportRequestStateManager() {
// DBReportRequestStateManager manager
// = new DBReportRequestStateManager(sessionFactory);
// setDbManagerRetrySettings(manager);
// return manager;
// }
//
// /**
// * Creates a {@link hirs.persist.PortalInfoManager} ready to use.
// *
// * @return {@link hirs.persist.PortalInfoManager}
// */
// @Bean
// public PortalInfoManager portalInfoManager() {
// DBPortalInfoManager manager = new DBPortalInfoManager(sessionFactory);
// setDbManagerRetrySettings(manager);
// return manager;
// }
//
// /**
// * Creates a {@link hirs.attestationca.persist.DBManager} for SupplyChainValidationSummary persistence, ready for use.
// *
// * @return {@link hirs.attestationca.persist.DBManager}
// */
// @Bean
// public CrudManager<SupplyChainValidationSummary> supplyChainValidationSummaryManager() {
// DBManager<SupplyChainValidationSummary> manager
// = new DBManager<SupplyChainValidationSummary>(
// SupplyChainValidationSummary.class,
// sessionFactory
// );
// setDbManagerRetrySettings(manager);
// return manager;
// }
//
// /**
// * Apply the spring-wired retry template settings to the db manager.
// * @param dbManager the manager to apply the retry settings to
// */
// private void setDbManagerRetrySettings(final DBManager dbManager) {
// dbManager.setRetryTemplate(maxTransactionRetryAttempts, retryWaitTimeMilliseconds);
// }
}