use of org.teiid.core.TeiidException in project teiid by teiid.
the class ExceptionUtil method sanitize.
/**
* Strip out the message and optionally the stacktrace
* @param t
* @return
*/
public static Throwable sanitize(Throwable t, boolean perserveStack) {
String code = null;
if (t instanceof TeiidException) {
code = ((TeiidException) t).getCode();
} else if (t instanceof TeiidRuntimeException) {
code = ((TeiidRuntimeException) t).getCode();
} else {
code = t.getClass().getName();
}
Throwable child = null;
if (t.getCause() != null && t.getCause() != t) {
child = sanitize(t.getCause(), perserveStack);
}
Class<?> clazz = t.getClass();
Throwable result = null;
while (clazz != null) {
if (clazz == Throwable.class || clazz == Exception.class) {
break;
}
Constructor<?> ctor = null;
try {
ctor = clazz.getDeclaredConstructor(new Class<?>[] { String.class });
result = (Throwable) ctor.newInstance(code);
break;
} catch (Exception e) {
}
clazz = clazz.getSuperclass();
}
if (result == null) {
result = new TeiidException(code);
}
if (result instanceof TeiidException) {
((TeiidException) result).setCode(code);
} else if (result instanceof TeiidRuntimeException) {
((TeiidException) result).setCode(code);
}
if (child != null) {
result.initCause(child);
}
if (perserveStack) {
result.setStackTrace(t.getStackTrace());
} else {
result.setStackTrace(new StackTraceElement[0]);
}
return result;
}
use of org.teiid.core.TeiidException in project teiid by teiid.
the class ReflectionHelper method create.
/**
* Helper method to create an instance of the class using the appropriate
* constructor based on the ctorObjs passed.
* @param className is the class to instantiate
* @param ctorObjs are the objects to pass to the constructor; optional, nullable
* @param classLoader the class loader to use; may be null if the current
* class loader is to be used
* @return Object is the instance of the class
* @throws TeiidException if an error occurs instantiating the class
*/
public static final Object create(String className, Collection<?> ctorObjs, final ClassLoader classLoader) throws TeiidException {
try {
int size = (ctorObjs == null ? 0 : ctorObjs.size());
Class[] names = new Class[size];
Object[] objArray = new Object[size];
int i = 0;
if (size > 0) {
for (Iterator<?> it = ctorObjs.iterator(); it.hasNext(); ) {
Object obj = it.next();
if (obj != null) {
names[i] = obj.getClass();
objArray[i] = obj;
}
i++;
}
}
return create(className, objArray, names, classLoader);
} catch (Exception e) {
throw new TeiidException(CorePlugin.Event.TEIID10033, e);
}
}
use of org.teiid.core.TeiidException in project teiid by teiid.
the class TestFileUtils method testDirectoryPermissions.
/**
* Test whether it's possible to read and write files in the specified directory.
* @param dirPath Name of the directory to test
* @throws TeiidException
* @since 4.3
*/
public static void testDirectoryPermissions(String dirPath) throws TeiidException {
// try to create a file
File tmpFile = new File(dirPath + File.separatorChar + TestFileUtils.TEMP_FILE);
boolean success = false;
try {
success = tmpFile.createNewFile();
} catch (IOException e) {
}
if (!success) {
// $NON-NLS-1$
throw new TeiidException("cannot create file in " + dirPath);
}
// test if file can be written to
if (!tmpFile.canWrite()) {
// $NON-NLS-1$
throw new TeiidException("cannot write " + dirPath);
}
// test if file can be read
if (!tmpFile.canRead()) {
// $NON-NLS-1$
throw new TeiidException("cannot read " + dirPath);
}
// test if file can be renamed
File newFile = new File(dirPath + File.separatorChar + TestFileUtils.TEMP_FILE_RENAMED);
success = false;
try {
success = tmpFile.renameTo(newFile);
} catch (Exception e) {
}
if (!success) {
// $NON-NLS-1$
throw new TeiidException("failed to rename " + dirPath);
}
// test if file can be deleted
success = false;
try {
success = newFile.delete();
} catch (Exception e) {
}
if (!success) {
// $NON-NLS-1$
final String msg = CorePlugin.Util.getString("FileUtils.Unable_to_delete_file_in", dirPath);
throw new TeiidException(msg);
}
}
use of org.teiid.core.TeiidException in project teiid by teiid.
the class TestSQLException method testCreateThrowable3.
@Test
public void testCreateThrowable3() {
TeiidSQLException e = testCreateThrowable(new TeiidException(new SocketTimeoutException(// $NON-NLS-1$
"A test MM Invalid Session Exception"), // $NON-NLS-1$
"Test MetaMatrixRuntimeException with a InvalidSessionException in it"), SQLStates.CONNECTION_EXCEPTION_STALE_CONNECTION);
// test to ensure that wrapping mmsqlexceptions works
// $NON-NLS-1$
TeiidSQLException e1 = TeiidSQLException.create(e, "new message");
// $NON-NLS-1$
assertEquals("new message", e1.getMessage());
testCreateThrowable(((TeiidSQLException) e1.getCause()).getCause(), SQLStates.CONNECTION_EXCEPTION_STALE_CONNECTION);
}
use of org.teiid.core.TeiidException in project teiid by teiid.
the class TestExceptionHolder method testDeserializationNotSerializable.
@Test
public void testDeserializationNotSerializable() throws Exception {
Exception ex = new TeiidException() {
NotSerializable ns = new NotSerializable();
};
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(new ExceptionHolder(ex));
oos.flush();
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
ExceptionHolder holder = (ExceptionHolder) ois.readObject();
Throwable e = holder.getException();
assertTrue(e instanceof TeiidException);
}
Aggregations