Search in sources :

Example 11 with ConversionManager

use of org.eclipse.persistence.internal.helper.ConversionManager in project eclipselink by eclipse-ee4j.

the class AggregateCollectionMapping method extractKeyFromTargetRow.

/**
 * INTERNAL:
 * Extract the source primary key value from the target row.
 * Used for batch reading, most following same order and fields as in the mapping.
 */
@Override
protected Object extractKeyFromTargetRow(AbstractRecord row, AbstractSession session) {
    int size = this.targetForeignKeyFields.size();
    Object[] key = new Object[size];
    ConversionManager conversionManager = session.getDatasourcePlatform().getConversionManager();
    for (int index = 0; index < size; index++) {
        DatabaseField targetField = this.targetForeignKeyFields.get(index);
        DatabaseField sourceField = this.sourceKeyFields.get(index);
        Object value = row.get(targetField);
        // Must ensure the classification gets a cache hit.
        try {
            value = conversionManager.convertObject(value, sourceField.getType());
        } catch (ConversionException e) {
            throw ConversionException.couldNotBeConverted(this, getDescriptor(), e);
        }
        key[index] = value;
    }
    return new CacheId(key);
}
Also used : ConversionException(org.eclipse.persistence.exceptions.ConversionException) CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) ConversionManager(org.eclipse.persistence.internal.helper.ConversionManager)

Example 12 with ConversionManager

use of org.eclipse.persistence.internal.helper.ConversionManager in project eclipselink by eclipse-ee4j.

the class OrderedListContainerPolicy method addAll.

protected boolean addAll(List elements, Object container, AbstractSession session, List<AbstractRecord> dbRows, ReadQuery query, CacheKey parentCacheKey) {
    int size = dbRows.size();
    if (this.elementDescriptor != null && this.elementDescriptor.getObjectBuilder().hasWrapperPolicy()) {
        ObjectBuilder objectBuilder = this.elementDescriptor.getObjectBuilder();
        List wrappedElements = new ArrayList(size);
        for (int i = 0; i < size; i++) {
            wrappedElements.add(objectBuilder.wrapObject(elements.get(i), session));
        }
        elements = wrappedElements;
    }
    ConversionManager conversionManager = session.getDatasourcePlatform().getConversionManager();
    // populate container with a dummy so the container.set(i, obj) could be used later.
    for (int i = 0; i < size; i++) {
        ((List) container).add(NOT_SET);
    }
    // insert the elements into container
    boolean failed = false;
    for (int i = 0; i < size; i++) {
        AbstractRecord row = dbRows.get(i);
        Object orderValue = row.get(this.listOrderField);
        // order value is null
        if (orderValue == null) {
            failed = true;
            break;
        }
        int intOrderValue = conversionManager.convertObject(orderValue, Integer.class);
        try {
            // one or more elements have the same order value
            if (NOT_SET != ((List) container).set(intOrderValue, elements.get(i))) {
                failed = true;
                break;
            }
        } catch (IndexOutOfBoundsException indexException) {
            // order value negative or greater/equal to size
            failed = true;
            break;
        }
    }
    if (failed) {
        ((List) container).clear();
        // extract order list - it will be set into exception or used by order correction.
        List<Integer> orderList = new ArrayList(size);
        for (int i = 0; i < size; i++) {
            AbstractRecord row = dbRows.get(i);
            Object orderValue = row.get(this.listOrderField);
            if (orderValue == null) {
                orderList.add(null);
            } else {
                orderList.add(conversionManager.convertObject(orderValue, Integer.class));
            }
        }
        if (this.orderCorrectionType == OrderCorrectionType.READ || this.orderCorrectionType == OrderCorrectionType.READ_WRITE) {
            // pair each element with its order index
            List<IndexedObject> indexedElements = new ArrayList(size);
            for (int i = 0; i < size; i++) {
                indexedElements.add(new IndexedObject(orderList.get(i), elements.get(i)));
            }
            // put elements in order and add to container
            ((List) container).addAll(correctOrderList(indexedElements));
            if (this.orderCorrectionType == OrderCorrectionType.READ_WRITE) {
                // mark IndirectList to have broken order
                ((IndirectList) container).setIsListOrderBrokenInDb(true);
            }
        } else {
            // this.orderCorrectionType == OrderCorrectionType.EXCEPTION
            throw QueryException.listOrderFieldWrongValue(query, this.listOrderField, orderList);
        }
    }
    return size > 0;
}
Also used : ArrayList(java.util.ArrayList) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) ObjectBuilder(org.eclipse.persistence.internal.descriptors.ObjectBuilder) IndirectList(org.eclipse.persistence.indirection.IndirectList) IndexedObject(org.eclipse.persistence.internal.helper.IndexedObject) ConversionManager(org.eclipse.persistence.internal.helper.ConversionManager) ArrayList(java.util.ArrayList) List(java.util.List) IndirectList(org.eclipse.persistence.indirection.IndirectList) IndexedObject(org.eclipse.persistence.internal.helper.IndexedObject) OrderedChangeObject(org.eclipse.persistence.internal.sessions.OrderedChangeObject)

Example 13 with ConversionManager

use of org.eclipse.persistence.internal.helper.ConversionManager in project eclipselink by eclipse-ee4j.

the class SecurableObjectHolder method initSecurableObject.

/**
 * Convert a String into a Securable object
 * Class name must be fully qualified, eg. org.eclipse.persistence.internal.security.JCEEncryptor
 * Default is the JCEEncryptor
 */
private void initSecurableObject() {
    boolean initPassThroughEncryptor = false;
    if (m_securableClassName == null) {
        // Since we are defaulting, hence, assuming they can initialize the JCE
        // libraries, if the init fails, this flag tells us to assume no encryption.
        // However, if the JCE init does work, the JCEEncryptor will need to
        // determine that a password was not encrypted by it, therefore, assume
        // clear text. See JCEEncryptor.
        initPassThroughEncryptor = true;
        m_securableClassName = JCE_ENCRYPTION_CLASS_NAME;
    }
    try {
        ConversionManager cm = ConversionManager.getDefaultManager();
        Class<?> securableClass = cm.convertObject(m_securableClassName, Class.class);
        if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
            try {
                m_securableObject = (Securable) AccessController.doPrivileged(new PrivilegedNewInstanceFromClass(securableClass));
            } catch (PrivilegedActionException exception) {
                throw exception.getException();
            }
        } else {
            m_securableObject = (Securable) PrivilegedAccessHelper.newInstanceFromClass(securableClass);
        }
    } catch (Throwable e) {
        if (initPassThroughEncryptor) {
            // default failed, so perform no encryption.
            m_securableObject = new PassThroughEncryptor();
        } else {
            throw ValidationException.invalidEncryptionClass(m_securableClassName, e);
        }
    }
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) ConversionManager(org.eclipse.persistence.internal.helper.ConversionManager)

Example 14 with ConversionManager

use of org.eclipse.persistence.internal.helper.ConversionManager in project eclipselink by eclipse-ee4j.

the class XMLProjectReader method read.

/**
 * PUBLIC:
 * Read the EclipseLink project deployment XML from the file or resource name.
 * If a resource name is used the class loader will be used to resolve the resource.
 * Note the class loader must be able to resolve the domain classes.
 * Note the file must be the deployment XML, not the Mapping Workbench project file.
 */
public static Project read(String fileOrResourceName, ClassLoader classLoader) {
    if (fileOrResourceName.toLowerCase().indexOf(".mwp") != -1) {
        throw ValidationException.invalidFileName(fileOrResourceName);
    }
    InputStream fileStream = null;
    if (classLoader == null) {
        fileStream = (new ConversionManager()).getLoader().getResourceAsStream(fileOrResourceName);
    } else {
        fileStream = classLoader.getResourceAsStream(fileOrResourceName);
    }
    if (fileStream == null) {
        File file = new File(fileOrResourceName);
        if (!file.exists()) {
            throw ValidationException.projectXMLNotFound(fileOrResourceName, null);
        }
        try {
            fileStream = new FileInputStream(fileOrResourceName);
        } catch (FileNotFoundException exception) {
            throw ValidationException.projectXMLNotFound(fileOrResourceName, exception);
        }
    }
    InputStreamReader reader = null;
    try {
        // Bug2631348  Only UTF-8 is supported
        reader = new InputStreamReader(fileStream, StandardCharsets.UTF_8);
        Project project = read(reader, classLoader);
        return project;
    } finally {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException exception) {
            throw ValidationException.fileError(exception);
        }
    }
}
Also used : EclipseLinkObjectPersistenceRuntimeXMLProject(org.eclipse.persistence.internal.sessions.factories.EclipseLinkObjectPersistenceRuntimeXMLProject) Project(org.eclipse.persistence.sessions.Project) ObjectPersistenceRuntimeXMLProject(org.eclipse.persistence.internal.sessions.factories.ObjectPersistenceRuntimeXMLProject) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) ConversionManager(org.eclipse.persistence.internal.helper.ConversionManager) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 15 with ConversionManager

use of org.eclipse.persistence.internal.helper.ConversionManager in project eclipselink by eclipse-ee4j.

the class DynamicClassLoader method lookup.

/**
 * Lookup the DynamicConversionManager for the given session. If the
 * existing ConversionManager is not an instance of DynamicConversionManager
 * then create a new one and replace the existing one.
 */
public static DynamicClassLoader lookup(Session session) {
    ConversionManager cm = null;
    if (session == null) {
        cm = ConversionManager.getDefaultManager();
    } else {
        cm = session.getPlatform().getConversionManager();
    }
    if (cm.getLoader() instanceof DynamicClassLoader) {
        return (DynamicClassLoader) cm.getLoader();
    }
    DynamicClassLoader dcl = new DynamicClassLoader(cm.getLoader());
    cm.setLoader(dcl);
    if (session == null) {
        ConversionManager.setDefaultLoader(dcl);
    }
    return dcl;
}
Also used : ConversionManager(org.eclipse.persistence.internal.helper.ConversionManager)

Aggregations

ConversionManager (org.eclipse.persistence.internal.helper.ConversionManager)32 Project (org.eclipse.persistence.sessions.Project)12 Platform (org.eclipse.persistence.internal.databaseaccess.Platform)11 XMLContext (org.eclipse.persistence.oxm.XMLContext)11 JAXBException (jakarta.xml.bind.JAXBException)10 StringReader (java.io.StringReader)10 XRDynamicClassLoader (org.eclipse.persistence.internal.xr.XRDynamicClassLoader)10 DatabaseLogin (org.eclipse.persistence.sessions.DatabaseLogin)10 JAXBContext (jakarta.xml.bind.JAXBContext)9 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)9 XMLPlatform (org.eclipse.persistence.platform.xml.XMLPlatform)9 DatabaseSession (org.eclipse.persistence.sessions.DatabaseSession)9 DatasourceLogin (org.eclipse.persistence.sessions.DatasourceLogin)9 Unmarshaller (jakarta.xml.bind.Unmarshaller)8 HashMap (java.util.HashMap)8 Map (java.util.Map)8 StreamSource (javax.xml.transform.stream.StreamSource)8 CacheId (org.eclipse.persistence.internal.identitymaps.CacheId)8 MetadataProcessor (org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor)8 DatabaseSessionImpl (org.eclipse.persistence.internal.sessions.DatabaseSessionImpl)8