Search in sources :

Example 1 with PrivilegedGetDeclaredFields

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

the class PersistenceContext method removeAttribute.

/**
 * Removes the attribute.
 *
 * @param tenantId the tenant id
 * @param entityName the entity name
 * @param id the id
 * @param attribute the attribute
 * @param partner the partner
 * @return the object
 */
@SuppressWarnings({ "rawtypes" })
public Object removeAttribute(Map<String, String> tenantId, String entityName, Object id, String attribute, String listItemId, Object entity, String partner) {
    EntityManager em = getEmf().createEntityManager(tenantId);
    String fieldName = null;
    try {
        Class<?> clazz = getClass(entityName);
        ClassDescriptor descriptor = getServerSession().getClassDescriptor(clazz);
        DatabaseMapping mapping = descriptor.getMappingForAttributeName(attribute);
        if (mapping == null) {
            return null;
        } else if (mapping.isObjectReferenceMapping() || mapping.isCollectionMapping()) {
            DatabaseMapping partnerMapping = null;
            Object originalAttributeValue = null;
            ClassDescriptor referenceDescriptor = mapping.getReferenceDescriptor();
            if (partner != null) {
                partnerMapping = referenceDescriptor.getMappingForAttributeName(partner);
                if (partnerMapping == null) {
                    return null;
                }
            }
            Field[] fields;
            if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
                fields = AccessController.doPrivileged(new PrivilegedGetDeclaredFields(clazz));
            } else {
                fields = PrivilegedAccessHelper.getDeclaredFields(clazz);
            }
            for (Field field : fields) {
                fieldName = field.getName();
                if (fieldName.equals(attribute)) {
                    try {
                        // call clear on this collection
                        Object attributeValue = getAttribute(entity, attribute);
                        originalAttributeValue = attributeValue;
                        if (attributeValue instanceof Collection) {
                            if (listItemId == null) {
                                // no collection member specified in request (listItemId=null) remove entire collection
                                ((Collection) attributeValue).clear();
                            } else {
                                Object realListItemId = IdHelper.buildId(this, referenceDescriptor.getAlias(), listItemId);
                                Object member = this.find(referenceDescriptor.getAlias(), realListItemId);
                                ((Collection) attributeValue).remove(member);
                            }
                        }
                        break;
                    } catch (Exception e) {
                        e.printStackTrace();
                        return null;
                    }
                }
            }
            transaction.beginTransaction(em);
            entity = em.merge(entity);
            removeMappingValueFromObject(entity, originalAttributeValue, mapping, partnerMapping);
            transaction.commitTransaction(em);
            return entity;
        }
        return null;
    } catch (Exception e) {
        JPARSLogger.exception(getSessionLog(), "exception_while_removing_attribute", new Object[] { fieldName, entityName, getName() }, e);
        transaction.rollbackTransaction(em);
        return null;
    } finally {
        em.close();
    }
}
Also used : Field(java.lang.reflect.Field) EntityManager(jakarta.persistence.EntityManager) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) ReportQueryResultCollection(org.eclipse.persistence.jpa.rs.util.list.ReportQueryResultCollection) Collection(java.util.Collection) ReadAllQueryResultCollection(org.eclipse.persistence.jpa.rs.util.list.ReadAllQueryResultCollection) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) PrivilegedGetDeclaredFields(org.eclipse.persistence.internal.security.PrivilegedGetDeclaredFields) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) JPARSException(org.eclipse.persistence.jpa.rs.exceptions.JPARSException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) RollbackException(jakarta.persistence.RollbackException) JAXBException(jakarta.xml.bind.JAXBException)

Example 2 with PrivilegedGetDeclaredFields

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

the class EntityManagerSetupImpl method preInitializeCanonicalMetamodel.

/**
 * INTERNAL:
 * First phase of canonical metamodel initialization.  For each class the metamodel is aware of, check
 * for a canonical metamodel class and initialize each attribute in it with a proxy that can cause the
 * rest of the metamodel population.  Attributes are found reflectively rather than through the metamodel
 * to avoid having to further initialize the metamodel.
 */
public void preInitializeCanonicalMetamodel(EntityManagerFactoryImpl factory) {
    // 338837: verify that the collection is not empty - this would mean entities did not make it into the search path
    if (null == metaModel.getManagedTypes() || metaModel.getManagedTypes().isEmpty()) {
        getSession().log(SessionLog.FINER, SessionLog.METAMODEL, "metamodel_type_collection_empty");
    }
    for (ManagedType manType : metaModel.getManagedTypes()) {
        boolean classInitialized = false;
        String className = MetadataHelper.getQualifiedCanonicalName(((ManagedTypeImpl) manType).getJavaTypeName(), getSession());
        try {
            Class<?> clazz = this.getSession().getDatasourcePlatform().convertObject(className, ClassConstants.CLASS);
            classInitialized = true;
            this.getSession().log(SessionLog.FINER, SessionLog.METAMODEL, "metamodel_canonical_model_class_found", className);
            Field[] fields = null;
            if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
                fields = AccessController.doPrivileged(new PrivilegedGetDeclaredFields(clazz));
            } else {
                fields = PrivilegedAccessHelper.getDeclaredFields(clazz);
            }
            for (Field attribute : fields) {
                if (Attribute.class.isAssignableFrom(attribute.getType())) {
                    Object assignedAttribute = null;
                    if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
                        assignedAttribute = AccessController.doPrivileged(new PrivilegedGetValueFromField(attribute, null));
                    } else {
                        assignedAttribute = PrivilegedAccessHelper.getValueFromField(attribute, null);
                    }
                    AttributeProxyImpl proxy = null;
                    if (assignedAttribute == null) {
                        if (SingularAttribute.class.isAssignableFrom(attribute.getType())) {
                            proxy = new SingularAttributeProxyImpl();
                        } else if (MapAttribute.class.isAssignableFrom(attribute.getType())) {
                            proxy = new MapAttributeProxyImpl();
                        } else if (SetAttribute.class.isAssignableFrom(attribute.getType())) {
                            proxy = new SetAttributeProxyImpl();
                        } else if (ListAttribute.class.isAssignableFrom(attribute.getType())) {
                            proxy = new ListAttributeProxyImpl();
                        } else if (CollectionAttribute.class.isAssignableFrom(attribute.getType())) {
                            proxy = new CollectionAttributeProxyImpl();
                        }
                        if (proxy != null) {
                            attribute.setAccessible(true);
                            attribute.set(null, proxy);
                        }
                    } else if (assignedAttribute instanceof AttributeProxyImpl) {
                        proxy = (AttributeProxyImpl) assignedAttribute;
                    }
                    if (proxy != null) {
                        proxy.addFactory(factory);
                    }
                }
            }
        } catch (PrivilegedActionException pae) {
            getSession().logThrowable(SessionLog.FINEST, SessionLog.METAMODEL, pae);
        } catch (IllegalAccessException iae) {
            getSession().logThrowable(SessionLog.FINEST, SessionLog.METAMODEL, iae);
        } catch (ConversionException ce) {
        }
        if (!classInitialized) {
            getSession().log(SessionLog.FINER, SessionLog.METAMODEL, "metamodel_canonical_model_class_not_found", className);
        }
    }
}
Also used : ConversionException(org.eclipse.persistence.exceptions.ConversionException) ManagedType(jakarta.persistence.metamodel.ManagedType) PrivilegedActionException(java.security.PrivilegedActionException) MapAttributeProxyImpl(org.eclipse.persistence.internal.jpa.metamodel.proxy.MapAttributeProxyImpl) CollectionAttributeProxyImpl(org.eclipse.persistence.internal.jpa.metamodel.proxy.CollectionAttributeProxyImpl) EntityManagerFactoryProvider.getConfigPropertyAsString(org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.getConfigPropertyAsString) SingularAttributeProxyImpl(org.eclipse.persistence.internal.jpa.metamodel.proxy.SingularAttributeProxyImpl) Field(java.lang.reflect.Field) PrivilegedGetDeclaredField(org.eclipse.persistence.internal.security.PrivilegedGetDeclaredField) PrivilegedGetValueFromField(org.eclipse.persistence.internal.security.PrivilegedGetValueFromField) MapAttribute(jakarta.persistence.metamodel.MapAttribute) ListAttributeProxyImpl(org.eclipse.persistence.internal.jpa.metamodel.proxy.ListAttributeProxyImpl) PrivilegedGetValueFromField(org.eclipse.persistence.internal.security.PrivilegedGetValueFromField) SingularAttributeProxyImpl(org.eclipse.persistence.internal.jpa.metamodel.proxy.SingularAttributeProxyImpl) CollectionAttributeProxyImpl(org.eclipse.persistence.internal.jpa.metamodel.proxy.CollectionAttributeProxyImpl) AttributeProxyImpl(org.eclipse.persistence.internal.jpa.metamodel.proxy.AttributeProxyImpl) MapAttributeProxyImpl(org.eclipse.persistence.internal.jpa.metamodel.proxy.MapAttributeProxyImpl) ListAttributeProxyImpl(org.eclipse.persistence.internal.jpa.metamodel.proxy.ListAttributeProxyImpl) SetAttributeProxyImpl(org.eclipse.persistence.internal.jpa.metamodel.proxy.SetAttributeProxyImpl) PrivilegedGetDeclaredFields(org.eclipse.persistence.internal.security.PrivilegedGetDeclaredFields) SetAttributeProxyImpl(org.eclipse.persistence.internal.jpa.metamodel.proxy.SetAttributeProxyImpl) ListAttribute(jakarta.persistence.metamodel.ListAttribute)

Aggregations

Field (java.lang.reflect.Field)2 PrivilegedActionException (java.security.PrivilegedActionException)2 PrivilegedGetDeclaredFields (org.eclipse.persistence.internal.security.PrivilegedGetDeclaredFields)2 EntityManager (jakarta.persistence.EntityManager)1 RollbackException (jakarta.persistence.RollbackException)1 ListAttribute (jakarta.persistence.metamodel.ListAttribute)1 ManagedType (jakarta.persistence.metamodel.ManagedType)1 MapAttribute (jakarta.persistence.metamodel.MapAttribute)1 JAXBException (jakarta.xml.bind.JAXBException)1 IntrospectionException (java.beans.IntrospectionException)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Collection (java.util.Collection)1 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 ConversionException (org.eclipse.persistence.exceptions.ConversionException)1 EntityManagerFactoryProvider.getConfigPropertyAsString (org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.getConfigPropertyAsString)1 AttributeProxyImpl (org.eclipse.persistence.internal.jpa.metamodel.proxy.AttributeProxyImpl)1 CollectionAttributeProxyImpl (org.eclipse.persistence.internal.jpa.metamodel.proxy.CollectionAttributeProxyImpl)1 ListAttributeProxyImpl (org.eclipse.persistence.internal.jpa.metamodel.proxy.ListAttributeProxyImpl)1 MapAttributeProxyImpl (org.eclipse.persistence.internal.jpa.metamodel.proxy.MapAttributeProxyImpl)1