X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fexception%2FDatabaseException.java;fp=bundles%2Forg.simantics.db%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fexception%2FDatabaseException.java;h=4fc0a4e136dc09635710d2fbc0286e5d55e2cc4c;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git 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 index 000000000..4fc0a4e13 --- /dev/null +++ b/bundles/org.simantics.db/src/org/simantics/db/exception/DatabaseException.java @@ -0,0 +1,164 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.db.exception; + +import java.util.ArrayList; +import java.util.Collection; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; + +/** + * + * A base class for exceptions in org.simantics.db. + * + * There are three classes of DatabaseException + * + * + * The resources related to an exception are available for pretty printing uses. + * + * @author Antti Villberg + * @version 0.7 + * @see ValidationException + * @see ServiceException + * @see AssumptionException + * + */ +public class DatabaseException extends Exception { + + private static final long serialVersionUID = -6234485044648476711L; + + private ArrayList resources; + private ArrayList names; + private ArrayList indices; + + public DatabaseException() { + super(); + } + + public DatabaseException(String message, Throwable cause) { + super(message, cause); + } + + public DatabaseException(String message, Throwable cause, Resource ... rs) { + super(message, cause); + resources = new ArrayList(); + for(Resource r : rs) resources.add(r); + } + + public DatabaseException(Throwable cause, Resource ... rs) { + super(cause); + resources = new ArrayList(); + for(Resource r : rs) resources.add(r); + } + + public DatabaseException(String message, Resource ... rs) { + super(message); + resources = new ArrayList(); + for(Resource r : rs) resources.add(r); + } + + public DatabaseException(String message) { + super(message); + } + + public DatabaseException(String message, int ... resources) { + super(message); + for(int r : resources) addIndex(r); + } + + public DatabaseException(int ... resources) { + super(); + for(int r : resources) addIndex(r); + } + + public DatabaseException(Throwable cause) { + super(cause); + } + + public void addResource(Resource resource) { + + assert(resource != null); + + if(resources == null) resources = new ArrayList(); + + resources.add(resource); + + } + + public void addIndex(int index) { + + assert(index != 0); + + if(indices == null) indices = new ArrayList(); + + indices.add(index); + + } + + public int getIndex(int index) { + return indices.get(index); + } + + public Collection getResources() { + return resources; + } + + public void setNames(ArrayList names) { + this.names = names; + } + + /** + * Throws the exception caused by the invocation of this request. The + * parameter allows checked exceptions to be thrown. + * + * @param + * @param clazz + * @throws T + */ + @SuppressWarnings("unchecked") + public void throwCause(Class clazz) throws T { + Throwable t = getCause(); + if (t == null) + return; + if (clazz.isInstance(t)) + throw (T) t; + if (t instanceof RuntimeException) + throw (RuntimeException) t; + if (t instanceof Error) + throw (Error) t; + throw new RuntimeException("See cause for the real exception.", t); + } + + public DatabaseException newStack() { + return new DatabaseException(this); + } + + @Override + public String toString() { + if(names != null) System.out.println(names); + return super.toString(); + } + + public String getShortExplanation() { + return getMessage(); + } + + public String getExplanation(ReadGraph graph) throws DatabaseException { + return getShortExplanation(); + } + +}