All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.addc.jndi.ecn.ECNNameParser Maven / Gradle / Ivy

package com.addc.jndi.ecn;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.naming.Name;
import javax.naming.NamingException;

import org.apache.commons.lang.StringUtils;
import org.omg.CosNaming.NameComponent;

import com.addc.commons.naming.SimpleNameParser;

/**
 * The BalancerNameParser class supplies a parser for converting between
 * NameComponets, Names and Strings.
 * 
 */
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.ModifiedCyclomaticComplexity", "PMD.StdCyclomaticComplexity" })
public class ECNNameParser extends SimpleNameParser {

    /**
     * Convert a Name object into a sequence of NameComponent objects.
     * 
     * @param name The Name instance to convert.
     * @return The corresponding NameComponent sequence.
     */
    public NameComponent[] toNC(Name name) {
        List corbaName= new ArrayList<>();
        Enumeration names= name.getAll();
        while (names.hasMoreElements()) {
            String str= names.nextElement();
            int pos= str.lastIndexOf('.');
            if (pos == -1) {
                corbaName.add(new NameComponent(str, ""));
            } else {
                corbaName.add(new NameComponent(str.substring(0, pos), str.substring(pos + 1)));
            }
        }
        return corbaName.toArray(new NameComponent[0]);
    }

    /**
     * Convert a NameComponent sequence to an INS String. This method
     * circumvents using the NamingContextExt.to_string() method.
     * 
     * @param nc The NameComponent sequence to convert.
     * @return An INS string.
     */
    public String toINS(NameComponent[] nc) {
        if (nc.length == 0) {
            return "";
        }
        StringBuilder sb= new StringBuilder();
        for (NameComponent nameComponent : nc) {
            sb.append(nameComponent.id);
            if (!StringUtils.isBlank(nameComponent.kind)) {
                sb.append('.').append(nameComponent.kind);
            }
            sb.append('/');
        }
        String ins= sb.toString();
        return StringUtils.stripEnd(ins, "/");
    }

    /**
     * Convert a NameComponent sequence to an Name object.
     * 
     * @param nc The NameComponent sequence to convert.
     * @return An Name object.
     * @throws NamingException
     */
    public Name toName(NameComponent[] nc) throws NamingException {
        return parse(toINS(nc));
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy