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