Search in sources :

Example 1 with Log

use of org.apache.juli.logging.Log in project tomcat by apache.

the class StandardManager method doLoad.

/**
     * Load any currently active sessions that were previously unloaded
     * to the appropriate persistence mechanism, if any.  If persistence is not
     * supported, this method returns without doing anything.
     *
     * @exception ClassNotFoundException if a serialized class cannot be
     *  found during the reload
     * @exception IOException if an input/output error occurs
     */
protected void doLoad() throws ClassNotFoundException, IOException {
    if (log.isDebugEnabled()) {
        log.debug("Start: Loading persisted sessions");
    }
    // Initialize our internal data structures
    sessions.clear();
    // Open an input stream to the specified pathname, if any
    File file = file();
    if (file == null) {
        return;
    }
    if (log.isDebugEnabled()) {
        log.debug(sm.getString("standardManager.loading", pathname));
    }
    Loader loader = null;
    ClassLoader classLoader = null;
    Log logger = null;
    try (FileInputStream fis = new FileInputStream(file.getAbsolutePath());
        BufferedInputStream bis = new BufferedInputStream(fis)) {
        Context c = getContext();
        loader = c.getLoader();
        logger = c.getLogger();
        if (loader != null) {
            classLoader = loader.getClassLoader();
        }
        if (classLoader == null) {
            classLoader = getClass().getClassLoader();
        }
        // Load the previously unloaded active sessions
        synchronized (sessions) {
            try (ObjectInputStream ois = new CustomObjectInputStream(bis, classLoader, logger, getSessionAttributeValueClassNamePattern(), getWarnOnSessionAttributeFilterFailure())) {
                Integer count = (Integer) ois.readObject();
                int n = count.intValue();
                if (log.isDebugEnabled())
                    log.debug("Loading " + n + " persisted sessions");
                for (int i = 0; i < n; i++) {
                    StandardSession session = getNewSession();
                    session.readObjectData(ois);
                    session.setManager(this);
                    sessions.put(session.getIdInternal(), session);
                    session.activate();
                    if (!session.isValidInternal()) {
                        // If session is already invalid,
                        // expire session to prevent memory leak.
                        session.setValid(true);
                        session.expire();
                    }
                    sessionCounter++;
                }
            } finally {
                // Delete the persistent storage file
                if (file.exists()) {
                    file.delete();
                }
            }
        }
    } catch (FileNotFoundException e) {
        if (log.isDebugEnabled()) {
            log.debug("No persisted data file found");
        }
        return;
    }
    if (log.isDebugEnabled()) {
        log.debug("Finish: Loading persisted sessions");
    }
}
Also used : Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) Log(org.apache.juli.logging.Log) FileNotFoundException(java.io.FileNotFoundException) Loader(org.apache.catalina.Loader) CustomObjectInputStream(org.apache.catalina.util.CustomObjectInputStream) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) File(java.io.File) ObjectInputStream(java.io.ObjectInputStream) CustomObjectInputStream(org.apache.catalina.util.CustomObjectInputStream)

Example 2 with Log

use of org.apache.juli.logging.Log in project tomcat by apache.

the class JDBCStore method load.

/**
     * Load the Session associated with the id <code>id</code>.
     * If no such session is found <code>null</code> is returned.
     *
     * @param id a value of type <code>String</code>
     * @return the stored <code>Session</code>
     * @exception ClassNotFoundException if an error occurs
     * @exception IOException if an input/output error occurred
     */
@Override
public Session load(String id) throws ClassNotFoundException, IOException {
    StandardSession _session = null;
    org.apache.catalina.Context context = getManager().getContext();
    Log contextLog = context.getLogger();
    synchronized (this) {
        int numberOfTries = 2;
        while (numberOfTries > 0) {
            Connection _conn = getConnection();
            if (_conn == null) {
                return null;
            }
            ClassLoader oldThreadContextCL = context.bind(Globals.IS_SECURITY_ENABLED, null);
            try {
                if (preparedLoadSql == null) {
                    String loadSql = "SELECT " + sessionIdCol + ", " + sessionDataCol + " FROM " + sessionTable + " WHERE " + sessionIdCol + " = ? AND " + sessionAppCol + " = ?";
                    preparedLoadSql = _conn.prepareStatement(loadSql);
                }
                preparedLoadSql.setString(1, id);
                preparedLoadSql.setString(2, getName());
                try (ResultSet rst = preparedLoadSql.executeQuery()) {
                    if (rst.next()) {
                        try (ObjectInputStream ois = getObjectInputStream(rst.getBinaryStream(2))) {
                            if (contextLog.isDebugEnabled()) {
                                contextLog.debug(sm.getString(getStoreName() + ".loading", id, sessionTable));
                            }
                            _session = (StandardSession) manager.createEmptySession();
                            _session.readObjectData(ois);
                            _session.setManager(manager);
                        }
                    } else if (context.getLogger().isDebugEnabled()) {
                        contextLog.debug(getStoreName() + ": No persisted data object found");
                    }
                    // Break out after the finally block
                    numberOfTries = 0;
                }
            } catch (SQLException e) {
                contextLog.error(sm.getString(getStoreName() + ".SQLException", e));
                if (dbConnection != null)
                    close(dbConnection);
            } finally {
                context.unbind(Globals.IS_SECURITY_ENABLED, oldThreadContextCL);
                release(_conn);
            }
            numberOfTries--;
        }
    }
    return _session;
}
Also used : Log(org.apache.juli.logging.Log) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ObjectInputStream(java.io.ObjectInputStream)

Example 3 with Log

use of org.apache.juli.logging.Log in project tomcat by apache.

the class FileStore method load.

/**
     * Load and return the Session associated with the specified session
     * identifier from this Store, without removing it.  If there is no
     * such stored Session, return <code>null</code>.
     *
     * @param id Session identifier of the session to load
     *
     * @exception ClassNotFoundException if a deserialization error occurs
     * @exception IOException if an input/output error occurs
     */
@Override
public Session load(String id) throws ClassNotFoundException, IOException {
    // Open an input stream to the specified pathname, if any
    File file = file(id);
    if (file == null) {
        return null;
    }
    if (!file.exists()) {
        return null;
    }
    Context context = getManager().getContext();
    Log contextLog = context.getLogger();
    if (contextLog.isDebugEnabled()) {
        contextLog.debug(sm.getString(getStoreName() + ".loading", id, file.getAbsolutePath()));
    }
    ClassLoader oldThreadContextCL = context.bind(Globals.IS_SECURITY_ENABLED, null);
    try (FileInputStream fis = new FileInputStream(file.getAbsolutePath());
        ObjectInputStream ois = getObjectInputStream(fis)) {
        StandardSession session = (StandardSession) manager.createEmptySession();
        session.readObjectData(ois);
        session.setManager(manager);
        return session;
    } catch (FileNotFoundException e) {
        if (contextLog.isDebugEnabled()) {
            contextLog.debug("No persisted data file found");
        }
        return null;
    } finally {
        context.unbind(Globals.IS_SECURITY_ENABLED, oldThreadContextCL);
    }
}
Also used : Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) Log(org.apache.juli.logging.Log) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) FileInputStream(java.io.FileInputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 4 with Log

use of org.apache.juli.logging.Log in project tomcat by apache.

the class Introspection method loadClass.

/**
     * Attempt to load a class using the given Container's class loader. If the
     * class cannot be loaded, a debug level log message will be written to the
     * Container's log and null will be returned.
     * @param context The class loader of this context will be used to attempt
     *  to load the class
     * @param className The class name
     * @return the loaded class or <code>null</code> if loading failed
     */
public static Class<?> loadClass(Context context, String className) {
    ClassLoader cl = context.getLoader().getClassLoader();
    Log log = context.getLogger();
    Class<?> clazz = null;
    try {
        clazz = cl.loadClass(className);
    } catch (ClassNotFoundException | NoClassDefFoundError | ClassFormatError e) {
        log.debug(sm.getString("introspection.classLoadFailed", className), e);
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        log.debug(sm.getString("introspection.classLoadFailed", className), t);
    }
    return clazz;
}
Also used : Log(org.apache.juli.logging.Log)

Aggregations

Log (org.apache.juli.logging.Log)4 ObjectInputStream (java.io.ObjectInputStream)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 ServletContext (javax.servlet.ServletContext)2 Context (org.apache.catalina.Context)2 BufferedInputStream (java.io.BufferedInputStream)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Loader (org.apache.catalina.Loader)1 CustomObjectInputStream (org.apache.catalina.util.CustomObjectInputStream)1