本文整理匯總了Java中javax.naming.directory.InitialDirContext.lookup方法的典型用法代碼示例。如果您正苦於以下問題:Java InitialDirContext.lookup方法的具體用法?Java InitialDirContext.lookup怎麽用?Java InitialDirContext.lookup使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.naming.directory.InitialDirContext
的用法示例。
在下文中一共展示了InitialDirContext.lookup方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createLdapDataSource
import javax.naming.directory.InitialDirContext; //導入方法依賴的package包/類
@Override
public DataSource createLdapDataSource(Properties loginProperties, String ldapName) throws NamingException
{
DataSource ds;
// borisv: this code stores all parameters in static baseEnvironment. So if you have multiple connections, next connection will get parameters from previous (unless overwriten by loginParamters).
loginProperties.put(USE_JNDI_JDBC_CONNECTION_POOL_KEY, "false");
if (loginProperties.getProperty(JAVA_NAMING_FACTORY_INITIAL) == null)
{
loginProperties.put(JAVA_NAMING_FACTORY_INITIAL, "com.sun.jndi.ldap.LdapCtxFactory");
}
InitialDirContext context = new JdbcInitialDirContext(loginProperties);
Enumeration propKeys = loginProperties.keys();
while(propKeys.hasMoreElements())
{
Object key = propKeys.nextElement();
context.addToEnvironment((String)key, loginProperties.get(key));
}
ds = (DataSource) context.lookup(ldapName);
return ds;
}
示例2: testConnectControls2
import javax.naming.directory.InitialDirContext; //導入方法依賴的package包/類
public void testConnectControls2() throws Exception {
// set connect controls by property "java.naming.ldap.control.connect"
env.put("java.naming.ldap.control.connect",
new Control[] { new SortControl("", Control.NONCRITICAL) });
server.setResponseSeq(new LdapMessage[] { new LdapMessage(
LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
InitialDirContext initialDirContext = new InitialDirContext(env);
server.setResponseSeq(new LdapMessage[] { new LdapMessage(
LdapASN1Constant.OP_SEARCH_RESULT_DONE,
new EncodableLdapResult(), null) });
LdapContext context = (LdapContext) initialDirContext.lookup("");
Control[] controls = context.getConnectControls();
assertNotNull(controls);
assertEquals(1, controls.length);
Control c = controls[0];
assertTrue(c instanceof SortControl);
assertEquals(Control.NONCRITICAL, c.isCritical());
}
示例3: performTask
import javax.naming.directory.InitialDirContext; //導入方法依賴的package包/類
/**
* Method performTask Control the flow of control of the logical operations
* (方法performTask 可控製邏輯操作的控製流)
*/
public synchronized void performTask() throws Exception {
System.out.println("\n Setting Up Initial JNDI Context ");
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
env.put(Context.PROVIDER_URL, providerUrl);
env.put(Context.REFERRAL, "throw");
jndiContext = new InitialDirContext(env);
System.out.println("\n Get QueueConnectionFactory ");
queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup(qcfName);
System.out.println("\n Get Queue ");
queue = (Queue) jndiContext.lookup(queueName);
System.out.println("\n Create Queue Connections ");
queueConnection = queueConnectionFactory.createQueueConnection();
//為連接對象queueConnection 設置了異常偵聽器
PtpListener jmsListener = new PtpListener();
queueConnection.setExceptionListener(jmsListener);
System.out.println("\n Start Queue Connection ");
queueConnection.start();
System.out.println("\n Create Queue Session ");
queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
System.out.println("\n Create Queue Receiver ");
queueReceiver = queueSession.createReceiver(queue);
//注冊消息偵聽器
System.out.println("\n Register the Listener");
queueReceiver.setMessageListener(jmsListener);
// Wait for new messages.(等待新消息。)
wait();
}
示例4: start
import javax.naming.directory.InitialDirContext; //導入方法依賴的package包/類
/**
* start the connector
*/
public void start() throws Exception {
LOG.info("connecting...");
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
this.ldapURI = getUri();
LOG.debug(" URI [{}]", this.ldapURI);
env.put(Context.PROVIDER_URL, this.ldapURI.toString());
if (anonymousAuthentication) {
LOG.debug(" login credentials [anonymous]");
env.put(Context.SECURITY_AUTHENTICATION, "none");
} else {
LOG.debug(" login credentials [{}:******]", user);
env.put(Context.SECURITY_PRINCIPAL, user);
env.put(Context.SECURITY_CREDENTIALS, password);
}
boolean isConnected = false;
while (!isConnected) {
try {
context = new InitialDirContext(env);
isConnected = true;
} catch (CommunicationException err) {
if (failover) {
this.ldapURI = getUri();
LOG.error("connection error [{}], failover connection to [{}]", env.get(Context.PROVIDER_URL), this.ldapURI.toString());
env.put(Context.PROVIDER_URL, this.ldapURI.toString());
Thread.sleep(curReconnectDelay);
curReconnectDelay = Math.min(curReconnectDelay * 2, maxReconnectDelay);
} else {
throw err;
}
}
}
// add connectors from search results
LOG.info("searching for network connectors...");
LOG.debug(" base [{}]", base);
LOG.debug(" filter [{}]", searchFilter);
LOG.debug(" scope [{}]", searchControls.getSearchScope());
NamingEnumeration<SearchResult> results = context.search(base, searchFilter, searchControls);
while (results.hasMore()) {
addConnector(results.next());
}
// register persistent search event listener
if (searchEventListener) {
LOG.info("registering persistent search listener...");
EventDirContext eventContext = (EventDirContext) context.lookup("");
eventContext.addNamingListener(base, searchFilter, searchControls, this);
} else { // otherwise close context (i.e. connection as it is no longer needed)
context.close();
}
}
示例5: testAddToEnvironment
import javax.naming.directory.InitialDirContext; //導入方法依賴的package包/類
public void testAddToEnvironment() throws Exception {
server.setResponseSeq(new LdapMessage[] { new LdapMessage(
LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
assertNull(env.get(Context.REFERRAL));
InitialDirContext initialDirContext = new InitialDirContext(env);
// Context.REFERRAL changed doesn't cause re-bind operation
initialDirContext.addToEnvironment(Context.REFERRAL, "ignore");
assertEquals("ignore", initialDirContext.getEnvironment().get(
Context.REFERRAL));
server.setResponseSeq(new LdapMessage[] { new LdapMessage(
LdapASN1Constant.OP_DEL_RESPONSE, new EncodableLdapResult(),
null) });
initialDirContext.destroySubcontext("cn=test");
/*
* Context.SECURITY_AUTHENTICATION will case re-bind when invoke context
* methods at first time
*/
Object preValue = initialDirContext.addToEnvironment(
Context.SECURITY_AUTHENTICATION, "none");
assertFalse("none".equals(preValue));
server.setResponseSeq(new LdapMessage[] {
new LdapMessage(LdapASN1Constant.OP_BIND_RESPONSE,
new BindResponse(), null),
new LdapMessage(LdapASN1Constant.OP_SEARCH_RESULT_DONE,
new EncodableLdapResult(), null) });
initialDirContext.lookup("");
preValue = initialDirContext.addToEnvironment(
Context.SECURITY_AUTHENTICATION, "simple");
assertFalse("simple".equals(preValue));
// initialDirContext is shared connection, will create new connection
server = new MockLdapServer(server);
server.start();
server.setResponseSeq(new LdapMessage[] {
new LdapMessage(LdapASN1Constant.OP_BIND_RESPONSE,
new BindResponse(), null),
new LdapMessage(LdapASN1Constant.OP_SEARCH_RESULT_DONE,
new EncodableLdapResult(), null) });
initialDirContext.lookup("");
}