Skip to content

Commit

Permalink
First round of exceptions paths cleanup. This commit introduces an Ol…
Browse files Browse the repository at this point in the history
…apExceptionHandler which was factored out of the XmlaOlap4jConnection class. I made sure that all OlapException get thrown through the helper in the xmla driver. Eventually, we could do the same with the rest of Olap4j.

git-svn-id: https://olap4j.svn.sourceforge.net/svnroot/olap4j/trunk@110 c6a108a4-781c-0410-a6c6-c2d559e19af0
  • Loading branch information
lucboudreau committed Aug 15, 2008
1 parent 1e25fee commit 3108dd2
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 73 deletions.
51 changes: 51 additions & 0 deletions src/org/olap4j/OlapExceptionHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
// This software is subject to the terms of the Common Public License
// Agreement, available at the following URL:
// http://www.opensource.org/licenses/cpl.html.
// Copyright (C) 2007-2008 Julian Hyde
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
*/
package org.olap4j;

import java.sql.SQLException;

/**
* Sugar class to help create OlapExceptions.
* @author Luc Boudreau
* @version $Id: $
*/
public class OlapExceptionHelper {

public static OlapException createException(String msg) {
return new OlapException(msg);
}

public static OlapException createException(String msg, Throwable cause) {
return new OlapException(msg, cause);
}

public static OlapException createException(Cell context, String msg) {
OlapException exception = new OlapException(msg);
exception.setContext(context);
return exception;
}

public static OlapException createException(
Cell context, String msg, Throwable cause)
{
OlapException exception = new OlapException(msg, cause);
exception.setContext(context);
return exception;
}

public static OlapException toOlapException(SQLException e) {
if (e instanceof OlapException) {
return (OlapException) e;
} else {
return new OlapException(null, e);
}
}
}

//End ExceptionHelper.java
2 changes: 1 addition & 1 deletion src/org/olap4j/driver/xmla/DeferredNamedListImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private NamedList<T> getList() {
populateList(list);
state = State.POPULATED;
} catch (OlapException e) {
// todo: fetch metadata on getCollection() method, so we
// TODO: fetch metadata on getCollection() method, so we
// can't get an exception while traversing the list
throw new RuntimeException(e);
}
Expand Down
3 changes: 2 additions & 1 deletion src/org/olap4j/driver/xmla/EmptyResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
package org.olap4j.driver.xmla;

import org.olap4j.OlapExceptionHelper;
import org.olap4j.OlapWrapper;

import javax.sql.rowset.RowSetMetaDataImpl;
Expand Down Expand Up @@ -724,7 +725,7 @@ public <T> T unwrap(Class<T> iface) throws SQLException {
if (iface.isInstance(this)) {
return iface.cast(this);
}
throw olap4jConnection.helper.createException("cannot cast");
throw OlapExceptionHelper.createException("cannot cast");
}

public boolean isWrapperFor(Class<?> iface) throws SQLException {
Expand Down
12 changes: 6 additions & 6 deletions src/org/olap4j/driver/xmla/XmlaOlap4jCellSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ void populate() throws OlapException {
try {
doc = parse(bytes);
} catch (IOException e) {
throw olap4jStatement.olap4jConnection.helper.createException(
throw OlapExceptionHelper.createException(
"error creating CellSet", e);
} catch (SAXException e) {
throw olap4jStatement.olap4jConnection.helper.createException(
throw OlapExceptionHelper.createException(
"error creating CellSet", e);
}
// <SOAP-ENV:Envelope>
Expand Down Expand Up @@ -125,7 +125,7 @@ void populate() throws OlapException {
// TODO: log doc to logfile
final Element faultstring = findChild(fault, null, "faultstring");
String message = faultstring.getTextContent();
throw olap4jStatement.olap4jConnection.helper.createException(
throw OlapExceptionHelper.createException(
"XMLA provider gave exception: " + message);
}
Element executeResponse =
Expand Down Expand Up @@ -338,7 +338,7 @@ private Object getTypedValue(Element cell) throws OlapException {
return XmlaOlap4jUtil.stringElement(cell, "Value");
}
} catch (Exception e) {
throw new OlapException(
throw OlapExceptionHelper.createException(
"Error while casting a cell value to the correct java type for"
+ " its XSD type " + type,
e);
Expand Down Expand Up @@ -369,7 +369,7 @@ private XmlaOlap4jCellSetMetaData createMetaData(Element root)
this.olap4jStatement.olap4jConnection.getSchema().getCubes().get(
cubeName);
if (cube == null) {
throw olap4jStatement.olap4jConnection.helper.createException(
throw OlapExceptionHelper.createException(
"Internal error: cube '" + cubeName + "' not found");
}
final Element axesInfo =
Expand Down Expand Up @@ -483,7 +483,7 @@ private Hierarchy lookupHierarchy(XmlaOlap4jCube cube, String hierarchyName)
}
}
if (hierarchy == null) {
throw this.olap4jStatement.olap4jConnection.helper
throw OlapExceptionHelper
.createException(
"Internal error: hierarchy '" + hierarchyName
+ "' not found in cube '" + cube.getName()
Expand Down
56 changes: 9 additions & 47 deletions src/org/olap4j/driver/xmla/XmlaOlap4jConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@
* @since May 23, 2007
*/
abstract class XmlaOlap4jConnection implements OlapConnection {
/**
* <p>Handler for errors.
*/
final Helper helper = new Helper();

/**
* <p>Current schema.
*/
Expand Down Expand Up @@ -150,7 +145,7 @@ abstract class XmlaOlap4jConnection implements OlapConnection {
// Set URL of HTTP server.
String serverUrl = map.get(XmlaOlap4jDriver.Property.Server.name());
if (serverUrl == null) {
throw helper.createException("Connection property '"
throw OlapExceptionHelper.createException("Connection property '"
+ XmlaOlap4jDriver.Property.Server.name()
+ "' must be specified");
}
Expand All @@ -174,7 +169,7 @@ abstract class XmlaOlap4jConnection implements OlapConnection {
try {
this.serverUrl = new URL(serverUrl);
} catch (MalformedURLException e) {
throw helper.createException(
throw OlapExceptionHelper.createException(
"Error while creating connection", e);
}

Expand Down Expand Up @@ -290,15 +285,15 @@ String getDataSourceInfo() throws OlapException {
// Throws exception to the client.
//Tells that there are no datasource corresponding to the search criterias.
if (this.nativeDatasourceName == null) {
throw new OlapException("No datasource could be found.");
throw OlapExceptionHelper.createException("No datasource could be found.");
}

// If there is a provider
return this.nativeDatasourceName;
} catch (OlapException e) {
throw e;
} catch (SQLException e) {
throw new OlapException("Datasource name not found.", e);
throw OlapExceptionHelper.createException("Datasource name not found.", e);
} finally {
try {
if (rSet != null) {
Expand Down Expand Up @@ -485,7 +480,7 @@ public <T> T unwrap(Class<T> iface) throws SQLException {
if (iface.isInstance(this)) {
return iface.cast(this);
}
throw helper.createException("does not implement '" + iface + "'");
throw OlapExceptionHelper.createException("does not implement '" + iface + "'");
}

public boolean isWrapperFor(Class<?> iface) throws SQLException {
Expand Down Expand Up @@ -587,16 +582,16 @@ Element xxx(String request) throws OlapException {
try {
bytes = proxy.get(serverUrl, request);
} catch (IOException e) {
throw helper.createException(null, e);
throw OlapExceptionHelper.createException(null, e);
}
Document doc;
try {
doc = parse(bytes);
} catch (IOException e) {
throw helper.createException(
throw OlapExceptionHelper.createException(
"error discovering metadata", e);
} catch (SAXException e) {
throw helper.createException(
throw OlapExceptionHelper.createException(
"error discovering metadata", e);
}
// <SOAP-ENV:Envelope>
Expand Down Expand Up @@ -639,7 +634,7 @@ Element xxx(String request) throws OlapException {
// TODO: log doc to logfile
final Element faultstring = findChild(fault, null, "faultstring");
String message = faultstring.getTextContent();
throw helper.createException(
throw OlapExceptionHelper.createException(
"XMLA provider gave exception: " + message
+ "; request: " + request);
}
Expand Down Expand Up @@ -757,39 +752,6 @@ private static String xmlEncode(String value) {
}

// ~ inner classes --------------------------------------------------------
@SuppressWarnings({"ThrowableInstanceNeverThrown"})
static class Helper {
OlapException createException(String msg) {
return new OlapException(msg);
}

OlapException createException(String msg, Throwable cause) {
return new OlapException(msg, cause);
}

OlapException createException(Cell context, String msg) {
OlapException exception = new OlapException(msg);
exception.setContext(context);
return exception;
}

OlapException createException(
Cell context, String msg, Throwable cause)
{
OlapException exception = new OlapException(msg, cause);
exception.setContext(context);
return exception;
}

public OlapException toOlapException(SQLException e) {
if (e instanceof OlapException) {
return (OlapException) e;
} else {
return new OlapException(null, e);
}
}
}

static class CatalogHandler
extends HandlerImpl<XmlaOlap4jCatalog>
{
Expand Down
2 changes: 1 addition & 1 deletion src/org/olap4j/driver/xmla/XmlaOlap4jDatabaseMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ public <T> T unwrap(Class<T> iface) throws SQLException {
if (iface.isInstance(this)) {
return iface.cast(this);
}
throw olap4jConnection.helper.createException(
throw OlapExceptionHelper.createException(
"does not implement '" + iface + "'");
}

Expand Down
7 changes: 3 additions & 4 deletions src/org/olap4j/driver/xmla/XmlaOlap4jPreparedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ abstract class XmlaOlap4jPreparedStatement
statement.close();

} catch (SQLException e) {
throw olap4jConnection.helper.createException(
throw OlapExceptionHelper.createException(
"Error while preparing statement '" + mdx + "'",
e);
}
Expand Down Expand Up @@ -252,9 +252,8 @@ public String getParameterName(int param) throws OlapException {
private Parameter getParameter(int param) throws OlapException {
final List<Parameter> parameters = getParameters();
if (param < 1 || param > parameters.size()) {
throw olap4jConnection.helper.toOlapException(
olap4jConnection.helper.createException(
"parameter ordinal " + param + " out of range"));
throw OlapExceptionHelper.createException(
"parameter ordinal " + param + " out of range");
}
return parameters.get(param - 1);
}
Expand Down
16 changes: 8 additions & 8 deletions src/org/olap4j/driver/xmla/XmlaOlap4jStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public ResultSet executeQuery(String sql) throws SQLException {

private void checkOpen() throws SQLException {
if (closed) {
throw olap4jConnection.helper.createException("closed");
throw OlapExceptionHelper.createException("closed");
}
}

Expand Down Expand Up @@ -100,7 +100,7 @@ public int getQueryTimeout() throws SQLException {

public void setQueryTimeout(int seconds) throws SQLException {
if (seconds < 0) {
throw olap4jConnection.helper.createException(
throw OlapExceptionHelper.createException(
"illegal timeout value " + seconds);
}
this.timeoutSeconds = seconds;
Expand Down Expand Up @@ -243,7 +243,7 @@ public <T> T unwrap(Class<T> iface) throws SQLException {
if (iface.isInstance(this)) {
return iface.cast(this);
}
throw olap4jConnection.helper.createException(
throw OlapExceptionHelper.createException(
"does not implement '" + iface + "'");
}

Expand Down Expand Up @@ -299,7 +299,7 @@ public CellSet executeOlapQuery(String mdx) throws OlapException {
try {
cs.close();
} catch (SQLException e) {
throw olap4jConnection.helper.createException(
throw OlapExceptionHelper.createException(
"Error while closing previous CellSet", e);
}
}
Expand Down Expand Up @@ -347,14 +347,14 @@ byte[] getBytes() throws OlapException {
return future.get();
}
} catch (InterruptedException e) {
throw olap4jConnection.helper.createException(null, e);
throw OlapExceptionHelper.createException(null, e);
} catch (ExecutionException e) {
throw olap4jConnection.helper.createException(null, e.getCause());
throw OlapExceptionHelper.createException(null, e.getCause());
} catch (TimeoutException e) {
throw olap4jConnection.helper.createException(
throw OlapExceptionHelper.createException(
"Query timeout of " + timeoutSeconds + " seconds exceeded");
} catch (CancellationException e) {
throw olap4jConnection.helper.createException("Query canceled");
throw OlapExceptionHelper.createException("Query canceled");
} finally {
synchronized (this) {
if (future == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.concurrent.*;

import org.olap4j.OlapException;
import org.olap4j.OlapExceptionHelper;
import org.olap4j.driver.xmla.XmlaOlap4jDriver;
import org.olap4j.driver.xmla.cache.XmlaOlap4jCache;

Expand Down Expand Up @@ -127,23 +128,23 @@ public void setCache(Map<String,String> config, Map<String,String> properties)
// Configures it
this.cacheId = this.cache.setParameters(config, properties);
} catch (ClassNotFoundException e) {
throw new OlapException(
throw OlapExceptionHelper.createException(
"The specified cache class name could not be found : "
+ config.get(XmlaOlap4jDriver.Property.Cache.name()), e);
} catch (InstantiationException e) {
throw new OlapException(
throw OlapExceptionHelper.createException(
"The specified cache class name could not be instanciated : "
+ config.get(XmlaOlap4jDriver.Property.Cache.name()), e);
} catch (IllegalAccessException e) {
throw new OlapException(
throw OlapExceptionHelper.createException(
"An error was encountered while instanciating the cache : "
+ config.get(XmlaOlap4jDriver.Property.Cache.name()), e);
} catch (IllegalArgumentException e) {
throw new OlapException(
throw OlapExceptionHelper.createException(
"An error was encountered while instanciating the cache : "
+ config.get(XmlaOlap4jDriver.Property.Cache.name()), e);
} catch (SecurityException e) {
throw new OlapException(
throw OlapExceptionHelper.createException(
"An error was encountered while instanciating the cache : "
+ config.get(XmlaOlap4jDriver.Property.Cache.name()), e);
}
Expand Down

0 comments on commit 3108dd2

Please sign in to comment.