KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > sessions > CommitOrderDependencyNode


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.sessions;
23
24 import java.util.*;
25 import oracle.toplink.essentials.descriptors.InheritancePolicy;
26 import oracle.toplink.essentials.mappings.*;
27 import oracle.toplink.essentials.internal.helper.*;
28 import oracle.toplink.essentials.internal.localization.*;
29 import oracle.toplink.essentials.descriptors.ClassDescriptor;
30
31 /**
32  * This wraps a descriptor with information required to compute an order for
33  * dependencies. The algorithm is a simple topological sort.
34  */

35 public class CommitOrderDependencyNode {
36     protected CommitOrderCalculator owner;
37     protected ClassDescriptor descriptor;
38     protected AbstractSession session;
39
40     // These are the descriptors to which we have 1:1 relationships
41
protected Vector relatedNodes;
42     protected CommitOrderDependencyNode predecessor;
43
44     // Indicates the state of the traversal
45
protected int traversalState;
46     static public int NotVisited = 1;
47     static public int InProgress = 2;
48     static public int Visited = 3;
49
50     // When we first saw this node in the traversal
51
protected int discoveryTime;
52
53     // When we finished visiting this node
54
protected int finishingTime;
55
56     public CommitOrderDependencyNode(CommitOrderCalculator calculator, ClassDescriptor descriptor, AbstractSession session) {
57         this.owner = calculator;
58         this.descriptor = descriptor;
59         this.relatedNodes = new Vector();
60         this.session = session;
61     }
62
63     public ClassDescriptor getDescriptor() {
64         return descriptor;
65     }
66
67     public int getFinishingTime() {
68         return finishingTime;
69     }
70
71     public CommitOrderCalculator getOwner() {
72         return owner;
73     }
74
75     public CommitOrderDependencyNode getPredecessor() {
76         return predecessor;
77     }
78
79     public Vector getRelatedNodes() {
80         return relatedNodes;
81     }
82
83     public boolean hasBeenVisited() {
84         return (traversalState == Visited);
85     }
86
87     public boolean hasNotBeenVisited() {
88         return (traversalState == NotVisited);
89     }
90
91     public void markInProgress() {
92         traversalState = InProgress;
93     }
94
95     public void markNotVisited() {
96         traversalState = NotVisited;
97     }
98
99     public void markVisited() {
100         traversalState = Visited;
101     }
102
103     /**
104      * Add all owned classes for each descriptor through checking the mappings.
105      * If I have a foreign mapping with a constraint dependency, then add it
106      * If I'm related to a class, I'm related to all its subclasses and superclasses.
107      * If my superclass is related to a class, I'm related to it.
108      */

109     public void recordMappingDependencies() {
110         for (Enumeration mappings = getDescriptor().getMappings().elements();
111                  mappings.hasMoreElements();) {
112             DatabaseMapping mapping = (DatabaseMapping)mappings.nextElement();
113             if (mapping.isForeignReferenceMapping()) {
114                 if (((ForeignReferenceMapping)mapping).hasConstraintDependency()) {
115                     Class JavaDoc ownedClass;
116                     ClassDescriptor refDescriptor = ((ForeignReferenceMapping)mapping).getReferenceDescriptor();
117                     if (refDescriptor == null) {
118                         refDescriptor = session.getDescriptor(((ForeignReferenceMapping)mapping).getReferenceClass());
119                     }
120                     ownedClass = refDescriptor.getJavaClass();
121
122                     if (ownedClass == null) {
123                         throw oracle.toplink.essentials.exceptions.DescriptorException.referenceClassNotSpecified(mapping);
124                     }
125                     CommitOrderDependencyNode node = getOwner().nodeFor(ownedClass);
126                     Vector ownedNodes = withAllSubclasses(node);
127
128                     // I could remove duplicates here, but it's not that big a deal.
129
Helper.addAllToVector(relatedNodes, ownedNodes);
130                 } else if (((ForeignReferenceMapping)mapping).hasInverseConstraintDependency()) {
131                     Class JavaDoc ownerClass;
132                     ClassDescriptor refDescriptor = ((ForeignReferenceMapping)mapping).getReferenceDescriptor();
133                     if (refDescriptor == null) {
134                         refDescriptor = session.getDescriptor(((ForeignReferenceMapping)mapping).getReferenceClass());
135                     }
136                     ownerClass = refDescriptor.getJavaClass();
137                     if (ownerClass == null) {
138                         throw oracle.toplink.essentials.exceptions.DescriptorException.referenceClassNotSpecified(mapping);
139                     }
140                     CommitOrderDependencyNode ownerNode = getOwner().nodeFor(ownerClass);
141                     Vector ownedNodes = withAllSubclasses(this);
142
143                     // I could remove duplicates here, but it's not that big a deal.
144
Helper.addAllToVector(ownerNode.getRelatedNodes(), ownedNodes);
145                 }
146             }
147         }
148     }
149
150     /**
151      * Add all owned classes for each descriptor through checking the mappings.
152      * If I have a foreign mapping with a constraint dependency, then add it
153      * If I'm related to a class, I'm related to all its subclasses and superclasses.
154      * If my superclass is related to a class, I'm related to it.
155      */

156     public void recordSpecifiedDependencies() {
157         for (Enumeration constraintsEnum = getDescriptor().getConstraintDependencies().elements();
158                  constraintsEnum.hasMoreElements();) {
159             Class JavaDoc ownedClass = (Class JavaDoc)constraintsEnum.nextElement();
160             CommitOrderDependencyNode node = getOwner().nodeFor(ownedClass);
161             Vector ownedNodes = withAllSubclasses(node);
162
163             // I could remove duplicates here, but it's not that big a deal.
164
Helper.addAllToVector(relatedNodes, ownedNodes);
165         }
166     }
167
168     public void setDiscoveryTime(int time) {
169         discoveryTime = time;
170     }
171
172     public void setFinishingTime(int time) {
173         finishingTime = time;
174     }
175
176     public void setPredecessor(CommitOrderDependencyNode n) {
177         predecessor = n;
178     }
179
180     public String JavaDoc toString() {
181         if (descriptor == null) {
182             return ToStringLocalization.buildMessage("empty_commit_order_dependency_node", (Object JavaDoc[])null);
183         } else {
184             Object JavaDoc[] args = { descriptor };
185             return ToStringLocalization.buildMessage("node", args);
186         }
187     }
188
189     public void visit() {
190         //Visit this node as part of a topological sort
191
int startTime;
192         markInProgress();
193         startTime = getOwner().getNextTime();
194         setDiscoveryTime(startTime);
195
196         for (Enumeration e = getRelatedNodes().elements(); e.hasMoreElements();) {
197             CommitOrderDependencyNode node = (CommitOrderDependencyNode)e.nextElement();
198             if (node.hasNotBeenVisited()) {
199                 node.setPredecessor(this);
200                 node.visit();
201             }
202             if (node.getPredecessor() == null) {
203                 node.setPredecessor(this);
204             }
205         }
206
207         markVisited();
208         setFinishingTime(getOwner().getNextTime());
209     }
210
211     // Return an enumeration of all mappings for my descriptor, including those inherited
212
public Vector withAllSubclasses(CommitOrderDependencyNode node) {
213         Vector results = new Vector();
214         results.addElement(node);
215
216         if (node.getDescriptor().hasInheritance()) {
217             InheritancePolicy p = node.getDescriptor().getInheritancePolicy();
218
219             // For bug 3019934 replace getChildDescriptors with getAllChildDescriptors.
220
for (Enumeration e = p.getAllChildDescriptors().elements(); e.hasMoreElements();) {
221                 results.addElement(getOwner().nodeFor((ClassDescriptor)e.nextElement()));
222             }
223         }
224         return results;
225     }
226 }
227
Popular Tags