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

About Ilias Tsagklis

Ilias Tsagklis
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

Parsing a JNDI composite name example

In this example we are going to see how to perform Parsing on JNDI composite name. In composite names components are / delimited. We are going to use cn=byron,o=hits/reports/summary.xls as a composite name.

Basically in order to Parse JDNI composite name,one should follow these steps:

  • Create a new CompositeName.
  • Use composite.get to get the component you want by providing its index.
  • Use composite.add(0, "jcg.com") to add component at the beginning of the name.
  • Use composite.remove(2) to Remove the second component.

Here is the code:

package com.javacodegeeks.snippets.enterprise;

import javax.naming.CompositeName;
import javax.naming.NamingException;

public class ParseJNDICompositeName {

  public static void main(String[] args) {

    try {

  // In composite names components are / delimited

  CompositeName composite = new CompositeName("cn=byron,o=hits/reports/summary.xls");

  String firstComponent = composite.get(0);

  System.out.println("First component : " + firstComponent);

  String lastComponent = composite.get(composite.size() - 1);

  System.out.println("Last component : " + lastComponent);

  // Add component at the beginning of the name

  composite.add(0, "jcg.com");

  System.out.println(composite);

  // Remove the second component

  composite.remove(2);

  System.out.println(composite);

    } catch (NamingException e) {

  System.out.println("Could not parse JNDI composite name : " + e.getMessage());
    }

 }

}

Output:

First component : cn=byron,o=hits
Last component : summary.xls
jcg.com/cn=byron,o=hits/report/summary.xls
jcg.com/cn=byron,o=hits/summary.xls

This is an example on parsing a JNDI composite name example.

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