001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.common;
012:
013: import com.versant.core.jdo.*;
014: import java.util.NoSuchElementException;
015: import java.lang.reflect.InvocationTargetException;
016: import javax.jdo.*;
017:
018: /**
019: The purpose of this class is to encapsulate the Exception throwing.<br/>
020: This is necessary to allow different exception classes for JDO, EJB and other api's <br/>
021: Make sure, that all exceptions are thrown via this facility!
022: (Exceptions include classes, that are JDO-only, like the JDO enhancer.)<br/><br/>
023: This class is intended to be stateless.<br/><br/>
024: With JDO an instance of BindingSupportImpl is directly used.
025:
026: Replacement pattern (incomplete, if not found in list: look here in source code):
027: JDOUserException invalidOperation
028: JDOFatalUserException runtime
029: JDOFatalInternalException internal
030: JDOUnsupportedOptionException unsupported
031: JDOException exception
032: JDOFatalException fatal
033: JDODataStore datastore
034: JDOFatalDataStore fatalDatastore
035:
036: IllegalArgumentException illegalArgument
037: NoSuchElementException noSuchElement
038: IndexOutOfBoundsException indexOutOfBounds
039: UnsupportedOperation unsupportedOperation
040:
041: VersantObjectNotFoundException objectNotFound
042: VersantConcurrentUpdateException concurrentUpdate
043: VersantDetachedFieldAccessException fieldDetached
044: NotImplementedException notImplemented
045: VersantAuthenticationException authentication
046:
047: List of exceptions from the exceptions:
048: (this list should include exceptions, where BindingSupportImpl
049: is not necessary, because they are never user-visible, but
050: always caught internally)
051: TargetLostException (?)
052: ...
053:
054: List of packages, where using BindingSupportImpl is not required:
055: com.versant.core.jdo.tools.enhancer
056: com.versant.core.jdo.jca
057: com.versant.core.jdo.junit
058: com.versant.core.jdo.licensegenerator
059: com.versant.core.jdo.mbean
060: com.versant.core.jdo.tools.workbench
061:
062: @author Christian Romberg
063: */
064:
065: public class BindingSupportImpl {
066:
067: private static BindingSupportImpl theInstance = new BindingSupportImpl();
068:
069: public static BindingSupportImpl getInstance() {
070: return theInstance;
071: }
072:
073: private BindingSupportImpl() {
074: }
075:
076: public RuntimeException noSuchElement(String msg) {
077: return new NoSuchElementException(msg);
078: }
079:
080: public RuntimeException illegalArgument(String msg) {
081: return new IllegalArgumentException(msg);
082: }
083:
084: public RuntimeException security(String msg) {
085: return new SecurityException(msg);
086: }
087:
088: public RuntimeException runtime(String msg) {
089: return new JDOFatalUserException(msg);
090: }
091:
092: public RuntimeException license(String msg, Throwable t) {
093: return runtime(msg);
094: }
095:
096: /**
097: * Recursively extract the real cause of e if it is wrapping other
098: * exceptions.
099: */
100: public Throwable findCause(Throwable e) {
101: if (e instanceof InvocationTargetException) {
102: Throwable te = ((InvocationTargetException) e)
103: .getTargetException();
104: if (te != null)
105: return findCause(te);
106: }
107: return e;
108: }
109:
110: public RuntimeException runtime(String msg, Throwable e) {
111: return new JDOFatalUserException(msg, findCause(e));
112: }
113:
114: public RuntimeException unsupported(String m) {
115: return new JDOUnsupportedOptionException(m);
116: }
117:
118: public RuntimeException unsupported() {
119: return new JDOUnsupportedOptionException();
120: }
121:
122: public RuntimeException unsupportedOperation(String msg) {
123: return new UnsupportedOperationException(msg);
124: }
125:
126: public RuntimeException indexOutOfBounds(String msg) {
127: return new IndexOutOfBoundsException(msg);
128: }
129:
130: public RuntimeException arrayIndexOutOfBounds(String msg) {
131: return new ArrayIndexOutOfBoundsException(msg);
132: }
133:
134: public RuntimeException arrayIndexOutOfBounds(int val) {
135: return new ArrayIndexOutOfBoundsException(val);
136: }
137:
138: public RuntimeException internal(String msg, Throwable cause) {
139: return new JDOFatalInternalException(msg, findCause(cause));
140: }
141:
142: public RuntimeException internal(String msg, Throwable[] cause) {
143: return new JDOFatalInternalException(msg, cause);
144: }
145:
146: public RuntimeException internal(String msg) {
147: return new JDOFatalInternalException(msg);
148: }
149:
150: public RuntimeException objectNotFound(String msg) {
151: return new JDOObjectNotFoundException(msg);
152: }
153:
154: public RuntimeException exception(String msg) {
155: return new JDOException(msg);
156: }
157:
158: public RuntimeException exception(String msg, Throwable nested) {
159: return new JDOException(msg, findCause(nested));
160: }
161:
162: public RuntimeException exception(String msg, Throwable[] nested) {
163: return new JDOException(msg, nested);
164: }
165:
166: public RuntimeException exception(String msg, Throwable nested,
167: Object failed) {
168: return new JDOException(msg, findCause(nested), failed);
169: }
170:
171: public RuntimeException duplicateKey(String msg, Throwable nested,
172: Object failed) {
173: return new VersantDuplicateKeyException(msg, findCause(nested));
174: }
175:
176: public RuntimeException lockNotGranted(String msg,
177: Throwable[] nested, Object failed) {
178: if (nested == null)
179: return new VersantLockTimeoutException(msg);
180: return new VersantLockTimeoutException(msg, nested);
181: }
182:
183: public RuntimeException fatal(String msg) {
184: return new JDOFatalException(msg);
185: }
186:
187: public RuntimeException fatal(String msg, Throwable t) {
188: return new JDOFatalException(msg, findCause(t));
189: }
190:
191: public RuntimeException concurrentUpdate(String msg, Object failed) {
192: return new JDOOptimisticVerificationException(msg);
193: }
194:
195: public RuntimeException optimisticVerification(String msg,
196: Object failed) {
197: return new JDOOptimisticVerificationException(msg);
198: }
199:
200: public RuntimeException fieldDetached() {
201: return new VersantDetachedFieldAccessException();
202: }
203:
204: public RuntimeException datastore(String msg, Throwable t) {
205: return new JDODataStoreException(msg, findCause(t));
206: }
207:
208: public RuntimeException datastore(String msg) {
209: return new JDODataStoreException(msg);
210: }
211:
212: public RuntimeException fatalDatastore(String msg, Throwable t) {
213: return new JDOFatalDataStoreException(msg, findCause(t));
214: }
215:
216: public RuntimeException fatalDatastore(String msg) {
217: return new JDOFatalDataStoreException(msg);
218: }
219:
220: public RuntimeException poolFull(String msg) {
221: return new VersantConnectionPoolFullException(msg);
222: }
223:
224: public RuntimeException nullElement(String msg) {
225: return new VersantNullElementException(msg);
226: }
227:
228: public RuntimeException invalidObjectId(String msg, Throwable cause) {
229: return new JDOUserException(msg, findCause(cause)); //no specific exception class yet for JDO Genie
230: }
231:
232: public RuntimeException query(String msg, Throwable cause) {
233: return new JDOUserException(msg, findCause(cause)); //no specific exception class yet for JDO Genie
234: }
235:
236: public RuntimeException query(String msg) {
237: return new JDOUserException(msg, (Throwable) null); //no specific exception class yet for JDO Genie
238: }
239:
240: public boolean isOwnQueryException(Throwable e) {
241: return e instanceof JDOUserException; //no specific exception class yet for JDO Genie
242: }
243:
244: public boolean isOwnInvalidObjectIdException(Throwable e) {
245: return e instanceof JDOUserException; //no specific exception class yet for JDO Genie
246: }
247:
248: public boolean isOwnException(Throwable e) {
249: return e instanceof JDOException;
250: }
251:
252: public boolean isOwnFatalUserException(Throwable e) {
253: return e instanceof JDOFatalUserException;
254: }
255:
256: public boolean isOwnFatalException(Throwable e) {
257: return e instanceof JDOFatalException;
258: }
259:
260: public boolean isOwnInternalException(Throwable t) {
261: return t instanceof JDOFatalInternalException;
262: }
263:
264: public boolean isOwnDatastoreException(Throwable t) {
265: return t instanceof JDODataStoreException;
266: }
267:
268: public boolean isOwnFatalDatastoreException(Throwable t) {
269: return t instanceof JDOFatalDataStoreException;
270: }
271:
272: public boolean isOwnConcurrentUpdateException(Throwable t) {
273: return t instanceof JDOOptimisticVerificationException;
274: }
275:
276: public boolean isOwnObjectNotFoundException(Throwable t) {
277: return t instanceof JDOObjectNotFoundException;
278: }
279:
280: public boolean isOwnUnsupportedException(Throwable t) {
281: return t instanceof JDOUnsupportedOptionException;
282: }
283:
284: public boolean isOutOfMemoryError(Throwable t) {
285: return t instanceof OutOfMemoryError;
286: }
287:
288: public boolean isError(Throwable t) {
289: return t instanceof Error;
290: }
291:
292: public Object getFailedObject(Exception e) {
293: return ((JDOException) e).getFailedObject();
294: }
295:
296: public RuntimeException invalidOperation(String msg, Throwable cause) {
297: return new JDOUserException(msg, findCause(cause));
298: }
299:
300: //these are currently defined in BindingSupportIfc:
301: //implementation copied from JDOBindingSupport
302:
303: public RuntimeException objectNotEnhanced(Object o) {
304: String cl = (o == null ? "Class of passed object" : "Class "
305: + o.getClass().getName());
306: String msg = cl
307: + " is not declared persistent or not enhanced.";
308: return new JDOFatalUserException(msg);
309: }
310:
311: public RuntimeException transactionConflict(Object obj) {
312: return transactionConflict(
313: "Object conflicts with other PersistenceManager", obj);
314: }
315:
316: public RuntimeException transactionConflict(String msg, Object obj) {
317: // TransactionConflictException
318: //return new TransactionConflictException(msg,obj); //todo
319: return new JDOUserException(msg, obj);
320: }
321:
322: public RuntimeException notImplemented(String m) {
323: return new NotImplementedException(m);
324: }
325:
326: public RuntimeException invalidOperation(String msg) {
327: return new JDOUserException(msg);
328: }
329:
330: }
|