Hashtable env = new Hashtable(5); env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); //Assuming weblogic server is running on localhost at port 7001 env.put(Context.PROVIDER_URL, "t3://localhost:7001"); Context ic = new InitialContext(env); //obtain a reference to the home or local home interface FooHome fooHome = (FooHome)ic.lookup("MyBeans/FooHome"); //Get a reference to an object that implements the beans remote (component) interface Foo foo = fooHome.create(); //call the service exposed by the bean foo.shoutFoo()
ctx = (props != null) ? new InitialContext(props): new InitialContext(); ds = ctx.lookup(url); if (!isAlwaysLookup()) { this.datasource = ds; return (((XADataSource) ds).getXAConnection().getConnection()); } else if (ds instanceof DataSource) { return ((DataSource) ds).getConnection(); } else { throw new SQLException("Object at JNDI URL '" + url + "' is not a DataSource."); } finally { if (ctx != null) { try { ctx.close(); } catch(Exception ignore) {}
ctx.rebind(name, val); Name n = ctx.getNameParser( "" ).parse( name ); while ( n.size() > 1 ) { final String ctxName = n.get( 0 ); subctx = (Context) ctx.lookup( ctxName ); ctx = ctx.createSubcontext( ctxName ); n = n.getSuffix( 1 ); ctx.rebind( n, val );
/** * Create a subcontext including any intermediate contexts. * @param ctx the parent JNDI Context under which value will be bound * @param name the name relative to ctx of the subcontext. * @return The new or existing JNDI subcontext * @throws NamingException on any JNDI failure */ private static Context createSubcontext(Context ctx, final Name name) throws NamingException { Context subctx = ctx; for (int pos = 0; pos < name.size(); pos++) { final String ctxName = name.get(pos); try { subctx = (Context) ctx.lookup(ctxName); } catch (NameNotFoundException e) { subctx = ctx.createSubcontext(ctxName); } // The current subctx will be the ctx for the next name component ctx = subctx; } return subctx; }
/** * Unbinds a name from ctx, and removes parents if they are empty * * @param ctx the parent JNDI Context under which the name will be unbound * @param name The name to unbind * @throws NamingException for any error */ public static void unbind(Context ctx, Name name) throws NamingException { ctx.unbind(name); //unbind the end node in the name int sz = name.size(); // walk the tree backwards, stopping at the domain while (--sz > 0) { Name pname = name.getPrefix(sz); try { ctx.destroySubcontext(pname); } catch (NamingException e) { //log.trace("Unable to remove context " + pname, e); break; } } } }
assertTrue("Correct DataSource registered", context1.lookup("java:comp/env/jdbc/myds") == ds); assertTrue("Correct Object registered", context1.lookup("myobject") == obj); Hashtable<String, String> env2 = new Hashtable<>(); env2.put("key1", "value1"); Context context2 = factory.getInitialContext(env2); assertTrue("Correct DataSource registered", context2.lookup("java:comp/env/jdbc/myds") == ds); assertTrue("Correct Object registered", context2.lookup("myobject") == obj); assertTrue("Correct environment", context2.getEnvironment() != env2); assertTrue("Correct key1", "value1".equals(context2.getEnvironment().get("key1"))); context1.rebind("myinteger", i); String s = ""; context2.bind("mystring", s); Context context3 = (Context) context2.lookup(""); context3.rename("java:comp/env/jdbc/myds", "jdbc/myds"); context3.unbind("myobject"); assertTrue("Correct environment", context3.getEnvironment() != context2.getEnvironment()); context3.addToEnvironment("key2", "value2"); assertTrue("key2 added", "value2".equals(context3.getEnvironment().get("key2"))); context3.removeFromEnvironment("key1"); assertTrue("key1 removed", context3.getEnvironment().get("key1") == null); assertTrue("Correct DataSource registered", context1.lookup("jdbc/myds") == ds); try { context1.lookup("myobject"); fail("Should have thrown NameNotFoundException");
// Obtain our environment naming context Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); // Look up our data source DataSource ds = (DataSource) envCtx.lookup("jdbc/EmployeeDB"); // Allocate and use a connection from the pool Connection conn = ds.getConnection();
@Override protected RedissonClient buildClient() throws LifecycleException { InitialContext context = null; try { context = new InitialContext(); Context envCtx = (Context) context.lookup("java:comp/env"); return (RedissonClient) envCtx.lookup(jndiName); } catch (NamingException e) { throw new LifecycleException("Unable to locate Redisson instance by name: " + jndiName, e); } finally { if (context != null) { try { context.close(); } catch (NamingException e) { throw new LifecycleException("Unable to close JNDI context", e); } } } }
@Override public Object lookup(final String name) throws NamingException { try { final int index = name.indexOf('#'); if (index != -1) { final String server = name.substring(0, index); final String lookup = name.substring(index + 1); @SuppressWarnings("unchecked") final Hashtable<Object,Object> environment = (Hashtable<Object,Object>) this.environment.clone(); environment.put(Context.PROVIDER_URL, server); environment.put(ORBConstants.ORB_SERVER_ID_PROPERTY, "1"); return CNCtxFactory.INSTANCE.getInitialContext(environment).lookup(lookup); } else { return CNCtxFactory.INSTANCE.getInitialContext(environment).lookup(name); } } catch (NamingException e) { throw e; } catch (Exception e) { throw new NamingException(e.getMessage()); } }
DataSource dataSource = null; try { Context context = new InitialContext(); dataSource = (DataSource) context.lookup("Database"); } catch (NamingException e) { // Couldn't find the data source: give up }
@Override public Object lookup(Name name) throws NamingException { if (name.isEmpty()) { return this.clone(); String atomicName = normalizedName.get(0); Object atomicNameBinding = bindings.get(atomicName); if (normalizedName.size() == 1) { if (atomicNameBinding == null) { throw new NameNotFoundException("Name ["+atomicName+"] from ["+normalizedName+"] not found in this binding"); env); } catch (Exception e) { NamingException _e = new NamingException("NamingManager.getObjectInstance(...) failed"); _e.setRootCause(e); throw _e; return ((Context)atomicNameBinding).lookup(normalizedName.getSuffix(1)); throw new NotContextException(atomicName+" from ["+name+"] is not a context, cannot perform further lookup");
@Override public void unbind(Name name) throws NamingException { if (name.isEmpty()) { throw new InvalidNameException("Name ["+name+"] is empty "); } Name normalizedName = normalizeName(name); String atomicName = normalizedName.get(0); // first component Object atomicNameBinding = bindings.get(atomicName); if (normalizedName.size() == 1) { bindings.remove(atomicName); } else { if (atomicNameBinding instanceof Context) { ((Context)atomicNameBinding).unbind(normalizedName.getSuffix(1)); } else { throw new NotContextException(atomicName+" from ["+name+"] is not a context, cannot perform further binding"); } } }
public JndiDataSourceProvider(String dsName, String jndiName, String dialect, boolean showSql) { this.dsName = dsName; Context ctx; try { ctx = new InitialContext(); ds = (DataSource) ctx.lookup(jndiName); if (ds == null) { throw new DBException("Jndi could not found error for " + jndiName); } } catch (NamingException e) { throw new DBException(e.getMessage(), e); } this.dialect = DialectFactory.get(dialect == null ? "mysql" : dialect); this.showSql = showSql; }
builder.bind(name, o); Context ctx = new InitialContext(); assertTrue(ctx.lookup(name) == o); ctx.unbind(name); try { ctx = new InitialContext(); ctx.lookup(name); fail("Should have thrown NamingException"); ctx = new InitialContext(); ctx.lookup(name); fail("Should have thrown NamingException"); assertEquals(ctx.lookup(name), o2);
/** * @see javax.naming.Context#unbind(javax.naming.Name) */ public void unbind(Name name) throws NamingException { if(name.isEmpty()) { throw new InvalidNameException("Cannot unbind to empty name"); } if(name.size() == 1) { if(table.containsKey(name)) { table.remove(name); } return; } /* Look up the target context first. */ Object targetContext = lookup(name.getPrefix(name.size() - 1)); if(targetContext == null || !(targetContext instanceof Context)) { throw new NamingException("Cannot unbind object. Target context does not exist."); } ((Context)targetContext).unbind(name.getSuffix(name.size() - 1)); }
Context ctx = NamingManager.getURLContext(scheme, environment); if (ctx == null) { throw new NamingException("scheme " + scheme + " not recognized"); return ctx.lookup(name); } else { Object obj = bindings.get(first); if (obj == null) { throw new NameNotFoundException(name); } else if (obj instanceof Context && path.size() > 1) { Context subContext = (Context) obj; obj = subContext.lookup(path.getSuffix(1)); throw e; } catch (Exception e) { throw (NamingException) new NamingException("could not look up : " + name).initCause(e);
/** * Unbinds the named object. Removes the terminal atomic name in * <code>name</code> from the target context--that named by all but the * terminal atomic part of <code>name</code>. * @param name the name to unbind; may not be empty * @throws NamingException if a naming exception is encountered */ public void unbind(final Name name) throws NamingException { if (name.isEmpty()) { throw new NamingException("Cannot unbind empty name"); } try { wrappedContext.unbind(encode(name)); if (exportedObjects.containsKey(name)) { ConfigurationRepository.getCurrentConfiguration().getProtocol().getPortableRemoteObject().unexportObject( (Remote) exportedObjects.remove(name)); } } catch (Exception e) { throw NamingExceptionHelper.create("Cannot unbind name '" + name + "' : " + e.getMessage(), e); } }
public static String lookup(Context ctx, String name) { if (ctx == null) { return null; } try { Object lookup = ctx.lookup(name); return lookup == null ? null : lookup.toString(); } catch (NamingException e) { return null; } } }
checkIsDestroyed(); Name parsedName = getParsedName(name); if (parsedName.size() == 0 || parsedName.get(0).length() == 0) { throw new InvalidNameException( "Name can not be empty!"); String nameToRemove = parsedName.get(0); if (boundObject instanceof Context) { ((Context) boundObject).unbind(parsedName.getSuffix(1)); } else { throw new NameNotFoundException( String.format("Can not find %s", name)); throw new NotContextException(String.format("Expected Context but found %s", boundObject));