@Override
  @SuppressWarnings("unchecked")
  public EntityManagerFactory createContainerEntityManagerFactory(
      PersistenceUnitInfo info, Map properties) {
    properties.put("datanucleus.jpa.addClassTransformer", "false");
    properties.put(
        "datanucleus.plugin.pluginRegistryClassName", "org.datanucleus.plugin.OSGiPluginRegistry");

    info.addTransformer(
        new ClassTransformer() {

          @Override
          public byte[] transform(
              ClassLoader loader,
              String className,
              Class<?> classBeingRedefined,
              ProtectionDomain protectionDomain,
              byte[] classfileBuffer)
              throws IllegalClassFormatException {

            // TODO Auto-generated method stub
            return null;
          }
        });
    return pp.createContainerEntityManagerFactory(info, properties);
  }
  @Override
  protected EntityManagerFactory createNativeEntityManagerFactory() throws PersistenceException {
    PersistenceUnitManager managerToUse = this.persistenceUnitManager;
    if (this.persistenceUnitManager == null) {
      this.internalPersistenceUnitManager.afterPropertiesSet();
      managerToUse = this.internalPersistenceUnitManager;
    }

    this.persistenceUnitInfo = determinePersistenceUnitInfo(managerToUse);
    JpaVendorAdapter jpaVendorAdapter = getJpaVendorAdapter();
    if (jpaVendorAdapter != null && this.persistenceUnitInfo instanceof SmartPersistenceUnitInfo) {
      ((SmartPersistenceUnitInfo) this.persistenceUnitInfo)
          .setPersistenceProviderPackageName(jpaVendorAdapter.getPersistenceProviderRootPackage());
    }

    PersistenceProvider provider = getPersistenceProvider();
    if (provider == null) {
      String providerClassName = this.persistenceUnitInfo.getPersistenceProviderClassName();
      if (providerClassName == null) {
        throw new IllegalArgumentException(
            "No PersistenceProvider specified in EntityManagerFactory configuration, "
                + "and chosen PersistenceUnitInfo does not specify a provider class name either");
      }
      Class<?> providerClass = ClassUtils.resolveClassName(providerClassName, getBeanClassLoader());
      provider = (PersistenceProvider) BeanUtils.instantiateClass(providerClass);
    }
    if (provider == null) {
      throw new IllegalStateException(
          "Unable to determine persistence provider. "
              + "Please check configuration of "
              + getClass().getName()
              + "; "
              + "ideally specify the appropriate JpaVendorAdapter class for this provider.");
    }

    if (logger.isInfoEnabled()) {
      logger.info(
          "Building JPA container EntityManagerFactory for persistence unit '"
              + this.persistenceUnitInfo.getPersistenceUnitName()
              + "'");
    }
    this.nativeEntityManagerFactory =
        provider.createContainerEntityManagerFactory(this.persistenceUnitInfo, getJpaPropertyMap());
    postProcessEntityManagerFactory(this.nativeEntityManagerFactory, this.persistenceUnitInfo);

    return this.nativeEntityManagerFactory;
  }
 /**
  * Create EE container entity manager factory
  *
  * @return EntityManagerFactory
  */
 private EntityManagerFactory createContainerEntityManagerFactory() {
   persistenceProviderAdaptor.beforeCreateContainerEntityManagerFactory(pu);
   try {
     return persistenceProvider.createContainerEntityManagerFactory(pu, properties.getValue());
   } finally {
     try {
       persistenceProviderAdaptor.afterCreateContainerEntityManagerFactory(pu);
     } finally {
       pu.setAnnotationIndex(
           null); // close reference to Annotation Index (only needed during call to
                  // createContainerEntityManagerFactory)
       // This is needed if the datasource is restarted
       // pu.setTempClassLoaderFactory(null);    // close reference to temp classloader factory
       // (only needed during call to createEntityManagerFactory)
     }
   }
 }
 /**
  * Create EE container entity manager factory
  *
  * @return EntityManagerFactory
  */
 private EntityManagerFactory createContainerEntityManagerFactory() {
   persistenceProviderAdaptor.beforeCreateContainerEntityManagerFactory(pu);
   try {
     JPA_LOGGER.tracef(
         "calling createContainerEntityManagerFactory for pu=%s with integration properties=%s, application properties=%s",
         pu.getScopedPersistenceUnitName(), properties.getValue(), pu.getProperties());
     return persistenceProvider.createContainerEntityManagerFactory(pu, properties.getValue());
   } finally {
     try {
       persistenceProviderAdaptor.afterCreateContainerEntityManagerFactory(pu);
     } finally {
       pu.setAnnotationIndex(
           null); // close reference to Annotation Index (only needed during call to
                  // createContainerEntityManagerFactory)
       // This is needed if the datasource is restarted
       // pu.setTempClassLoaderFactory(null);    // close reference to temp classloader factory
       // (only needed during call to createEntityManagerFactory)
     }
   }
 }
Пример #5
0
    @Override
    public EntityManagerFactory get() {
      PersistenceUnitInfo info =
          new PersistenceUnitInfo() {
            @Override
            public String getPersistenceUnitName() {
              String name = Thread.currentThread().getName();
              String id = getId();
              if (id == null) {
                name += "-default";
              } else {
                name += "-" + id;
              }
              return name;
            }

            @Override
            public String getPersistenceProviderClassName() {
              return null;
            }

            @Override
            public PersistenceUnitTransactionType getTransactionType() {
              return PersistenceUnitTransactionType.JTA;
            }

            @Override
            public DataSource getJtaDataSource() {
              return dataSource;
            }

            @Override
            public DataSource getNonJtaDataSource() {
              return null;
            }

            @Override
            public List<String> getMappingFileNames() {
              return Collections.emptyList();
            }

            @Override
            public List<URL> getJarFileUrls() {
              return Collections.emptyList();
            }

            @Override
            public URL getPersistenceUnitRootUrl() {
              try {
                return new URL("file:///Moonshine: please ignore this warning");
              } catch (MalformedURLException e) {
                throw new RuntimeException(e);
              }
            }

            @Override
            public List<String> getManagedClassNames() {
              List<String> names = new ArrayList<>();
              for (Class<?> klass : ClassIndex.getAnnotated(Entity.class)) {
                names.add(klass.getCanonicalName());
              }
              return names;
            }

            @Override
            public boolean excludeUnlistedClasses() {
              return true;
            }

            @Override
            public Properties getProperties() {
              Properties properties = new Properties();
              return properties;
            }

            @Override
            public ClassLoader getClassLoader() {
              return Thread.currentThread().getContextClassLoader();
            }

            @Override
            public void addTransformer(ClassTransformer transformer) {
              throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public ClassLoader getNewTempClassLoader() {
              return getClassLoader();
            }

            @Override
            public SharedCacheMode getSharedCacheMode() {
              return SharedCacheMode.UNSPECIFIED;
            }

            @Override
            public ValidationMode getValidationMode() {
              return ValidationMode.AUTO;
            }

            @Override
            public String getPersistenceXMLSchemaVersion() {
              return "";
            }
          };

      PersistenceProvider provider = new HibernatePersistence();

      Map<String, Object> map = new HashMap<>();
      map.put("hibernate.search.autoregister_listeners", false);
      if (plugins != null) {
        for (HibernatePlugin plugin : plugins) {
          map.putAll(plugin.getProperties());
        }
      }
      map.put(AvailableSettings.JTA_PLATFORM, jtaPlatform);
      map.put(AvailableSettings.HBM2DDL_AUTO, initSchema);
      map.put(AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, useNewIdGeneratorMappings);
      map.put(AvailableSettings.USE_SQL_COMMENTS, useSqlComments);
      map.put(AvailableSettings.FORMAT_SQL, formatSql);
      map.put(AvailableSettings.SHOW_SQL, showSql);
      map.put("javax.persistence.validation.factory", validatorFactory);

      factory = provider.createContainerEntityManagerFactory(info, map);
      return factory;
    }