]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.db/src/org/simantics/db/exception/DatabaseException.java b/bundles/org.simantics.db/src/org/simantics/db/exception/DatabaseException.java
new file mode 100644 (file)
index 0000000..4fc0a4e
--- /dev/null
@@ -0,0 +1,164 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.exception;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+\r
+/**\r
+ *\r
+ * A base class for exceptions in org.simantics.db.\r
+ * \r
+ * There are three classes of DatabaseException\r
+ * <ul>\r
+ * <li>ServiceException is a class of serious failures e.g. (connection failure, database corruption) \r
+ * <li>ValidationException is a class of exceptions due to invalid semantic graph \r
+ * <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
+ * </ul>\r
+ * \r
+ * The resources related to an exception are available for pretty printing uses. \r
+ * \r
+ * @author Antti Villberg\r
+ * @version 0.7\r
+ * @see ValidationException\r
+ * @see ServiceException\r
+ * @see AssumptionException\r
+ * \r
+ */\r
+public class DatabaseException extends Exception {\r
+\r
+    private static final long serialVersionUID = -6234485044648476711L;\r
+\r
+    private ArrayList<Resource> resources;\r
+    private ArrayList<String> names;\r
+    private ArrayList<Integer> indices;\r
+    \r
+    public DatabaseException() {\r
+        super();\r
+    }\r
+\r
+    public DatabaseException(String message, Throwable cause) {\r
+        super(message, cause);\r
+    }\r
+\r
+    public DatabaseException(String message, Throwable cause, Resource ... rs) {\r
+        super(message, cause);\r
+        resources = new ArrayList<Resource>();\r
+        for(Resource r : rs) resources.add(r); \r
+    }\r
+\r
+    public DatabaseException(Throwable cause, Resource ... rs) {\r
+        super(cause);\r
+        resources = new ArrayList<Resource>();\r
+        for(Resource r : rs) resources.add(r); \r
+    }\r
+\r
+    public DatabaseException(String message, Resource ... rs) {\r
+        super(message);\r
+        resources = new ArrayList<Resource>();\r
+        for(Resource r : rs) resources.add(r); \r
+    }\r
+\r
+    public DatabaseException(String message) {\r
+        super(message);\r
+    }\r
+\r
+    public DatabaseException(String message, int ... resources) {\r
+        super(message);\r
+        for(int r : resources) addIndex(r);\r
+    }\r
+\r
+    public DatabaseException(int ... resources) {\r
+        super();\r
+        for(int r : resources) addIndex(r);\r
+    }\r
+\r
+    public DatabaseException(Throwable cause) {\r
+        super(cause);\r
+    }\r
+    \r
+    public void addResource(Resource resource) {\r
+\r
+        assert(resource != null);\r
+        \r
+        if(resources == null) resources = new ArrayList<Resource>();\r
+        \r
+        resources.add(resource);\r
+        \r
+    }\r
+    \r
+    public void addIndex(int index) {\r
+\r
+        assert(index != 0);\r
+        \r
+        if(indices == null) indices = new ArrayList<Integer>();\r
+        \r
+        indices.add(index);\r
+        \r
+    }\r
+\r
+    public int getIndex(int index) {\r
+        return indices.get(index);\r
+    }\r
+\r
+    public Collection<Resource> getResources() {\r
+        return resources;\r
+    }\r
+    \r
+    public void setNames(ArrayList<String> names) {\r
+        this.names  = names;\r
+    }\r
+    \r
+    /**\r
+     * Throws the exception caused by the invocation of this request. The\r
+     * parameter allows checked exceptions to be thrown.\r
+     * \r
+     * @param <T>\r
+     * @param clazz\r
+     * @throws T\r
+     */\r
+    @SuppressWarnings("unchecked")\r
+    public <T extends Throwable> void throwCause(Class<T> clazz) throws T {\r
+        Throwable t = getCause();\r
+        if (t == null)\r
+            return;\r
+        if (clazz.isInstance(t))\r
+            throw (T) t;\r
+        if (t instanceof RuntimeException)\r
+            throw (RuntimeException) t;\r
+        if (t instanceof Error)\r
+            throw (Error) t;\r
+        throw new RuntimeException("See cause for the real exception.", t);\r
+    }\r
+\r
+    public DatabaseException newStack() {\r
+       return new DatabaseException(this);\r
+    }\r
+    \r
+    @Override\r
+    public String toString() {\r
+        if(names != null) System.out.println(names);\r
+        return super.toString();\r
+    }\r
+    \r
+    public String getShortExplanation() {\r
+       return getMessage();\r
+    }\r
+    \r
+    public String getExplanation(ReadGraph graph) throws DatabaseException {\r
+       return getShortExplanation();\r
+    }\r
+    \r
+}\r