001 /* 002 * Copyright 2002-2004 The Apache Software Foundation 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.apache.commons.clazz; 017 018 import java.util.HashMap; 019 020 import org.apache.commons.clazz.common.GroupClazzLoader; 021 022 /** 023 * An implementation of <code>ClazzLoader</code> that functions as the top- 024 * level clazz loader for a model and caches all clazzes by name. 025 * 026 * @author <a href="mailto:dmitri@apache.org">Dmitri Plotnikov</a> 027 * @version $Id: ModelClazzLoader.java 155436 2005-02-26 13:17:48Z dirkv $ 028 */ 029 public class ModelClazzLoader extends GroupClazzLoader { 030 031 private String model; 032 private HashMap clazzMap = new HashMap(); 033 034 public ModelClazzLoader(String model) { 035 super(null); 036 this.model = model; 037 } 038 039 public String getModel() { 040 return model; 041 } 042 043 /** 044 * @see ClazzLoader#getClazzForName(String) 045 */ 046 public Clazz getClazzForName(String name) { 047 Clazz clazz = (Clazz) clazzMap.get(name); 048 if (clazz == null) { 049 clazz = super.getClazzForName(name); 050 if (clazz != null) { 051 clazzMap.put(name, clazz); 052 } 053 } 054 return clazz; 055 } 056 057 /** 058 * @see ClazzLoader#defineClazz(String, Class, Class) 059 */ 060 public Clazz defineClazz( 061 String name, 062 Class clazzType, 063 Class instanceClass) 064 { 065 Clazz clazz = super.defineClazz(name, clazzType, instanceClass); 066 if (clazz != null) { 067 clazzMap.put(name, clazz); 068 } 069 return clazz; 070 } 071 }