列出远程计算机上 jndi 中所有条目的代码

发布于 2024-08-22 08:00:03 字数 42 浏览 3 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温柔嚣张 2024-08-29 08:00:03

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

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

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

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

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.

楠木可依 2024-08-29 08:00:03

前面的答案没有给我“完整的图片”(在 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;
}

用法:

toMap(new InitialContext());

在 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"
      }
    }
  }
}

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 toString values):

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:

{
  "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"
      }
    }
  }
}
与往事干杯 2024-08-29 08:00:03

我需要列出上下文(tomee 上下文)中可用的所有 JDBC 数据源。

就我而言,我需要的不仅仅是 list("") (遗憾的是,这对我不起作用)来实现我的目标。
我在这里找到了命名环境列表:
J2EE 应用程序组件的命名环境

,我使用以下代码片段获取了所有可用的 JDBC 资源:

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

仅此而已。

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

I needed to list all JDBC datasources available in a context (tomee context).

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:
Naming Environment for J2EE Application Components

Having this, I got all available JDBC resources using following code snippet:

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.

爱已欠费 2024-08-29 08:00:03

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

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) {}
}

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) {}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文