我想在Glassfish JNDI中为{strong>所有 list()
或listBindings()
。我想要整个树,或者作为对象,或者只是打印到控制台。
绝对是EJB已部署,因此位于Glassfish的JNDI树中:
thufir@doge:~$
thufir@doge:~$ asadmin list-applications
SimpleEJB <ear, ejb>
Command list-applications executed successfully.
thufir@doge:~$
thufir@doge:~$ appclient -client NetBeansProjects/corba/JNDI/dist/JNDI.jar
starting..
printing tree...
{org.omg.CORBA.ORBInitialPort=3700, java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, Context.SECURITY_CREDENTIALS=pass123, org.omg.CORBA.ORBInitialHost=localhost, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, Context.SECURITY_PRINCIPAL=user1}
..done
thufir@doge:~$
但是,我如何实际连接到Glassfish JNDI树?
package jndi;
import javax.naming.Binding;
import javax.naming.InitialContext;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
public class JndiTree {
public static void main(String[] args) throws NamingException {
System.out.println("starting..");
new JndiTree().printJndiTree();
System.out.println("..done");
}
private void printJndiTree() throws NamingException {
System.out.println("printing tree...");
InitialContext context = new InitialContext();
System.out.println(context.getEnvironment().toString());
NamingEnumeration list = context.list("");
NamingEnumeration<Binding> bindings = context.listBindings("");
while (list.hasMore()) {
NameClassPair nameClassPair = (NameClassPair) list.next();
System.out.println(nameClassPair);
}
while (bindings.hasMore()) {
Binding binding = bindings.next();
System.out.println(binding.getClassName());
System.out.println(binding.toString());
}
}
}
当然,我不是唯一一个想要在树上打印JNDI的人:
http://tripoverit.blogspot.ca/2007/03/print-jndi-tree.html
正如Glassfish日志所示,它有一个JNDI名称:
[2015-03-12T04:56:36.156-0700] [glassfish 4.1] [INFO] [AS-EJB-00054] [javax.enterprise.ejb.container] [tid: _ThreadID=42 _ThreadName=admin-listener(2)] [timeMillis: 1426161396156] [levelValue: 800] [[
Portable JNDI names for EJB NewSessionBean: [java:global/SimpleEJB/SimpleEJB-ejb/NewSessionBean, java:global/SimpleEJB/SimpleEJB-ejb/NewSessionBean!jkl.NewSessionBeanLocal]]]
这是在理解CORBA查找的背景下:
以上所有代码都在代理上。此外,代理上的jndi-entries
是:
thufir@doge:~$
thufir@doge:~$ asadmin list-jndi-entries
java:global: com.sun.enterprise.naming.impl.TransientContext
arrakis: org.glassfish.resourcebase.resources.api.ResourceProxy
UserTransaction: com.sun.enterprise.transaction.startup.TransactionLifecycleService$2
concurrent: com.sun.enterprise.naming.impl.TransientContext
com.sun.enterprise.container.common.spi.util.InjectionManager: com.sun.enterprise.container.common.impl.util.InjectionManagerImpl
jms: com.sun.enterprise.naming.impl.TransientContext
ejb: com.sun.enterprise.naming.impl.TransientContext
jdbc: com.sun.enterprise.naming.impl.TransientContext
Command list-jndi-entries executed successfully.
thufir@doge:~$
以下实用程序:
private List<JndiEntry> browse(final String path) throws NamingException {
final JndiCallback<List<JndiEntry>> contextCallback = new JndiCallback<List<JndiEntry>>() {
@Override
public List<JndiEntry> doInContext(final Context context) throws NamingException {
if (JAVA_GLOBAL.equals(path)) {
// Do a little trick to handle "java:global"
final NamingEnumeration<Binding> root = context.listBindings("");
Context javaGlobalContext = null;
while (root.hasMore()) {
final Binding binding = root.next();
if (JAVA_GLOBAL.equals(binding.getName())) {
final Object obj = binding.getObject();
if (obj instanceof Context) {
javaGlobalContext = (Context) obj;
}
break;
}
}
if (javaGlobalContext != null) {
return examineBindings(javaGlobalContext, path, javaGlobalContext.listBindings(""));
}
logger.warning("Unable to browse \"" + JAVA_GLOBAL + "\" namespace!");
return emptyList();
}
return examineBindings(context, path, context.listBindings(path));
}
};
return jndiTemplate.execute(contextCallback);
}
http://code.google.com/p/jndi-view/source/browse/trunk/src/main/java/jndi/view/JndiView.java
似乎以某种方式获取了树的所有条目。我不知道怎么......