private static void ambiguousPUError( DeploymentUnit unit, String persistenceUnitName, PersistenceUnitMetadataHolder holder) { if (holder.getPersistenceUnits().size() > 1 && (persistenceUnitName == null || persistenceUnitName.length() == 0)) { int numberOfDefaultPersistenceUnits = 0; // get number of persistence units that are marked as default for (PersistenceUnitMetadata persistenceUnit : holder.getPersistenceUnits()) { String defaultPU = persistenceUnit.getProperties().getProperty(Configuration.JPA_DEFAULT_PERSISTENCE_UNIT); if (Boolean.TRUE.toString().equals(defaultPU)) { numberOfDefaultPersistenceUnits++; } } ROOT_LOGGER.tracef( "checking for ambiguous persistence unit injection error, " + "number of persistence units marked default (%s) = %d", Configuration.JPA_DEFAULT_PERSISTENCE_UNIT, numberOfDefaultPersistenceUnits); // don't throw an error if there is exactly one default persistence unit if (numberOfDefaultPersistenceUnits != 1) { // AS7-2275 no unitName and there is more than one persistence unit; throw MESSAGES.noPUnitNameSpecifiedAndMultiplePersistenceUnits( holder.getPersistenceUnits().size(), unit); } } }
public static PersistenceUnitMetadata resolvePersistenceUnitSupplier( DeploymentUnit deploymentUnit, String persistenceUnitName) { if (traceEnabled) { ROOT_LOGGER.tracef( "pu search for name '%s' inside of %s", persistenceUnitName, deploymentUnit.getName()); } int scopeSeparatorCharacter = (persistenceUnitName == null ? -1 : persistenceUnitName.indexOf('#')); if (scopeSeparatorCharacter != -1) { final String path = persistenceUnitName.substring(0, scopeSeparatorCharacter); final String name = persistenceUnitName.substring(scopeSeparatorCharacter + 1); PersistenceUnitMetadata pu = getPersistenceUnit(deploymentUnit, path, name); if (traceEnabled) { ROOT_LOGGER.tracef("pu search found %s", pu.getScopedPersistenceUnitName()); } return pu; } else { PersistenceUnitMetadata name = findPersistenceUnitSupplier(deploymentUnit, persistenceUnitName); if (traceEnabled) { if (name != null) { ROOT_LOGGER.tracef("pu search found %s", name.getScopedPersistenceUnitName()); } } return name; } }
/** * if no persistence unit name is specified, return name of default persistence unit * * @param persistenceUnitName that was specified to be used (null means to use the default * persistence unit) * @param holder * @return */ private static String defaultPersistenceUnitName( String persistenceUnitName, PersistenceUnitMetadataHolder holder) { if ((persistenceUnitName == null || persistenceUnitName.length() == 0)) { for (PersistenceUnitMetadata persistenceUnit : holder.getPersistenceUnits()) { String defaultPU = persistenceUnit.getProperties().getProperty(Configuration.JPA_DEFAULT_PERSISTENCE_UNIT); if (Boolean.TRUE.toString().equals(defaultPU)) { persistenceUnitName = persistenceUnit.getPersistenceUnitName(); } } } return persistenceUnitName; }
/** * 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) } } }
private static PersistenceUnitMetadata getPersistenceUnit( DeploymentUnit current, final String absolutePath, String puName) { final String path; if (absolutePath.startsWith("../")) { path = absolutePath.substring(3); } else { path = absolutePath; } final VirtualFile parent = current.getAttachment(Attachments.DEPLOYMENT_ROOT).getRoot().getParent(); final VirtualFile resolvedPath = parent.getChild(path); List<ResourceRoot> resourceRoots = DeploymentUtils.allResourceRoots(DeploymentUtils.getTopDeploymentUnit(current)); for (ResourceRoot resourceRoot : resourceRoots) { if (resourceRoot.getRoot().equals(resolvedPath)) { PersistenceUnitMetadataHolder holder = resourceRoot.getAttachment(PersistenceUnitMetadataHolder.PERSISTENCE_UNITS); if (holder != null) { for (PersistenceUnitMetadata pu : holder.getPersistenceUnits()) { if (traceEnabled) { ROOT_LOGGER.tracef( "getPersistenceUnit check '%s' against pu '%s'", puName, pu.getPersistenceUnitName()); } if (pu.getPersistenceUnitName().equals(puName)) { if (traceEnabled) { ROOT_LOGGER.tracef( "getPersistenceUnit matched '%s' against pu '%s'", puName, pu.getPersistenceUnitName()); } return pu; } } } } } throw MESSAGES.persistenceUnitNotFound(absolutePath, puName, current); }
/* * When finding the default persistence unit, the first persistence unit encountered is returned. */ private static PersistenceUnitMetadata findWithinDeployment( DeploymentUnit unit, String persistenceUnitName) { if (traceEnabled) { ROOT_LOGGER.tracef("pu findWithinDeployment searching for %s", persistenceUnitName); } for (ResourceRoot root : DeploymentUtils.allResourceRoots(unit)) { PersistenceUnitMetadataHolder holder = root.getAttachment(PersistenceUnitMetadataHolder.PERSISTENCE_UNITS); if (holder == null || holder.getPersistenceUnits() == null) { if (traceEnabled) { ROOT_LOGGER.tracef( "pu findWithinDeployment skipping empty pu holder for %s", persistenceUnitName); } continue; } ambiguousPUError(unit, persistenceUnitName, holder); persistenceUnitName = defaultPersistenceUnitName(persistenceUnitName, holder); for (PersistenceUnitMetadata persistenceUnit : holder.getPersistenceUnits()) { if (traceEnabled) { ROOT_LOGGER.tracef( "findWithinDeployment check '%s' against pu '%s'", persistenceUnitName, persistenceUnit.getPersistenceUnitName()); } if (persistenceUnitName == null || persistenceUnitName.length() == 0 || persistenceUnit.getPersistenceUnitName().equals(persistenceUnitName)) { if (traceEnabled) { ROOT_LOGGER.tracef( "findWithinDeployment matched '%s' against pu '%s'", persistenceUnitName, persistenceUnit.getPersistenceUnitName()); } return persistenceUnit; } } } return null; }
private static PersistenceUnitMetadata findWithinLibraryJar( DeploymentUnit unit, ResourceRoot moduleResourceRoot, String persistenceUnitName) { final ResourceRoot deploymentRoot = moduleResourceRoot; PersistenceUnitMetadataHolder holder = deploymentRoot.getAttachment(PersistenceUnitMetadataHolder.PERSISTENCE_UNITS); if (holder == null || holder.getPersistenceUnits() == null) { if (traceEnabled) { ROOT_LOGGER.tracef( "findWithinLibraryJar checking for '%s' found no persistence units", persistenceUnitName); } return null; } ambiguousPUError(unit, persistenceUnitName, holder); persistenceUnitName = defaultPersistenceUnitName(persistenceUnitName, holder); for (PersistenceUnitMetadata persistenceUnit : holder.getPersistenceUnits()) { if (traceEnabled) { ROOT_LOGGER.tracef( "findWithinLibraryJar check '%s' against pu '%s'", persistenceUnitName, persistenceUnit.getPersistenceUnitName()); } if (persistenceUnitName == null || persistenceUnitName.length() == 0 || persistenceUnit.getPersistenceUnitName().equals(persistenceUnitName)) { if (traceEnabled) { ROOT_LOGGER.tracef( "findWithinLibraryJar matched '%s' against pu '%s'", persistenceUnitName, persistenceUnit.getPersistenceUnitName()); } return persistenceUnit; } } return null; }
/** * Returns the Persistence Unit service name used for creation or lookup. The service name * contains the unique fully scoped persistence unit name * * @param pu persistence unit definition * @return */ public static ServiceName getPUServiceName(PersistenceUnitMetadata pu) { return JPAServiceNames.getPUServiceName(pu.getScopedPersistenceUnitName()); }
@Override public String getScopedPersistenceUnitName() { return pu.getScopedPersistenceUnitName(); }