1. /*
  2. * @(#)FactoryEnumeration.java 1.3 00/02/02
  3. *
  4. * Copyright 1999, 2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package com.sun.naming.internal;
  11. import java.util.Vector;
  12. import javax.naming.NamingException;
  13. /**
  14. * The FactoryEnumeration is used for returning factory instances.
  15. *
  16. * @author Rosanna Lee
  17. * @author Scott Seligman
  18. * @version 1.3 00/02/02
  19. */
  20. // no need to implement Enumeration since this is only for internal use
  21. public final class FactoryEnumeration {
  22. private Vector vec;
  23. private int posn = 0;
  24. /**
  25. * Records the input vector and uses it directly to satisfy
  26. * hasMore()/next() requests. An alternative would have been to use
  27. * vec.elements(), but we want to update the vector so we keep the
  28. * original Vector. The Vector initially contains Class objects.
  29. * As each element is used, the Class object is replaced by an
  30. * instance of the Class itself; eventually, the vector contains
  31. * only a list of factory instances and no more updates are required.
  32. *
  33. * @param A non-null vector.
  34. */
  35. FactoryEnumeration(Vector vec) {
  36. this.vec = vec;
  37. }
  38. public Object next() throws NamingException {
  39. synchronized (vec) {
  40. Object answer = vec.elementAt(posn++);
  41. if (!(answer instanceof Class)) {
  42. return answer;
  43. }
  44. // Still a Class; need to instantiate
  45. try {
  46. answer = ((Class)answer).newInstance();
  47. vec.setElementAt(answer, posn-1); // replace Class object
  48. return answer;
  49. } catch (InstantiationException e) {
  50. NamingException ne =
  51. new NamingException("Cannot instantiate " + answer);
  52. ne.setRootCause(e);
  53. throw ne;
  54. } catch (IllegalAccessException e) {
  55. NamingException ne = new NamingException("Cannot access " + answer);
  56. ne.setRootCause(e);
  57. throw ne;
  58. }
  59. }
  60. }
  61. public boolean hasMore() {
  62. return posn < vec.size();
  63. }
  64. }