Example usage for javax.naming Context getNameInNamespace

List of usage examples for javax.naming Context getNameInNamespace

Introduction

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

Prototype

public String getNameInNamespace() throws NamingException;

Source Link

Document

Retrieves the full name of this context within its own namespace.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String url = "iiop://localhost/";
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory");
    env.put(Context.PROVIDER_URL, url);

    Context ctx = new InitialContext(env);
    // Create a subcontext.
    Context childCtx = ctx.createSubcontext("child");

    // Destroy the subcontext.
    ctx.destroySubcontext("child");

    Context obj = (Context) childCtx.lookup("grandChild");
    String fullname = obj.getNameInNamespace();
}

From source file:com.dattack.naming.loader.NamingLoader.java

private static void createAndBind(final Properties properties, final Context context, final String name,
        final Collection<File> extraClasspath) throws NamingException {

    final String type = properties.getProperty(TYPE_KEY);
    final ResourceFactory<?> factory = ResourceFactoryRegistry.getFactory(type);
    if (factory == null) {
        LOGGER.warn("Unable to get a factory for type ''{0}''", type);
        return;/*  www .ja v a 2  s  . c  o  m*/
    }

    final Object value = factory.getObjectInstance(properties, extraClasspath);
    if (value != null) {
        LOGGER.info("Binding object to '{}/{}' (type: '{}')", context.getNameInNamespace(), name, type);
        execBind(context, name, value);
    }
}

From source file:com.egt.core.util.Utils.java

public static void traceContext(String name) {
    Context c;
    try {//from w ww. ja  va  2  s.  c om
        c = new InitialContext();
        Bitacora.trace(c.getClass(), "list", c.getNameInNamespace());
        traceContext(c, name);
    } catch (NamingException ex) {
        Bitacora.logFatal(ThrowableUtils.getString(ex));
    }
}

From source file:com.egt.core.util.Utils.java

public static void traceContext() {
    Context c;
    try {/*  w  w  w.ja  v  a2  s  .  co  m*/
        c = new InitialContext();
        Bitacora.trace(c.getClass(), "list", c.getNameInNamespace());
        //          traceContext(c, "java:comp");
        //          traceContext(c, "java:global");
    } catch (NamingException ex) {
        Bitacora.logFatal(ThrowableUtils.getString(ex));
    }
}

From source file:com.cyberway.issue.crawler.Heritrix.java

protected static void registerJndi(final ObjectName name) throws NullPointerException, NamingException {
    Context c = getJndiContext();
    if (c == null) {
        return;//from w  ww  .j a va2s .  c  om
    }
    CompoundName key = JndiUtils.bindObjectName(c, name);
    if (logger.isLoggable(Level.FINE)) {
        logger.fine("Bound '" + key + "' to '" + JndiUtils.getCompoundName(c.getNameInNamespace()).toString()
                + "' jndi context");
    }
}

From source file:com.cyberway.issue.crawler.Heritrix.java

protected static void deregisterJndi(final ObjectName name) throws NullPointerException, NamingException {
    Context c = getJndiContext();
    if (c == null) {
        return;/*from  w  w w .jav a  2  s .  c  om*/
    }
    CompoundName key = JndiUtils.unbindObjectName(c, name);
    if (logger.isLoggable(Level.FINE)) {
        logger.fine("Unbound '" + key + "' from '"
                + JndiUtils.getCompoundName(c.getNameInNamespace()).toString() + "' jndi context");
    }
}

From source file:org.acegisecurity.ldap.LdapUtils.java

/**
 * Obtains the part of a DN relative to a supplied base context.<p>If the DN is
 * "cn=bob,ou=people,dc=acegisecurity,dc=org" and the base context name is "ou=people,dc=acegisecurity,dc=org" it
 * would return "cn=bob".</p>/*from   w  ww  .  j av a2 s.com*/
 *
 * @param fullDn the DN
 * @param baseCtx the context to work out the name relative to.
 *
 * @return the
 *
 * @throws NamingException any exceptions thrown by the context are propagated.
 */
public static String getRelativeName(String fullDn, Context baseCtx) throws NamingException {
    String baseDn = baseCtx.getNameInNamespace();

    if (baseDn.length() == 0) {
        return fullDn;
    }

    if (baseDn.equals(fullDn)) {
        return "";
    }

    int index = fullDn.lastIndexOf(baseDn);

    Assert.isTrue(index > 0, "Context base DN is not contained in the full DN");

    // remove the base name and preceding comma.
    return fullDn.substring(0, index - 1);
}

From source file:org.netoprise.neo4j.ConnectorTest.java

@Test
@Ignore//ww  w  .j av  a2  s. c o m
@OperateOnDeployment("test")
public void listJNDI() {
    try {
        Context context = new InitialContext();
        System.out.println("Context namespace: " + context.getNameInNamespace());
        NamingEnumeration<NameClassPair> content = context.list("comp");
        while (content.hasMoreElements()) {
            NameClassPair nameClassPair = (NameClassPair) content.nextElement();
            System.out
                    .println("Name :" + nameClassPair.getName() + " with type:" + nameClassPair.getClassName());
        }
    } catch (NamingException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.nuxeo.runtime.datasource.DataSourceFactory.java

@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> env) throws Exception {
    Reference ref = (Reference) obj;
    if (!DataSource.class.getName().equals(ref.getClassName())) {
        return null;
    }//from   w w w  .j  a v a 2s.co m

    TransactionManager transactionManager;
    try {
        transactionManager = TransactionHelper.lookupTransactionManager();
    } catch (NamingException e) {
        transactionManager = null;
    }

    boolean xa = ref.get(BasicManagedDataSourceFactory.PROP_XADATASOURCE) != null;
    log.info(String.format("Creating pooled %s datasource: %s/%s", xa ? "XA" : "non-XA",
            nameCtx.getNameInNamespace(), name));

    if (xa && transactionManager == null) {
        throw new RuntimeException(
                "Cannot configure XA datasource " + name + " without an available transaction manager");
    }

    // extract properties from Reference
    Map<String, String> properties = new HashMap<String, String>();
    Enumeration<RefAddr> refAddrs = ref.getAll();
    while (refAddrs.hasMoreElements()) {
        RefAddr ra = refAddrs.nextElement();
        String key = ra.getType();
        String value = ra.getContent().toString();
        if (key.startsWith(DataSourceDescriptor.PROP_PREFIX)) {
            key = key.substring(DataSourceDescriptor.PROP_PREFIX.length());
            properties.put(key, value);
        }
    }

    DataSource ds;
    if (!xa) {
        // fetch url from properties
        for (Entry<String, String> en : properties.entrySet()) {
            // often misspelled, thus the ignore case
            if (URL_LOWER.equalsIgnoreCase(en.getKey())) {
                ref.add(new StringRefAddr(URL_LOWER, en.getValue()));
            }
        }
        ObjectFactory factory = new BasicDataSourceFactory();
        ds = (DataSource) factory.getObjectInstance(ref, name, nameCtx, env);
        BasicDataSource bds = (BasicDataSource) ds;

        // set properties
        for (Entry<String, String> en : properties.entrySet()) {
            String key = en.getKey();
            if (URL_LOWER.equalsIgnoreCase(key)) {
                continue;
            }
            bds.addConnectionProperty(key, en.getValue());
        }
    } else {
        ObjectFactory factory = new BasicManagedDataSourceFactory();
        ds = (DataSource) factory.getObjectInstance(obj, name, nameCtx, env);
        if (ds == null) {
            return null;
        }
        BasicManagedDataSource bmds = (BasicManagedDataSource) ds;

        // set transaction manager
        bmds.setTransactionManager(transactionManager);

        // set properties
        XADataSource xaDataSource = bmds.getXaDataSourceInstance();
        if (xaDataSource == null) {
            return null;
        }
        for (Entry<String, String> en : properties.entrySet()) {
            String key = en.getKey();
            // proper JavaBean convention for initial cap
            if (Character.isLowerCase(key.charAt(1))) {
                key = Character.toLowerCase(key.charAt(0)) + key.substring(1);
            }
            String value = en.getValue();
            boolean ok = false;
            try {
                BeanUtils.setProperty(xaDataSource, key, value);
                ok = true;
            } catch (Exception e) {
                if (URL_LOWER.equals(key)) {
                    // commonly misspelled
                    try {
                        BeanUtils.setProperty(xaDataSource, URL_UPPER, value);
                        ok = true;
                    } catch (Exception ee) {
                        // log error below
                    }
                }
            }
            if (!ok) {
                log.error(String.format("Cannot set %s = %s on %s", key, value,
                        xaDataSource.getClass().getName()));
            }
        }
    }
    return ds;
}

From source file:org.springframework.ldap.core.support.DefaultDirObjectFactory.java

public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment, Attributes attrs)
        throws Exception {

    try {/*from   w ww  .  jav a 2 s .  c o m*/
        String nameInNamespace = null;
        if (nameCtx != null) {
            nameInNamespace = nameCtx.getNameInNamespace();
        } else {
            nameInNamespace = "";
        }

        return constructAdapterFromName(attrs, name, nameInNamespace);
    } finally {
        // It seems that the object supplied to the obj parameter is a
        // DirContext instance with reference to the same Ldap connection as
        // the original context. Since it is not the same instance (that's
        // the nameCtx parameter) this one really needs to be closed in
        // order to correctly clean up and return the connection to the pool
        // when we're finished with the surrounding operation.
        if (obj instanceof Context) {

            Context ctx = (Context) obj;
            try {
                ctx.close();
            } catch (Exception e) {
                // Never mind this
            }

        }
    }
}