refName = member.getDeclaringClass().getName() + "/" + member.getName();
}
refName = normalize(refName);
JndiReference reference = consumer.getEnvEntryMap().get(refName);
if (reference == null) {
/**
* Was @Resource mistakenly used when either @PersistenceContext or @PersistenceUnit should have been used?
*/
if (member != null) { // Little quick validation for common mistake
Class type = member.getType();
boolean shouldReturn = false;
if (EntityManager.class.isAssignableFrom(type)) {
fail(consumer.getJndiConsumerName(), "resourceRef.onEntityManager", refName);
shouldReturn = true;
} else if (EntityManagerFactory.class.isAssignableFrom(type)) {
fail(consumer.getJndiConsumerName(), "resourceRef.onEntityManagerFactory", refName);
shouldReturn = true;
}
if (shouldReturn) return;
}
Class type;
if (member == null) {
type = resource.type();
} else {
type = member.getType();
}
if (knownResourceEnvTypes.contains(type.getName())) {
/*
* @Resource <resource-env-ref>
*/
ResourceEnvRef resourceEnvRef = consumer.getResourceEnvRefMap().get(refName);
if (resourceEnvRef == null) {
resourceEnvRef = new ResourceEnvRef();
resourceEnvRef.setName(refName);
consumer.getResourceEnvRef().add(resourceEnvRef);
}
if (resourceEnvRef.getResourceEnvRefType() == null || ("").equals(resourceEnvRef.getResourceEnvRefType())) {
resourceEnvRef.setResourceEnvRefType(type.getName());
}
reference = resourceEnvRef;
} else if (isKnownEnvironmentEntryType(type)) {
/*
* @Resource <env-entry>
*
* Add an env-entry via @Resource if 'lookup' attribute is set.
*/
String lookupName = getLookupName(resource);
if (!lookupName.equals("")) {
EnvEntry envEntry = new EnvEntry();
envEntry.setName(refName);
consumer.getEnvEntry().add(envEntry);
envEntry.setLookupName(lookupName);
reference = envEntry;
} else if(isShareableJNDINamespace(refName)){
EnvEntry envEntry = new EnvEntry();
envEntry.setName(refName);
consumer.getEnvEntry().add(envEntry);
reference = envEntry;
} else {
final String shortName = normalize(member.getName());
reference = consumer.getEnvEntryMap().get(shortName);
if (reference == null) {
EnvEntry envEntry = new EnvEntry();
envEntry.setName(refName);
consumer.getEnvEntry().add(envEntry);
reference = envEntry;
}
// /*
// * Can't add env-entry since @Resource.lookup is not set and it is NOT in a shareable JNDI name space
// */
// return;
}
} else {
/*
* @Resource <resource-ref>
*/
ResourceRef resourceRef = consumer.getResourceRefMap().get(refName);
if (resourceRef == null) {
resourceRef = new ResourceRef();
resourceRef.setName(refName);
consumer.getResourceRef().add(resourceRef);
}
if (member != null) {
resourceRef.setOrigin(member.getDeclaringClass() + "#" + member.getName());
} // TODO: else @Resource on a class
if (resourceRef.getResAuth() == null) {
if (resource.authenticationType() == Resource.AuthenticationType.APPLICATION) {
resourceRef.setResAuth(ResAuth.APPLICATION);
} else {
resourceRef.setResAuth(ResAuth.CONTAINER);
}
}
if (resourceRef.getResType() == null || ("").equals(resourceRef.getResType())) {
resourceRef.setResType(type.getName());
}
if (resourceRef.getResSharingScope() == null) {
if (resource.shareable()) {
resourceRef.setResSharingScope(ResSharingScope.SHAREABLE);
} else {
resourceRef.setResSharingScope(ResSharingScope.UNSHAREABLE);
}
}
reference = resourceRef;
}
}
/*
* Fill in the injection information <injection-target>
*/
if (member != null) {
// Set the member name where this will be injected
InjectionTarget target = new InjectionTarget();
target.setInjectionTargetClass(member.getDeclaringClass().getName());
target.setInjectionTargetName(member.getName());
reference.getInjectionTarget().add(target);
}
// Override the mapped name if not set
if (reference.getMappedName() == null && !resource.mappedName().equals("")) {
reference.setMappedName(resource.mappedName());
}
// Override the lookup name if not set
if (reference.getLookupName() == null) {
String lookupName = getLookupName(resource);
if (!lookupName.equals("")) {
reference.setLookupName(lookupName);
}
}
}