Search in sources :

Example 1 with SessionLogEntry

use of org.eclipse.persistence.logging.SessionLogEntry in project eclipselink by eclipse-ee4j.

the class PersistenceWeaver method transform.

/**
 * INTERNAL:
 * Perform dynamic byte code weaving of class.
 * @param loader              The defining loader of the class to be transformed, may be {@code null}
 *                            if the bootstrap loader.
 * @param className           The name of the class in the internal form of fully qualified class and interface
 *                            names.
 * @param classBeingRedefined If this is a redefine, the class being redefined, otherwise {@code null}.
 * @param protectionDomain    The protection domain of the class being defined or redefined.
 * @param classfileBuffer     The input byte buffer in class file format (must not be modified).
 * @return  A well-formed class file buffer (the result of the transform), or {@code null} if no transform
 *          is performed.
 */
@Override
public byte[] transform(final ClassLoader loader, final String className, final Class<?> classBeingRedefined, final ProtectionDomain protectionDomain, final byte[] classfileBuffer) throws TransformerException {
    final SessionLog log = AbstractSessionLog.getLog();
    // PERF: Is finest logging on weaving turned on?
    final boolean shouldLogFinest = log.shouldLog(SessionLog.FINEST, SessionLog.WEAVER);
    final Map<String, ClassDetails> classDetailsMap = this.classDetailsMap;
    // Check if cleared already.
    if (classDetailsMap == null) {
        return null;
    }
    try {
        /*
             * The ClassFileTransformer callback - when called by the JVM's
             * Instrumentation implementation - is invoked for every class loaded.
             * Thus, we must check the classDetailsMap to see if we are 'interested'
             * in the class.
             */
        final ClassDetails classDetails = classDetailsMap.get(Helper.toSlashedClassName(className));
        if (classDetails != null) {
            if (shouldLogFinest) {
                log.log(SessionLog.FINEST, SessionLog.WEAVER, "begin_weaving_class", className);
            }
            ClassReader classReader = null;
            try {
                classReader = new ClassReader(classfileBuffer);
            } catch (IllegalArgumentException iae) {
                // in such case log a warning and try to re-read the class without class version check
                if (log.shouldLog(SessionLog.FINE, SessionLog.WEAVER)) {
                    SessionLogEntry entry = new SessionLogEntry(null, SessionLog.FINE, SessionLog.WEAVER, iae);
                    entry.setMessage(ExceptionLocalization.buildMessage("unsupported_classfile_version", new Object[] { className }));
                    log.log(entry);
                }
                classReader = new EclipseLinkClassReader(classfileBuffer);
            }
            final String reflectiveIntrospectionProperty = PrivilegedAccessHelper.getSystemProperty(SystemProperties.WEAVING_REFLECTIVE_INTROSPECTION);
            final ClassWriter classWriter = reflectiveIntrospectionProperty != null ? new ClassWriter(ClassWriter.COMPUTE_FRAMES) : new ComputeClassWriter(loader, ClassWriter.COMPUTE_FRAMES);
            final ClassWeaver classWeaver = new ClassWeaver(classWriter, classDetails);
            final ClassVisitor sv = new SerialVersionUIDAdder(classWeaver);
            classReader.accept(sv, 0);
            if (classWeaver.alreadyWeaved) {
                if (shouldLogFinest) {
                    log.log(SessionLog.FINEST, SessionLog.WEAVER, "end_weaving_class", className);
                }
                return null;
            }
            if (classWeaver.weaved) {
                final byte[] bytes = classWriter.toByteArray();
                final String outputPath = PrivilegedAccessHelper.getSystemProperty(SystemProperties.WEAVING_OUTPUT_PATH, "");
                if (!outputPath.equals("")) {
                    Helper.outputClassFile(className, bytes, outputPath);
                }
                // PERF: Don't execute this set of if statements with logging turned off.
                if (shouldLogFinest) {
                    if (classWeaver.weavedPersistenceEntity) {
                        log.log(SessionLog.FINEST, SessionLog.WEAVER, "weaved_persistenceentity", className);
                    }
                    if (classWeaver.weavedChangeTracker) {
                        log.log(SessionLog.FINEST, SessionLog.WEAVER, "weaved_changetracker", className);
                    }
                    if (classWeaver.weavedLazy) {
                        log.log(SessionLog.FINEST, SessionLog.WEAVER, "weaved_lazy", className);
                    }
                    if (classWeaver.weavedFetchGroups) {
                        log.log(SessionLog.FINEST, SessionLog.WEAVER, "weaved_fetchgroups", className);
                    }
                    if (classWeaver.weavedRest) {
                        log.log(SessionLog.FINEST, SessionLog.WEAVER, "weaved_rest", className);
                    }
                    log.log(SessionLog.FINEST, SessionLog.WEAVER, "end_weaving_class", className);
                }
                return bytes;
            }
            if (shouldLogFinest) {
                log.log(SessionLog.FINEST, SessionLog.WEAVER, "end_weaving_class", className);
            }
        } else {
            if (shouldLogFinest) {
                log.log(SessionLog.FINEST, SessionLog.WEAVER, "transform_missing_class_details", className);
            }
        }
    } catch (Throwable exception) {
        if (log.shouldLog(SessionLog.WARNING, SessionLog.WEAVER)) {
            log.log(SessionLog.WARNING, SessionLog.WEAVER, "exception_while_weaving", new Object[] { className, exception.getLocalizedMessage() });
            if (shouldLogFinest) {
                log.logThrowable(SessionLog.FINEST, SessionLog.WEAVER, exception);
            }
        }
    }
    if (shouldLogFinest) {
        log.log(SessionLog.FINEST, SessionLog.WEAVER, "transform_existing_class_bytes", className);
    }
    // Returning null means 'use existing class bytes'.
    return null;
}
Also used : EclipseLinkClassReader(org.eclipse.persistence.internal.libraries.asm.EclipseLinkClassReader) ClassVisitor(org.eclipse.persistence.internal.libraries.asm.ClassVisitor) SessionLogEntry(org.eclipse.persistence.logging.SessionLogEntry) ClassWriter(org.eclipse.persistence.internal.libraries.asm.ClassWriter) SerialVersionUIDAdder(org.eclipse.persistence.internal.libraries.asm.commons.SerialVersionUIDAdder) AbstractSessionLog(org.eclipse.persistence.logging.AbstractSessionLog) SessionLog(org.eclipse.persistence.logging.SessionLog) EclipseLinkClassReader(org.eclipse.persistence.internal.libraries.asm.EclipseLinkClassReader) ClassReader(org.eclipse.persistence.internal.libraries.asm.ClassReader)

Example 2 with SessionLogEntry

use of org.eclipse.persistence.logging.SessionLogEntry in project eclipselink by eclipse-ee4j.

the class MetadataAsmFactory method buildClassMetadata.

/**
 * Build the class metadata for the class name using ASM to read the class
 * byte codes.
 */
protected void buildClassMetadata(MetadataClass metadataClass, String className, boolean isLazy) {
    ClassMetadataVisitor visitor = new ClassMetadataVisitor(metadataClass, isLazy);
    InputStream stream = null;
    try {
        String resourceString = className.replace('.', '/') + ".class";
        if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
            final String f_resourceString = resourceString;
            stream = AccessController.doPrivileged(new PrivilegedAction<InputStream>() {

                @Override
                public InputStream run() {
                    return m_loader.getResourceAsStream(f_resourceString);
                }
            });
        } else {
            stream = m_loader.getResourceAsStream(resourceString);
        }
        ClassReader reader = new ClassReader(stream);
        Attribute[] attributes = new Attribute[0];
        reader.accept(visitor, attributes, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
    } catch (IllegalArgumentException iae) {
        // class was probably compiled with some newer than officially
        // supported and tested JDK
        // in such case log a warning and try to re-read the class
        // without class version check
        SessionLog log = getLogger().getSession() != null ? getLogger().getSession().getSessionLog() : AbstractSessionLog.getLog();
        if (log.shouldLog(SessionLog.WARNING, SessionLog.METADATA)) {
            SessionLogEntry entry = new SessionLogEntry(getLogger().getSession(), SessionLog.WARNING, SessionLog.METADATA, iae);
            entry.setMessage(ExceptionLocalization.buildMessage("unsupported_classfile_version", new Object[] { className }));
            log.log(entry);
        }
        if (stream != null) {
            try {
                ClassReader reader = new EclipseLinkClassReader(stream);
                Attribute[] attributes = new Attribute[0];
                reader.accept(visitor, attributes, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
            } catch (Exception e) {
                // our fall-back failed, this is severe
                if (log.shouldLog(SessionLog.SEVERE, SessionLog.METADATA)) {
                    SessionLogEntry entry = new SessionLogEntry(getLogger().getSession(), SessionLog.SEVERE, SessionLog.METADATA, e);
                    entry.setMessage(ExceptionLocalization.buildMessage("unsupported_classfile_version", new Object[] { className }));
                    log.log(entry);
                }
                addMetadataClass(getVirtualMetadataClass(className));
            }
        } else {
            addMetadataClass(getVirtualMetadataClass(className));
        }
    } catch (Exception exception) {
        SessionLog log = getLogger().getSession() != null ? getLogger().getSession().getSessionLog() : AbstractSessionLog.getLog();
        if (log.shouldLog(SessionLog.FINEST, SessionLog.METADATA)) {
            log.logThrowable(SessionLog.FINEST, SessionLog.METADATA, exception);
        }
        addMetadataClass(getVirtualMetadataClass(className));
    } finally {
        try {
            if (stream != null) {
                stream.close();
            }
        } catch (IOException ignore) {
        // Ignore.
        }
    }
}
Also used : Attribute(org.eclipse.persistence.internal.libraries.asm.Attribute) InputStream(java.io.InputStream) EclipseLinkClassReader(org.eclipse.persistence.internal.libraries.asm.EclipseLinkClassReader) IOException(java.io.IOException) SessionLogEntry(org.eclipse.persistence.logging.SessionLogEntry) IOException(java.io.IOException) SessionLog(org.eclipse.persistence.logging.SessionLog) AbstractSessionLog(org.eclipse.persistence.logging.AbstractSessionLog) PrivilegedAction(java.security.PrivilegedAction) ClassReader(org.eclipse.persistence.internal.libraries.asm.ClassReader) EclipseLinkClassReader(org.eclipse.persistence.internal.libraries.asm.EclipseLinkClassReader)

Example 3 with SessionLogEntry

use of org.eclipse.persistence.logging.SessionLogEntry in project eclipselink by eclipse-ee4j.

the class JPARSLogger method entering.

/**
 * Entering
 *
 * @param sessionLog log receiving the message
 * @param sourceClass the source class
 * @param sourceMethod the source method
 * @param in the input stream
 */
public static void entering(SessionLog sessionLog, String sourceClass, String sourceMethod, InputStream in) {
    // stands ready to supply those same bytes again if and whenever the method reset is called.
    if (isLoggableFinest(sessionLog) && (in.markSupported())) {
        try {
            String data = readData(in);
            in.reset();
            if (data != null) {
                SessionLogEntry sle = newLogEntry(sessionLog.getSession());
                sle.setSourceClassName(sourceClass);
                sle.setSourceMethodName(sourceMethod);
                sle.setMessage("ENTRY {0}");
                sle.setParameters(getParamsWithAdditionalInfo(new Object[] { data }));
                sessionLog.log(sle);
            }
        } catch (Throwable throwable) {
            exception(throwable.getMessage(), new Object[] {}, throwable);
        }
    }
}
Also used : SessionLogEntry(org.eclipse.persistence.logging.SessionLogEntry)

Example 4 with SessionLogEntry

use of org.eclipse.persistence.logging.SessionLogEntry in project eclipselink by eclipse-ee4j.

the class JPARSLogger method entering.

/**
 * Entering
 *
 * @param sessionLog the log
 * @param sourceClass the source class
 * @param sourceMethod the source method
 * @param params parameters
 */
public static void entering(SessionLog sessionLog, String sourceClass, String sourceMethod, Object[] params) {
    // Logger logs entering logs when log level <= FINER. But, we want to get these logs created only when the log level is FINEST.
    if (isLoggableFinest(sessionLog)) {
        SessionLogEntry sle = newLogEntry(sessionLog.getSession());
        sle.setSourceClassName(sourceClass);
        sle.setSourceMethodName(sourceMethod);
        sle.setMessage("ENTRY {0}");
        sle.setParameters(getParamsWithAdditionalInfo(params));
        sessionLog.log(sle);
    }
}
Also used : SessionLogEntry(org.eclipse.persistence.logging.SessionLogEntry)

Example 5 with SessionLogEntry

use of org.eclipse.persistence.logging.SessionLogEntry in project eclipselink by eclipse-ee4j.

the class AbstractSession method log.

/**
 * PUBLIC:
 * <p>
 * Log a message with level, category, parameters and accessor.  shouldTranslate determines if the message needs to be translated.
 * </p>
 *
 * @param level  the log request level value
 * @param message  the string message
 * @param params  array of parameters to the message
 * @param accessor  the connection that generated the log entry
 * @param category  the string representation of a EclipseLink category.
 * @param shouldTranslate  true if the message needs to be translated.
 */
public void log(int level, String category, String message, Object[] params, Accessor accessor, boolean shouldTranslate) {
    if (this.isLoggingOff) {
        return;
    }
    if (shouldLog(level, category)) {
        startOperationProfile(SessionProfiler.Logging);
        log(new SessionLogEntry(level, category, this, message, params, accessor, shouldTranslate));
        endOperationProfile(SessionProfiler.Logging);
    }
}
Also used : SessionLogEntry(org.eclipse.persistence.logging.SessionLogEntry)

Aggregations

SessionLogEntry (org.eclipse.persistence.logging.SessionLogEntry)18 Logger (ch.qos.logback.classic.Logger)3 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)3 LogCategory (org.eclipse.persistence.logging.LogCategory)3 LogLevel (org.eclipse.persistence.logging.LogLevel)3 SLF4JLogger (org.eclipse.persistence.logging.slf4j.SLF4JLogger)3 EntityManager (jakarta.persistence.EntityManager)2 Query (jakarta.persistence.Query)2 Collection (java.util.Collection)2 ClassReader (org.eclipse.persistence.internal.libraries.asm.ClassReader)2 EclipseLinkClassReader (org.eclipse.persistence.internal.libraries.asm.EclipseLinkClassReader)2 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)2 JpaQuery (org.eclipse.persistence.jpa.JpaQuery)2 AbstractSessionLog (org.eclipse.persistence.logging.AbstractSessionLog)2 SessionLog (org.eclipse.persistence.logging.SessionLog)2 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)2 ObjectLevelReadQuery (org.eclipse.persistence.queries.ObjectLevelReadQuery)2 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1