Example usage for javax.naming Context listBindings

List of usage examples for javax.naming Context listBindings

Introduction

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

Prototype

public NamingEnumeration<Binding> listBindings(String name) throws NamingException;

Source Link

Document

Enumerates the names bound in the named context, along with the objects bound to them.

Usage

From source file:com.jsmartframework.web.manager.BeanHandler.java

private void lookupInContext(Context context, String prefix) {
    try {//  ww w.  j  a va  2  s. c o  m
        prefix += "/";
        NamingEnumeration<Binding> bindList = context.listBindings("");
        while (bindList.hasMore()) {
            Binding bind = bindList.next();
            if (bind != null) {
                if (bind.getObject() instanceof Context) {
                    lookupInContext((Context) bind.getObject(), prefix + bind.getName());
                }
                String[] binds = bind.getName().split("!");
                if (binds.length > 1) {
                    try {
                        jndiMapping.put(Class.forName(binds[1]), prefix + binds[0]);
                    } catch (Throwable ex) {
                        LOGGER.log(Level.WARNING,
                                "Class could not be found for EJB mapping: " + ex.getMessage());
                    }
                }
            }
        }
    } catch (Throwable ex) {
        LOGGER.log(Level.WARNING, "Bindings could not be found for EJB context: " + ex.getMessage());
    }
}

From source file:com.ritchey.naming.InitialContextFactory.java

/**
 * Get Context that has access to default Namespace. This method won't be
 * called if a name URL beginning with java: is passed to an InitialContext.
 *
 * @see org.mortbay.naming.java.javaURLContextFactory
 * @param env a <code>Hashtable</code> value
 * @return a <code>Context</code> value
 *///from   w w w .  j a  v  a2s  . co m
public Context getInitialContext(Hashtable env) {
    Log.debug("InitialContext loaded");
    Context ctx = new localContextRoot(env);

    Properties properties = new Properties();
    try {
        properties.load(new FileInputStream("build.properties"));
    } catch (Exception e1) {
        e1.printStackTrace();
    }

    Context jdbc = null;
    try {
        jdbc = ctx.createSubcontext("jdbc");
    } catch (NamingException e) {
        try {
            jdbc = (Context) ctx.lookup("jdbc");
        } catch (NamingException e1) {
            e1.printStackTrace();
        }
    }
    Context ldap = null;
    try {
        ldap = ctx.createSubcontext("ldap");
    } catch (NamingException e) {
        try {
            ldap = (Context) ctx.lookup("ldap");
        } catch (NamingException e1) {
            e1.printStackTrace();
        }
    }

    Log.debug("getInitialContext");

    String databaseNames = properties.getProperty("database.jndi.names");
    if (databaseNames == null) {
        Log.warn(new RuntimeException("database.jndi.names is not defined"
                + " in build.properties as a comma separated list in " + "build.properties"));
        return ctx;
    }

    for (String database : databaseNames.split(" *, *")) {
        Log.debug("create " + database);
        try {
            createDs(database, properties, jdbc);
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }

    try {
        createLdapStrings(properties, ldap);
    } catch (NamingException e1) {
        e1.printStackTrace();
    }

    String url = getValue(false, "picture", null, properties);
    try {
        ctx.bind("picture", url);
    } catch (NamingException ex) {
        Logger.getLogger(InitialContextFactory.class.getName()).log(Level.SEVERE, null, ex);
    }

    try {
        Log.debug("jdbc initial context = " + ctx.listBindings("jdbc"));
        NamingEnumeration<Binding> ldapBindings = ctx.listBindings("ldap");
        Log.debug("ldap initial context = " + ctx.listBindings("ldap"));
        while (ldapBindings.hasMore()) {
            Binding binding = ldapBindings.next();
            Log.debug("binding: " + binding.getName());
        }
    } catch (NamingException e) {
        e.printStackTrace();
    }
    return ctx;
}

From source file:hermes.browser.HermesBrowser.java

/**
 * Initialise the underlying Hermes that we're gonna do all our work with
 * /*from  w w w. j a  v a2s  .c o  m*/
 * @throws HermesException
 * @throws NamingException
 */
public void loadConfig() throws NamingException, HermesException {
    Properties props = new Properties();
    Context oldContext = context;
    HermesConfig oldConfig = null;

    props.put(Context.INITIAL_CONTEXT_FACTORY, HermesInitialContextFactory.class.getName());
    props.put(Context.PROVIDER_URL, getCurrentConfigURL());
    props.put("hermes.loader", JAXBHermesLoader.class.getName());

    log.debug("props=" + props);

    Iterator listeners = null;

    if (loader != null) {
        listeners = loader.getConfigurationListeners();
        oldConfig = loader.getConfig();
    }

    if (oldConfig != null) {
        Set naming = new HashSet();
        naming.addAll(oldConfig.getNaming());

        for (Iterator iter = naming.iterator(); iter.hasNext();) {
            NamingConfig oldNaming = (NamingConfig) iter.next();

            loader.notifyNamingRemoved(oldNaming);
        }
    }

    context = new InitialContext(props);
    loader = (HermesLoader) context.lookup(HermesContext.LOADER);

    if (listeners != null) {
        while (listeners.hasNext()) {
            loader.addConfigurationListener((HermesConfigurationListener) listeners.next());
        }
    }

    if (oldContext != null) {
        for (NamingEnumeration iter = oldContext.listBindings(""); iter.hasMoreElements();) {
            Binding binding = (Binding) iter.next();

            try {
                if (oldContext.lookup(binding.getName()) instanceof Hermes) {
                    Hermes hermes = (Hermes) oldContext.lookup(binding.getName());
                    Hermes newHermes = null;

                    try {
                        newHermes = (Hermes) context.lookup(hermes.getId());
                    } catch (NamingException e) {
                        // NOP
                    }

                    if (newHermes == null) {
                        loader.notifyHermesRemoved(hermes);
                    }
                }
            } catch (NamingException ex) {
                // NOP
            }
        }
    }

    if (!firstLoad) {
        closeWatches();
        final ArrayList tmpList = new ArrayList();
        tmpList.addAll(loader.getConfig().getWatch());
        loader.getConfig().getWatch().clear();

        for (Iterator iter = tmpList.iterator(); iter.hasNext();) {
            WatchConfig wConfig = (WatchConfig) iter.next();
            createWatch(wConfig);
        }
    }

    setTitle("HermesJMS - " + TextUtils.crumble(getCurrentConfigURL(), 100));
}

From source file:org.apache.activemq.jndi.JNDITestSupport.java

protected void assertBinding(Binding binding) throws NamingException {
    Object object = binding.getObject();
    assertTrue("Should have got a child context but got: " + object, object instanceof Context);

    Context childContext = (Context) object;
    NamingEnumeration<Binding> iter = childContext.listBindings("");
    while (iter.hasMore()) {
        Binding destinationBinding = iter.next();
        LOG.info("Found destination: " + destinationBinding.getName());
        Object destination = destinationBinding.getObject();
        assertTrue("Should have a Destination but got: " + destination, destination instanceof Destination);
    }/* w ww  .j av  a  2s . co  m*/
}

From source file:org.springframework.ejb.support.JndiEnvironmentBeanDefinitionReader.java

/**
 * Creates new JNDIBeanFactory/*from   w ww  . ja va  2 s  . c  o  m*/
 * @param root likely to be "java:comp/env"
 */
public JndiEnvironmentBeanDefinitionReader(BeanDefinitionRegistry beanFactory, String root)
        throws BeansException {
    // We'll take everything from the NamingContext and dump it in a
    // Properties object, so that the superclass can efficiently manipulate it
    // after we've closed the context.
    HashMap m = new HashMap();

    Context initCtx = null;
    try {
        initCtx = new InitialContext();
        // Parameterize
        NamingEnumeration bindings = initCtx.listBindings(root);

        // Orion 1.5.2 doesn't seem to regard anything under a /
        // as a true subcontext, so we need to search all bindings
        // Not all that fast, but it doesn't matter            
        while (bindings.hasMore()) {
            Binding binding = (Binding) bindings.next();
            logger.debug("Name: " + binding.getName());
            logger.debug("Type: " + binding.getClassName());
            logger.debug("Value: " + binding.getObject());
            m.put(binding.getName(), binding.getObject());
        }
        bindings.close();

        PropertiesBeanDefinitionReader propReader = new PropertiesBeanDefinitionReader(beanFactory);
        propReader.registerBeanDefinitions(m, BEANS_PREFIX);
    } catch (NamingException ex) {
        logger.debug("----- NO PROPERTIES FOUND " + ex);
    } finally {
        try {
            if (initCtx != null) {
                initCtx.close();
            }
        } catch (NamingException ex) {
            // IGNORE OR THROW RTE?
        }
    }
}

From source file:org.wso2.carbon.uuf.core.API.java

/**
 * Returns a map of service implementation class names and instances of all OSGi services for the given service
 * class name./*from w  w w . j a  v a2  s .  c  o m*/
 *
 * @param serviceClassName service class name
 * @return a map of implementation class and instances
 */
public static Map<String, Object> getOSGiServices(String serviceClassName) {
    try {
        Context context = new InitialContext();
        NamingEnumeration<Binding> enumeration = context.listBindings("osgi:service/" + serviceClassName);
        Map<String, Object> services = new HashMap<>();
        while (enumeration.hasMore()) {
            Binding binding = enumeration.next();
            services.put(binding.getClassName(), binding.getObject());
        }
        return services;
    } catch (NamingException e) {
        throw new UUFException(
                "Cannot create the initial context when calling OSGi service '" + serviceClassName + "'.");
    }
}