1. /*
  2. * Copyright 2003-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: Utils.java,v 1.3 2004/02/17 04:18:18 minchau Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.serializer;
  20. import java.util.Hashtable;
  21. /**
  22. * This class contains utilities used by the serializer
  23. */
  24. class Utils
  25. {
  26. /**
  27. * This nested class acts as a way to lazy load the hashtable
  28. * in a thread safe way.
  29. */
  30. static private class CacheHolder
  31. {
  32. static final Hashtable cache;
  33. static {
  34. cache = new Hashtable();
  35. }
  36. }
  37. /**
  38. * Load the class by name.
  39. *
  40. * This implementation, for performance reasons,
  41. * caches all classes loaded by name and
  42. * returns the cached Class object if it can previously
  43. * loaded classes that were load by name. If not previously loaded
  44. * an attempt is made to load with Class.forName(classname)
  45. * @param classname the name of the class to be loaded
  46. * @return the loaded class, never null. If the class could not be
  47. * loaded a ClassNotFound exception is thrown.
  48. * @throws ClassNotFoundException if the class was not loaded
  49. */
  50. static Class ClassForName(String classname) throws ClassNotFoundException
  51. {
  52. Class c;
  53. // the first time the next line runs will reference
  54. // CacheHolder, causing the class to load and create the
  55. // Hashtable.
  56. Object o = CacheHolder.cache.get(classname);
  57. if (o == null)
  58. {
  59. // class was not in the cache, so try to load it
  60. c = Class.forName(classname);
  61. // if the class is not found we will have thrown a
  62. // ClassNotFoundException on the statement above
  63. // if we get here c is not null
  64. CacheHolder.cache.put(classname, c);
  65. }
  66. else
  67. {
  68. c = (Class)o;
  69. }
  70. return c;
  71. }
  72. }