use of org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass in project eclipselink by eclipse-ee4j.
the class SAXUnmarshallerHandler method startElement.
@Override
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
try {
String name;
if (localName == null || localName.length() == 0) {
name = qName;
} else {
name = localName;
}
XPathQName rootQName;
if (namespaceURI == null || namespaceURI.length() == 0) {
rootQName = new XPathQName(name, xmlReader.isNamespaceAware());
} else {
rootQName = new XPathQName(namespaceURI, name, xmlReader.isNamespaceAware());
}
Class<?> primitiveWrapperClass = null;
Descriptor xmlDescriptor = xmlContext.getDescriptor(rootQName);
// if no match on root element look for xsi:type
if (xmlDescriptor == null || (unmarshaller.getMediaType() == MediaType.APPLICATION_JSON && unmarshaller.getJsonTypeConfiguration().getJsonTypeAttributeName() != null && !Constants.SCHEMA_TYPE_ATTRIBUTE.equals(unmarshaller.getJsonTypeConfiguration().getJsonTypeAttributeName()))) {
boolean isPrimitiveType = false;
String type = null;
if (xmlReader.isNamespaceAware()) {
type = atts.getValue(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, Constants.SCHEMA_TYPE_ATTRIBUTE);
} else if (unmarshaller.getMediaType() != MediaType.APPLICATION_JSON || unmarshaller.getJsonTypeConfiguration().useJsonTypeCompatibility()) {
type = atts.getValue(Constants.EMPTY_STRING, Constants.SCHEMA_TYPE_ATTRIBUTE);
} else if (unmarshaller.getMediaType() == MediaType.APPLICATION_JSON && unmarshaller.getJsonTypeConfiguration().getJsonTypeAttributeName() != null) {
type = atts.getValue(Constants.EMPTY_STRING, unmarshaller.getJsonTypeConfiguration().getJsonTypeAttributeName());
}
if (null != type) {
XPathFragment typeFragment = new XPathFragment(type, xmlReader.getNamespaceSeparator(), xmlReader.isNamespaceAware());
// set the prefix using a reverse key lookup by uri value on namespaceMap
if (xmlReader.isNamespaceAware() && null != unmarshalNamespaceResolver) {
typeFragment.setNamespaceURI(unmarshalNamespaceResolver.getNamespaceURI(typeFragment.getPrefix()));
}
Descriptor lookupDescriptor = xmlContext.getDescriptorByGlobalType(typeFragment);
if (lookupDescriptor == null) {
QName lookupQName = null;
if (typeFragment.getNamespaceURI() == null) {
lookupQName = new QName(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI, typeFragment.getLocalName());
} else {
lookupQName = new QName(typeFragment.getNamespaceURI(), typeFragment.getLocalName());
}
// check to see if type attribute represents simple type
if (null == session) {
session = (CoreAbstractSession) xmlContext.getSession();
}
ConversionManager conversionManager = (ConversionManager) session.getDatasourcePlatform().getConversionManager();
primitiveWrapperClass = conversionManager.javaType(lookupQName);
} else {
// found descriptor based on type attribute
xmlDescriptor = lookupDescriptor;
session = xmlContext.getSession(xmlDescriptor);
}
}
} else {
if (null != xmlDescriptor.getDefaultRootElementField() && !unmarshaller.isResultAlwaysXMLRoot()) {
String descLocalName = xmlDescriptor.getDefaultRootElementField().getXPathFragment().getLocalName();
if (descLocalName != null && descLocalName.equals(localName)) {
String descUri = xmlDescriptor.getDefaultRootElementField().getXPathFragment().getNamespaceURI();
if (!xmlReader.isNamespaceAware() || (xmlReader.isNamespaceAware() && ((namespaceURI == null && descUri == null) || (namespaceURI != null && namespaceURI.length() == 0 && descUri == null) || (namespaceURI != null && namespaceURI.equals(descUri))))) {
// found a descriptor based on root element then know we won't need to wrap in an XMLRoot
shouldWrap = false;
}
}
}
if (xmlDescriptor.hasInheritance()) {
// if descriptor has inheritance check class indicator
session = xmlContext.getSession(xmlDescriptor);
UnmarshalRecord tmpUnmarshalRecord = new UnmarshalRecordImpl(null);
tmpUnmarshalRecord.setUnmarshaller(unmarshaller);
tmpUnmarshalRecord.setUnmarshalNamespaceResolver(unmarshalNamespaceResolver);
tmpUnmarshalRecord.setXMLReader(this.getXMLReader());
tmpUnmarshalRecord.setAttributes(atts);
Class<?> classValue = xmlDescriptor.getInheritancePolicy().classFromRow(new org.eclipse.persistence.oxm.record.UnmarshalRecord(tmpUnmarshalRecord), session);
if (classValue == null) {
// no xsi:type attribute - look for type indicator on the default root element
QName leafElementType = xmlDescriptor.getDefaultRootElementType();
// if we have a user-set type, try to get the class from the inheritance policy
if (leafElementType != null) {
Object indicator = xmlDescriptor.getInheritancePolicy().getClassIndicatorMapping().get(leafElementType);
if (indicator != null) {
classValue = (Class) indicator;
}
}
}
if (classValue != null) {
xmlDescriptor = (Descriptor) session.getDescriptor(classValue);
} else {
// sure it is non-abstract
if (Modifier.isAbstract(xmlDescriptor.getJavaClass().getModifiers())) {
// need to throw an exception here
throw DescriptorException.missingClassIndicatorField(tmpUnmarshalRecord, (org.eclipse.persistence.oxm.XMLDescriptor) xmlDescriptor.getInheritancePolicy().getDescriptor());
}
}
}
}
if (null == xmlDescriptor) {
// check for a cached object and look for descriptor by class
Object obj = this.xmlReader.getCurrentObject(session, null);
if (obj != null) {
xmlDescriptor = (Descriptor) xmlContext.getSession(obj.getClass()).getDescriptor(obj.getClass());
}
}
if (null == xmlDescriptor && primitiveWrapperClass == null) {
if (!this.keepAsElementPolicy.isKeepNoneAsElement()) {
this.documentBuilder = new SAXDocumentBuilder();
documentBuilder.startDocument();
// start any prefixes that have already been started
for (String prefix : this.unmarshalNamespaceResolver.getPrefixes()) {
documentBuilder.startPrefixMapping(prefix, this.unmarshalNamespaceResolver.getNamespaceURI(prefix));
}
documentBuilder.startElement(namespaceURI, localName, qName, atts);
this.xmlReader.setContentHandler(documentBuilder);
return;
}
Class<?> unmappedContentHandlerClass = unmarshaller.getUnmappedContentHandlerClass();
if (null == unmappedContentHandlerClass) {
throw XMLMarshalException.noDescriptorWithMatchingRootElement(rootQName.toString());
} else {
UnmappedContentHandler unmappedContentHandler;
try {
PrivilegedNewInstanceFromClass privilegedNewInstanceFromClass = new PrivilegedNewInstanceFromClass(unmappedContentHandlerClass);
unmappedContentHandler = (UnmappedContentHandler) privilegedNewInstanceFromClass.run();
} catch (ClassCastException e) {
throw XMLMarshalException.unmappedContentHandlerDoesntImplement(e, unmappedContentHandlerClass.getName());
} catch (IllegalAccessException e) {
throw XMLMarshalException.errorInstantiatingUnmappedContentHandler(e, unmappedContentHandlerClass.getName());
} catch (InstantiationException e) {
throw XMLMarshalException.errorInstantiatingUnmappedContentHandler(e, unmappedContentHandlerClass.getName());
}
UnmappedContentHandlerWrapper unmappedContentHandlerWrapper = new UnmappedContentHandlerWrapper(unmappedContentHandler, this);
unmappedContentHandler.startElement(namespaceURI, localName, qName, atts);
xmlReader.setContentHandler(unmappedContentHandler);
setObject(unmappedContentHandlerWrapper.getCurrentObject());
return;
}
}
if (xmlDescriptor == null && primitiveWrapperClass != null) {
session = xmlContext.getSession((Descriptor) null);
rootRecord = unmarshaller.createRootUnmarshalRecord(primitiveWrapperClass);
rootRecord.setSession((CoreAbstractSession) unmarshaller.getContext().getSession());
} else {
if (session == null) {
session = xmlContext.getSession(xmlDescriptor);
}
rootRecord = unmarshaller.createUnmarshalRecord(xmlDescriptor, session);
}
this.descriptor = xmlDescriptor;
rootRecord.setUnmarshaller(this.unmarshaller);
rootRecord.setXMLReader(this.getXMLReader());
if (locator != null) {
rootRecord.setDocumentLocator(xmlReader.getLocator());
}
rootRecord.setAttributes(atts);
boolean hasNilAttribute = (atts != null && null != atts.getValue(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, Constants.SCHEMA_NIL_ATTRIBUTE));
rootRecord.setNil(isNil || hasNilAttribute);
rootRecord.setUnmarshalNamespaceResolver(unmarshalNamespaceResolver);
rootRecord.startDocument();
rootRecord.initializeRecord(null);
xmlReader.setContentHandler(rootRecord);
xmlReader.setLexicalHandler(rootRecord);
Object attributeGroup = this.unmarshaller.getUnmarshalAttributeGroup();
if (attributeGroup != null) {
if (attributeGroup.getClass() == CoreClassConstants.STRING) {
CoreAttributeGroup group = descriptor.getAttributeGroup((String) attributeGroup);
if (group != null) {
rootRecord.setUnmarshalAttributeGroup(group);
} else {
// Error
}
} else if (attributeGroup instanceof CoreAttributeGroup) {
rootRecord.setUnmarshalAttributeGroup((CoreAttributeGroup) attributeGroup);
} else {
// Error case
}
}
rootRecord.startElement(namespaceURI, localName, qName, atts);
// if we located the descriptor via xsi:type attribute, create and
// return an XMLRoot object
} catch (EclipseLinkException e) {
if (null == xmlReader.getErrorHandler()) {
throw e;
} else {
SAXParseException saxParseException = new SAXParseException(null, null, null, 0, 0, e);
xmlReader.getErrorHandler().error(saxParseException);
}
}
}
use of org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass in project eclipselink by eclipse-ee4j.
the class UnmarshalRecordImpl method startUnmappedElement.
public void startUnmappedElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
if (xmlReader.getMediaType().isApplicationXML() && null == selfRecords && !isSelfRecord) {
ErrorHandler errorHandler = xmlReader.getErrorHandler();
// Bug 452584 - check if a warning exception should be generated when an unmapped element is encountered
if (null != errorHandler && unmarshaller.shouldWarnOnUnmappedElement()) {
StringBuilder messageBuilder = new StringBuilder("unexpected element (uri:\"");
if (null != namespaceURI) {
messageBuilder.append(namespaceURI);
}
messageBuilder.append("\", local:\"");
messageBuilder.append(localName);
messageBuilder.append("\"). Expected elements are ");
List<XPathNode> nonAttributeChildren = xPathNode.getNonAttributeChildren();
if (nonAttributeChildren == null || nonAttributeChildren.size() == 0) {
messageBuilder.append("(none)");
} else {
for (int x = 0, size = nonAttributeChildren.size(); x < size; x++) {
XPathFragment nonAttributeChildXPathFragment = nonAttributeChildren.get(x).getXPathFragment();
messageBuilder.append("<{");
String nonAttributeChildXPathFragmentNamespaceURI = nonAttributeChildXPathFragment.getNamespaceURI();
if (null != nonAttributeChildXPathFragmentNamespaceURI) {
messageBuilder.append(nonAttributeChildXPathFragmentNamespaceURI);
}
messageBuilder.append('}');
messageBuilder.append(nonAttributeChildXPathFragment.getLocalName());
messageBuilder.append('>');
if (x < size - 1) {
messageBuilder.append(',');
}
}
}
errorHandler.warning(new SAXParseException(messageBuilder.toString(), getDocumentLocator()));
}
}
if ((null != selfRecords) || (null == xmlReader) || isSelfRecord()) {
if (-1 == unmappedLevel) {
this.unmappedLevel = this.levelIndex;
}
return;
}
Class<?> unmappedContentHandlerClass = unmarshaller.getUnmappedContentHandlerClass();
UnmappedContentHandler unmappedContentHandler;
if (null == unmappedContentHandlerClass) {
unmappedContentHandler = DEFAULT_UNMAPPED_CONTENT_HANDLER;
} else {
try {
PrivilegedNewInstanceFromClass privilegedNewInstanceFromClass = new PrivilegedNewInstanceFromClass(unmappedContentHandlerClass);
unmappedContentHandler = (UnmappedContentHandler) privilegedNewInstanceFromClass.run();
} catch (ClassCastException e) {
throw XMLMarshalException.unmappedContentHandlerDoesntImplement(e, unmappedContentHandlerClass.getName());
} catch (IllegalAccessException e) {
throw XMLMarshalException.errorInstantiatingUnmappedContentHandler(e, unmappedContentHandlerClass.getName());
} catch (InstantiationException e) {
throw XMLMarshalException.errorInstantiatingUnmappedContentHandler(e, unmappedContentHandlerClass.getName());
}
}
UnmappedContentHandlerWrapper unmappedContentHandlerWrapper = new UnmappedContentHandlerWrapper(this, unmappedContentHandler);
unmappedContentHandlerWrapper.startElement(namespaceURI, localName, qName, atts);
xmlReader.setContentHandler(unmappedContentHandlerWrapper);
xmlReader.setLexicalHandler(unmappedContentHandlerWrapper);
}
use of org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass in project eclipselink by eclipse-ee4j.
the class AbstractColumnMapping method convertClassNamesToClasses.
/**
* INTERNAL:
* Convert all the class-name-based settings in this mapping to actual class-based settings
* This method is implemented by subclasses as necessary.
*/
@Override
public void convertClassNamesToClasses(ClassLoader classLoader) {
super.convertClassNamesToClasses(classLoader);
// Field may have a type name that needs to be initialize.
if (field != null) {
field.convertClassNamesToClasses(classLoader);
}
// Convert and any Converter class names.
convertConverterClassNamesToClasses(converter, classLoader);
// Instantiate any custom converter class
if (converterClassName != null) {
Class<?> converterClass;
Converter converter;
try {
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
try {
converterClass = AccessController.doPrivileged(new PrivilegedClassForName<>(converterClassName, true, classLoader));
} catch (PrivilegedActionException exception) {
throw ValidationException.classNotFoundWhileConvertingClassNames(converterClassName, exception.getException());
}
try {
converter = (Converter) AccessController.doPrivileged(new PrivilegedNewInstanceFromClass(converterClass));
} catch (PrivilegedActionException exception) {
throw ValidationException.classNotFoundWhileConvertingClassNames(converterClassName, exception.getException());
}
} else {
converterClass = PrivilegedAccessHelper.getClassForName(converterClassName, true, classLoader);
converter = (Converter) PrivilegedAccessHelper.newInstanceFromClass(converterClass);
}
} catch (ClassNotFoundException exc) {
throw ValidationException.classNotFoundWhileConvertingClassNames(converterClassName, exc);
} catch (Exception e) {
// Catches IllegalAccessException and InstantiationException
throw ValidationException.classNotFoundWhileConvertingClassNames(converterClassName, e);
}
setConverter(converter);
}
}
use of org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass 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.PrivilegedNewInstanceFromClass in project eclipselink by eclipse-ee4j.
the class AbstractSession method buildDefaultQueryBuilder.
/**
* INTERNAL
* Build the JPQL builder based on session properties.
*/
protected JPAQueryBuilder buildDefaultQueryBuilder() {
String queryBuilderClassName = (String) getProperty(PersistenceUnitProperties.JPQL_PARSER);
if (queryBuilderClassName == null) {
queryBuilderClassName = "org.eclipse.persistence.internal.jpa.jpql.HermesParser";
// queryBuilderClassName = "org.eclipse.persistence.queries.ANTLRQueryBuilder";
}
String validation = (String) getProperty(PersistenceUnitProperties.JPQL_VALIDATION);
JPAQueryBuilder builder = null;
try {
Class<? extends JPAQueryBuilder> parserClass = null;
// Use Class.forName not thread class loader to avoid class loader issues.
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
parserClass = AccessController.doPrivileged(new PrivilegedClassForName<>(queryBuilderClassName));
builder = AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(parserClass));
} else {
parserClass = PrivilegedAccessHelper.getClassForName(queryBuilderClassName);
builder = PrivilegedAccessHelper.newInstanceFromClass(parserClass);
}
} catch (Exception e) {
throw new IllegalStateException("Could not load the JPQL parser class.", /* TODO: Localize string */
e);
}
if (validation != null) {
builder.setValidationLevel(validation);
}
return builder;
}
Aggregations