View Javadoc

1   /*
2    * Copyright 2002-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  package org.apache.commons.clazz;
17  
18  import java.util.HashMap;
19  
20  import org.apache.commons.clazz.common.GroupClazzLoader;
21  
22  /**
23   * An implementation of <code>ClazzLoader</code> that functions as the top-
24   * level clazz loader for a model and caches all clazzes by name.
25   *
26   * @author <a href="mailto:dmitri@apache.org">Dmitri Plotnikov</a>
27   * @version $Id: ModelClazzLoader.java 155436 2005-02-26 13:17:48Z dirkv $
28   */
29  public class ModelClazzLoader extends GroupClazzLoader {
30  
31      private String model;
32      private HashMap clazzMap = new HashMap();
33  
34      public ModelClazzLoader(String model) {
35          super(null);
36          this.model = model;
37      }
38      
39      public String getModel() {
40          return model;
41      }
42      
43      /**
44       * @see ClazzLoader#getClazzForName(String)
45       */
46      public Clazz getClazzForName(String name) {
47          Clazz clazz = (Clazz) clazzMap.get(name);
48          if (clazz == null) {
49              clazz = super.getClazzForName(name);
50              if (clazz != null) {
51                  clazzMap.put(name, clazz);
52              }
53          }
54          return clazz;
55      }
56  
57      /**
58       * @see ClazzLoader#defineClazz(String, Class, Class)
59       */
60      public Clazz defineClazz(
61              String name,
62              Class clazzType,
63              Class instanceClass) 
64      {
65          Clazz clazz = super.defineClazz(name, clazzType, instanceClass);
66          if (clazz != null) {
67              clazzMap.put(name, clazz);
68          }
69          return clazz;
70      }
71  }