naming

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.

Ilias Tsagklis

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

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button