- this.parameters = parameters;
- }
-
- @Override
- public void init() throws PfModelException {
- LOGGER.debug("opening the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
- parameters.getPersistenceUnit());
-
- if (pfDao != null) {
- String errorMessage = "provider is already initialized";
- LOGGER.warn(errorMessage);
- throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage);
- }
-
- // Parameters for the DAO
- final DaoParameters daoParameters = new DaoParameters();
- daoParameters.setPluginClass(DefaultPfDao.class.getName());
- daoParameters.setPersistenceUnit(parameters.getPersistenceUnit());
-
- // @formatter:off
- Properties jdbcProperties = new Properties();
- jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, parameters.getDatabaseDriver());
- jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, parameters.getDatabaseUrl());
- jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, parameters.getDatabaseUser());
- jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, parameters.getDatabasePassword());
- // @formatter:on
-
- daoParameters.setJdbcProperties(jdbcProperties);
-
- try {
- pfDao = new PfDaoFactory().createPfDao(daoParameters);
- pfDao.init(daoParameters);
- } catch (Exception exc) {
- String errorMessage = "could not create Data Access Object (DAO) using url \"" + parameters.getDatabaseUrl()
- + "\" and persistence unit \"" + parameters.getPersistenceUnit() + "\"";
-
- this.close();
- throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc);
- }
- }
-
- @Override
- public void close() throws PfModelException {
- LOGGER.debug("closing the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
- parameters.getPersistenceUnit());
-
- if (pfDao != null) {
- pfDao.close();
- pfDao = null;
- }
-
- LOGGER.debug("closed the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
- parameters.getPersistenceUnit());