Java 列出远程机器上 jndi 中所有条目的代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2324937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:10:03  来源:igfitidea点击:

Code to list all the entries in jndi on remote machine

javajndi

提问by Anand Sunderraman

Can any one tell or point me to code to list all the jndi entries in a remote machine

任何人都可以告诉或指向我的代码以列出远程机器中的所有 jndi 条目

采纳答案by Steve

It is possible to list all entries of an InitialContext. You can use this snippet:

可以列出 InitialContext 的所有条目。您可以使用此代码段:

InitialContext ctx = new InitialContext();
NamingEnumeration<NameClassPair> list = ctx.list("");
while (list.hasMore()) {
  System.out.println(list.next().getName());
}

If you are using an application server, there is usually the option to browse the JNDI tree.

如果您使用的是应用程序服务器,通常可以选择浏览 JNDI 树。

回答by Vielinko

I know, there are lot of time from last answer, but I needed to list all jdbc datasource available in a context (tomee context).

我知道,距离上一个答案还有很多时间,但是我需要列出上下文(tomee 上下文)中可用的所有 jdbc 数据源。

In my case, I needed more than list("")(sadly, this didn't work for me) to reach my goal. I found a naming environment list here:

就我而言,我需要的不仅仅是list("")(可悲的是,这对我不起作用)来实现我的目标。我在这里找到了一个命名环境列表:

Naming Environment for J2EE Application Components

J2EE 应用程序组件的命名环境

Having this, I got all available jdbc resources using next code snippet

有了这个,我使用下一个代码片段获得了所有可用的 jdbc 资源

InitialContext ctx = new InitialContext();
NamingEnumeration<NameClassPair> list = ctx.list("java:comp/env/jdbc");
while (list.hasMore()) {
    System.out.println(list.next().getName());
}

That's all.

就这样。

I hope this can helps someone else, as helps me.

我希望这可以帮助别人,就像帮助我一样。

回答by Nick Grealy

The previous answers didn't give me the "full picture" (on Tomcat7), so I've thrown together the following amalgamation, which converts the JNDI values to a Tree Map (with toStringvalues):

以前的答案没有给我“全貌”(在 Tomcat7 上),所以我将以下合并放在一起,它将 JNDI 值转换为树图(带toString值):

import javax.naming.*;
...

public static Map toMap(Context ctx) throws NamingException {
    String namespace = ctx instanceof InitialContext ? ctx.getNameInNamespace() : "";
    HashMap<String, Object> map = new HashMap<String, Object>();
    log.info("> Listing namespace: " + namespace);
    NamingEnumeration<NameClassPair> list = ctx.list(namespace);
    while (list.hasMoreElements()) {
        NameClassPair next = list.next();
        String name = next.getName();
        String jndiPath = namespace + name;
        Object lookup;
        try {
            log.info("> Looking up name: " + jndiPath);
            Object tmp = ctx.lookup(jndiPath);
            if (tmp instanceof Context) {
                lookup = toMap((Context) tmp);
            } else {
                lookup = tmp.toString();
            }
        } catch (Throwable t) {
            lookup = t.getMessage();
        }
        map.put(name, lookup);

    }
    return map;
}

Usage:

用法:

toMap(new InitialContext());

Gives the following output in Tomcat7:

在 Tomcat7 中给出以下输出:

{
  "comp": {
    "env": {
      "myCustomVar": "foobar"
    },
    "UserTransaction": "Cannot create resource instance",
    "Resources": {
      "index.html": "org.apache.naming.resources.FileDirContext$FileResource@32edeea8",
      "WEB-INF": {
        "ibm-web-ext.xml": "org.apache.naming.resources.FileDirContext$FileResource@6132b73b",
        "ibm-web-bnd.xml": "org.apache.naming.resources.FileDirContext$FileResource@22cf71b7"
      }
    }
  }
}

回答by Alexey

I'm using following code (not for production):

我正在使用以下代码(不用于生产):

public void discoverJndi(String path, Context context) throws TestClientException, NamingException {
    try {
        NamingEnumeration<NameClassPair> list = context.list(path);
        while (list.hasMore()) {
            String name = list.next().getName();
            String child = path.equals("") ? name : path + "/" + name;
            System.out.println(child);
            discoverJndi(child, context);
        }
    } catch (NotContextException e) {}
}