本文整理汇总了Java中javax.naming.directory.DirContext.close方法的典型用法代码示例。如果您正苦于以下问题:Java DirContext.close方法的具体用法?Java DirContext.close怎么用?Java DirContext.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.naming.directory.DirContext
的用法示例。
在下文中一共展示了DirContext.close方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doInContext
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
public <T> T doInContext(DirContext ctx, InContext<T> inContext)
{
try
{
return inContext.execute(ctx);
}
catch( NamingException ne )
{
throw new RuntimeException("LDAP Error", ne);
}
finally
{
try
{
ctx.close();
}
catch( NamingException e )
{
throw new RuntimeException(e);
}
}
}
示例2: close
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
/**
* Close any open connection to the directory server for this Realm.
*
* @param context The directory context to be closed
*/
protected void close(DirContext context) {
// Do nothing if there is no opened connection
if (context == null)
return;
// Close our opened connection
try {
if (containerLog.isDebugEnabled())
containerLog.debug("Closing directory context");
context.close();
} catch (NamingException e) {
containerLog.error(sm.getString("jndiRealm.close"), e);
}
this.context = null;
}
示例3: uid2ext
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
public String uid2ext(String uid) {
try {
DirContext ctx = null;
try {
ctx = getDirContext();
Attributes attributes = ctx.getAttributes(
ApplicationProperties.getProperty("tmtbl.authenticate.ldap.uid2ext").replaceAll("%", uid),
new String[] {
ApplicationProperties.getProperty("tmtbl.authenticate.ldap.externalId", "puid")
});
if (attributes!=null) {
Attribute puid = attributes.get(ApplicationProperties.getProperty("tmtbl.authenticate.ldap.externalId", "puid"));
if (puid!=null) return (String)puid.get();
}
} finally {
if (ctx!=null) ctx.close();
}
} catch (Exception e) {
Debug.error("Unable to translate uid to ext, "+e.getMessage());
}
return null;
}
示例4: ext2uid
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
public String ext2uid(String puid) {
try {
DirContext ctx = null;
try {
ctx = getDirContext();
Attributes attributes = ctx.getAttributes(
ApplicationProperties.getProperty("tmtbl.authenticate.ldap.ext2uid").replaceAll("%", puid),
new String[] {
ApplicationProperties.getProperty("tmtbl.authenticate.ldap.login", "uid")
});
if (attributes!=null) {
Attribute uid = attributes.get(ApplicationProperties.getProperty("tmtbl.authenticate.ldap.login", "uid"));
if (uid!=null) return (String)uid.get();
}
} finally {
if (ctx!=null) ctx.close();
}
} catch (Exception e) {
Debug.error("Unable to translate ext to uid, "+e.getMessage());
}
return null;
}
示例5: close
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
/**
* Close any open connection to the directory server for this Realm.
*
* @param context The directory context to be closed
*/
protected void close(DirContext context) {
// Do nothing if there is no opened connection
if (context == null)
return;
// Close our opened connection
try {
if (debug >= 1)
log("Closing directory context");
context.close();
} catch (NamingException e) {
log(sm.getString("jndiRealm.close"), e);
}
this.context = null;
}
示例6: getUserAttributes
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
public static Map<String, String> getUserAttributes(DirContext ctx, String searchBase, String userName,
String principalDomain, String... attributeNames)
throws NamingException {
if (StringUtils.isBlank(userName)) {
throw new IllegalArgumentException("Username and password can not be blank.");
}
if (attributeNames.length == 0) {
return Collections.emptyMap();
}
Attributes matchAttr = new BasicAttributes(true);
BasicAttribute basicAttr = new BasicAttribute("userPrincipalName", userName + principalDomain);
matchAttr.put(basicAttr);
NamingEnumeration<? extends SearchResult> searchResult = ctx.search(searchBase, matchAttr, attributeNames);
if (ctx != null) {
ctx.close();
}
Map<String, String> result = new HashMap<>();
if (searchResult.hasMore()) {
NamingEnumeration<? extends Attribute> attributes = searchResult.next().getAttributes().getAll();
while (attributes.hasMore()) {
Attribute attr = attributes.next();
String attrId = attr.getID();
String attrValue = (String) attr.get();
result.put(attrId, attrValue);
}
}
return result;
}
示例7: reverseDns
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
/**
* Returns the hostname associated with the specified IP address by the
* provided nameserver.
*
* Loopback addresses
* @param hostIp The address to reverse lookup
* @param ns The host name of a reachable DNS server
* @return The host name associated with the provided IP
* @throws NamingException If a NamingException is encountered
*/
public static String reverseDns(InetAddress hostIp, @Nullable String ns)
throws NamingException {
//
// Builds the reverse IP lookup form
// This is formed by reversing the IP numbers and appending in-addr.arpa
//
String[] parts = hostIp.getHostAddress().split("\\.");
String reverseIP = parts[3] + "." + parts[2] + "." + parts[1] + "."
+ parts[0] + ".in-addr.arpa";
DirContext ictx = new InitialDirContext();
Attributes attribute;
try {
attribute = ictx.getAttributes("dns://" // Use "dns:///" if the default
+ ((ns == null) ? "" : ns) +
// nameserver is to be used
"/" + reverseIP, new String[] { "PTR" });
} finally {
ictx.close();
}
String hostname = attribute.get("PTR").get().toString();
int hostnameLength = hostname.length();
if (hostname.charAt(hostnameLength - 1) == '.') {
hostname = hostname.substring(0, hostnameLength - 1);
}
return hostname;
}
示例8: closeContext
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
/**
* Closes the given directory context.
*
* @param The
* context to close.
*/
private void closeContext(DirContext ctx) {
if (ctx != null) {
try {
ctx.close();
} catch (Exception e) {
logger.logError(
Log4jLogger.SYSTEM_LOG,
e,
LogMessageIdentifier.ERROR_CLOSE_DIRECTORY_CONTEXT_FAILED);
}
}
}
示例9: reverseDNS
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
/**
* This method uses JNDI to look up an address in DNS and return its name
*
* @param addr
*
* @return the host name associated with the address or null if lookup isn't possible or there is
* no host name for this address
*/
public static String reverseDNS(InetAddress addr) {
byte[] addrBytes = addr.getAddress();
// reverse the address suitable for reverse lookup
String lookup = "";
for (int index = addrBytes.length - 1; index >= 0; index--) {
lookup = lookup + (addrBytes[index] & 0xff) + '.';
}
lookup += "in-addr.arpa";
// System.out.println("Looking up: " + lookup);
try {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
DirContext ctx = new InitialDirContext(env);
Attributes attrs = ctx.getAttributes(lookup, new String[] {"PTR"});
for (NamingEnumeration ae = attrs.getAll(); ae.hasMoreElements();) {
Attribute attr = (Attribute) ae.next();
for (Enumeration vals = attr.getAll(); vals.hasMoreElements();) {
Object elem = vals.nextElement();
if ("PTR".equals(attr.getID()) && elem != null) {
return elem.toString();
}
}
}
ctx.close();
} catch (Exception e) {
// ignored
}
return null;
}
示例10: reverseDNS
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
/**
* This method uses JNDI to look up an address in DNS and return its name
*
* @return the host name associated with the address or null if lookup isn't possible or there is
* no host name for this address
*/
private static String reverseDNS(InetAddress addr) {
byte[] addrBytes = addr.getAddress();
// reverse the address suitable for reverse lookup
StringBuilder sb = new StringBuilder();
for (int index = addrBytes.length - 1; index >= 0; index--) {
// lookup = lookup + (addrBytes[index] & 0xff) + '.';
sb.append((addrBytes[index] & 0xff)).append('.');
}
sb.append("in-addr.arpa");
String lookup = sb.toString();
try {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
DirContext ctx = new InitialDirContext(env);
Attributes attrs = ctx.getAttributes(lookup, new String[] {"PTR"});
for (NamingEnumeration ae = attrs.getAll(); ae.hasMoreElements();) {
Attribute attr = (Attribute) ae.next();
for (Enumeration vals = attr.getAll(); vals.hasMoreElements();) {
Object elem = vals.nextElement();
if ("PTR".equals(attr.getID()) && elem != null) {
return elem.toString();
}
}
}
ctx.close();
} catch (Exception e) {
// ignored
}
return null;
}
示例11: authenticate
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
@Override
public Principal authenticate(final Properties credentials, final DistributedMember member) {
final String userName = credentials.getProperty(UserPasswordAuthInit.USER_NAME);
if (userName == null) {
throw new AuthenticationFailedException("LdapUserAuthenticator: user name property ["
+ UserPasswordAuthInit.USER_NAME + "] not provided");
}
String password = credentials.getProperty(UserPasswordAuthInit.PASSWORD);
if (password == null) {
password = "";
}
final Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, com.sun.jndi.ldap.LdapCtxFactory.class.getName());
env.put(Context.PROVIDER_URL, this.ldapUrlScheme + this.ldapServer + '/' + this.baseDomainName);
env.put(Context.SECURITY_PRINCIPAL, "uid=" + userName + "," + this.baseDomainName);
env.put(Context.SECURITY_CREDENTIALS, password);
try {
final DirContext ctx = new InitialDirContext(env);
ctx.close();
} catch (Exception e) {
throw new AuthenticationFailedException(
"LdapUserAuthenticator: Failure with provided username, password combination for user name: "
+ userName,
e);
}
return new UsernamePrincipal(userName);
}
示例12: closeHard
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
/**
* Close any open connection to the directory server for this Realm.
*
* @param context The directory context to be closed
*/
private void closeHard(final DirContext context) {
// Do nothing if there is no opened connection
if (context == null) {
return;
}
// Close our opened connection
try {
context.close();
} catch (NamingException e) {
log.error("Error while closing context", e);
}
}
示例13: reverseDns
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
/**
* Returns the hostname associated with the specified IP address by the
* provided nameserver.
*
* Loopback addresses
* @param hostIp The address to reverse lookup
* @param ns The host name of a reachable DNS server
* @return The host name associated with the provided IP
* @throws NamingException If a NamingException is encountered
*/
public static String reverseDns(InetAddress hostIp, String ns)
throws NamingException {
//
// Builds the reverse IP lookup form
// This is formed by reversing the IP numbers and appending in-addr.arpa
//
String[] parts = hostIp.getHostAddress().split("\\.");
String reverseIP = parts[3] + "." + parts[2] + "." + parts[1] + "."
+ parts[0] + ".in-addr.arpa";
DirContext ictx = new InitialDirContext();
Attributes attribute;
try {
attribute = ictx.getAttributes("dns://" // Use "dns:///" if the default
+ ((ns == null) ? "" : ns) +
// nameserver is to be used
"/" + reverseIP, new String[] { "PTR" });
} finally {
ictx.close();
}
String hostname = attribute.get("PTR").get().toString();
int hostnameLength = hostname.length();
if (hostname.charAt(hostnameLength - 1) == '.') {
hostname = hostname.substring(0, hostnameLength - 1);
}
return hostname;
}
示例14: doLookup
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
@Override
public UserInfo doLookup(String searchId) throws Exception {
String query = ApplicationProperties.getProperty("tmtbl.authenticate.ldap.identify");
if (query == null) return null;
DirContext ctx = null;
try {
ctx = getDirContext();
String idAttributeName = ApplicationProperties.getProperty("tmtbl.authenticate.ldap.externalId","uid");
String loginAttributeName = ApplicationProperties.getProperty("tmtbl.authenticate.ldap.login", "uid");
Attributes attributes = ctx.getAttributes(query.replaceAll("%", searchId), new String[] {idAttributeName, loginAttributeName, "cn", "givenName", "sn", "mail"});
Attribute idAttribute = attributes.get(idAttributeName);
if (idAttribute == null) return null;
UserInfo user = new UserInfo();
user.setExternalId((String)idAttribute.get());
user.setUserName((String)attributes.get(loginAttributeName).get());
if (attributes.get("cn") != null)
user.setName((String)attributes.get("cn").get());
if (attributes.get("givenName") != null)
user.setFirstName((String)attributes.get("givenName").get());
if (attributes.get("cn") != null)
user.setName((String)attributes.get("cn").get());
if (attributes.get("sn") != null)
user.setLastName((String)attributes.get("sn").get());
if (attributes.get("mail") != null) {
user.setEmail((String)attributes.get("mail").get());
} else {
String email = user.getUserName() + "@";
for (String x: query.split(","))
if (x.startsWith("dc=")) email += (email.endsWith("@") ? "" : ".") + x.substring(3);
if (!email.endsWith("@")) user.setEmail(email);
}
return user;
} finally {
if (ctx != null) ctx.close();
}
}