Search in sources :

Example 1 with PrivilegedClassForName

use of org.eclipse.persistence.internal.security.PrivilegedClassForName in project eclipselink by eclipse-ee4j.

the class EntityManagerSetupImpl method addBeanValidationListeners.

/**
 * If Bean Validation is enabled, bootstraps Bean Validation on descriptors.
 * @param puProperties merged properties for this persistence unit
 */
private void addBeanValidationListeners(Map puProperties, ClassLoader appClassLoader) {
    ValidationMode validationMode = getValidationMode(persistenceUnitInfo, puProperties);
    if (validationMode == ValidationMode.AUTO || validationMode == ValidationMode.CALLBACK) {
        // BeanValidationInitializationHelper contains static reference to jakarta.validation.* classes. We need to support
        // environment where these classes are not available.
        // To guard against some vms that eagerly resolve, reflectively load class to prevent any static reference to it
        String helperClassName = "org.eclipse.persistence.internal.jpa.deployment.BeanValidationInitializationHelper$BeanValidationInitializationHelperImpl";
        Class<?> helperClass;
        try {
            if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
                try {
                    helperClass = AccessController.doPrivileged(new PrivilegedClassForName<>(helperClassName, true, appClassLoader));
                } catch (Throwable t) {
                    // Try the ClassLoader that loaded Eclipselink classes
                    ClassLoader eclipseLinkClassLoader = EntityManagerSetupImpl.class.getClassLoader();
                    helperClass = AccessController.doPrivileged(new PrivilegedClassForName<>(helperClassName, true, eclipseLinkClassLoader));
                }
            } else {
                try {
                    helperClass = PrivilegedAccessHelper.getClassForName(helperClassName, true, appClassLoader);
                } catch (Throwable t) {
                    // Try the ClassLoader that loaded Eclipselink classes
                    ClassLoader eclipseLinkClassLoader = EntityManagerSetupImpl.class.getClassLoader();
                    helperClass = PrivilegedAccessHelper.getClassForName(helperClassName, true, eclipseLinkClassLoader);
                }
            }
            BeanValidationInitializationHelper beanValidationInitializationHelper = (BeanValidationInitializationHelper) helperClass.getConstructor().newInstance();
            beanValidationInitializationHelper.bootstrapBeanValidation(puProperties, session, appClassLoader);
        } catch (Throwable e) {
            // Catching Throwable to catch any linkage errors on vms that resolve eagerly
            if (validationMode == ValidationMode.CALLBACK) {
                throw PersistenceUnitLoadingException.exceptionObtainingRequiredBeanValidatorFactory(e);
            }
            // else validationMode == ValidationMode.AUTO. Log a message, Ignore the exception
            this.session.log(SessionLog.FINEST, SessionLog.JPA, "validation_factory_not_initialized", new Object[] { e.getMessage() });
        }
    }
}
Also used : DynamicClassLoader(org.eclipse.persistence.dynamic.DynamicClassLoader) BeanValidationInitializationHelper(org.eclipse.persistence.internal.jpa.deployment.BeanValidationInitializationHelper) ValidationMode(jakarta.persistence.ValidationMode) EntityManagerFactoryProvider.getConfigPropertyAsString(org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.getConfigPropertyAsString) PrivilegedClassForName(org.eclipse.persistence.internal.security.PrivilegedClassForName)

Example 2 with PrivilegedClassForName

use of org.eclipse.persistence.internal.security.PrivilegedClassForName in project eclipselink by eclipse-ee4j.

the class DBWSBuilder method getConnection.

public Connection getConnection() {
    if (conn == null) {
        String driverClassName = getDriver();
        try {
            @SuppressWarnings("unused") Class<?> driverClass = null;
            if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
                driverClass = AccessController.doPrivileged(new PrivilegedClassForName<>(driverClassName));
            } else {
                driverClass = PrivilegedAccessHelper.getClassForName(driverClassName);
            }
            Properties props = new Properties();
            props.put("user", getUsername());
            props.put("password", getPassword());
            if (getPlatformClassname().contains("MySQL")) {
                props.put("useInformationSchema", "true");
            }
            conn = DriverManager.getConnection(getUrl(), props);
        } catch (Exception e) {
            logMessage(SEVERE, "JDBC driver error: " + driverClassName, e);
        }
    }
    return conn;
}
Also used : Properties(java.util.Properties) FileNotFoundException(java.io.FileNotFoundException) WSDLException(javax.wsdl.WSDLException) PrivilegedClassForName(org.eclipse.persistence.internal.security.PrivilegedClassForName)

Example 3 with PrivilegedClassForName

use of org.eclipse.persistence.internal.security.PrivilegedClassForName in project eclipselink by eclipse-ee4j.

the class MissingDescriptorListener method missingDescriptor.

@Override
public void missingDescriptor(SessionEvent event) {
    String name = ((Class<?>) event.getResult()).getName();
    DatabaseSession session = (DatabaseSession) ((AbstractSession) event.getSession()).getRootSession(null);
    Project project = session.getProject();
    String namespaceXPath = "";
    NamespaceResolverWithPrefixes namespaceResolverWithPrefixes;
    if (project instanceof NamespaceResolvableProject) {
        // this should always be true
        NamespaceResolvableProject nrpProject = (NamespaceResolvableProject) project;
        namespaceXPath = nrpProject.getPrimaryNamespaceXPath();
        namespaceResolverWithPrefixes = nrpProject.getNamespaceResolver();
    } else {
        // this shouldn't happen - but if it does, build a new (empty) NamespaceResolverWithPrefixes
        namespaceResolverWithPrefixes = new NamespaceResolverWithPrefixes();
    }
    DirectToXMLTypeMappingHelper.getInstance().addXDBDescriptors(name, (DatabaseSessionImpl) session, namespaceResolverWithPrefixes);
    if (name.equals(EIS_DESCRIPTOR_CLASS) || name.equals(XML_INTERACTION_CLASS) || name.equals(EIS_LOGIN_CLASS)) {
        try {
            Class<?> javaClass;
            if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
                try {
                    javaClass = AccessController.doPrivileged(new PrivilegedClassForName<>(XML_INTERACTION_CLASS));
                } catch (PrivilegedActionException ex) {
                    if (ex.getCause() instanceof ClassNotFoundException) {
                        throw (ClassNotFoundException) ex.getCause();
                    }
                    throw (RuntimeException) ex.getCause();
                }
            } else {
                javaClass = PrivilegedAccessHelper.getClassForName(XML_INTERACTION_CLASS);
            }
            session.getDescriptor(Call.class).getInheritancePolicy().addClassIndicator(javaClass, namespaceXPath + "xml-interaction");
        } catch (Exception classLoadFailure) {
            throw ValidationException.fatalErrorOccurred(classLoadFailure);
        }
        session.addDescriptors(new EISObjectPersistenceXMLProject(namespaceResolverWithPrefixes));
    }
    if (name.equals(XML_BINARY_MAPPING_CLASS) || name.equals(XML_BINARY_COLLECTION_MAPPING_CLASS)) {
        session.addDescriptors(new OXMObjectPersistenceRuntimeXMLProject(namespaceResolverWithPrefixes));
    }
}
Also used : EISObjectPersistenceXMLProject(org.eclipse.persistence.eis.EISObjectPersistenceXMLProject) PrivilegedActionException(java.security.PrivilegedActionException) PrivilegedActionException(java.security.PrivilegedActionException) EISObjectPersistenceXMLProject(org.eclipse.persistence.eis.EISObjectPersistenceXMLProject) PrivilegedClassForName(org.eclipse.persistence.internal.security.PrivilegedClassForName)

Example 4 with PrivilegedClassForName

use of org.eclipse.persistence.internal.security.PrivilegedClassForName 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);
    }
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) PrivilegedNewInstanceFromClass(org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass) Converter(org.eclipse.persistence.mappings.converters.Converter) PrivilegedActionException(java.security.PrivilegedActionException) ValidationException(org.eclipse.persistence.exceptions.ValidationException) PrivilegedClassForName(org.eclipse.persistence.internal.security.PrivilegedClassForName)

Example 5 with PrivilegedClassForName

use of org.eclipse.persistence.internal.security.PrivilegedClassForName in project eclipselink by eclipse-ee4j.

the class DirectCollectionMapping 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);
    // Tell the direct field to convert any class names (type name).
    directField.convertClassNamesToClasses(classLoader);
    // Convert and any Converter class names.
    convertConverterClassNamesToClasses(valueConverter, classLoader);
    // Instantiate any custom converter class
    if (valueConverterClassName != null) {
        Class<?> valueConverterClass;
        Converter valueConverter;
        try {
            if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
                try {
                    valueConverterClass = AccessController.doPrivileged(new PrivilegedClassForName<>(valueConverterClassName, true, classLoader));
                } catch (PrivilegedActionException exception) {
                    throw ValidationException.classNotFoundWhileConvertingClassNames(valueConverterClassName, exception.getException());
                }
                try {
                    valueConverter = (Converter) AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(valueConverterClass));
                } catch (PrivilegedActionException exception) {
                    throw ValidationException.classNotFoundWhileConvertingClassNames(valueConverterClassName, exception.getException());
                }
            } else {
                valueConverterClass = PrivilegedAccessHelper.getClassForName(valueConverterClassName, true, classLoader);
                valueConverter = (Converter) PrivilegedAccessHelper.newInstanceFromClass(valueConverterClass);
            }
        } catch (ClassNotFoundException exc) {
            throw ValidationException.classNotFoundWhileConvertingClassNames(valueConverterClassName, exc);
        } catch (Exception e) {
            // Catches IllegalAccessException and InstantiationException
            throw ValidationException.classNotFoundWhileConvertingClassNames(valueConverterClassName, e);
        }
        setValueConverter(valueConverter);
    }
    // Check if the attribute classification is set (either directly or through a type conversion converter)
    if (attributeClassification == null) {
        // Look for an attribute classification name
        if (attributeClassificationName != null) {
            try {
                if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
                    try {
                        attributeClassification = AccessController.doPrivileged(new PrivilegedClassForName<>(attributeClassificationName, true, classLoader));
                    } catch (PrivilegedActionException pae) {
                        throw ValidationException.classNotFoundWhileConvertingClassNames(attributeClassificationName, pae.getException());
                    }
                } else {
                    attributeClassification = PrivilegedAccessHelper.getClassForName(attributeClassificationName, true, classLoader);
                }
            } catch (Exception exception) {
                throw ValidationException.classNotFoundWhileConvertingClassNames(attributeClassificationName, exception);
            }
        } else {
            // Still nothing, default to the type from the direct field.
            attributeClassification = getDirectField().getType();
        }
    }
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) Converter(org.eclipse.persistence.mappings.converters.Converter) TypeConversionConverter(org.eclipse.persistence.mappings.converters.TypeConversionConverter) SerializedObjectConverter(org.eclipse.persistence.mappings.converters.SerializedObjectConverter) ObjectTypeConverter(org.eclipse.persistence.mappings.converters.ObjectTypeConverter) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException) ValidationException(org.eclipse.persistence.exceptions.ValidationException) ConversionException(org.eclipse.persistence.exceptions.ConversionException) PrivilegedActionException(java.security.PrivilegedActionException) QueryException(org.eclipse.persistence.exceptions.QueryException) PrivilegedClassForName(org.eclipse.persistence.internal.security.PrivilegedClassForName)

Aggregations

PrivilegedClassForName (org.eclipse.persistence.internal.security.PrivilegedClassForName)15 PrivilegedActionException (java.security.PrivilegedActionException)11 PrivilegedGetContextClassLoader (org.eclipse.persistence.internal.security.PrivilegedGetContextClassLoader)5 PrivilegedGetMethod (org.eclipse.persistence.internal.security.PrivilegedGetMethod)5 Method (java.lang.reflect.Method)4 ValidationException (org.eclipse.persistence.exceptions.ValidationException)3 PrivilegedNewInstanceFromClass (org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass)3 DatabaseException (org.eclipse.persistence.exceptions.DatabaseException)2 QueryException (org.eclipse.persistence.exceptions.QueryException)2 Converter (org.eclipse.persistence.mappings.converters.Converter)2 ObjectRelationalDatabaseField (org.eclipse.persistence.mappings.structures.ObjectRelationalDatabaseField)2 ValidationMode (jakarta.persistence.ValidationMode)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 CharArrayReader (java.io.CharArrayReader)1 FileNotFoundException (java.io.FileNotFoundException)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 Calendar (java.util.Calendar)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1