1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 package com.sun.star.comp.sdbc; 22 23 import org.apache.openoffice.comp.sdbc.dbtools.comphelper.ResourceBasedEventLogger; 24 import org.apache.openoffice.comp.sdbc.dbtools.util.StandardSQLState; 25 26 import com.sun.star.beans.NamedValue; 27 import com.sun.star.beans.PropertyValue; 28 import com.sun.star.lang.IllegalArgumentException; 29 import com.sun.star.logging.LogLevel; 30 import com.sun.star.sdbc.SQLException; 31 import com.sun.star.uno.Any; 32 import com.sun.star.uno.AnyConverter; 33 34 public class Tools { toUnoException(Object source, Throwable throwable)35 public static SQLException toUnoException(Object source, Throwable throwable) { 36 // FIXME: use SQLException.getNextException() instead of getCause()? 37 // There are up to 3 dimensions of exception chaining of warnings in Java, 38 // getCause(), getNextException(), and getNextWarning(). 39 // The C++ implementation used getNextException() only, 40 // but I am using the widely used and more helpful getCause(). 41 Throwable cause = throwable.getCause(); 42 Object unoCause = Any.VOID; 43 if (cause != null) { 44 unoCause = toUnoException(source, throwable); 45 } 46 if (throwable instanceof SQLException) { 47 return (SQLException)throwable; 48 } else if (throwable instanceof java.sql.SQLException) { 49 java.sql.SQLException sqlException = (java.sql.SQLException) throwable; 50 return new SQLException(sqlException.getMessage(), source, 51 sqlException.getSQLState(), sqlException.getErrorCode(), unoCause); 52 } else if (throwable instanceof com.sun.star.uno.Exception) { 53 // General UNO exception. Wrap in an SQLException and rethrow: 54 com.sun.star.uno.Exception exception = (com.sun.star.uno.Exception) throwable; 55 return new SQLException(exception.getMessage(), source, StandardSQLState.SQL_GENERAL_ERROR.text(), 56 0, exception); 57 } else { 58 // General Java exception. We can't pass this to UNO, so convert it to an UNO SQLException: 59 String message = throwable.getMessage(); 60 if (message.isEmpty()) { 61 message = throwable.getLocalizedMessage(); 62 } 63 if (message.isEmpty()) { 64 message = throwable.toString(); 65 } 66 return new SQLException(message, source, "", -1, unoCause); 67 } 68 } 69 toUnoExceptionLogged(Object source, ResourceBasedEventLogger logger, Throwable throwable)70 public static SQLException toUnoExceptionLogged(Object source, ResourceBasedEventLogger logger, Throwable throwable) { 71 72 SQLException exception = toUnoException(source, throwable); 73 logger.log(LogLevel.SEVERE, exception); 74 return exception; 75 } 76 getOrDefault(PropertyValue[] properties, String name, String defaultValue)77 public static String getOrDefault(PropertyValue[] properties, String name, String defaultValue) throws IllegalArgumentException { 78 String ret = defaultValue; 79 for (PropertyValue property : properties) { 80 if (property.Name.equals(name)) { 81 ret = AnyConverter.toString(property.Value); 82 break; 83 } 84 } 85 return ret; 86 } 87 getOrDefault(PropertyValue[] properties, String name, boolean defaultValue)88 public static boolean getOrDefault(PropertyValue[] properties, String name, boolean defaultValue) throws IllegalArgumentException { 89 boolean ret = defaultValue; 90 for (PropertyValue property : properties) { 91 if (property.Name.equals(name)) { 92 ret = AnyConverter.toBoolean(property.Value); 93 break; 94 } 95 } 96 return ret; 97 } 98 getOrDefault(PropertyValue[] properties, String name, Object defaultValue)99 public static Object getOrDefault(PropertyValue[] properties, String name, Object defaultValue) throws IllegalArgumentException { 100 Object ret = defaultValue; 101 for (PropertyValue property : properties) { 102 if (property.Name.equals(name)) { 103 ret = property.Value; 104 break; 105 } 106 } 107 return ret; 108 } 109 getOrDefault(PropertyValue[] properties, String name, NamedValue[] defaultValue)110 public static NamedValue[] getOrDefault(PropertyValue[] properties, String name, NamedValue[] defaultValue) throws IllegalArgumentException { 111 NamedValue[] ret = defaultValue; 112 for (PropertyValue property : properties) { 113 if (property.Name.equals(name)) { 114 Object[] array = (Object[]) AnyConverter.toArray(property.Value); 115 ret = new NamedValue[array.length]; 116 for (int i = 0; i < array.length; i++) { 117 ret[i] = (NamedValue) array[i]; 118 } 119 break; 120 } 121 } 122 return ret; 123 } 124 } 125