JNDI est système d’annuaire générique. Il sait gérer “out-of-box” : LDAP, DNS, NIS, NDS, RMI et CORBA.

packages JNDI

  • javax.naming Manipulation des associations.

  • javax.naming.directory Manipulation des répertoires.

  • javax.naming.event Traitement des évènements en provenance de l’annuaire.

  • javax.naming.ldap API spécialisée pour les annuaires compatibles LDAP.

  • javax.naming.spi API de programmation des pilotes JNDI.

Bind d’un objet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class Bind
{

 public static String BIND = "drazzib/test";

 public static void main (String[] args)
 {
   Object object = "DATA";

   try
   {
    Context initialContext = new InitialContext();
    initialContext.bind(BIND, object);
    System.out.println("Bound object to name: " + BIND);
   }
   catch (NamingException e)
   {
    System.err.println("Unable to bind (" + object + ") to the name '" + BIND + "'");
    e.printStackTrace();
   }
 }
}

Lookup d’un objet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class Lookup
{
 public static void main (String[] args)
 {
   try
   {
    Context initialContext = new InitialContext();
    Object object = initialContext.lookup(Bind.BIND);

    if ( object instanceof java.lang.String)
    {
     String s = (String) object;
     System.out.println("Looked up this object: " + s);
    }
    else
    {
     System.err.println("Object is not of type " + "java.lang.String");
    }
   }
   catch (NamingException e)
   {
    System.err.println("Unable to find an object bound to the name " + Bind.BIND);
   }
 }
}

Références