Example usage for javax.naming Context rebind

List of usage examples for javax.naming Context rebind

Introduction

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

Prototype

public void rebind(String name, Object obj) throws NamingException;

Source Link

Document

Binds a name to an object, overwriting any existing binding.

Usage

From source file:org.eclipse.ecr.testlib.runner.RuntimeFeature.java

public static void rebind(Context ctx, String key, Object value) throws NamingException {
    Name name = ctx.getNameParser("").parse(key);
    int depth = name.size() - 1;
    for (int i = 0; i < depth; i++) {
        String segment = name.get(i);
        try {/*from   w  w  w  .  ja v a2 s.c om*/
            ctx = (Context) ctx.lookup(segment);
        } catch (NameNotFoundException e) {
            ctx = ctx.createSubcontext(segment);
        }
    }
    ctx.rebind(name.get(depth), value);
}

From source file:org.hibersap.ejb.util.JndiUtil.java

@PostConstruct
public static void rebindSessionManager(final SessionManager sessionManager, final String jndiName) {
    LOG.info("Binding Hibersap SessionManager '" + sessionManager.getConfig().getName() + "' to JNDI name '"
            + jndiName + "'");

    try {/*from  w  w  w .  j  a  v a 2s.  c om*/
        Context ctx = new InitialContext();
        ctx.rebind(jndiName, sessionManager);
    } catch (NamingException e) {
        throw new HibersapException("Failed binding Hibersap SessionManager to JNDI name [" + jndiName + "]",
                e);
    }
}

From source file:org.jsecurity.jndi.JndiTemplate.java

/**
 * Rebind the given object to the current JNDI context, using the given name.
 * Overwrites any existing binding./*from w ww.  jav  a  2s .  com*/
 *
 * @param name   the JNDI name of the object
 * @param object the object to rebind
 * @throws NamingException thrown by JNDI
 */
public void rebind(final String name, final Object object) throws NamingException {
    if (log.isDebugEnabled()) {
        log.debug("Rebinding JNDI object with name [" + name + "]");
    }
    execute(new JndiCallback() {
        public Object doInContext(Context ctx) throws NamingException {
            ctx.rebind(name, object);
            return null;
        }
    });
}

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;
    Name name = ctx.getNameParser("").parse(aliasName);
    int len = name.size() - 1;
    String atom = name.get(len);//  ww  w  .  j  ava 2  s.  c  o  m
    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 + "]");
    }
}

From source file:org.sonar.server.database.JndiDatabaseConnector.java

private void createAndBindDatasource() {
    Reference ref = createDatasourceReference();

    // bind the datasource to JNDI
    Context ctx = null;
    try {//w ww. j a v a 2 s.  co  m
        ctx = new InitialContext();
        createJNDISubContexts(ctx, getJndiName());
        ctx.rebind(getJndiName(), ref);
        datasource = (DataSource) ctx.lookup(getJndiName());
        Logs.INFO.info("JDBC datasource bound to JNDI, name=" + getJndiName());

    } catch (NamingException e) {
        throw new JndiException("Can not bind JDBC datasource to JNDI", e);

    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (NamingException e) {
            }
        }
    }
}

From source file:org.squale.jraf.bootstrap.naming.NamingHelper.java

/**
 * Bind le couple nom/valeur dans le contexte jndi.
 * @param in_context contexte jndi racine
 * @param in_name nom/* ww w  .j  a v  a 2 s. c o m*/
 * @param in_value valeur
 * @throws NamingException
 */
public static void bind(Context in_context, String in_name, Object in_value) throws JrafConfigException {

    if (log.isDebugEnabled()) {
        log.debug("binding : " + in_name);
    }
    String lc_name = in_name;

    try {
        in_context.rebind(lc_name, in_value);
    } catch (NamingException e) {

        try {
            Name n = in_context.getNameParser("").parse(lc_name);
            while (n.size() > 1) {
                String ctxName = n.get(0);

                Context subctx = null;
                try {
                    if (log.isDebugEnabled()) {
                        log.debug("lookup: " + ctxName);
                    }
                    subctx = (Context) in_context.lookup(ctxName);
                } catch (NameNotFoundException nfe) {
                }

                if (subctx != null) {
                    if (log.isDebugEnabled()) {
                        log.debug("Found subcontext: " + ctxName);
                    }
                    in_context = subctx;
                } else {
                    //               log.info("Creating subcontext: " + ctxName);
                    in_context = in_context.createSubcontext(ctxName);
                }
                n = n.getSuffix(1);
            }
            if (log.isDebugEnabled()) {
                log.debug("binding: " + n);
            }
            in_context.rebind(n, in_value);
        } catch (NamingException e1) {
            log.error("Impossible de recupere l'objet dans JNDI.", e);
            throw new JrafConfigException("Impossible de recupere l'objet dans JNDI.", e);
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("Bound name: " + in_name);
    }
}