use of org.eclipse.persistence.internal.security.PrivilegedInvokeConstructor in project eclipselink by eclipse-ee4j.
the class ContainerPolicy method containerInstance.
/**
* INTERNAL:
* Return an instance of the container class with the specified initial capacity.
* Null should never be returned.
* A ValidationException is thrown on error.
*/
@Override
public Object containerInstance(int initialCapacity) {
if (this.constructor == null) {
return containerInstance();
}
Class<?> containerClass = getContainerClass();
try {
// PERF: Avoid reflection for common cases.
if (containerClass == ClassConstants.IndirectList_Class) {
return IndirectCollectionsFactory.createIndirectList(initialCapacity);
} else if (containerClass == ClassConstants.IndirectSet_Class) {
return IndirectCollectionsFactory.createIndirectSet(initialCapacity);
} else if (containerClass == ClassConstants.ArrayList_class) {
return new ArrayList(initialCapacity);
} else if (containerClass == ClassConstants.Vector_class) {
return new Vector(initialCapacity);
} else if (containerClass == ClassConstants.HashSet_class) {
return new HashSet(initialCapacity);
}
Object[] arguments = new Object[1];
// Code change for 3732. No longer need to add 1 as this was for JDK 1.1
arguments[0] = initialCapacity;
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
try {
return AccessController.doPrivileged(new PrivilegedInvokeConstructor(this.constructor, arguments));
} catch (PrivilegedActionException exception) {
throw QueryException.couldNotInstantiateContainerClass(containerClass, exception.getException());
}
} else {
return PrivilegedAccessHelper.invokeConstructor(this.constructor, arguments);
}
} catch (Exception ex) {
throw QueryException.couldNotInstantiateContainerClass(containerClass, ex);
}
}
use of org.eclipse.persistence.internal.security.PrivilegedInvokeConstructor in project eclipselink by eclipse-ee4j.
the class ReportQueryResult method processConstructorItem.
private Object processConstructorItem(ReportQuery query, AbstractRecord row, Vector toManyData, ConstructorReportItem constructorItem) {
// For constructor items need to process each constructor argument.
Class<?>[] constructorArgTypes = constructorItem.getConstructorArgTypes();
int numberOfArguments = constructorItem.getReportItems().size();
Object[] constructorArgs = new Object[numberOfArguments];
for (int argumentIndex = 0; argumentIndex < numberOfArguments; argumentIndex++) {
ReportItem argumentItem = constructorItem.getReportItems().get(argumentIndex);
Object result = null;
if (argumentItem.isConstructorItem()) {
result = processConstructorItem(query, row, toManyData, (ConstructorReportItem) argumentItem);
} else {
result = processItem(query, row, toManyData, argumentItem);
}
constructorArgs[argumentIndex] = ConversionManager.getDefaultManager().convertObject(result, constructorArgTypes[argumentIndex]);
}
try {
Constructor constructor = constructorItem.getConstructor();
Object returnValue = null;
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
try {
returnValue = AccessController.doPrivileged(new PrivilegedInvokeConstructor(constructor, constructorArgs));
} catch (PrivilegedActionException exception) {
throw QueryException.exceptionWhileUsingConstructorExpression(exception.getException(), query);
}
} else {
returnValue = PrivilegedAccessHelper.invokeConstructor(constructor, constructorArgs);
}
return returnValue;
} catch (ReflectiveOperationException exc) {
throw QueryException.exceptionWhileUsingConstructorExpression(exc, query);
}
}
use of org.eclipse.persistence.internal.security.PrivilegedInvokeConstructor in project eclipselink by eclipse-ee4j.
the class ConstructorResult method getValueFromRecord.
/**
* INTERNAL:
* This method is a convenience method for extracting values from results/
*/
@Override
public Object getValueFromRecord(DatabaseRecord record, ResultSetMappingQuery query) {
if (constructor == null) {
initialize(record, query);
}
int columnResultsSize = getColumnResults().size();
Object[] constructorArgs = new Object[columnResultsSize];
for (int i = 0; i < columnResultsSize; i++) {
constructorArgs[i] = ConversionManager.getDefaultManager().convertObject(record.get(getColumnResults().get(i).getColumn()), constructorArgTypes[i]);
}
try {
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
try {
return AccessController.doPrivileged(new PrivilegedInvokeConstructor(constructor, constructorArgs));
} catch (PrivilegedActionException exception) {
throw QueryException.exceptionWhileInitializingConstructor(exception.getException(), query, targetClass);
}
} else {
return PrivilegedAccessHelper.invokeConstructor(constructor, constructorArgs);
}
} catch (IllegalAccessException exception) {
throw QueryException.exceptionWhileInitializingConstructor(exception, query, targetClass);
} catch (java.lang.reflect.InvocationTargetException exception) {
throw QueryException.exceptionWhileInitializingConstructor(exception, query, targetClass);
} catch (InstantiationException exception) {
throw QueryException.exceptionWhileInitializingConstructor(exception, query, targetClass);
}
}
use of org.eclipse.persistence.internal.security.PrivilegedInvokeConstructor in project eclipselink by eclipse-ee4j.
the class JavaSECMPInitializer method createTempLoader.
@Override
protected ClassLoader createTempLoader(Collection col, boolean shouldOverrideLoadClassForCollectionMembers) {
if (!shouldCreateInternalLoader) {
return Thread.currentThread().getContextClassLoader();
}
ClassLoader currentLoader = Thread.currentThread().getContextClassLoader();
if (!(currentLoader instanceof URLClassLoader)) {
// shouldn't be a problem (and should only occur) in JavaSE
return currentLoader;
}
URL[] urlPath = ((URLClassLoader) currentLoader).getURLs();
ClassLoader tempLoader = null;
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
try {
Class<?>[] argsClasses = new Class<?>[] { URL[].class, ClassLoader.class, Collection.class, boolean.class };
Object[] args = new Object[] { urlPath, currentLoader, col, shouldOverrideLoadClassForCollectionMembers };
Constructor<TempEntityLoader> classLoaderConstructor = AccessController.doPrivileged(new PrivilegedGetConstructorFor<>(TempEntityLoader.class, argsClasses, true));
tempLoader = (ClassLoader) AccessController.doPrivileged(new PrivilegedInvokeConstructor(classLoaderConstructor, args));
} catch (PrivilegedActionException privilegedException) {
throw new PersistenceException(EntityManagerSetupException.failedToInstantiateTemporaryClassLoader(privilegedException));
}
} else {
tempLoader = new TempEntityLoader(urlPath, currentLoader, col, shouldOverrideLoadClassForCollectionMembers);
}
AbstractSessionLog.getLog().log(SessionLog.FINER, SessionLog.WEAVER, "cmp_init_tempLoader_created", tempLoader);
AbstractSessionLog.getLog().log(SessionLog.FINER, SessionLog.WEAVER, "cmp_init_shouldOverrideLoadClassForCollectionMembers", shouldOverrideLoadClassForCollectionMembers);
return tempLoader;
}
use of org.eclipse.persistence.internal.security.PrivilegedInvokeConstructor in project eclipselink by eclipse-ee4j.
the class QueryOperation method createSimpleXMLFormat.
@SuppressWarnings({ "unchecked" })
public Object createSimpleXMLFormat(XRServiceAdapter xrService, Object value) {
XMLRoot xmlRoot = new XMLRoot();
SimpleXMLFormat simpleXMLFormat = result.getSimpleXMLFormat();
String tempSimpleXMLFormatTag = SimpleXMLFormat.DEFAULT_SIMPLE_XML_FORMAT_TAG;
String simpleXMLFormatTag = simpleXMLFormat.getSimpleXMLFormatTag();
if (simpleXMLFormatTag != null && !EMPTY_STR.equals(simpleXMLFormatTag)) {
tempSimpleXMLFormatTag = simpleXMLFormatTag;
}
xmlRoot.setLocalName(tempSimpleXMLFormatTag);
String tempXMLTag = DEFAULT_SIMPLE_XML_TAG;
String xmlTag = simpleXMLFormat.getXMLTag();
if (xmlTag != null && !EMPTY_STR.equals(xmlTag)) {
tempXMLTag = xmlTag;
}
Vector<DatabaseRecord> records = null;
if (value instanceof ArrayList) {
// JPA query results in a list of raw values
// Here we have raw values returned as opposed to DatabaseRecords - this means
// we need to figure out the tag names based on the call's output parameters.
// assumes JPAQuery
JPAQuery jpaQuery = (JPAQuery) queryHandler.getDatabaseQuery();
// to match field names with results, we need to gather the database fields from each of the Output parameters
List<DatabaseField> paramFlds = new ArrayList<>();
DatasourceCall dsCall = (DatasourceCall) jpaQuery.getDatabaseQuery().getDatasourceCall();
for (Object obj : dsCall.getParameters()) {
if (obj instanceof OutputParameterForCallableStatement) {
paramFlds.add(((OutputParameterForCallableStatement) obj).getOutputField());
} else if (obj instanceof Object[]) {
Object[] objArray = (Object[]) obj;
for (int i = 0; i < objArray.length; i++) {
Object o = objArray[i];
if (o instanceof OutputParameterForCallableStatement) {
paramFlds.add(((OutputParameterForCallableStatement) o).getOutputField());
}
}
}
}
// now create a record using DatabaseField/value pairs
DatabaseRecord dr = new DatabaseRecord();
if (paramFlds.size() > 0) {
for (int i = 0; i < ((ArrayList<?>) value).size(); i++) {
dr.add(paramFlds.get(i), ((ArrayList<?>) value).get(i));
}
} else {
dr.add(new DatabaseField(RESULT_STR), ((ArrayList<?>) value).get(0));
}
records = new Vector<>();
records.add(dr);
} else if (value instanceof Vector) {
Class<?> vectorContent = ((Vector<?>) value).firstElement().getClass();
if (DatabaseRecord.class.isAssignableFrom(vectorContent)) {
records = (Vector<DatabaseRecord>) value;
} else {
records = new Vector<>();
DatabaseRecord dr = new DatabaseRecord();
dr.add(new DatabaseField(RESULT_STR), ((Vector<?>) value).firstElement());
records.add(dr);
}
} else {
records = new Vector<>();
DatabaseRecord dr = new DatabaseRecord();
dr.add(new DatabaseField(RESULT_STR), value);
records.add(dr);
}
SimpleXMLFormatModel simpleXMLFormatModel = new SimpleXMLFormatModel();
XMLConversionManager conversionManager = (XMLConversionManager) xrService.getOXSession().getDatasourcePlatform().getConversionManager();
SessionLog log = xrService.getOXSession().getSessionLog();
for (DatabaseRecord dr : records) {
Element rowElement = TEMP_DOC.createElement(tempXMLTag);
for (DatabaseField field : dr.getFields()) {
// handle complex types, i.e. ones we have a descriptor for
if (field instanceof ObjectRelationalDatabaseField) {
ObjectRelationalDatabaseField ordtField = (ObjectRelationalDatabaseField) field;
if (xrService.getOXSession().getDescriptor(ordtField.getType()) != null) {
xrService.getXMLContext().createMarshaller().marshal(dr.get(field), rowElement);
continue;
}
}
Object fieldValue = dr.get(field);
if (fieldValue != null) {
if (fieldValue instanceof Calendar) {
Calendar cValue = (Calendar) fieldValue;
fieldValue = conversionManager.convertObject(cValue, STRING, DATE_TIME_QNAME);
}
if (fieldValue instanceof Date) {
Date dValue = (Date) fieldValue;
fieldValue = conversionManager.convertObject(dValue, STRING, DATE_QNAME);
} else if (fieldValue instanceof Time) {
Time tValue = (Time) fieldValue;
fieldValue = conversionManager.convertObject(tValue, STRING, TIME_QNAME);
} else if (fieldValue instanceof Timestamp) {
Timestamp tsValue = (Timestamp) fieldValue;
fieldValue = conversionManager.convertObject(tsValue, STRING, DATE_TIME_QNAME);
} else if (fieldValue instanceof Blob) {
fieldValue = conversionManager.convertObject(fieldValue, ClassConstants.APBYTE);
} else if (SQLXML.class.isAssignableFrom(fieldValue.getClass())) {
// handle XMLType case where an oracle.jdbc.driver.OracleSQLXML instance was returned
SQLXML sqlXml = (SQLXML) fieldValue;
try {
String str = sqlXml.getString();
sqlXml.free();
// Oracle 12c appends a \n character to the xml string
fieldValue = str.endsWith("\n") ? str.substring(0, str.length() - 1) : str;
} catch (SQLException e) {
log.logThrowable(SessionLog.FINE, SessionLog.DBWS, e);
}
} else if (fieldValue.getClass().getName().equalsIgnoreCase(ORACLEOPAQUE_STR)) {
// handle XMLType case where an oracle.sql.OPAQUE instance was returned
try {
Class<?> oracleOPAQUE;
Class<?> xmlTypeFactoryClass;
Constructor<?> xmlTypeFactoryConstructor;
Object xmlTypeFactory;
Method getStringMethod;
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
oracleOPAQUE = AccessController.doPrivileged(new PrivilegedClassForName<>(IORACLEOPAQUE_STR, true, this.getClass().getClassLoader()));
xmlTypeFactoryClass = AccessController.doPrivileged(new PrivilegedClassForName<>(XMLTYPEFACTORY_STR, true, this.getClass().getClassLoader()));
xmlTypeFactoryConstructor = AccessController.doPrivileged(new PrivilegedGetConstructorFor<>(xmlTypeFactoryClass, new Class<?>[0], true));
xmlTypeFactory = AccessController.doPrivileged(new PrivilegedInvokeConstructor<>(xmlTypeFactoryConstructor, new Object[0]));
getStringMethod = AccessController.doPrivileged(new PrivilegedGetDeclaredMethod(xmlTypeFactoryClass, GETSTRING_METHOD, new Class<?>[] { oracleOPAQUE }));
fieldValue = AccessController.doPrivileged(new PrivilegedMethodInvoker<>(getStringMethod, xmlTypeFactory, new Object[] { fieldValue }));
} else {
oracleOPAQUE = PrivilegedAccessHelper.getClassForName(IORACLEOPAQUE_STR, false, this.getClass().getClassLoader());
xmlTypeFactoryClass = PrivilegedAccessHelper.getClassForName(XMLTYPEFACTORY_STR, true, this.getClass().getClassLoader());
xmlTypeFactoryConstructor = PrivilegedAccessHelper.getConstructorFor(xmlTypeFactoryClass, new Class<?>[0], true);
xmlTypeFactory = PrivilegedAccessHelper.invokeConstructor(xmlTypeFactoryConstructor, new Object[0]);
getStringMethod = PrivilegedAccessHelper.getDeclaredMethod(xmlTypeFactoryClass, GETSTRING_METHOD, new Class<?>[] { oracleOPAQUE });
fieldValue = PrivilegedAccessHelper.invokeMethod(getStringMethod, xmlTypeFactory, new Object[] { fieldValue });
}
} catch (ReflectiveOperationException | PrivilegedActionException e) {
// if the required resources are not available there's nothing we can do...
log.logThrowable(SessionLog.FINE, SessionLog.DBWS, e);
}
}
String elementName;
if (field.getName() == null || (elementName = sqlToXmlName(field.getName())).equals(EMPTY_STR)) {
// return arg from stored function has no name
elementName = RESULT_STR;
}
Element columnElement = TEMP_DOC.createElement(elementName);
rowElement.appendChild(columnElement);
String fieldValueString = fieldValue.toString();
// handle binary content - attachments dealt with in invoke() above
if (result.getType().equals(BASE_64_BINARY_QNAME)) {
fieldValueString = Helper.buildHexStringFromBytes(Base64.base64Encode((byte[]) fieldValue));
columnElement.setAttributeNS(XMLNS_URL, XSD_STR, SCHEMA_URL);
columnElement.setAttributeNS(XMLNS_URL, XSI_STR, SCHEMA_INSTANCE_URL);
columnElement.setAttributeNS(SCHEMA_INSTANCE_URL, XSITYPE_STR, BASE64_BINARY_STR);
}
columnElement.appendChild(TEMP_DOC.createTextNode(fieldValueString));
}
}
simpleXMLFormatModel.simpleXML.add(rowElement);
}
xmlRoot.setObject(simpleXMLFormatModel);
return xmlRoot;
}
Aggregations