KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > serializer > Utils


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
21 import java.util.Hashtable JavaDoc;
22
23 /**
24  * This class contains utilities used by the serializer
25  */

26 class Utils
27 {
28
29     /**
30      * This nested class acts as a way to lazy load the hashtable
31      * in a thread safe way.
32      */

33     static private class CacheHolder
34     {
35         static final Hashtable JavaDoc cache;
36         static {
37             cache = new Hashtable JavaDoc();
38         }
39     }
40     /**
41      * Load the class by name.
42      *
43      * This implementation, for performance reasons,
44      * caches all classes loaded by name and
45      * returns the cached Class object if it can previously
46      * loaded classes that were load by name. If not previously loaded
47      * an attempt is made to load with Class.forName(classname)
48      * @param classname the name of the class to be loaded
49      * @return the loaded class, never null. If the class could not be
50      * loaded a ClassNotFound exception is thrown.
51      * @throws ClassNotFoundException if the class was not loaded
52      */

53     static Class JavaDoc ClassForName(String JavaDoc classname) throws ClassNotFoundException JavaDoc
54     {
55         Class JavaDoc c;
56         // the first time the next line runs will reference
57
// CacheHolder, causing the class to load and create the
58
// Hashtable.
59
Object JavaDoc o = CacheHolder.cache.get(classname);
60         if (o == null)
61         {
62             // class was not in the cache, so try to load it
63
c = Class.forName(classname);
64             // if the class is not found we will have thrown a
65
// ClassNotFoundException on the statement above
66

67             // if we get here c is not null
68
CacheHolder.cache.put(classname, c);
69         }
70         else
71         {
72             c = (Class JavaDoc)o;
73         }
74         return c;
75     }
76 }
77
Popular Tags