Last active
May 24, 2018 20:52
-
-
Save fmbenhassine/942db98afa13a1e940a6 to your computer and use it in GitHub Desktop.
JNDI hello world #lab
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.benas.labs.javase.jndi; | |
import java.io.Serializable; | |
import java.rmi.Remote; | |
public class Foo implements Remote, Serializable { | |
private String bar; | |
public Foo(String bar) { | |
this.bar = bar; | |
} | |
public String getBar() { | |
return bar; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
java.naming.factory.initial=com.sun.jndi.rmi.registry.RegistryContextFactory | |
java.naming.provider.url=rmi://localhost:2000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.benas.labs.javase.jndi; | |
import javax.naming.*; | |
import java.io.IOException; | |
public class Main { | |
public static void main(String[] args) throws NamingException, IOException { | |
Context ctx = new InitialContext(); | |
NamingEnumeration<NameClassPair> bindings = ctx.list(ctx.getNameInNamespace()); | |
while (bindings.hasMore()) { | |
NameClassPair ncp = bindings.next(); | |
System.out.println(ncp.getName()); | |
} | |
Foo foo = new Foo("bar"); | |
ctx.bind("foo", foo); | |
Foo f = (Foo) ctx.lookup("foo"); | |
System.out.println("res = " + f.getBar()); | |
ctx.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment