Skip to content

Commit

Permalink
Fixed ResourceNamingService + added important logs
Browse files Browse the repository at this point in the history
Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Nov 14, 2022
1 parent 04e9837 commit f964c8c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ public PoolInfo(SimpleJndiName name) {
}


public PoolInfo(SimpleJndiName name, String applicationName) {
this(name, applicationName, null);
}


public PoolInfo(SimpleJndiName name, String applicationName, String moduleName) {
this.name = name;
this.applicationName = applicationName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public class ResourceProxy implements NamingObjectProxy.InitializationNamingObje
@Inject
private org.glassfish.resourcebase.resources.naming.ResourceNamingService namingService;

private Resource resource = null;
private Object result = null;
private org.glassfish.resourcebase.resources.api.ResourceInfo resourceInfo = null;
private Resource resource;
private Object result;
private ResourceInfo resourceInfo;

@Override
public <T> T create(Context ic) throws NamingException {
Expand Down Expand Up @@ -84,11 +84,11 @@ public void setResource(Resource resource){
* Name by which the proxy (or the resource) will be bound in JNDI
* @param resourceInfo resource-info
*/
public void setResourceInfo(org.glassfish.resourcebase.resources.api.ResourceInfo resourceInfo) {
public void setResourceInfo(ResourceInfo resourceInfo) {
this.resourceInfo = resourceInfo;
}

protected void throwResourceNotFoundException(Exception e, org.glassfish.resourcebase.resources.api.ResourceInfo resourceInfo) throws NamingException {
protected void throwResourceNotFoundException(Exception e, ResourceInfo resourceInfo) throws NamingException {
NamingException ne = new NamingException("Unable to lookup resource : " + resourceInfo);
ne.initCause(e);
throw ne;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@

import jakarta.inject.Inject;

import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.naming.InitialContext;
import javax.naming.NameNotFoundException;
Expand All @@ -34,10 +34,7 @@
import org.glassfish.api.naming.GlassfishNamingManager;
import org.glassfish.api.naming.JNDIBinding;
import org.glassfish.api.naming.SimpleJndiName;
import org.glassfish.logging.annotation.LogMessagesResourceBundle;
import org.glassfish.logging.annotation.LoggerInfo;
import org.glassfish.resourcebase.resources.api.GenericResourceInfo;
import org.glassfish.resourcebase.resources.api.ResourceInfo;
import org.glassfish.resourcebase.resources.util.ResourceUtil;
import org.jvnet.hk2.annotations.Service;

Expand All @@ -50,12 +47,7 @@
@Service
public final class ResourceNamingService {

@LogMessagesResourceBundle
public static final String LOGMESSAGE_RESOURCE = "org.glassfish.resourcebase.resources.LogMessages";

@LoggerInfo(subsystem = "RESOURCE", description = "Nucleus Resource", publish = true)
public static final String LOGGER = "jakarta.enterprise.resources.naming";
private static final Logger LOG = Logger.getLogger(LOGGER, LOGMESSAGE_RESOURCE);
private static final Logger LOG = System.getLogger(ResourceNamingService.class.getName());

// TODO ASR introduce contract for this service and refactor this service to connector-runtime ?
@Inject
Expand All @@ -67,15 +59,22 @@ public final class ResourceNamingService {
@Inject
private ProcessEnvironment processEnvironment;

public void publishObject(GenericResourceInfo resourceInfo, Object object, boolean rebind) throws NamingException {
publishObject(resourceInfo, resourceInfo.getName(), object, rebind);
}

public void publishObject(final GenericResourceInfo resourceInfo, final SimpleJndiName jndiName,
final Object object, final boolean rebind) throws NamingException {
String applicationName = resourceInfo.getApplicationName();
String moduleName = ResourceUtil.getActualModuleName(resourceInfo.getModuleName());

if (applicationName != null && moduleName != null && resourceInfo.getName().isJavaModule()) {
SimpleJndiName resJndiName = resourceInfo.getName();
LOG.log(Level.DEBUG, "publishObject: applicationName={0}, moduleName={1}, jndiName={2}, resourceJndiName={3}",
applicationName, moduleName, jndiName, resJndiName);
if (applicationName != null && moduleName != null && resJndiName.isJavaModule()) {

Object alreadyBoundObject = null;
if (rebind) {
// FIXME: It would be better to implement rebind in JavaNamespace
try {
namingManager.unbindModuleObject(applicationName, moduleName, jndiName);
} catch (NameNotFoundException e) {
Expand All @@ -99,10 +98,8 @@ public void publishObject(final GenericResourceInfo resourceInfo, final SimpleJn
JNDIBinding bindings = new ModuleScopedResourceBinding(jndiName, object);
List<JNDIBinding> list = new ArrayList<>();
list.add(bindings);
LOG.log(Level.FINE, "applicationName={0}, moduleName={1}, jndiName={2}",
new Object[] {applicationName, moduleName, jndiName});
namingManager.bindToModuleNamespace(applicationName, moduleName, list);
} else if (applicationName != null && (jndiName.isJavaApp() || jndiName.isJavaModule())) {
} else if (applicationName != null && resJndiName.isJavaApp()) {
Object alreadyBoundObject = null;
if (rebind) {
try {
Expand All @@ -117,36 +114,37 @@ public void publishObject(final GenericResourceInfo resourceInfo, final SimpleJn
// ignore
}
if (alreadyBoundObject != null) {
throw new NamingException("Object already bound for jndiName " + "[ " + jndiName + " ] of application's namespace ["
+ applicationName + "]");
throw new NamingException("Object already bound for jndiName " + "[ " + jndiName
+ " ] of application's namespace [" + applicationName + "]");
}
}

JNDIBinding bindings = new ApplicationScopedResourceBinding(jndiName, object);
namingManager.bindToAppNamespace(applicationName, List.of(bindings));
JNDIBinding binding = new ApplicationScopedResourceBinding(jndiName, object);
namingManager.bindToAppNamespace(applicationName, List.of(binding));
bindAppScopedNameForAppclient(object, jndiName, applicationName);
} else {
namingManager.publishObject(jndiName, object, true);
}
}

public void publishObject(ResourceInfo resourceInfo, Object object, boolean rebind) throws NamingException {
SimpleJndiName jndiName = resourceInfo.getName();
publishObject(resourceInfo, jndiName, object, rebind);
}

private void bindAppScopedNameForAppclient(Object object, SimpleJndiName jndiName, String applicationName) throws NamingException {
SimpleJndiName name = componentNamingUtil.composeInternalGlobalJavaAppName(applicationName, jndiName);
namingManager.publishObject(name, object, true);
}

public void unpublishObject(GenericResourceInfo resourceInfo) throws NamingException {
unpublishObject(resourceInfo, resourceInfo.getName());
}

public void unpublishObject(GenericResourceInfo resourceInfo, SimpleJndiName jndiName) throws NamingException {
String applicationName = resourceInfo.getApplicationName();
String moduleName = ResourceUtil.getActualModuleName(resourceInfo.getModuleName());

if (!resourceInfo.getName().isJavaGlobal() && applicationName != null && moduleName != null) {
SimpleJndiName resJndiName = resourceInfo.getName();
LOG.log(Level.DEBUG, "unpublishObject: applicationName={0}, moduleName={1}, jndiName={2}, resourceJndiName={3}",
applicationName, moduleName, jndiName, resJndiName);
if (applicationName != null && moduleName != null && resJndiName.isJavaModule()) {
namingManager.unbindModuleObject(applicationName, moduleName, jndiName);
} else if (!resourceInfo.getName().isJavaGlobal() && applicationName != null) {
} else if (applicationName != null && resJndiName.isJavaApp()) {
namingManager.unbindAppObject(applicationName, jndiName);
unbindAppScopedNameForAppclient(jndiName, applicationName);
} else {
Expand All @@ -155,33 +153,36 @@ public void unpublishObject(GenericResourceInfo resourceInfo, SimpleJndiName jnd
}

private void unbindAppScopedNameForAppclient(SimpleJndiName jndiName, String applicationName) throws NamingException {
SimpleJndiName internalGlobalJavaAppName = componentNamingUtil.composeInternalGlobalJavaAppName(applicationName, jndiName);
namingManager.unpublishObject(internalGlobalJavaAppName);
SimpleJndiName internalGlobalJndiName = componentNamingUtil.composeInternalGlobalJavaAppName(applicationName, jndiName);
namingManager.unpublishObject(internalGlobalJndiName);
}

public <T> T lookup(GenericResourceInfo resourceInfo, SimpleJndiName name) throws NamingException {
return lookup(resourceInfo, name, null);
public <T> T lookup(GenericResourceInfo resourceInfo, SimpleJndiName jndiName) throws NamingException {
return lookup(resourceInfo, jndiName, null);
}

public <T> T lookup(GenericResourceInfo resourceInfo, SimpleJndiName name, Hashtable env) throws NamingException {
public <T> T lookup(GenericResourceInfo resourceInfo, SimpleJndiName jndiName, Hashtable env) throws NamingException {
String applicationName = resourceInfo.getApplicationName();
String moduleName = ResourceUtil.getActualModuleName(resourceInfo.getModuleName());
SimpleJndiName jndiName = resourceInfo.getName();
if (applicationName != null && moduleName != null && (jndiName.isJavaApp() || jndiName.isJavaModule())) {
return namingManager.lookupFromModuleNamespace(applicationName, moduleName, name, env);
}
if (applicationName != null && (jndiName.isJavaApp() || jndiName.isJavaModule())) {
SimpleJndiName resJndiName = resourceInfo.getName();
LOG.log(Level.DEBUG, "lookup: applicationName={0}, moduleName={1}, jndiName={2}, resourceJndiName={3}",
applicationName, moduleName, jndiName, resJndiName);
if (applicationName != null && moduleName != null && resJndiName.isJavaModule()) {
return namingManager.lookupFromModuleNamespace(applicationName, moduleName, jndiName, env);
} else if (applicationName != null && resJndiName.isJavaApp()) {
if (processEnvironment.getProcessType().isServer() || processEnvironment.getProcessType().isEmbedded()) {
return namingManager.lookupFromAppNamespace(applicationName, name, env);
return namingManager.lookupFromAppNamespace(applicationName, jndiName, env);
}
SimpleJndiName internalGlobalJavaAppName = componentNamingUtil.composeInternalGlobalJavaAppName(applicationName, name);
LOG.log(Level.FINEST, "appclient lookup: {0}", internalGlobalJavaAppName);
return (T) namingManager.getInitialContext().lookup(internalGlobalJavaAppName.toString());
}
if (env == null) {
return (T) namingManager.getInitialContext().lookup(name.toString());
SimpleJndiName internalGlobalJndiName = componentNamingUtil
.composeInternalGlobalJavaAppName(applicationName, jndiName);
LOG.log(Level.DEBUG, "internalGlobalJndiName={0}", internalGlobalJndiName);
return (T) namingManager.lookup(internalGlobalJndiName);
} else if (env == null || env.isEmpty()) {
return (T) namingManager.lookup(jndiName);
} else {
// probably some remote context, corba.
return (T) new InitialContext(env).lookup(jndiName.toString());
}
return (T) new InitialContext(env).lookup(name.toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,13 @@ public static PoolInfo getPoolInfo(ResourcePool resource){
SimpleJndiName jndiName = SimpleJndiName.of(resource.getName());
if (resource.getParent() != null && resource.getParent().getParent() instanceof Application) {
Application application = (Application) resource.getParent().getParent();
return new PoolInfo(jndiName, application.getName());
return new PoolInfo(jndiName, application.getName(), null);
} else if (resource.getParent() != null && resource.getParent().getParent() instanceof Module) {
Module module = (Module) resource.getParent().getParent();
Application application = (Application) module.getParent();
return new PoolInfo(jndiName, application.getName(), module.getName());
} else {
return new PoolInfo(jndiName);
return new PoolInfo(jndiName, null, null);
}
}

Expand All @@ -156,6 +156,9 @@ public static String getActualModuleNameWithExtension(String moduleName) {
return actualModuleName;
}

/**
* @return moduleName without .rar or .jar suffix
*/
public static String getActualModuleName(String moduleName){
if (moduleName != null) {
if (moduleName.endsWith(".jar") || moduleName.endsWith(".rar")) {
Expand Down

0 comments on commit f964c8c

Please sign in to comment.