PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/shine/src/main/java/org/apache/shiro/realm/ldap/LdapUtils.java

http://cng1985.googlecode.com/
Java | 97 lines | 45 code | 10 blank | 42 comment | 5 complexity | a28884be8d4219e78064d46e06b5b589 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.apache.shiro.realm.ldap;
  20. import java.util.Collection;
  21. import java.util.HashSet;
  22. import java.util.Set;
  23. import javax.naming.NamingEnumeration;
  24. import javax.naming.NamingException;
  25. import javax.naming.directory.Attribute;
  26. import javax.naming.ldap.LdapContext;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;
  29. /**
  30. * Utility class providing static methods to make working with LDAP
  31. * easier.
  32. *
  33. * @since 0.2
  34. */
  35. public final class LdapUtils {
  36. /**
  37. * Private internal log instance.
  38. */
  39. private static final Logger log = LoggerFactory.getLogger(LdapUtils.class);
  40. /**
  41. * Closes an LDAP context, logging any errors, but not throwing
  42. * an exception if there is a failure.
  43. *
  44. * @param ctx the LDAP context to close.
  45. */
  46. public static void closeContext(LdapContext ctx) {
  47. try {
  48. if (ctx != null) {
  49. ctx.close();
  50. }
  51. } catch (NamingException e) {
  52. log.error("Exception while closing LDAP context. ", e);
  53. }
  54. }
  55. /**
  56. * Helper method used to retrieve all attribute values from a particular context attribute.
  57. *
  58. * @param attr the LDAP attribute.
  59. * @return the values of the attribute.
  60. * @throws javax.naming.NamingException if there is an LDAP error while reading the values.
  61. */
  62. public static Collection<String> getAllAttributeValues(Attribute attr) throws NamingException {
  63. Set<String> values = new HashSet<String>();
  64. NamingEnumeration ne = null;
  65. try {
  66. ne = attr.getAll();
  67. while (ne.hasMore()) {
  68. String value = (String) ne.next();
  69. values.add(value);
  70. }
  71. } finally {
  72. closeEnumeration(ne);
  73. }
  74. return values;
  75. }
  76. //added based on SHIRO-127, per Emmanuel's comment [1]
  77. // [1] https://issues.apache.org/jira/browse/SHIRO-127?focusedCommentId=12891380&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12891380
  78. public static void closeEnumeration(NamingEnumeration ne) {
  79. try {
  80. if (ne != null) {
  81. ne.close();
  82. }
  83. } catch (NamingException e) {
  84. log.error("Exception while closing NamingEnumeration: ", e);
  85. }
  86. }
  87. }