Search in sources :

Example 1 with AbstractSessionLog

use of org.eclipse.persistence.logging.AbstractSessionLog in project adeptj-modules by AdeptJ.

the class JpaActivator method disposeEclipseLinkSingletonLog.

static void disposeEclipseLinkSingletonLog() {
    // of SLF4JLogger if this bundle or EntityManagerFactoryLifecycle is going to be stopped.
    try {
        Field field = FieldUtils.getDeclaredField(AbstractSessionLog.class, FIELD_DEFAULT_LOG, true);
        if (field == null) {
            LOGGER.warn("Field defaultLog is not found in EclipseLink AbstractSessionLog!!");
            return;
        }
        Object defaultLog = field.get(null);
        if (defaultLog instanceof SessionLog) {
            ((SessionLog) defaultLog).setSession(null);
            field.set(null, null);
        }
    } catch (IllegalAccessException ex) {
        LOGGER.error(ex.getMessage(), ex);
    }
}
Also used : Field(java.lang.reflect.Field) AbstractSessionLog(org.eclipse.persistence.logging.AbstractSessionLog) SessionLog(org.eclipse.persistence.logging.SessionLog)

Example 2 with AbstractSessionLog

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

the class EntityManagerSetupImpl method updateLoggers.

/**
 * Update loggers and settings for the singleton logger and the session logger.
 * @param persistenceProperties the properties map
 * @param serverPlatformChanged the boolean that denotes a serverPlatform change in the session.
 */
protected void updateLoggers(Map persistenceProperties, boolean serverPlatformChanged, ClassLoader loader) {
    // Logger(SessionLog type) can be specified by the logger property or ServerPlatform.getServerLog().
    // The logger property has a higher priority to ServerPlatform.getServerLog().
    String loggerClassName = PropertiesHandler.getPropertyValueLogDebug(PersistenceUnitProperties.LOGGING_LOGGER, persistenceProperties, session);
    // The sessionLog instance should be different from the singletonLog because they have
    // different state.
    SessionLog singletonLog = null, sessionLog = null;
    if (loggerClassName != null) {
        SessionLog currentLog = session.getSessionLog();
        if (loggerClassName.equals(LoggerType.ServerLogger)) {
            ServerPlatform serverPlatform = session.getServerPlatform();
            singletonLog = serverPlatform.getServerLog();
            sessionLog = serverPlatform.getServerLog();
        } else if (!currentLog.getClass().getName().equals(loggerClassName)) {
            // Logger class was specified and it's not what's already there.
            try {
                Class<? extends SessionLog> sessionLogClass = findClassForProperty(loggerClassName, PersistenceUnitProperties.LOGGING_LOGGER, loader);
                singletonLog = sessionLogClass.getConstructor().newInstance();
                sessionLog = sessionLogClass.getConstructor().newInstance();
            } catch (Exception ex) {
                throw EntityManagerSetupException.failedToInstantiateLogger(loggerClassName, PersistenceUnitProperties.LOGGING_LOGGER, ex);
            }
        }
    } else if (serverPlatformChanged) {
        ServerPlatform serverPlatform = session.getServerPlatform();
        singletonLog = serverPlatform.getServerLog();
        sessionLog = serverPlatform.getServerLog();
    }
    // Don't change default loggers if the new loggers have not been created.
    if (singletonLog != null && sessionLog != null) {
        AbstractSessionLog.setLog(singletonLog);
        session.setSessionLog(sessionLog);
    }
    // Bug5389828.  Update the logging settings for the singleton logger.
    initOrUpdateLogging(persistenceProperties, AbstractSessionLog.getLog());
    initOrUpdateLogging(persistenceProperties, session.getSessionLog());
    // Set logging file.
    String loggingFileString = (String) persistenceProperties.get(PersistenceUnitProperties.LOGGING_FILE);
    if (loggingFileString != null) {
        if (!loggingFileString.trim().equals("")) {
            try {
                if (sessionLog != null) {
                    if (sessionLog instanceof AbstractSessionLog) {
                        FileOutputStream fos = new FileOutputStream(loggingFileString);
                        ((AbstractSessionLog) sessionLog).setWriter(fos);
                    } else {
                        FileWriter fw = new FileWriter(loggingFileString);
                        sessionLog.setWriter(fw);
                    }
                }
            } catch (IOException e) {
                session.handleException(ValidationException.invalidLoggingFile(loggingFileString, e));
            }
        } else {
            session.handleException(ValidationException.invalidLoggingFile());
        }
    }
}
Also used : AbstractSessionLog(org.eclipse.persistence.logging.AbstractSessionLog) SessionLog(org.eclipse.persistence.logging.SessionLog) AbstractSessionLog(org.eclipse.persistence.logging.AbstractSessionLog) FileOutputStream(java.io.FileOutputStream) FileWriter(java.io.FileWriter) PrivilegedNewInstanceFromClass(org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass) MetadataClass(org.eclipse.persistence.internal.jpa.metadata.accessors.objects.MetadataClass) EntityManagerFactoryProvider.getConfigPropertyAsString(org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.getConfigPropertyAsString) IOException(java.io.IOException) ServerPlatform(org.eclipse.persistence.platform.server.ServerPlatform) CustomServerPlatform(org.eclipse.persistence.platform.server.CustomServerPlatform) ValidationException(org.eclipse.persistence.exceptions.ValidationException) EclipseLinkException(org.eclipse.persistence.exceptions.EclipseLinkException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) OptimisticLockException(jakarta.persistence.OptimisticLockException) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException) RemoteException(java.rmi.RemoteException) IntegrityException(org.eclipse.persistence.exceptions.IntegrityException) EntityManagerSetupException(org.eclipse.persistence.exceptions.EntityManagerSetupException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConversionException(org.eclipse.persistence.exceptions.ConversionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PersistenceException(jakarta.persistence.PersistenceException) MalformedURLException(java.net.MalformedURLException) PersistenceUnitLoadingException(org.eclipse.persistence.exceptions.PersistenceUnitLoadingException)

Aggregations

AbstractSessionLog (org.eclipse.persistence.logging.AbstractSessionLog)2 SessionLog (org.eclipse.persistence.logging.SessionLog)2 OptimisticLockException (jakarta.persistence.OptimisticLockException)1 PersistenceException (jakarta.persistence.PersistenceException)1 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MalformedURLException (java.net.MalformedURLException)1 RemoteException (java.rmi.RemoteException)1 PrivilegedActionException (java.security.PrivilegedActionException)1 ConversionException (org.eclipse.persistence.exceptions.ConversionException)1 DatabaseException (org.eclipse.persistence.exceptions.DatabaseException)1 DescriptorException (org.eclipse.persistence.exceptions.DescriptorException)1 EclipseLinkException (org.eclipse.persistence.exceptions.EclipseLinkException)1 EntityManagerSetupException (org.eclipse.persistence.exceptions.EntityManagerSetupException)1 IntegrityException (org.eclipse.persistence.exceptions.IntegrityException)1 PersistenceUnitLoadingException (org.eclipse.persistence.exceptions.PersistenceUnitLoadingException)1