Example usage for javax.naming LinkRef LinkRef

List of usage examples for javax.naming LinkRef LinkRef

Introduction

In this page you can find the example usage for javax.naming LinkRef LinkRef.

Prototype

public LinkRef(String linkName) 

Source Link

Document

Constructs a LinkRef for a string name.

Usage

From source file:Util.java

/**
 * Create a link/*from w  w w.jav  a  2 s.  com*/
 * 
 * @param ctx
 *          the context
 * @param fromName
 *          the from name
 * @param toName
 *          the to name
 * @throws NamingException
 *           for any error
 */
public static void createLinkRef(Context ctx, String fromName, String toName) throws NamingException {
    LinkRef link = new LinkRef(toName);
    Context fromCtx = ctx;
    Name name = ctx.getNameParser("").parse(fromName);
    String atom = name.get(name.size() - 1);
    for (int n = 0; n < name.size() - 1; n++) {
        String comp = name.get(n);
        try {
            fromCtx = (Context) fromCtx.lookup(comp);
        } catch (NameNotFoundException e) {
            fromCtx = fromCtx.createSubcontext(comp);
        }
    }

    System.out.println("atom: " + atom);
    System.out.println("link: " + link);

    fromCtx.rebind(atom, link);

    System.out.println("Bound link " + fromName + " to " + toName);
}

From source file:org.josso.jb32.agent.JBossCatalinaNativeRealm.java

/** This creates a java:comp/env/security context that contains a
 securityMgr binding pointing to an AuthenticationManager implementation
 and a realmMapping binding pointing to a RealmMapping implementation.
 *///from ww w.j av  a 2 s .  c o m
protected Context prepareENC() throws NamingException {

    if (logger.isDebugEnabled())
        logger.debug("JBossCatalinaRealm.prepareENC, Start");

    ClassLoader loader = Thread.currentThread().getContextClassLoader();

    InitialContext iniCtx = new InitialContext();

    boolean securityContextExists = false;
    boolean isJaasSecurityManager = false;
    try {
        Context envCtx = (Context) iniCtx.lookup("java:comp/env");
        Context securityCtx = (Context) envCtx.lookup("security");
        securityContextExists = true;

        AuthenticationManager securityMgr = (AuthenticationManager) securityCtx.lookup("securityMgr");

        // If the Security Manager set in the web application ENC is not
        // a JaasSecurityManager, unbind the Security context and rebind it
        // with the JaasSecurityManager associated with the JOSSO Security Domain.
        // Note: the jboss-web.xml file of the partner application MUST not have an
        // entry referring to a security domain.
        if (!(securityMgr instanceof JaasSecurityManager)) {
            Util.unbind(envCtx, "security");
        } else
            isJaasSecurityManager = true;
    } catch (NamingException e) {
        // No Security Context found
    }

    // If we do not have a SecurityContext create it
    Context envCtx = null;
    if (!securityContextExists) {
        Thread currentThread = Thread.currentThread();
        if (logger.isDebugEnabled())
            logger.debug("Creating ENC using ClassLoader: " + loader);
        ClassLoader parent = loader.getParent();
        while (parent != null) {

            if (logger.isDebugEnabled())
                logger.debug(".." + parent);

            parent = parent.getParent();
        }

        envCtx = (Context) iniCtx.lookup("java:comp");
        envCtx = envCtx.createSubcontext("env");
    } else
        envCtx = (Context) iniCtx.lookup("java:comp/env");

    // If the Security Manager binded is not a JaasSecurityManager, rebind using
    // the Security Manager associated with the JOSSO Security Domain.
    if (!isJaasSecurityManager) {
        // Prepare the Security JNDI subcontext
        if (logger.isDebugEnabled())
            logger.debug("Linking security/securityMgr to JNDI name: " + JOSSO_SECURITY_DOMAIN);

        Util.bind(envCtx, "security/securityMgr", new LinkRef(JOSSO_SECURITY_DOMAIN));
        Util.bind(envCtx, "security/realmMapping", new LinkRef(JOSSO_SECURITY_DOMAIN));
        Util.bind(envCtx, "security/security-domain", new LinkRef(JOSSO_SECURITY_DOMAIN));
        Util.bind(envCtx, "security/subject", new LinkRef(JOSSO_SECURITY_DOMAIN + "/subject"));
    }

    if (logger.isDebugEnabled())
        logger.debug("JBossCatalinaRealm.prepareENC, End");

    return (Context) iniCtx.lookup("java:comp/env/security");
}

From source file:org.josso.jb32.agent.JBossCatalinaRealm.java

/** This creates a java:comp/env/security context that contains a
 securityMgr binding pointing to an AuthenticationManager implementation
 and a realmMapping binding pointing to a RealmMapping implementation.
 *//*  w ww . j av a  2 s . c o  m*/
protected Context prepareENC() throws NamingException {

    ClassLoader loader = Thread.currentThread().getContextClassLoader();

    InitialContext iniCtx = new InitialContext();

    boolean securityContextExists = false;
    boolean isJaasSecurityManager = false;
    try {
        Context envCtx = (Context) iniCtx.lookup("java:comp/env");
        Context securityCtx = (Context) envCtx.lookup("security");
        securityContextExists = true;

        AuthenticationManager securityMgr = (AuthenticationManager) securityCtx.lookup("securityMgr");

        // If the Security Manager set in the web application ENC is not
        // a JaasSecurityManager, unbind the Security context and rebind it
        // with the JaasSecurityManager associated with the JOSSO Security Domain.
        // Note: the jboss-web.xml file of the partner application MUST not have an
        // entry referring to a security domain.
        if (!(securityMgr instanceof JaasSecurityManager)) {
            Util.unbind(envCtx, "security");
        } else
            isJaasSecurityManager = true;
    } catch (NamingException e) {
        // No Security Context found
    }

    // If we do not have a SecurityContext create it
    Context envCtx = null;
    if (!securityContextExists) {
        Thread currentThread = Thread.currentThread();
        logger.debug("Creating ENC using ClassLoader: " + loader);
        ClassLoader parent = loader.getParent();
        while (parent != null) {
            logger.debug(".." + parent);
            parent = parent.getParent();
        }

        envCtx = (Context) iniCtx.lookup("java:comp");
        envCtx = envCtx.createSubcontext("env");
    } else
        envCtx = (Context) iniCtx.lookup("java:comp/env");

    // If the Security Manager binded is not a JaasSecurityManager, rebind using
    // the Security Manager associated with the JOSSO Security Domain.
    if (!isJaasSecurityManager) {
        // Prepare the Security JNDI subcontext
        logger.debug("Linking security/securityMgr to JNDI name: " + JOSSO_SECURITY_DOMAIN);
        Util.bind(envCtx, "security/securityMgr", new LinkRef(JOSSO_SECURITY_DOMAIN));
        Util.bind(envCtx, "security/realmMapping", new LinkRef(JOSSO_SECURITY_DOMAIN));
        Util.bind(envCtx, "security/security-domain", new LinkRef(JOSSO_SECURITY_DOMAIN));
        Util.bind(envCtx, "security/subject", new LinkRef(JOSSO_SECURITY_DOMAIN + "/subject"));
    }

    logger.debug("JBossCatalinaRealm.prepareENC, End");

    return (Context) iniCtx.lookup("java:comp/env/security");
}

From source file:org.nuxeo.runtime.binding.ServiceBindings.java

public static void createAlias(InitialContext ctx, String existingName, String aliasName)
        throws NamingException {
    LinkRef link = new LinkRef(existingName);
    Context aliasCtx = ctx;// ww w .j  av a 2  s .  com
    Name name = ctx.getNameParser("").parse(aliasName);
    int len = name.size() - 1;
    String atom = name.get(len);
    for (int i = 0; i < len; i++) {
        String comp = name.get(i);
        try {
            aliasCtx = (Context) aliasCtx.lookup(comp);
        } catch (NameNotFoundException e) {
            aliasCtx = aliasCtx.createSubcontext(comp);
        }
    }

    aliasCtx.rebind(atom, link);

    if (log.isDebugEnabled()) {
        log.debug("Created JNDI link [" + aliasName + "] pointing to [" + existingName + "]");
    }
}