1 19 package org.apache.cayenne.reflect; 20 21 import java.util.ArrayList ; 22 import java.util.HashMap ; 23 import java.util.List ; 24 import java.util.ListIterator ; 25 import java.util.Map ; 26 27 import org.apache.cayenne.CayenneRuntimeException; 28 import org.apache.cayenne.map.EntityResolver; 29 30 37 public class ClassDescriptorMap { 38 39 protected EntityResolver resolver; 40 protected Map descriptors; 41 protected List factories; 42 43 public ClassDescriptorMap(EntityResolver resolver) { 44 this.descriptors = new HashMap (); 45 this.resolver = resolver; 46 this.factories = new ArrayList (); 47 } 48 49 public EntityResolver getResolver() { 50 return resolver; 51 } 52 53 56 public void addFactory(ClassDescriptorFactory factory) { 57 factories.add(factory); 58 } 59 60 public void removeFactory(ClassDescriptorFactory factory) { 61 factories.remove(factory); 62 } 63 64 public void clearFactories() { 65 factories.clear(); 66 } 67 68 public void clearDescriptors() { 69 descriptors.clear(); 70 } 71 72 75 public void removeDescriptor(String entityName) { 76 descriptors.remove(entityName); 77 } 78 79 82 public void addDescriptor(String entityName, ClassDescriptor descriptor) { 83 if (descriptor == null) { 84 removeDescriptor(entityName); 85 } 86 else { 87 descriptors.put(entityName, descriptor); 88 } 89 } 90 91 public ClassDescriptor getDescriptor(String entityName) { 92 ClassDescriptor cached = (ClassDescriptor) descriptors.get(entityName); 93 if (cached != null) { 94 return cached; 95 } 96 97 return createProxyDescriptor(entityName); 98 } 99 100 105 protected ClassDescriptor createProxyDescriptor(String entityName) { 106 ClassDescriptor descriptor = new LazyClassDescriptorDecorator(this, entityName); 107 addDescriptor(entityName, descriptor); 108 return descriptor; 109 } 110 111 114 protected ClassDescriptor createDescriptor(String entityName) { 115 116 ListIterator it = factories.listIterator(factories.size()); 120 while (it.hasPrevious()) { 121 ClassDescriptorFactory factory = (ClassDescriptorFactory) it.previous(); 122 ClassDescriptor descriptor = factory.getDescriptor(entityName); 123 124 if (descriptor != null) { 125 return descriptor; 126 } 127 } 128 129 throw new CayenneRuntimeException("Failed to create descriptor for entity: " 130 + entityName); 131 } 132 } 133 | Popular Tags |