Java Code Examples for javax.jdo.JDOHelper#getPersistenceManagerFactory()

The following examples show how to use javax.jdo.JDOHelper#getPersistenceManagerFactory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PersistenceManagerFactory.java    From Alpine with Apache License 2.0 6 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent event) {
    LOGGER.info("Initializing persistence framework");

    final String driverPath = Config.getInstance().getProperty(Config.AlpineKey.DATABASE_DRIVER_PATH);
    if (driverPath != null) {
        final JavaVersion jv = new JavaVersion();
        if (jv.getMajor() > 8) {
            LOGGER.warn("Cannot dynamically expand classpath to include database driver. This capability "
                    + "was removed in Java 11 and higher. Please add the database driver to the classpath "
                    + "when starting the application.");
        } else {
            Config.getInstance().expandClasspath(driverPath);
        }
    }

    pmf = (JDOPersistenceManagerFactory)JDOHelper.getPersistenceManagerFactory(JdoProperties.get(), "Alpine");

    // Ensure that the UpgradeMetaProcessor and SchemaVersion tables are created NOW, not dynamically at runtime.
    final PersistenceNucleusContext ctx = pmf.getNucleusContext();
    final Set<String> classNames = new HashSet<>();
    classNames.add(InstalledUpgrades.class.getCanonicalName());
    classNames.add(SchemaVersion.class.getCanonicalName());
    ((SchemaAwareStoreManager)ctx.getStoreManager()).createSchemaForClasses(classNames, new Properties());
}
 
Example 2
Source File: PersistenceManagerFactory.java    From Alpine with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new JDO PersistenceManager.
 * @return a PersistenceManager
 */
public static PersistenceManager createPersistenceManager() {
    if (Config.isUnitTestsEnabled()) {
        pmf = (JDOPersistenceManagerFactory)JDOHelper.getPersistenceManagerFactory(JdoProperties.unit(), "Alpine");
    }
    if (pmf == null) {
        throw new IllegalStateException("Context is not initialized yet.");
    }
    return pmf.getPersistenceManager();
}
 
Example 3
Source File: JDOFactory.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
private void registerFactory(String clientName, String databaseName, DataSource ds) {
  	Properties connectionProperties = (Properties) dataNucleusProperties.clone();
  	connectionProperties.put("javax.jdo.option.ConnectionFactory", ds);
if (databaseName != null)
  		connectionProperties.setProperty("datanucleus.mapping.Catalog", databaseName);
  	logger.info("Adding PMF factory for client "+clientName+" with database "+databaseName);
      PersistenceManagerFactory factory = JDOHelper.getPersistenceManagerFactory(connectionProperties);
      factories.put(clientName, factory);
  }
 
Example 4
Source File: TestSentryRole.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
  dataDir = new File(Files.createTempDir(), "sentry_policy_db");
  Properties prop = new Properties();
  prop.setProperty(ServerConfig.JAVAX_JDO_URL, "jdbc:derby:;databaseName=" + dataDir.getPath() + ";create=true");
  prop.setProperty(ServerConfig.JAVAX_JDO_USER, "Sentry");
  prop.setProperty(ServerConfig.JAVAX_JDO_PASS, "Sentry");
  prop.setProperty(ServerConfig.JAVAX_JDO_DRIVER_NAME, "org.apache.derby.jdbc.EmbeddedDriver");
  prop.setProperty("datanucleus.autoCreateSchema", "true");
  prop.setProperty("datanucleus.fixedDatastore", "false");
  prop.setProperty("datanucleus.NontransactionalRead", "false");
  prop.setProperty("datanucleus.NontransactionalWrite", "false");
  pmf = JDOHelper.getPersistenceManagerFactory(prop);
}
 
Example 5
Source File: UpgradeInitializer.java    From dependency-track with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void contextInitialized(final ServletContextEvent event) {
    LOGGER.info("Initializing upgrade framework");

    final String driverPath = Config.getInstance().getProperty(Config.AlpineKey.DATABASE_DRIVER_PATH);
    if (driverPath != null) {
        Config.getInstance().expandClasspath(driverPath);
    }
    final JDOPersistenceManagerFactory pmf  = (JDOPersistenceManagerFactory) JDOHelper.getPersistenceManagerFactory(JdoProperties.get(), "Alpine");

    // Ensure that the UpgradeMetaProcessor and SchemaVersion tables are created NOW, not dynamically at runtime.
    final PersistenceNucleusContext ctx = pmf.getNucleusContext();
    final Set<String> classNames = new HashSet<>();
    classNames.add(InstalledUpgrades.class.getCanonicalName());
    classNames.add(SchemaVersion.class.getCanonicalName());
    ((SchemaAwareStoreManager)ctx.getStoreManager()).createSchemaForClasses(classNames, new Properties());

    if (RequirementsVerifier.failedValidation()) {
        return;
    }
    try (final PersistenceManager pm = pmf.getPersistenceManager();
         final QueryManager qm = new QueryManager(pm)) {
        final UpgradeExecutor executor = new UpgradeExecutor(qm);
        try {
            executor.executeUpgrades(UpgradeItems.getUpgradeItems());
        } catch (UpgradeException e) {
            LOGGER.error("An error occurred performing upgrade processing. " + e.getMessage());
        }
    }
    pmf.close();
}
 
Example 6
Source File: SentryStore.java    From incubator-sentry with Apache License 2.0 4 votes vote down vote up
public SentryStore(Configuration conf) throws SentryNoSuchObjectException,
SentryAccessDeniedException, SentryConfigurationException, IOException {
  commitSequenceId = 0;
  this.conf = conf;
  Properties prop = new Properties();
  prop.putAll(ServerConfig.SENTRY_STORE_DEFAULTS);
  String jdbcUrl = conf.get(ServerConfig.SENTRY_STORE_JDBC_URL, "").trim();
  Preconditions.checkArgument(!jdbcUrl.isEmpty(), "Required parameter " +
      ServerConfig.SENTRY_STORE_JDBC_URL + " is missed");
  String user = conf.get(ServerConfig.SENTRY_STORE_JDBC_USER, ServerConfig.
      SENTRY_STORE_JDBC_USER_DEFAULT).trim();
  //Password will be read from Credential provider specified using property
  // CREDENTIAL_PROVIDER_PATH("hadoop.security.credential.provider.path" in sentry-site.xml
  // it falls back to reading directly from sentry-site.xml
  char[] passTmp = conf.getPassword(ServerConfig.SENTRY_STORE_JDBC_PASS);
  String pass = null;
  if(passTmp != null) {
    pass = new String(passTmp);
  } else {
    throw new SentryConfigurationException("Error reading " + ServerConfig.SENTRY_STORE_JDBC_PASS);
  }

  String driverName = conf.get(ServerConfig.SENTRY_STORE_JDBC_DRIVER,
      ServerConfig.SENTRY_STORE_JDBC_DRIVER_DEFAULT);
  prop.setProperty(ServerConfig.JAVAX_JDO_URL, jdbcUrl);
  prop.setProperty(ServerConfig.JAVAX_JDO_USER, user);
  prop.setProperty(ServerConfig.JAVAX_JDO_PASS, pass);
  prop.setProperty(ServerConfig.JAVAX_JDO_DRIVER_NAME, driverName);
  for (Map.Entry<String, String> entry : conf) {
    String key = entry.getKey();
    if (key.startsWith(ServerConfig.SENTRY_JAVAX_JDO_PROPERTY_PREFIX) ||
        key.startsWith(ServerConfig.SENTRY_DATANUCLEUS_PROPERTY_PREFIX)) {
      key = StringUtils.removeStart(key, ServerConfig.SENTRY_DB_PROPERTY_PREFIX);
      prop.setProperty(key, entry.getValue());
    }
  }


  boolean checkSchemaVersion = conf.get(
      ServerConfig.SENTRY_VERIFY_SCHEM_VERSION,
      ServerConfig.SENTRY_VERIFY_SCHEM_VERSION_DEFAULT).equalsIgnoreCase(
          "true");
  if (!checkSchemaVersion) {
    prop.setProperty("datanucleus.autoCreateSchema", "true");
    prop.setProperty("datanucleus.fixedDatastore", "false");
  }

  // Disallow operations outside of transactions
  prop.setProperty("datanucleus.NontransactionalRead", "false");
  prop.setProperty("datanucleus.NontransactionalWrite", "false");

  pmf = JDOHelper.getPersistenceManagerFactory(prop);
  verifySentryStoreSchema(checkSchemaVersion);

  // Kick off the thread that cleans orphaned privileges (unless told not to)
  privCleaner = this.new PrivCleaner();
  if (conf.get(ServerConfig.SENTRY_STORE_ORPHANED_PRIVILEGE_REMOVAL,
          ServerConfig.SENTRY_STORE_ORPHANED_PRIVILEGE_REMOVAL_DEFAULT)
          .equalsIgnoreCase("true")) {
    privCleanerThread = new Thread(privCleaner);
    privCleanerThread.start();
  }
}
 
Example 7
Source File: LocalPersistenceManagerFactoryBean.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Subclasses can override this to perform custom initialization of the
 * PersistenceManagerFactory instance, creating it for the specified name.
 * <p>The default implementation invokes JDOHelper's
 * {@code getPersistenceManagerFactory(String)} method.
 * A custom implementation could prepare the instance in a specific way,
 * or use a custom PersistenceManagerFactory implementation.
 * @param name the name of the desired PersistenceManagerFactory
 * @return the PersistenceManagerFactory instance
 * @see javax.jdo.JDOHelper#getPersistenceManagerFactory(String)
 */
protected PersistenceManagerFactory newPersistenceManagerFactory(String name) {
	return JDOHelper.getPersistenceManagerFactory(name, this.beanClassLoader);
}
 
Example 8
Source File: LocalPersistenceManagerFactoryBean.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Subclasses can override this to perform custom initialization of the
 * PersistenceManagerFactory instance, creating it via the given Properties
 * that got prepared by this LocalPersistenceManagerFactoryBean.
 * <p>The default implementation invokes JDOHelper's
 * {@code getPersistenceManagerFactory(Map)} method.
 * A custom implementation could prepare the instance in a specific way,
 * or use a custom PersistenceManagerFactory implementation.
 * @param props the merged properties prepared by this LocalPersistenceManagerFactoryBean
 * @return the PersistenceManagerFactory instance
 * @see javax.jdo.JDOHelper#getPersistenceManagerFactory(java.util.Map)
 */
protected PersistenceManagerFactory newPersistenceManagerFactory(Map<?, ?> props) {
	return JDOHelper.getPersistenceManagerFactory(props, this.beanClassLoader);
}
 
Example 9
Source File: LocalPersistenceManagerFactoryBean.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Subclasses can override this to perform custom initialization of the
 * PersistenceManagerFactory instance, creating it for the specified name.
 * <p>The default implementation invokes JDOHelper's
 * {@code getPersistenceManagerFactory(String)} method.
 * A custom implementation could prepare the instance in a specific way,
 * or use a custom PersistenceManagerFactory implementation.
 * @param name the name of the desired PersistenceManagerFactory
 * @return the PersistenceManagerFactory instance
 * @see javax.jdo.JDOHelper#getPersistenceManagerFactory(String)
 */
protected PersistenceManagerFactory newPersistenceManagerFactory(String name) {
	return JDOHelper.getPersistenceManagerFactory(name, this.beanClassLoader);
}
 
Example 10
Source File: LocalPersistenceManagerFactoryBean.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Subclasses can override this to perform custom initialization of the
 * PersistenceManagerFactory instance, creating it via the given Properties
 * that got prepared by this LocalPersistenceManagerFactoryBean.
 * <p>The default implementation invokes JDOHelper's
 * {@code getPersistenceManagerFactory(Map)} method.
 * A custom implementation could prepare the instance in a specific way,
 * or use a custom PersistenceManagerFactory implementation.
 * @param props the merged properties prepared by this LocalPersistenceManagerFactoryBean
 * @return the PersistenceManagerFactory instance
 * @see javax.jdo.JDOHelper#getPersistenceManagerFactory(java.util.Map)
 */
protected PersistenceManagerFactory newPersistenceManagerFactory(Map<?, ?> props) {
	return JDOHelper.getPersistenceManagerFactory(props, this.beanClassLoader);
}
 
Example 11
Source File: AbstractJDOTest.java    From hyperjaxb3 with BSD 2-Clause "Simplified" License 2 votes vote down vote up
protected PersistenceManagerFactory createPersistenceManagerFactory() {

    final Map properties = getPersistenceManagerFactoryProperties();

    return JDOHelper.getPersistenceManagerFactory(properties);
  }
 
Example 12
Source File: JDOTest.java    From hyperjaxb3 with BSD 2-Clause "Simplified" License 2 votes vote down vote up
protected PersistenceManagerFactory createPersistenceManagerFactory() {

    final Map properties = getPersistenceManagerFactoryProperties();

    return JDOHelper.getPersistenceManagerFactory(properties);
  }
 
Example 13
Source File: JDOTest.java    From hyperjaxb3 with BSD 2-Clause "Simplified" License 2 votes vote down vote up
protected PersistenceManagerFactory createPersistenceManagerFactory() {

    final Map properties = getPersistenceManagerFactoryProperties();

    return JDOHelper.getPersistenceManagerFactory(properties);
  }