KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > common > BindingSupportImpl


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.common;
13
14 import com.versant.core.jdo.*;
15 import java.util.NoSuchElementException JavaDoc;
16 import java.lang.reflect.InvocationTargetException JavaDoc;
17 import javax.jdo.*;
18
19
20
21 /**
22     The purpose of this class is to encapsulate the Exception throwing.<br/>
23     This is necessary to allow different exception classes for JDO, EJB and other api's <br/>
24     Make sure, that all exceptions are thrown via this facility!
25     (Exceptions include classes, that are JDO-only, like the JDO enhancer.)<br/><br/>
26     This class is intended to be stateless.<br/><br/>
27     With JDO an instance of BindingSupportImpl is directly used.
28     
29     Replacement pattern (incomplete, if not found in list: look here in source code):
30         JDOUserException invalidOperation
31         JDOFatalUserException runtime
32         JDOFatalInternalException internal
33         JDOUnsupportedOptionException unsupported
34         JDOException exception
35         JDOFatalException fatal
36         JDODataStore datastore
37         JDOFatalDataStore fatalDatastore
38         
39         IllegalArgumentException illegalArgument
40         NoSuchElementException noSuchElement
41         IndexOutOfBoundsException indexOutOfBounds
42         UnsupportedOperation unsupportedOperation
43         
44         VersantObjectNotFoundException objectNotFound
45         VersantConcurrentUpdateException concurrentUpdate
46         VersantDetachedFieldAccessException fieldDetached
47         NotImplementedException notImplemented
48         VersantAuthenticationException authentication
49         
50     List of exceptions from the exceptions:
51         (this list should include exceptions, where BindingSupportImpl
52          is not necessary, because they are never user-visible, but
53          always caught internally)
54         TargetLostException (?)
55         ...
56         
57     List of packages, where using BindingSupportImpl is not required:
58         com.versant.core.jdo.tools.enhancer
59         com.versant.core.jdo.jca
60         com.versant.core.jdo.junit
61         com.versant.core.jdo.licensegenerator
62         com.versant.core.jdo.mbean
63         com.versant.core.jdo.tools.workbench
64     
65     @author Christian Romberg
66 */

67
68 public class BindingSupportImpl
69 {
70     
71     
72
73     private static BindingSupportImpl theInstance = new BindingSupportImpl();
74     
75     public static BindingSupportImpl getInstance()
76     {
77         return theInstance;
78     }
79
80     
81     private BindingSupportImpl() {}
82     
83     public RuntimeException JavaDoc noSuchElement(String JavaDoc msg)
84     {
85         return new NoSuchElementException JavaDoc(msg);
86     }
87     
88     public RuntimeException JavaDoc illegalArgument(String JavaDoc msg)
89     {
90         return new IllegalArgumentException JavaDoc(msg);
91     }
92     
93     public RuntimeException JavaDoc security(String JavaDoc msg)
94     {
95         return new SecurityException JavaDoc(msg);
96     }
97     
98     public RuntimeException JavaDoc runtime(String JavaDoc msg)
99     {
100         return new JDOFatalUserException(msg);
101     }
102
103     public RuntimeException JavaDoc license(String JavaDoc msg, Throwable JavaDoc t)
104     {
105         return runtime(msg);
106     }
107
108     /**
109      * Recursively extract the real cause of e if it is wrapping other
110      * exceptions.
111      */

112     public Throwable JavaDoc findCause(Throwable JavaDoc e) {
113         if (e instanceof InvocationTargetException JavaDoc) {
114             Throwable JavaDoc te = ((InvocationTargetException JavaDoc)e).getTargetException();
115             if (te != null) return findCause(te);
116         }
117         return e;
118     }
119
120     public RuntimeException JavaDoc runtime(String JavaDoc msg, Throwable JavaDoc e)
121     {
122         return new JDOFatalUserException(msg, findCause(e));
123     }
124     
125     public RuntimeException JavaDoc unsupported(String JavaDoc m)
126     {
127         return new JDOUnsupportedOptionException(m);
128     }
129     
130     public RuntimeException JavaDoc unsupported()
131     {
132         return new JDOUnsupportedOptionException();
133     }
134     
135     public RuntimeException JavaDoc unsupportedOperation(String JavaDoc msg)
136     {
137         return new UnsupportedOperationException JavaDoc(msg);
138     }
139     
140     public RuntimeException JavaDoc indexOutOfBounds(String JavaDoc msg)
141     {
142         return new IndexOutOfBoundsException JavaDoc(msg);
143     }
144     
145     public RuntimeException JavaDoc arrayIndexOutOfBounds(String JavaDoc msg)
146     {
147         return new ArrayIndexOutOfBoundsException JavaDoc(msg);
148     }
149     
150     public RuntimeException JavaDoc arrayIndexOutOfBounds(int val)
151     {
152         return new ArrayIndexOutOfBoundsException JavaDoc(val);
153     }
154     
155     public RuntimeException JavaDoc internal(String JavaDoc msg, Throwable JavaDoc cause)
156     {
157         return new JDOFatalInternalException(msg, findCause(cause));
158     }
159     
160     public RuntimeException JavaDoc internal(String JavaDoc msg, Throwable JavaDoc[] cause)
161     {
162         return new JDOFatalInternalException(msg, cause);
163     }
164     
165     public RuntimeException JavaDoc internal(String JavaDoc msg)
166     {
167         return new JDOFatalInternalException(msg);
168     }
169     
170     public RuntimeException JavaDoc objectNotFound(String JavaDoc msg)
171     {
172         return new JDOObjectNotFoundException(msg);
173     }
174     
175     public RuntimeException JavaDoc exception(String JavaDoc msg)
176     {
177         return new JDOException(msg);
178     }
179
180     public RuntimeException JavaDoc exception(String JavaDoc msg, Throwable JavaDoc nested)
181     {
182         return new JDOException(msg, findCause(nested));
183     }
184         
185     public RuntimeException JavaDoc exception(String JavaDoc msg, Throwable JavaDoc[] nested)
186     {
187         return new JDOException(msg, nested);
188     }
189     
190     public RuntimeException JavaDoc exception(String JavaDoc msg, Throwable JavaDoc nested, Object JavaDoc failed)
191     {
192         return new JDOException(msg, findCause(nested), failed);
193     }
194     
195     public RuntimeException JavaDoc duplicateKey(String JavaDoc msg, Throwable JavaDoc nested,
196                                          Object JavaDoc failed)
197     {
198         return new VersantDuplicateKeyException(msg, findCause(nested));
199     }
200
201     public RuntimeException JavaDoc lockNotGranted(String JavaDoc msg, Throwable JavaDoc[] nested,
202                                     Object JavaDoc failed)
203     {
204         if (nested==null)
205             return new VersantLockTimeoutException(msg);
206         return new VersantLockTimeoutException(msg, nested);
207     }
208
209     public RuntimeException JavaDoc fatal(String JavaDoc msg)
210     {
211         return new JDOFatalException(msg);
212     }
213     
214     public RuntimeException JavaDoc fatal(String JavaDoc msg, Throwable JavaDoc t)
215     {
216         return new JDOFatalException(msg, findCause(t));
217     }
218     
219     public RuntimeException JavaDoc concurrentUpdate(String JavaDoc msg, Object JavaDoc failed)
220     {
221         return new JDOOptimisticVerificationException(msg);
222     }
223     
224     public RuntimeException JavaDoc optimisticVerification(String JavaDoc msg, Object JavaDoc failed)
225     {
226         return new JDOOptimisticVerificationException(msg);
227     }
228     
229     public RuntimeException JavaDoc fieldDetached()
230     {
231         return new VersantDetachedFieldAccessException();
232     }
233     
234     public RuntimeException JavaDoc datastore(String JavaDoc msg, Throwable JavaDoc t)
235     {
236         return new JDODataStoreException(msg, findCause(t));
237     }
238     
239     public RuntimeException JavaDoc datastore(String JavaDoc msg)
240     {
241         return new JDODataStoreException(msg);
242     }
243     
244     public RuntimeException JavaDoc fatalDatastore(String JavaDoc msg, Throwable JavaDoc t)
245     {
246         return new JDOFatalDataStoreException(msg, findCause(t));
247     }
248     
249     public RuntimeException JavaDoc fatalDatastore(String JavaDoc msg)
250     {
251         return new JDOFatalDataStoreException(msg);
252     }
253     
254     public RuntimeException JavaDoc poolFull(String JavaDoc msg)
255     {
256         return new VersantConnectionPoolFullException(msg);
257     }
258     
259     public RuntimeException JavaDoc nullElement(String JavaDoc msg)
260     {
261         return new VersantNullElementException(msg);
262     }
263     
264     public RuntimeException JavaDoc invalidObjectId(String JavaDoc msg, Throwable JavaDoc cause)
265     {
266         return new JDOUserException(msg, findCause(cause)); //no specific exception class yet for JDO Genie
267
}
268     
269     public RuntimeException JavaDoc query(String JavaDoc msg, Throwable JavaDoc cause)
270     {
271         return new JDOUserException(msg, findCause(cause)); //no specific exception class yet for JDO Genie
272
}
273     
274     public RuntimeException JavaDoc query(String JavaDoc msg)
275     {
276         return new JDOUserException(msg, (Throwable JavaDoc)null); //no specific exception class yet for JDO Genie
277
}
278     
279     public boolean isOwnQueryException(Throwable JavaDoc e)
280     {
281         return e instanceof JDOUserException; //no specific exception class yet for JDO Genie
282
}
283     
284     public boolean isOwnInvalidObjectIdException(Throwable JavaDoc e)
285     {
286         return e instanceof JDOUserException; //no specific exception class yet for JDO Genie
287
}
288     
289     public boolean isOwnException(Throwable JavaDoc e)
290     {
291         return e instanceof JDOException;
292     }
293     
294     public boolean isOwnFatalUserException(Throwable JavaDoc e)
295     {
296         return e instanceof JDOFatalUserException;
297     }
298     
299     public boolean isOwnFatalException(Throwable JavaDoc e)
300     {
301         return e instanceof JDOFatalException;
302     }
303     
304     public boolean isOwnInternalException(Throwable JavaDoc t)
305     {
306         return t instanceof JDOFatalInternalException;
307     }
308     
309     public boolean isOwnDatastoreException(Throwable JavaDoc t)
310     {
311         return t instanceof JDODataStoreException;
312     }
313     
314     public boolean isOwnFatalDatastoreException(Throwable JavaDoc t)
315     {
316         return t instanceof JDOFatalDataStoreException;
317     }
318     
319     public boolean isOwnConcurrentUpdateException(Throwable JavaDoc t)
320     {
321         return t instanceof JDOOptimisticVerificationException;
322     }
323     
324     public boolean isOwnObjectNotFoundException(Throwable JavaDoc t)
325     {
326         return t instanceof JDOObjectNotFoundException;
327     }
328     
329     public boolean isOwnUnsupportedException(Throwable JavaDoc t)
330     {
331         return t instanceof JDOUnsupportedOptionException;
332     }
333
334     public boolean isOutOfMemoryError(Throwable JavaDoc t) {
335         return t instanceof OutOfMemoryError JavaDoc;
336     }
337
338     public boolean isError(Throwable JavaDoc t) {
339         return t instanceof Error JavaDoc;
340     }
341
342     public Object JavaDoc getFailedObject(Exception JavaDoc e)
343     {
344         return ((JDOException)e).getFailedObject();
345     }
346     
347     public RuntimeException JavaDoc invalidOperation(String JavaDoc msg, Throwable JavaDoc cause)
348     {
349         return new JDOUserException(msg, findCause(cause));
350     }
351     //these are currently defined in BindingSupportIfc:
352
//implementation copied from JDOBindingSupport
353

354     public RuntimeException JavaDoc objectNotEnhanced(Object JavaDoc o)
355     {
356         String JavaDoc cl = (o == null ? "Class of passed object"
357                                 : "Class "+o.getClass().getName());
358         String JavaDoc msg = cl+" is not declared persistent or not enhanced.";
359         return new JDOFatalUserException(msg);
360     }
361
362     public RuntimeException JavaDoc transactionConflict(Object JavaDoc obj)
363     {
364         return transactionConflict("Object conflicts with other PersistenceManager", obj);
365     }
366
367     public RuntimeException JavaDoc transactionConflict(String JavaDoc msg, Object JavaDoc obj)
368     {
369         // TransactionConflictException
370
//return new TransactionConflictException(msg,obj); //todo
371
return new JDOUserException(msg, obj);
372     }
373
374     public RuntimeException JavaDoc notImplemented(String JavaDoc m)
375     {
376         return new NotImplementedException(m);
377     }
378
379     public RuntimeException JavaDoc invalidOperation(String JavaDoc msg)
380     {
381         return new JDOUserException(msg);
382     }
383
384 }
385
Popular Tags