1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.exception;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Collection;
\r
17 import org.simantics.db.ReadGraph;
\r
18 import org.simantics.db.Resource;
\r
22 * A base class for exceptions in org.simantics.db.
\r
24 * There are three classes of DatabaseException
\r
26 * <li>ServiceException is a class of serious failures e.g. (connection failure, database corruption)
\r
27 * <li>ValidationException is a class of exceptions due to invalid semantic graph
\r
28 * <li>AssumptionException is a class of exceptions due to unsatisfied user assumptions (e.g. user assumes a single result or existence of an adapter)
\r
31 * The resources related to an exception are available for pretty printing uses.
\r
33 * @author Antti Villberg
\r
35 * @see ValidationException
\r
36 * @see ServiceException
\r
37 * @see AssumptionException
\r
40 public class DatabaseException extends Exception {
\r
42 private static final long serialVersionUID = -6234485044648476711L;
\r
44 private ArrayList<Resource> resources;
\r
45 private ArrayList<String> names;
\r
46 private ArrayList<Integer> indices;
\r
48 public DatabaseException() {
\r
52 public DatabaseException(String message, Throwable cause) {
\r
53 super(message, cause);
\r
56 public DatabaseException(String message, Throwable cause, Resource ... rs) {
\r
57 super(message, cause);
\r
58 resources = new ArrayList<Resource>();
\r
59 for(Resource r : rs) resources.add(r);
\r
62 public DatabaseException(Throwable cause, Resource ... rs) {
\r
64 resources = new ArrayList<Resource>();
\r
65 for(Resource r : rs) resources.add(r);
\r
68 public DatabaseException(String message, Resource ... rs) {
\r
70 resources = new ArrayList<Resource>();
\r
71 for(Resource r : rs) resources.add(r);
\r
74 public DatabaseException(String message) {
\r
78 public DatabaseException(String message, int ... resources) {
\r
80 for(int r : resources) addIndex(r);
\r
83 public DatabaseException(int ... resources) {
\r
85 for(int r : resources) addIndex(r);
\r
88 public DatabaseException(Throwable cause) {
\r
92 public void addResource(Resource resource) {
\r
94 assert(resource != null);
\r
96 if(resources == null) resources = new ArrayList<Resource>();
\r
98 resources.add(resource);
\r
102 public void addIndex(int index) {
\r
104 assert(index != 0);
\r
106 if(indices == null) indices = new ArrayList<Integer>();
\r
108 indices.add(index);
\r
112 public int getIndex(int index) {
\r
113 return indices.get(index);
\r
116 public Collection<Resource> getResources() {
\r
120 public void setNames(ArrayList<String> names) {
\r
121 this.names = names;
\r
125 * Throws the exception caused by the invocation of this request. The
\r
126 * parameter allows checked exceptions to be thrown.
\r
132 @SuppressWarnings("unchecked")
\r
133 public <T extends Throwable> void throwCause(Class<T> clazz) throws T {
\r
134 Throwable t = getCause();
\r
137 if (clazz.isInstance(t))
\r
139 if (t instanceof RuntimeException)
\r
140 throw (RuntimeException) t;
\r
141 if (t instanceof Error)
\r
143 throw new RuntimeException("See cause for the real exception.", t);
\r
146 public DatabaseException newStack() {
\r
147 return new DatabaseException(this);
\r
151 public String toString() {
\r
152 if(names != null) System.out.println(names);
\r
153 return super.toString();
\r
156 public String getShortExplanation() {
\r
157 return getMessage();
\r
160 public String getExplanation(ReadGraph graph) throws DatabaseException {
\r
161 return getShortExplanation();
\r