AuthInfo.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* AuthInfo.java --
  2. Copyright (C) 2003, 2006 Free Software Foundation, Inc.
  3. This file is a part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or (at
  7. your option) any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  15. USA
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.crypto.sasl;
  32. import gnu.java.security.Registry;
  33. import java.util.ArrayList;
  34. import java.util.Iterator;
  35. import java.util.StringTokenizer;
  36. /**
  37. * A static class for creating {@link IAuthInfoProvider} providers. It
  38. * transparently locates and uses any provider instances, based on the value
  39. * assigned to the System property with the key
  40. * <code>gnu.crypto.sasl.auth.info.provider.pkgs</code>. If more than one is
  41. * specified they SHOULD be separated with a vertical bar character. Please note
  42. * that the GNU provider is always added last to the list, disregarding whether
  43. * it was mentioned or not in the value of that property, or if it that property
  44. * was not defined.
  45. */
  46. public class AuthInfo
  47. {
  48. private static final ArrayList factories = new ArrayList();
  49. static
  50. {
  51. IAuthInfoProviderFactory ours = new AuthInfoProviderFactory();
  52. // if SASL_AUTH_INFO_PROVIDER_PKGS is defined then parse it
  53. String clazz;
  54. String pkgs = System.getProperty(Registry.SASL_AUTH_INFO_PROVIDER_PKGS,
  55. null);
  56. if (pkgs != null)
  57. {
  58. for (StringTokenizer st = new StringTokenizer(pkgs, "|"); st.hasMoreTokens();)
  59. {
  60. clazz = st.nextToken().trim();
  61. if (! "gnu.javax.crypto.sasl".equals(clazz))
  62. {
  63. clazz += ".AuthInfoProviderFactory";
  64. try
  65. {
  66. IAuthInfoProviderFactory factory =
  67. (IAuthInfoProviderFactory) Class.forName(clazz).newInstance();
  68. factories.add(factory);
  69. }
  70. catch (ClassCastException ignored)
  71. {
  72. }
  73. catch (ClassNotFoundException ignored)
  74. {
  75. }
  76. catch (InstantiationException ignored)
  77. {
  78. }
  79. catch (IllegalAccessException ignored)
  80. {
  81. }
  82. }
  83. }
  84. }
  85. // always add ours last; unless it's already there
  86. if (!factories.contains(ours))
  87. factories.add(ours);
  88. }
  89. /** Trivial constructor to enforce Singleton pattern. */
  90. private AuthInfo()
  91. {
  92. super();
  93. }
  94. /**
  95. * A convenience method to return the authentication information provider for
  96. * a designated SASL mechnanism. It goes through all the installed provider
  97. * factories, one at a time, and attempts to return a new instance of the
  98. * provider for the designated mechanism. It stops at the first factory
  99. * returning a non-null provider.
  100. *
  101. * @param mechanism the name of a SASL mechanism.
  102. * @return an implementation that provides {@link IAuthInfoProvider} for that
  103. * mechanism; or <code>null</code> if none found.
  104. */
  105. public static IAuthInfoProvider getProvider(String mechanism)
  106. {
  107. for (Iterator it = factories.iterator(); it.hasNext();)
  108. {
  109. IAuthInfoProviderFactory factory = (IAuthInfoProviderFactory) it.next();
  110. IAuthInfoProvider result = factory.getInstance(mechanism);
  111. if (result != null)
  112. return result;
  113. }
  114. return null;
  115. }
  116. }