KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > ojb > constraints > ReferenceDescriptorConstraints


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 /* Copyright 2004-2005 The Apache Software Foundation
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */

23
24 /**
25  * Checks constraints for reference descriptors. Note that constraints may modify the reference descriptor.
26  * For checks of the relationships (e.g. foreignkey) see ModelConstraints.
27  *
28  * @author <a HREF="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
29  */

30 public class ReferenceDescriptorConstraints extends FeatureDescriptorConstraints
31 {
32     /**
33      * Checks the given reference descriptor.
34      *
35      * @param refDef The reference descriptor
36      * @param checkLevel The amount of checks to perform
37      * @exception ConstraintException If a constraint has been violated
38      */

39     public void check(ReferenceDescriptorDef refDef, String JavaDoc checkLevel) throws ConstraintException
40     {
41         ensureClassRef(refDef, checkLevel);
42         checkProxyPrefetchingLimit(refDef, checkLevel);
43     }
44
45     /**
46      * Ensures that the given reference descriptor has the class-ref property.
47      *
48      * @param refDef The reference descriptor
49      * @param checkLevel The current check level (this constraint is checked in basic (partly) and strict)
50      * @exception ConstraintException If a constraint has been violated
51      */

52     private void ensureClassRef(ReferenceDescriptorDef refDef, String JavaDoc 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                 // we use the type of the reference variable
64
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         // now checking the type
73
ClassDescriptorDef ownerClassDef = (ClassDescriptorDef)refDef.getOwner();
74         ModelDef model = (ModelDef)ownerClassDef.getOwner();
75         String JavaDoc 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                     // anonymous reference: class must be a baseclass of the owner class
96
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                     // specified element class must be a subtype of the variable type (if it exists, i.e. not for anonymous references)
104
String JavaDoc varType = refDef.getProperty(PropertyHelper.OJB_PROPERTY_VARIABLE_TYPE);
105                     boolean performCheck = true;
106     
107                     // but we first check whether there is a useable type for the the variable type
108
if (model.getClass(varType) == null)
109                     {
110                         try
111                         {
112                             InheritanceHelper.getClass(varType);
113                         }
114                         catch (ClassNotFoundException JavaDoc ex)
115                         {
116                             // no, so defer the check but issue a warning
117
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 JavaDoc 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         // we're adjusting the property to use the classloader-compatible form
136
refDef.setProperty(PropertyHelper.OJB_PROPERTY_CLASS_REF, targetClassDef.getName());
137     }
138 }
139
Popular Tags