if (ref.getMappedName() != null && ref.getMappedName().startsWith("jndi:")) {
return null;
}
PersistenceUnit unit = persistenceUnits.resolveLink(ref.getPersistenceUnitName(), moduleURI);
// Explicitly check if we messed up the "if there's only one,
// that's what you get" rule by adding our "cmp" unit.
final Collection<PersistenceUnit> cmpUnits = persistenceUnits.values("cmp");
if (unit == null && cmpUnits.size() > 0 && persistenceUnits.values().size() - cmpUnits.size() == 1) {
// We did, there is exactly one non-cmp unit. Let's find it.
for (final PersistenceUnit persistenceUnit : persistenceUnits.values()) {
if (!persistenceUnit.getName().equals("cmp")) {
// Found it
unit = persistenceUnit;
break;
}
}
}
// try again using the ref name
if (unit == null) {
unit = persistenceUnits.resolveLink(ref.getName(), moduleURI);
}
// try again using the ref name with any prefix removed
if (unit == null) {
final String shortName = ref.getName().replaceFirst(".*/", "");
unit = persistenceUnits.resolveLink(shortName, moduleURI);
}
if (unit != null) {
ref.setPersistenceUnitName(unit.getName());
ref.setMappedName(unit.getId());
} else {
// ----------------------------------------------
// Nothing was found. Let's try and figure out
// what went wrong and log a validation message