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