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)
29 * <li>VariableException is a class of exceptions related to Simantics Variable-interface operations
32 * The resources related to an exception are available for pretty printing uses.
34 * @author Antti Villberg
36 * @see ValidationException
37 * @see ServiceException
38 * @see AssumptionException
39 * @see VariableException
42 public class DatabaseException extends Exception {
44 private static final long serialVersionUID = -6234485044648476711L;
46 private ArrayList<Resource> resources;
47 private ArrayList<String> names;
48 private ArrayList<Integer> indices;
50 public DatabaseException() {
54 public DatabaseException(String message, Throwable cause) {
55 super(message, cause);
58 public DatabaseException(String message, Throwable cause, Resource ... rs) {
59 super(message, cause);
60 resources = new ArrayList<Resource>();
61 for(Resource r : rs) resources.add(r);
64 public DatabaseException(Throwable cause, Resource ... rs) {
66 resources = new ArrayList<Resource>();
67 for(Resource r : rs) resources.add(r);
70 public DatabaseException(String message, Resource ... rs) {
72 resources = new ArrayList<Resource>();
73 for(Resource r : rs) resources.add(r);
76 public DatabaseException(String message) {
80 public DatabaseException(String message, int ... resources) {
82 for(int r : resources) addIndex(r);
85 public DatabaseException(int ... resources) {
87 for(int r : resources) addIndex(r);
90 public DatabaseException(Throwable cause) {
94 public void addResource(Resource resource) {
96 assert(resource != null);
98 if(resources == null) resources = new ArrayList<Resource>();
100 resources.add(resource);
104 public void addIndex(int index) {
108 if(indices == null) indices = new ArrayList<Integer>();
114 public int getIndex(int index) {
115 return indices.get(index);
118 public Collection<Resource> getResources() {
122 public void setNames(ArrayList<String> names) {
127 * Throws the exception caused by the invocation of this request. The
128 * parameter allows checked exceptions to be thrown.
134 @SuppressWarnings("unchecked")
135 public <T extends Throwable> void throwCause(Class<T> clazz) throws T {
136 Throwable t = getCause();
139 if (clazz.isInstance(t))
141 if (t instanceof RuntimeException)
142 throw (RuntimeException) t;
143 if (t instanceof Error)
145 throw new RuntimeException("See cause for the real exception.", t);
148 public DatabaseException newStack() {
149 return new DatabaseException(this);
153 public String toString() {
154 if(names != null) System.out.println(names);
155 return super.toString();
158 public String getShortExplanation() {
162 public String getExplanation(ReadGraph graph) throws DatabaseException {
163 return getShortExplanation();