Home » Enterprise Java » naming » Parsing a JNDI compound name example

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Parsing a JNDI compound name example

In this example we are going to see how to perform Parsing on JNDI compound name. This example parses a compound name using a parser from an LDAP service in which components are arranged from right to left, delimited by the comma character (,). The LDAP service is running on localhost and on default port (389)

Parsing a JNDI compound name requires that you:

  • Create a new Hashtable and name it env.
  • Use env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory").
  • Use env.put(Context.PROVIDER_URL,"ldap://localhost/o=JNDIExample")
  • Create new Context using InitialContext(env).
  • Use ctx.getNameParser to get a NameParser.
  • And get a Name with parser.parse("cn=byron, ou=People, o=JNDIExample").

Here is the code:

package com.javacodegeeks.snippets.enterprise;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.Name;
import javax.naming.NameParser;
import javax.naming.NamingException;

public class ParseJNDICompoundName {

  public static void main(String[] args) {

    try {

/* 

  * This example parses a compound name using a parser from an LDAP service in which 

  * components are arranged from right to left, delimited by the comma character (,). 

  * The LDAP service is running on localhost and on default port (389)

  */

  Hashtable env = new Hashtable();

  env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

  env.put(Context.PROVIDER_URL, "ldap://localhost/o=JNDIExample");

  Context ctx = new InitialContext(env);

  System.out.println("Initial Context created successfully");

  NameParser parser = ctx.getNameParser("");

  Name dn = parser.parse("cn=byron, ou=People, o=JNDIExample");

  System.out.println("Compound name : " + dn);

  System.out.println("Second component : " + dn.remove(1));

  dn.add(0, "c=gr");

  System.out.println("After adding component 'c=gr' at the end : " + dn);

  dn.add("cn=ilias");

  System.out.println("After adding component 'cn=ilias' at the beginning : " + dn);

    } catch (NamingException e) {

  System.out.println("Exception occurred while parsing Compound name : " + e.getMessage());
    }

 }

}

Output:

Initial Context created successfully
Compound name : cn=byron, ou=People, o=JNDIExample
Second component : ou=People
After adding component 'c=gr' at the end : cn=byron,o=JNDIExample,c=gr
After adding component 'cn=ilias' at the beginning : cn=ilias,cn=byron,o=JNDIExample,c=gr

 
This was an example on how to parse a JNDI compound name.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments