]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db/src/org/simantics/db/exception/DatabaseException.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db / src / org / simantics / db / exception / DatabaseException.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.exception;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19
20 /**
21 *
22 * A base class for exceptions in org.simantics.db.
23
24 * There are three classes of DatabaseException
25 * <ul>
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
30 * </ul>
31
32 * The resources related to an exception are available for pretty printing uses. 
33
34 * @author Antti Villberg
35 * @version 0.7
36 * @see ValidationException
37 * @see ServiceException
38 * @see AssumptionException
39 * @see VariableException
40
41 */
42 public class DatabaseException extends Exception {
43
44     private static final long serialVersionUID = -6234485044648476711L;
45
46     private ArrayList<Resource> resources;
47     private ArrayList<String> names;
48     private ArrayList<Integer> indices;
49     
50     public DatabaseException() {
51         super();
52     }
53
54     public DatabaseException(String message, Throwable cause) {
55         super(message, cause);
56     }
57
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); 
62     }
63
64     public DatabaseException(Throwable cause, Resource ... rs) {
65         super(cause);
66         resources = new ArrayList<Resource>();
67         for(Resource r : rs) resources.add(r); 
68     }
69
70     public DatabaseException(String message, Resource ... rs) {
71         super(message);
72         resources = new ArrayList<Resource>();
73         for(Resource r : rs) resources.add(r); 
74     }
75
76     public DatabaseException(String message) {
77         super(message);
78     }
79
80     public DatabaseException(String message, int ... resources) {
81         super(message);
82         for(int r : resources) addIndex(r);
83     }
84
85     public DatabaseException(int ... resources) {
86         super();
87         for(int r : resources) addIndex(r);
88     }
89
90     public DatabaseException(Throwable cause) {
91         super(cause);
92     }
93     
94     public void addResource(Resource resource) {
95
96         assert(resource != null);
97         
98         if(resources == null) resources = new ArrayList<Resource>();
99         
100         resources.add(resource);
101         
102     }
103     
104     public void addIndex(int index) {
105
106         assert(index != 0);
107         
108         if(indices == null) indices = new ArrayList<Integer>();
109         
110         indices.add(index);
111         
112     }
113
114     public int getIndex(int index) {
115         return indices.get(index);
116     }
117
118     public Collection<Resource> getResources() {
119         return resources;
120     }
121     
122     public void setNames(ArrayList<String> names) {
123         this.names  = names;
124     }
125     
126     /**
127      * Throws the exception caused by the invocation of this request. The
128      * parameter allows checked exceptions to be thrown.
129      * 
130      * @param <T>
131      * @param clazz
132      * @throws T
133      */
134     @SuppressWarnings("unchecked")
135     public <T extends Throwable> void throwCause(Class<T> clazz) throws T {
136         Throwable t = getCause();
137         if (t == null)
138             return;
139         if (clazz.isInstance(t))
140             throw (T) t;
141         if (t instanceof RuntimeException)
142             throw (RuntimeException) t;
143         if (t instanceof Error)
144             throw (Error) t;
145         throw new RuntimeException("See cause for the real exception.", t);
146     }
147
148     public DatabaseException newStack() {
149         return new DatabaseException(this);
150     }
151     
152     @Override
153     public String toString() {
154         if(names != null) System.out.println(names);
155         return super.toString();
156     }
157     
158     public String getShortExplanation() {
159         return getMessage();
160     }
161     
162     public String getExplanation(ReadGraph graph) throws DatabaseException {
163         return getShortExplanation();
164     }
165     
166 }