1 package xdoclet.modules.ojb.constraints; 2 3 import xdoclet.modules.ojb.LogHelper; 4 import xdoclet.modules.ojb.model.ClassDescriptorDef; 5 import xdoclet.modules.ojb.model.ModelDef; 6 import xdoclet.modules.ojb.model.PropertyHelper; 7 import xdoclet.modules.ojb.model.ReferenceDescriptorDef; 8 9 23 24 30 public class ReferenceDescriptorConstraints extends FeatureDescriptorConstraints 31 { 32 39 public void check(ReferenceDescriptorDef refDef, String checkLevel) throws ConstraintException 40 { 41 ensureClassRef(refDef, checkLevel); 42 checkProxyPrefetchingLimit(refDef, checkLevel); 43 } 44 45 52 private void ensureClassRef(ReferenceDescriptorDef refDef, String checkLevel) throws ConstraintException 53 { 54 if (CHECKLEVEL_NONE.equals(checkLevel)) 55 { 56 return; 57 } 58 59 if (!refDef.hasProperty(PropertyHelper.OJB_PROPERTY_CLASS_REF)) 60 { 61 if (refDef.hasProperty(PropertyHelper.OJB_PROPERTY_DEFAULT_CLASS_REF)) 62 { 63 refDef.setProperty(PropertyHelper.OJB_PROPERTY_CLASS_REF, refDef.getProperty(PropertyHelper.OJB_PROPERTY_DEFAULT_CLASS_REF)); 65 } 66 else 67 { 68 throw new ConstraintException("Reference "+refDef.getName()+" in class "+refDef.getOwner().getName()+" does not reference any class"); 69 } 70 } 71 72 ClassDescriptorDef ownerClassDef = (ClassDescriptorDef)refDef.getOwner(); 74 ModelDef model = (ModelDef)ownerClassDef.getOwner(); 75 String targetClassName = refDef.getProperty(PropertyHelper.OJB_PROPERTY_CLASS_REF); 76 ClassDescriptorDef targetClassDef = model.getClass(targetClassName); 77 78 if (targetClassDef == null) 79 { 80 throw new ConstraintException("The class "+targetClassName+" referenced by "+refDef.getName()+" in class "+ownerClassDef.getName()+" is unknown or not persistent"); 81 } 82 if (!targetClassDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_OJB_PERSISTENT, false)) 83 { 84 throw new ConstraintException("The class "+targetClassName+" referenced by "+refDef.getName()+" in class "+ownerClassDef.getName()+" is not persistent"); 85 } 86 87 if (CHECKLEVEL_STRICT.equals(checkLevel)) 88 { 89 try 90 { 91 InheritanceHelper helper = new InheritanceHelper(); 92 93 if (refDef.isAnonymous()) 94 { 95 if (!helper.isSameOrSubTypeOf(ownerClassDef, targetClassDef.getName(), true)) 97 { 98 throw new ConstraintException("The class "+targetClassName+" referenced by the anonymous reference "+refDef.getName()+" in class "+ownerClassDef.getName()+" is not a basetype of the class"); 99 } 100 } 101 else 102 { 103 String varType = refDef.getProperty(PropertyHelper.OJB_PROPERTY_VARIABLE_TYPE); 105 boolean performCheck = true; 106 107 if (model.getClass(varType) == null) 109 { 110 try 111 { 112 InheritanceHelper.getClass(varType); 113 } 114 catch (ClassNotFoundException ex) 115 { 116 performCheck = false; 118 LogHelper.warn(true, 119 getClass(), 120 "ensureClassRef", 121 "Cannot check whether the type "+targetClassDef.getQualifiedName()+" specified as class-ref at reference "+refDef.getName()+" in class "+ownerClassDef.getName()+" is assignable to the declared type "+varType+" of the reference because this variable type cannot be found in source or on the classpath"); 122 } 123 } 124 if (performCheck && !helper.isSameOrSubTypeOf(targetClassDef, varType, true)) 125 { 126 throw new ConstraintException("The class "+targetClassName+" referenced by "+refDef.getName()+" in class "+ownerClassDef.getName()+" is not the same or a subtype of the variable type "+varType); 127 } 128 } 129 } 130 catch (ClassNotFoundException ex) 131 { 132 throw new ConstraintException("Could not find the class "+ex.getMessage()+" on the classpath while checking the reference "+refDef.getName()+" in class "+refDef.getOwner().getName()); 133 } 134 } 135 refDef.setProperty(PropertyHelper.OJB_PROPERTY_CLASS_REF, targetClassDef.getName()); 137 } 138 } 139 | Popular Tags |