-
Notifications
You must be signed in to change notification settings - Fork 920
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ARTEMIS-2626]: Postgresql Journal implementation requires direct acc…
…ess to PostgeSQL driver internal classes. Issue: The BLOB manipulation is done using PostgreSQL internal classes starting from PGConnection. This leads to ClasCastExceptions if the connection is wrapped in a pool or if the driver is in a different classloader (WildFly). Fix: unwrap the connection and if the PostgreSQL classes are not directly available uses reflection to manipulate the BLOBs. Jira: https://issues.apache.org/jira/browse/ARTEMIS-2626
- Loading branch information
Showing
4 changed files
with
592 additions
and
29 deletions.
There are no files selected for viewing
209 changes: 209 additions & 0 deletions
209
...src/main/java/org/apache/activemq/artemis/jdbc/store/file/PostgresLargeObjectManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
/* | ||
* Copyright 2019 The Apache Software Foundation. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.activemq.artemis.jdbc.store.file; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import org.postgresql.PGConnection; | ||
import org.postgresql.largeobject.LargeObject; | ||
|
||
/** | ||
* Helper class for when the postresql driver is not directly availalbe. | ||
*/ | ||
public class PostgresLargeObjectManager { | ||
|
||
/** | ||
* This mode indicates we want to write to an object | ||
*/ | ||
public static final int WRITE = 0x00020000; | ||
|
||
/** | ||
* This mode indicates we want to read an object | ||
*/ | ||
public static final int READ = 0x00040000; | ||
|
||
/** | ||
* This mode is the default. It indicates we want read and write access to | ||
* a large object | ||
*/ | ||
public static final int READWRITE = READ | WRITE; | ||
|
||
private final Connection realConnection; | ||
private boolean shouldUseReflection; | ||
|
||
public PostgresLargeObjectManager(Connection connection) throws SQLException { | ||
this.realConnection = unwrap(connection); | ||
try { | ||
this.getClass().getClassLoader().loadClass("org.postgresql.PGConnection"); | ||
shouldUseReflection = false; | ||
} catch (ClassNotFoundException ex) { | ||
shouldUseReflection = true; | ||
} | ||
} | ||
|
||
public final Long createLO() throws SQLException { | ||
if (shouldUseReflection) { | ||
Object largeObjectManager = getLargeObjectManager(); | ||
try { | ||
Method method = largeObjectManager.getClass().getMethod("createLO"); | ||
return (Long) method.invoke(largeObjectManager); | ||
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { | ||
throw new SQLException("Couldn't access org.postgresql.largeobject.LargeObjectManager", ex); | ||
} | ||
} else { | ||
return ((PGConnection) realConnection).getLargeObjectAPI().createLO(); | ||
} | ||
} | ||
|
||
public Object open(long oid, int mode) throws SQLException { | ||
if (shouldUseReflection) { | ||
Object largeObjectManager = getLargeObjectManager(); | ||
try { | ||
Method method = largeObjectManager.getClass().getMethod("open", long.class, int.class); | ||
return method.invoke(largeObjectManager, oid, mode); | ||
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { | ||
throw new SQLException("Couldn't access org.postgresql.largeobject.LargeObjectManager", ex); | ||
} | ||
} else { | ||
return ((PGConnection) realConnection).getLargeObjectAPI().open(oid, mode); | ||
} | ||
} | ||
|
||
public int size(Object largeObject) throws SQLException { | ||
if (shouldUseReflection) { | ||
try { | ||
Method method = largeObject.getClass().getMethod("size"); | ||
return (int) method.invoke(largeObject); | ||
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { | ||
throw new SQLException("Couldn't access org.postgresql.largeobject.LargeObject", ex); | ||
} | ||
} else { | ||
return ((LargeObject) largeObject).size(); | ||
} | ||
} | ||
|
||
public void close(Object largeObject) throws SQLException { | ||
if (shouldUseReflection) { | ||
try { | ||
Method method = largeObject.getClass().getMethod("close"); | ||
method.invoke(largeObject); | ||
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { | ||
throw new SQLException("Couldn't access org.postgresql.largeobject.LargeObject", ex); | ||
} | ||
} else { | ||
((LargeObject) largeObject).close(); | ||
} | ||
} | ||
|
||
public byte[] read(Object largeObject, int length) throws SQLException { | ||
if (shouldUseReflection) { | ||
try { | ||
Method method = largeObject.getClass().getMethod("read", int.class); | ||
return (byte[]) method.invoke(largeObject, length); | ||
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { | ||
throw new SQLException("Couldn't access org.postgresql.largeobject.LargeObject", ex); | ||
} | ||
} else { | ||
return ((LargeObject) largeObject).read(length); | ||
} | ||
} | ||
|
||
public void write(Object largeObject, byte[] data) throws SQLException { | ||
if (shouldUseReflection) { | ||
try { | ||
Method method = largeObject.getClass().getMethod("write", byte[].class); | ||
method.invoke(largeObject, data); | ||
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { | ||
throw new SQLException("Couldn't access org.postgresql.largeobject.LargeObject", ex); | ||
} | ||
} else { | ||
((LargeObject) largeObject).write(data); | ||
} | ||
} | ||
|
||
public void seek(Object largeObject, int position) throws SQLException { | ||
if (shouldUseReflection) { | ||
try { | ||
Method method = largeObject.getClass().getMethod("seek", Integer.class); | ||
method.invoke(largeObject, position); | ||
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { | ||
throw new SQLException("Couldn't access org.postgresql.largeobject.LargeObject", ex); | ||
} | ||
} else { | ||
((LargeObject) largeObject).seek(position); | ||
} | ||
} | ||
|
||
public void truncate(Object largeObject, int position) throws SQLException { | ||
if (shouldUseReflection) { | ||
try { | ||
Method method = largeObject.getClass().getMethod("truncate", Integer.class); | ||
method.invoke(largeObject, position); | ||
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { | ||
throw new SQLException("Couldn't access org.postgresql.largeobject.LargeObject", ex); | ||
} | ||
} else { | ||
((LargeObject) largeObject).truncate(position); | ||
} | ||
} | ||
|
||
private Object getLargeObjectManager() throws SQLException { | ||
if (shouldUseReflection) { | ||
try { | ||
Method method = realConnection.getClass().getMethod("getLargeObjectAPI"); | ||
return method.invoke(realConnection); | ||
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { | ||
throw new SQLException("Couldn't access org.postgresql.largeobject.LargeObjectManager", ex); | ||
} | ||
} else { | ||
return ((PGConnection) realConnection).getLargeObjectAPI(); | ||
} | ||
} | ||
|
||
public final Connection unwrap(Connection connection) throws SQLException { | ||
Connection conn = connection.unwrap(Connection.class); | ||
return unwrapIronJacamar(unwrapDbcp(unwrapSpring(conn))); | ||
} | ||
|
||
private Connection unwrapIronJacamar(Connection conn) { | ||
try { | ||
Method method = conn.getClass().getMethod("getUnderlyingConnection"); | ||
return (Connection) method.invoke(conn); | ||
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { | ||
return conn; | ||
} | ||
} | ||
|
||
private Connection unwrapDbcp(Connection conn) { | ||
try { | ||
Method method = conn.getClass().getMethod("getDelegate"); | ||
return (Connection) method.invoke(conn); | ||
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { | ||
return conn; | ||
} | ||
} | ||
|
||
private Connection unwrapSpring(Connection conn) { | ||
try { | ||
Method method = conn.getClass().getMethod("getTargetConnection"); | ||
return (Connection) method.invoke(conn); | ||
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { | ||
return conn; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.