KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > exceptions > IntegrityChecker


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.exceptions;
23
24 import java.util.*;
25 import java.io.*;
26 import oracle.toplink.essentials.internal.helper.*;
27 import oracle.toplink.essentials.internal.sessions.AbstractRecord;
28 import oracle.toplink.essentials.internal.sessions.AbstractSession;
29
30 /**
31  * <p><b>Purpose</b>: IntegrityChecker is used for catching all the descriptor exceptions,
32  * and checking database tables. It gives the user options if he/she wants to
33  * catch descriptor exceptions, check database, and check InstantiationPolicy or not.
34  */

35 public class IntegrityChecker implements Serializable {
36
37     /** To add all the Descriptor exceptions */
38     protected Vector caughtExceptions = null;
39
40     /** To load the tables from database */
41     protected Vector tables = null;
42
43     /** To know that should we catch all the descriptors exceptions or not */
44     protected boolean shouldCatchExceptions;
45
46     /** To know that should we check database tables or not */
47     protected boolean shouldCheckDatabase;
48
49     /** To know that should we check InstantiationPolicy or not */
50     protected boolean shouldCheckInstantiationPolicy;
51
52     /**
53      * PUBLIC:
54      * IntegrityChecker is used for catching all the Descriptor Exceptions,
55      * and check database tables. IntegrityChecker gives the option to the user that does he wants to
56      * catch all the descriptor exceptions,check database, and check InstantiationPolicy or not.
57      */

58     public IntegrityChecker() {
59         super();
60         this.shouldCatchExceptions = true;
61         this.shouldCheckDatabase = false;
62         this.shouldCheckInstantiationPolicy = true;
63     }
64
65     /**
66      * PUBLIC:
67      * This method is used for catching all the Descriptor Exceptions
68      */

69     public void catchExceptions() {
70         setShouldCatchExceptions(true);
71     }
72
73     /**
74      * PUBLIC:
75      * This method is used to check the database tables.
76      */

77     public void checkDatabase() {
78         setShouldCheckDatabase(true);
79     }
80
81     /**
82      * PUBLIC:
83      * This method is used to check the InstantiationPolicy.
84      */

85     public void checkInstantiationPolicy() {
86         setShouldCheckInstantiationPolicy(true);
87     }
88
89     /**
90      * INTERNAL:
91      * This method checks that tables are present in the database.
92      */

93     public boolean checkTable(DatabaseTable table, AbstractSession session) {
94         if (getTables().size() == 0) {
95             // load the tables from the session
96
initializeTables(session);
97         }
98         //MySQL converts all the table names to lower case.
99
if (session.getPlatform().isMySQL()) {
100             return getTables().contains(table.getName().toLowerCase());
101         }
102         return getTables().contains(table.getName());
103     }
104
105     /**
106      * PUBLIC:
107      * This method is used for don't catching all the Descriptor Exceptions
108      */

109     public void dontCatchExceptions() {
110         setShouldCatchExceptions(false);
111     }
112
113     /**
114      * PUBLIC:
115      * This method is used for don't checking the database tables and fields.
116      */

117     public void dontCheckDatabase() {
118         setShouldCheckDatabase(false);
119     }
120
121     /**
122      * PUBLIC:
123      * This method is used for don't checking the InstantiationPolicy.
124      */

125     public void dontCheckInstantiationPolicy() {
126         setShouldCheckInstantiationPolicy(false);
127     }
128
129     /**
130      * PUBLIC:
131      * This method returns the vecotr which adds all the Descriptors Exceptions.
132      */

133     public Vector getCaughtExceptions() {
134         if (caughtExceptions == null) {
135             caughtExceptions = new Vector();
136         }
137         return caughtExceptions;
138     }
139
140     /**
141      * INTERNAL:
142      * This method returns a vector which holds all the tables of database
143      */

144     public Vector getTables() {
145         if (tables == null) {
146             tables = new Vector();
147         }
148         return tables;
149     }
150
151     /**
152      * INTERNAL:
153      * This method handle all the Descriptor Exceptions.
154      * This method throw the exception or add the exceptions into a vector depand on the value of shouldCatchExceptions.
155      */

156     public void handleError(RuntimeException JavaDoc runtimeException) {
157         if (!shouldCatchExceptions()) {
158             throw runtimeException;
159         }
160         getCaughtExceptions().addElement(runtimeException);
161     }
162
163     /**
164      * INTERNAL:
165      * Return if any errors occured.
166      */

167     public boolean hasErrors() {
168         if ((caughtExceptions != null) && (caughtExceptions.size() > 0)) {
169             return true;
170         }
171         return false;
172     }
173
174     /**
175      * INTERNAL:
176      * Return if any runtime errors occured.
177      */

178     public boolean hasRuntimeExceptions() {
179         if (hasErrors()) {
180             for (Enumeration exceptionsEnum = getCaughtExceptions().elements();
181                      exceptionsEnum.hasMoreElements();) {
182                 if (exceptionsEnum.nextElement() instanceof RuntimeException JavaDoc) {
183                     return true;
184                 }
185             }
186         }
187         return false;
188     }
189
190     /**
191      * INTERNAL:
192      * This method is used to get all the database tables and add them into a vector.
193      */

194     public void initializeTables(AbstractSession session) {
195         Vector result = session.getAccessor().getTableInfo(null, null, null, null, session);
196         for (Enumeration resultEnum = result.elements(); resultEnum.hasMoreElements();) {
197             AbstractRecord row = (AbstractRecord)resultEnum.nextElement();
198             tables.addElement(row.get("TABLE_NAME"));
199         }
200     }
201
202     /**
203      * INTERNAL:
204      */

205     public void setCaughtExceptions(Vector exceptions) {
206         this.caughtExceptions = exceptions;
207     }
208
209     /**
210      * PUBLIC:
211      * This method assigns the value to the variable (shouldCatchExceptions)
212      * that we should catch all Descriptor Exceptions or not.
213      */

214     public void setShouldCatchExceptions(boolean answer) {
215         shouldCatchExceptions = answer;
216     }
217
218     /**
219      * PUBLIC:
220      * This method assigns the value to the variable (shouldCheckDatabase)
221      * that we should check database or not.
222      */

223     public void setShouldCheckDatabase(boolean answer) {
224         shouldCheckDatabase = answer;
225     }
226
227     /**
228      * PUBLIC:
229      * This method assigns the value to the variable (shouldCheckInstantiationPolicy)
230      * that we should check InstantiationPolicy or not.
231      */

232     public void setShouldCheckInstantiationPolicy(boolean answer) {
233         shouldCheckInstantiationPolicy = answer;
234     }
235
236     /**
237      * PUBLIC:
238      * This method is used to know that all the Descriptor Exceptions should be thrown or not.
239      */

240     public boolean shouldCatchExceptions() {
241         return shouldCatchExceptions;
242     }
243
244     /**
245      * PUBLIC:
246      * This method is used to know that database tables and fields should be checked or not.
247      */

248     public boolean shouldCheckDatabase() {
249         return shouldCheckDatabase;
250     }
251
252     /**
253      * PUBLIC:
254      * This method tells us that we should check InstantiationPolicy or not.
255      */

256     public boolean shouldCheckInstantiationPolicy() {
257         return shouldCheckInstantiationPolicy;
258     }
259 }
260
Popular Tags