use of org.eclipse.persistence.internal.security.PrivilegedGetMethod in project eclipselink by eclipse-ee4j.
the class WebSphereTransactionController method acquireTransactionManager.
/**
* INTERNAL:
* Obtain and return the JTA TransactionManager on this platform.
* This will be called once when the controller is initialized.
*/
@Override
protected TransactionManager acquireTransactionManager() throws Exception {
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
try {
Class<? extends TransactionManager> clazz = AccessController.doPrivileged(new PrivilegedClassForName<>(getTxManagerFactoryClass()));
Method method = AccessController.doPrivileged(new PrivilegedGetMethod(clazz, getTxManagerFactoryMethod(), null, false));
return AccessController.doPrivileged(new PrivilegedMethodInvoker<>(method, null, null));
} catch (PrivilegedActionException ex) {
if (ex.getCause() instanceof ClassNotFoundException) {
throw (ClassNotFoundException) ex.getCause();
}
if (ex.getCause() instanceof NoSuchMethodException) {
throw (NoSuchMethodException) ex.getCause();
}
if (ex.getCause() instanceof IllegalAccessException) {
throw (IllegalAccessException) ex.getCause();
}
if (ex.getCause() instanceof InvocationTargetException) {
throw (InvocationTargetException) ex.getCause();
}
throw (RuntimeException) ex.getCause();
}
} else {
Class<? extends TransactionManager> clazz = PrivilegedAccessHelper.getClassForName(getTxManagerFactoryClass());
Method method = PrivilegedAccessHelper.getMethod(clazz, getTxManagerFactoryMethod(), null, false);
return PrivilegedAccessHelper.invokeMethod(method, null, null);
}
}
use of org.eclipse.persistence.internal.security.PrivilegedGetMethod in project eclipselink by eclipse-ee4j.
the class JotmTransactionController method acquireTransactionManager.
/**
* INTERNAL:
* Obtain and return the JTA TransactionManager on this platform
*/
@Override
protected TransactionManager acquireTransactionManager() throws Exception {
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
try {
Class<? extends TransactionManager> clazz = AccessController.doPrivileged(new PrivilegedClassForName<>(TX_CURRENT_FACTORY_CLASS));
Method method = AccessController.doPrivileged(new PrivilegedGetMethod(clazz, TX_CURRENT_FACTORY_METHOD, null, false));
Method txMethod = AccessController.doPrivileged(new PrivilegedGetMethod(clazz, TX_MANAGER_FACTORY_METHOD, null, false));
TransactionManager current = AccessController.doPrivileged(new PrivilegedMethodInvoker<>(method, null, null));
return AccessController.doPrivileged(new PrivilegedMethodInvoker<>(txMethod, current, null));
} catch (PrivilegedActionException ex) {
if (ex.getCause() instanceof ClassNotFoundException) {
throw (ClassNotFoundException) ex.getCause();
}
if (ex.getCause() instanceof NoSuchMethodException) {
throw (NoSuchMethodException) ex.getCause();
}
if (ex.getCause() instanceof IllegalAccessException) {
throw (IllegalAccessException) ex.getCause();
}
if (ex.getCause() instanceof InvocationTargetException) {
throw (InvocationTargetException) ex.getCause();
}
throw (RuntimeException) ex.getCause();
}
} else {
Class<? extends TransactionManager> clazz = PrivilegedAccessHelper.getClassForName(TX_CURRENT_FACTORY_CLASS);
Method method = PrivilegedAccessHelper.getMethod(clazz, TX_CURRENT_FACTORY_METHOD, null, false);
Method txMethod = PrivilegedAccessHelper.getMethod(clazz, TX_MANAGER_FACTORY_METHOD, null, false);
TransactionManager current = PrivilegedAccessHelper.invokeMethod(method, null, null);
return PrivilegedAccessHelper.invokeMethod(txMethod, current, null);
}
}
use of org.eclipse.persistence.internal.security.PrivilegedGetMethod in project eclipselink by eclipse-ee4j.
the class DoPrivilegedTest method testDoPrivileged.
public void testDoPrivileged() {
String className = "org.eclipse.persistence.Version";
String fieldName = "product";
String fieldValue = "TopLink Blah";
String methodName = "setProduct";
Class<?> clazz = null;
Version version = null;
Method method = null;
try {
clazz = AccessController.doPrivileged(new PrivilegedClassForName<>(className));
Class<?>[] methodParameterTypes = { AccessController.doPrivileged(new PrivilegedClassForName<>("java.lang.String")) };
ClassLoader clazzloader = AccessController.doPrivileged(new PrivilegedGetClassLoaderForClass(clazz));
ClassLoader classloader = AccessController.doPrivileged(new PrivilegedGetContextClassLoader(Thread.currentThread()));
AccessController.doPrivileged(new PrivilegedClassForName<>(className, true, clazzloader));
version = (Version) AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(clazz));
Field[] fields = AccessController.doPrivileged(new PrivilegedGetFields(clazz));
Field field = AccessController.doPrivileged(new PrivilegedGetDeclaredField(clazz, fieldName, true));
try {
int intValueFromField = (Integer) AccessController.doPrivileged(new PrivilegedGetValueFromField(field, version));
} catch (Exception e) {
}
AccessController.doPrivileged(new PrivilegedGetValueFromField(field, version));
try {
AccessController.doPrivileged(new PrivilegedSetValueInField(field, version, fieldValue));
} catch (Exception e) {
}
String lineSeparator = PrivilegedAccessHelper.getLineSeparator();
method = AccessController.doPrivileged(new PrivilegedGetMethod(clazz, methodName, methodParameterTypes, true));
AccessController.doPrivileged(new PrivilegedGetMethodParameterTypes(method));
AccessController.doPrivileged(new PrivilegedGetMethodReturnType(method));
Object[] parameters = { "TopLink Blah." };
AccessController.doPrivileged(new PrivilegedMethodInvoker(method, version, parameters));
Constructor<?> constructor = AccessController.doPrivileged(new PrivilegedGetConstructorFor<>(clazz, null, true));
Constructor declaredConstructor = AccessController.doPrivileged(new PrivilegedGetDeclaredConstructorFor(clazz, null, true));
AccessController.doPrivileged(new PrivilegedInvokeConstructor(constructor, null));
// PrivilegedAccessController.loadDeploymentXML(new XMLSessionConfigLoader(),new SessionManager(), clazzloader, false, false);
} catch (Exception e) {
throw (new TestProblemException("An exception has been caught."));
}
}
use of org.eclipse.persistence.internal.security.PrivilegedGetMethod in project eclipselink by eclipse-ee4j.
the class DB2ZPlatform method getParameterValueFromDatabaseCall.
@Override
public Object getParameterValueFromDatabaseCall(CallableStatement statement, String name, AbstractSession session) throws SQLException {
String methodName;
Class<?>[] methodArgs;
Object[] parameters;
methodName = "getJccObjectAtName";
methodArgs = new Class<?>[] { String.class };
parameters = new Object[] { name };
try {
Class<?> clazz;
Method method;
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
try {
ClassLoader cl = AccessController.doPrivileged(new PrivilegedGetContextClassLoader(Thread.currentThread()));
clazz = AccessController.doPrivileged(new PrivilegedClassForName<>(DB2_CALLABLESTATEMENT_CLASS, true, cl));
method = AccessController.doPrivileged(new PrivilegedGetMethod(clazz, methodName, methodArgs, true));
Object o = statement.unwrap(clazz);
return AccessController.doPrivileged(new PrivilegedMethodInvoker<>(method, o, parameters));
} catch (PrivilegedActionException ex) {
if (ex.getCause() instanceof ClassNotFoundException) {
throw (ClassNotFoundException) ex.getCause();
}
throw (RuntimeException) ex.getCause();
}
} else {
ClassLoader cl = PrivilegedAccessHelper.getContextClassLoader(Thread.currentThread());
clazz = PrivilegedAccessHelper.getClassForName(DB2_CALLABLESTATEMENT_CLASS, true, cl);
method = PrivilegedAccessHelper.getMethod(clazz, methodName, methodArgs, true);
Object o = statement.unwrap(clazz);
return PrivilegedAccessHelper.invokeMethod(method, o, parameters);
}
} catch (ReflectiveOperationException e) {
AbstractSessionLog.getLog().logThrowable(SessionLog.WARNING, null, e);
}
// Didn't work, fall back. This most likely still won't work, but the driver exception from there will be helpful.
return super.getParameterValueFromDatabaseCall(statement, name, session);
}
use of org.eclipse.persistence.internal.security.PrivilegedGetMethod in project eclipselink by eclipse-ee4j.
the class DB2ZPlatform method setParameterValueInDatabaseCall.
@Override
@SuppressWarnings({ "rawtypes" })
public void setParameterValueInDatabaseCall(Object parameter, CallableStatement statement, String name, AbstractSession session) throws SQLException {
String methodName = null;
Class<?>[] methodArgs = null;
Object[] parameters = null;
// Process common types first.
if (parameter instanceof String) {
// Check for stream binding of large strings.
if (usesStringBinding() && (((String) parameter).length() > getStringBindingSize())) {
CharArrayReader reader = new CharArrayReader(((String) parameter).toCharArray());
methodName = "setJccCharacterStreamAtName";
methodArgs = new Class<?>[] { String.class, java.io.Reader.class, int.class };
parameters = new Object[] { name, reader, ((String) parameter).length() };
} else {
// TODO find shouldUseGetSetNString() support for DB2/Z
methodName = "setJccStringAtName";
methodArgs = new Class<?>[] { String.class, String.class };
parameters = new Object[] { name, parameter };
}
} else if (parameter instanceof Number) {
Number number = (Number) parameter;
if (number instanceof Integer) {
methodName = "setJccIntAtName";
methodArgs = new Class<?>[] { String.class, int.class };
parameters = new Object[] { name, number.intValue() };
} else if (number instanceof Long) {
methodName = "setJccLongAtName";
methodArgs = new Class<?>[] { String.class, long.class };
parameters = new Object[] { name, number.longValue() };
} else if (number instanceof BigDecimal) {
methodName = "setJccBigDecimalAtName";
methodArgs = new Class<?>[] { String.class, BigDecimal.class };
parameters = new Object[] { name, number };
} else if (number instanceof Double) {
methodName = "setJccDoubleAtName";
methodArgs = new Class<?>[] { String.class, double.class };
parameters = new Object[] { name, number.doubleValue() };
} else if (number instanceof Float) {
methodName = "setJccFloatAtName";
methodArgs = new Class<?>[] { String.class, float.class };
parameters = new Object[] { name, number.floatValue() };
} else if (number instanceof Short) {
methodName = "setJccShortAtName";
methodArgs = new Class<?>[] { String.class, short.class };
parameters = new Object[] { name, number.shortValue() };
} else if (number instanceof Byte) {
methodName = "setJccByteAtName";
methodArgs = new Class<?>[] { String.class, byte.class };
parameters = new Object[] { name, number.byteValue() };
} else if (number instanceof BigInteger) {
// Convert to BigDecimal.
methodName = "setJccBigDecimalAtName";
methodArgs = new Class<?>[] { String.class, BigDecimal.class };
parameters = new Object[] { name, new BigDecimal((BigInteger) number) };
} else {
methodName = "setJccObjectAtName";
methodArgs = new Class<?>[] { String.class, Object.class };
parameters = new Object[] { name, parameter };
}
} else if (parameter instanceof java.sql.Date) {
methodName = "setJccDateAtName";
methodArgs = new Class<?>[] { String.class, java.sql.Date.class };
parameters = new Object[] { name, parameter };
} else if (parameter instanceof java.time.LocalDate) {
// Convert to java.sql.Date
methodName = "setJccDateAtName";
methodArgs = new Class<?>[] { String.class, java.sql.Date.class };
parameters = new Object[] { name, java.sql.Date.valueOf((java.time.LocalDate) parameter) };
} else if (parameter instanceof java.sql.Timestamp) {
methodName = "setJccTimestampAtName";
methodArgs = new Class<?>[] { String.class, java.sql.Timestamp.class };
parameters = new Object[] { name, parameter };
} else if (parameter instanceof java.time.LocalDateTime) {
// Convert to java.sql.Timestamp
methodName = "setJccTimestampAtName";
methodArgs = new Class<?>[] { String.class, java.sql.Timestamp.class };
parameters = new Object[] { name, java.sql.Timestamp.valueOf((java.time.LocalDateTime) parameter) };
} else if (parameter instanceof java.time.OffsetDateTime) {
// Convert to java.sql.Timestamp
methodName = "setJccTimestampAtName";
methodArgs = new Class<?>[] { String.class, java.sql.Timestamp.class };
parameters = new Object[] { name, java.sql.Timestamp.from(((java.time.OffsetDateTime) parameter).toInstant()) };
} else if (parameter instanceof java.sql.Time) {
methodName = "setJccTimeAtName";
methodArgs = new Class<?>[] { String.class, java.sql.Time.class };
parameters = new Object[] { name, parameter };
} else if (parameter instanceof java.time.LocalTime) {
java.time.LocalTime lt = (java.time.LocalTime) parameter;
java.sql.Timestamp ts = java.sql.Timestamp.valueOf(java.time.LocalDateTime.of(java.time.LocalDate.ofEpochDay(0), lt));
methodName = "setJccTimestampAtName";
methodArgs = new Class<?>[] { String.class, java.sql.Timestamp.class };
parameters = new Object[] { name, ts };
} else if (parameter instanceof java.time.OffsetTime) {
java.time.OffsetTime ot = (java.time.OffsetTime) parameter;
java.sql.Timestamp ts = java.sql.Timestamp.valueOf(java.time.LocalDateTime.of(java.time.LocalDate.ofEpochDay(0), ot.toLocalTime()));
methodName = "setJccTimestampAtName";
methodArgs = new Class<?>[] { String.class, java.sql.Timestamp.class };
parameters = new Object[] { name, ts };
} else if (parameter instanceof Boolean) {
methodName = "setJccBooleanAtName";
methodArgs = new Class<?>[] { String.class, boolean.class };
parameters = new Object[] { name, parameter };
} else if (parameter == null) {
// Normally null is passed as a DatabaseField so the type is included, but in some case may be passed directly.
methodName = "setJccNullAtName";
methodArgs = new Class<?>[] { String.class, int.class };
parameters = new Object[] { name, getJDBCType((Class) null) };
} else if (parameter instanceof DatabaseField) {
setNullFromDatabaseField((DatabaseField) parameter, statement, name);
} else if (parameter instanceof byte[]) {
if (usesStreamsForBinding()) {
ByteArrayInputStream inputStream = new ByteArrayInputStream((byte[]) parameter);
methodName = "setJccBinaryStreamAtName";
methodArgs = new Class<?>[] { String.class, java.io.InputStream.class, int.class };
parameters = new Object[] { name, inputStream, ((byte[]) parameter).length };
} else {
methodName = "setJccBytesAtName";
methodArgs = new Class<?>[] { String.class, byte[].class };
parameters = new Object[] { name, parameter };
}
} else // Next process types that need conversion.
if (parameter instanceof Calendar) {
methodName = "setJccTimestampAtName";
methodArgs = new Class<?>[] { String.class, java.sql.Timestamp.class };
parameters = new Object[] { name, Helper.timestampFromDate(((Calendar) parameter).getTime()) };
} else if (parameter.getClass() == ClassConstants.UTILDATE) {
methodName = "setJccTimestampAtName";
methodArgs = new Class<?>[] { String.class, java.sql.Timestamp.class };
parameters = new Object[] { name, Helper.timestampFromDate((java.util.Date) parameter) };
} else if (parameter instanceof Character) {
methodName = "setJccStringAtName";
methodArgs = new Class<?>[] { String.class, String.class };
parameters = new Object[] { name, ((Character) parameter).toString() };
} else if (parameter instanceof char[]) {
methodName = "setJccStringAtName";
methodArgs = new Class<?>[] { String.class, String.class };
parameters = new Object[] { name, new String((char[]) parameter) };
} else if (parameter instanceof Character[]) {
methodName = "setJccStringAtName";
methodArgs = new Class<?>[] { String.class, String.class };
parameters = new Object[] { name, convertObject(parameter, ClassConstants.STRING) };
} else if (parameter instanceof Byte[]) {
methodName = "setJccBytesAtName";
methodArgs = new Class<?>[] { String.class, byte[].class };
parameters = new Object[] { name, convertObject(parameter, ClassConstants.APBYTE) };
} else if (parameter instanceof java.sql.SQLXML) {
methodName = "setJccSQLXMLAtName";
methodArgs = new Class<?>[] { String.class, java.sql.SQLXML.class };
parameters = new Object[] { name, parameter };
} else if (parameter instanceof BindCallCustomParameter) {
((BindCallCustomParameter) (parameter)).set(this, statement, name, session);
} else if (typeConverters != null && typeConverters.containsKey(parameter.getClass())) {
StructConverter converter = typeConverters.get(parameter.getClass());
java.sql.Struct struct = converter.convertToStruct(parameter, getConnection(session, statement.getConnection()));
methodName = "setJccObjectAtName";
methodArgs = new Class<?>[] { String.class, Object.class };
parameters = new Object[] { name, struct };
} else {
methodName = "setJccObjectAtName";
methodArgs = new Class<?>[] { String.class, Object.class };
parameters = new Object[] { name, parameter };
}
if (methodName != null) {
try {
Class<?> clazz;
Method method;
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
try {
ClassLoader cl = AccessController.doPrivileged(new PrivilegedGetContextClassLoader(Thread.currentThread()));
clazz = AccessController.doPrivileged(new PrivilegedClassForName<>(DB2_PREPAREDSTATEMENT_CLASS, true, cl));
method = AccessController.doPrivileged(new PrivilegedGetMethod(clazz, methodName, methodArgs, true));
Object o = statement.unwrap(clazz);
AccessController.doPrivileged(new PrivilegedMethodInvoker<>(method, o, parameters));
} catch (PrivilegedActionException ex) {
if (ex.getCause() instanceof ClassNotFoundException) {
throw (ClassNotFoundException) ex.getCause();
}
throw (RuntimeException) ex.getCause();
}
} else {
ClassLoader cl = PrivilegedAccessHelper.getContextClassLoader(Thread.currentThread());
clazz = PrivilegedAccessHelper.getClassForName(DB2_PREPAREDSTATEMENT_CLASS, true, cl);
method = PrivilegedAccessHelper.getMethod(clazz, methodName, methodArgs, true);
Object o = statement.unwrap(clazz);
PrivilegedAccessHelper.invokeMethod(method, o, parameters);
}
} catch (ReflectiveOperationException e) {
AbstractSessionLog.getLog().logThrowable(SessionLog.WARNING, null, e);
// Didn't work, fall back. This most likely still won't work, but the driver exception from there will be helpful.
super.setParameterValueInDatabaseCall(parameter, statement, name, session);
}
}
}
Aggregations