javax.naming.Context类的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(11.2k)|赞(0)|评价(0)|浏览(265)

本文整理了Java中javax.naming.Context类的一些代码示例,展示了Context类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Context类的具体详情如下:
包路径:javax.naming.Context
类名称:Context

Context介绍

暂无

代码示例

代码示例来源:origin: stackoverflow.com

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()

代码示例来源:origin: quartz-scheduler/quartz

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) {}

代码示例来源:origin: hibernate/hibernate-orm

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 );

代码示例来源:origin: wildfly/wildfly

/**
 * 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;
}

代码示例来源:origin: wildfly/wildfly

/**
   * 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;
      }
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

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");

代码示例来源:origin: stackoverflow.com

// 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();

代码示例来源:origin: stackoverflow.com

Hashtable props = new Hashtable();
String principalName = username + "@" + domainName;
props.put(Context.SECURITY_PRINCIPAL, principalName);
props.put(Context.SECURITY_CREDENTIALS, password);
DirContext context;
  context.close();

代码示例来源:origin: redisson/redisson

@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);
      }
    }
  }
}

代码示例来源:origin: wildfly/wildfly

@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());
  }
}

代码示例来源:origin: stackoverflow.com

DataSource dataSource = null;
try
{
  Context context = new InitialContext();
  dataSource = (DataSource) context.lookup("Database");
}
catch (NamingException e)
{
  // Couldn't find the data source: give up
}

代码示例来源:origin: com.ironoreserver/com.ironoreserver.core

@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");

代码示例来源:origin: com.ironoreserver/com.ironoreserver.core

@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");
    }
  }
}

代码示例来源:origin: Dreampie/Resty

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;
}

代码示例来源:origin: spring-projects/spring-framework

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);

代码示例来源:origin: simple-jndi/simple-jndi

/**
 * @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));
}

代码示例来源:origin: wildfly/wildfly

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);

代码示例来源:origin: org.ow2.carol/carol

/**
 * 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);
  }
}

代码示例来源:origin: ch.qos.logback/logback-classic

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;
    }
  }
}

代码示例来源:origin: apache/geode

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));

相关文章