Search in sources :

Example 16 with StringEx

use of com.twinsoft.util.StringEx in project convertigo by convertigo.

the class DatabaseCacheManager method removeExpiredCacheEntries.

protected void removeExpiredCacheEntries(long time) throws EngineException {
    try {
        Engine.logCacheManager.debug("Trying to remove expired cache entries from the cache Database");
        sqlRequester.checkConnection();
        StringEx sqlRequest = new StringEx(sqlRequester.getProperty(DatabaseCacheManager.PROPERTIES_SQL_REQUEST_REMOVE_EXPIRED_CACHE_ENTRY));
        String cacheTableName = sqlRequester.getProperty(DatabaseCacheManager.PROPERTIES_SQL_CACHE_TABLE_NAME, "CacheTable");
        sqlRequest.replace("CacheTable", cacheTableName);
        sqlRequest.replace("{CurrentTime}", Long.toString(time));
        String sSqlRequest = sqlRequest.toString();
        Engine.logCacheManager.debug("SQL: " + sSqlRequest);
        Statement statement = null;
        try {
            statement = sqlRequester.connection.createStatement();
            int nResult = statement.executeUpdate(sSqlRequest);
            Engine.logCacheManager.debug(nResult + " row(s) deleted.");
        } finally {
            if (statement != null) {
                statement.close();
            }
        }
        Engine.logCacheManager.debug("The expired cache entries has been removed.");
    } catch (Exception e) {
        throw new EngineException("Unable to remove the expired cache entries!", e);
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) EngineException(com.twinsoft.convertigo.engine.EngineException) StringEx(com.twinsoft.util.StringEx) IOException(java.io.IOException) SQLException(java.sql.SQLException) EngineException(com.twinsoft.convertigo.engine.EngineException)

Example 17 with StringEx

use of com.twinsoft.util.StringEx in project convertigo by convertigo.

the class DatabaseCacheManager method removeStoredResponse.

protected synchronized void removeStoredResponse(CacheEntry cacheEntry) throws EngineException {
    removeWeakResponse(cacheEntry);
    DatabaseCacheEntry dbCacheEntry = (DatabaseCacheEntry) cacheEntry;
    Engine.logCacheManager.debug("cacheEntry=[" + cacheEntry.toString() + "]");
    long id = 0;
    try {
        Engine.logCacheManager.debug("Trying to remove stored response from the cache Database");
        sqlRequester.checkConnection();
        StringEx sqlRequest = new StringEx(sqlRequester.getProperty(DatabaseCacheManager.PROPERTIES_SQL_REQUEST_REMOVE_RESPONSE));
        String cacheTableName = sqlRequester.getProperty(DatabaseCacheManager.PROPERTIES_SQL_CACHE_TABLE_NAME, "CacheTable");
        sqlRequest.replace("CacheTable", cacheTableName);
        id = dbCacheEntry.id;
        sqlRequest.replace("{Id}", Long.toString(id));
        Engine.logCacheManager.debug("Replacement done");
        String sSqlRequest = sqlRequest.toString();
        Engine.logCacheManager.debug("SQL: " + sSqlRequest);
        Statement statement = null;
        try {
            statement = sqlRequester.connection.createStatement();
            int nResult = statement.executeUpdate(sSqlRequest);
            Engine.logCacheManager.debug("" + nResult + " row(s) deleted.");
        } finally {
            if (statement != null) {
                statement.close();
            }
        }
        Engine.logCacheManager.debug("The stored response has sucessfully been removed.");
    } catch (Exception e) {
        throw new EngineException("Unable to remove the response! (id: " + id + ")", e);
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) EngineException(com.twinsoft.convertigo.engine.EngineException) StringEx(com.twinsoft.util.StringEx) IOException(java.io.IOException) SQLException(java.sql.SQLException) EngineException(com.twinsoft.convertigo.engine.EngineException)

Example 18 with StringEx

use of com.twinsoft.util.StringEx in project convertigo by convertigo.

the class DatabaseCacheManager method getCacheEntry.

public CacheEntry getCacheEntry(String requestString) throws EngineException {
    try {
        DatabaseCacheEntry cacheEntry = null;
        CacheEntry ce = null;
        if ((ce = getWeakEntry(requestString)) != null && ce instanceof DatabaseCacheEntry) {
            return ce;
        }
        Engine.logCacheManager.debug("Trying to get the cache entry from this request string: " + requestString);
        sqlRequester.checkConnection();
        StringEx sqlRequest = new StringEx(sqlRequester.getProperty(DatabaseCacheManager.PROPERTIES_SQL_REQUEST_GET_CACHE_ENTRY));
        String cacheTableName = sqlRequester.getProperty(DatabaseCacheManager.PROPERTIES_SQL_CACHE_TABLE_NAME, "CacheTable");
        sqlRequest.replace("CacheTable", cacheTableName);
        sqlRequest.replace("{RequestString}", escapeString(requestString));
        String sSqlRequest = sqlRequest.toString();
        Engine.logCacheManager.debug("SQL: " + sSqlRequest);
        Statement statement = null;
        ResultSet rs = null;
        try {
            statement = sqlRequester.connection.createStatement();
            rs = statement.executeQuery(sSqlRequest);
            if (rs.next()) {
                cacheEntry = new DatabaseCacheEntry();
                cacheEntry.requestString = requestString.trim();
                cacheEntry.id = rs.getLong("Id");
                cacheEntry.expiryDate = rs.getLong("ExpiryDate");
                cacheEntry.sheetUrl = rs.getString("SheetUrl");
                if (cacheEntry.sheetUrl != null)
                    cacheEntry.sheetUrl = cacheEntry.sheetUrl.trim();
                cacheEntry.absoluteSheetUrl = rs.getString("AbsoluteSheetUrl");
                if (cacheEntry.absoluteSheetUrl != null)
                    cacheEntry.absoluteSheetUrl = cacheEntry.absoluteSheetUrl.trim();
                cacheEntry.contentType = rs.getString("ContentType");
                if (cacheEntry.contentType != null)
                    cacheEntry.contentType = cacheEntry.contentType.trim();
            }
        } finally {
            if (rs != null)
                rs.close();
            if (statement != null)
                statement.close();
        }
        Engine.logCacheManager.debug("The cache entry has been retrieved: [" + cacheEntry + "]");
        storeWeakEntry(cacheEntry);
        return cacheEntry;
    } catch (Exception e) {
        throw new EngineException("Unable to get the cache entry! (requestString: " + requestString + ")", e);
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) EngineException(com.twinsoft.convertigo.engine.EngineException) StringEx(com.twinsoft.util.StringEx) IOException(java.io.IOException) SQLException(java.sql.SQLException) EngineException(com.twinsoft.convertigo.engine.EngineException)

Example 19 with StringEx

use of com.twinsoft.util.StringEx in project convertigo by convertigo.

the class DatabaseCacheManager method getId.

private long getId(String requestString) throws EngineException {
    try {
        Engine.logCacheManager.debug("Trying to get Id.");
        sqlRequester.checkConnection();
        StringEx sqlRequest = new StringEx(sqlRequester.getProperty(DatabaseCacheManager.PROPERTIES_SQL_REQUEST_GET_ID));
        String cacheTableName = sqlRequester.getProperty(DatabaseCacheManager.PROPERTIES_SQL_CACHE_TABLE_NAME, "CacheTable");
        sqlRequest.replace("CacheTable", cacheTableName);
        sqlRequest.replace("{RequestString}", escapeString(requestString));
        String sSqlRequest = sqlRequest.toString();
        Engine.logCacheManager.debug("SQL: " + sSqlRequest);
        Statement statement = null;
        ResultSet rs = null;
        long lId = 0;
        try {
            statement = sqlRequester.connection.createStatement();
            rs = statement.executeQuery(sSqlRequest);
            rs.next();
            String sId = new String(rs.getString("Id"));
            lId = Long.parseLong(sId);
        } finally {
            if (rs != null)
                rs.close();
            if (statement != null) {
                statement.close();
            }
        }
        Engine.logCacheManager.debug("ID retrieved: " + lId);
        return lId;
    } catch (Exception e) {
        throw new EngineException("Unable to get the Id.", e);
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) EngineException(com.twinsoft.convertigo.engine.EngineException) StringEx(com.twinsoft.util.StringEx) IOException(java.io.IOException) SQLException(java.sql.SQLException) EngineException(com.twinsoft.convertigo.engine.EngineException)

Example 20 with StringEx

use of com.twinsoft.util.StringEx in project convertigo by convertigo.

the class DatabaseCacheManager method getStoredResponse.

protected Document getStoredResponse(Requester requester, CacheEntry cacheEntry) throws EngineException {
    Document document = getWeakResponse(cacheEntry);
    if (document != null) {
        return document;
    }
    DatabaseCacheEntry dbCacheEntry = (DatabaseCacheEntry) cacheEntry;
    Engine.logCacheManager.debug("cacheEntry=[" + cacheEntry.toString() + "]");
    try {
        Engine.logCacheManager.debug("Trying to get from the cache the stored response corresponding to this cache entry.");
        sqlRequester.checkConnection();
        String jdbcURL = sqlRequester.getProperty(SqlRequester.PROPERTIES_JDBC_URL);
        boolean isOracleServerDatabase = jdbcURL.indexOf(":oracle:") != -1;
        StringEx sqlRequest = new StringEx(sqlRequester.getProperty(DatabaseCacheManager.PROPERTIES_SQL_REQUEST_GET_STORED_RESPONSE));
        String cacheTableName = sqlRequester.getProperty(DatabaseCacheManager.PROPERTIES_SQL_CACHE_TABLE_NAME, "CacheTable");
        sqlRequest.replace("CacheTable", cacheTableName);
        long id = dbCacheEntry.id;
        sqlRequest.replace("{Id}", Long.toString(id));
        Engine.logCacheManager.debug("Replacement done");
        String sSqlRequest = sqlRequest.toString();
        Engine.logCacheManager.debug("SQL: " + sSqlRequest);
        // SELECT x.Xml.getCLOBVal() Xml FROM CacheTable x WHERE Id \= {Id}
        if (isOracleServerDatabase && sSqlRequest.toUpperCase().indexOf("GETCLOBVAL()") != -1) {
            PreparedStatement statement = null;
            ResultSet rs = null;
            java.sql.Clob clb = null;
            try {
                statement = sqlRequester.connection.prepareStatement(sSqlRequest);
                rs = statement.executeQuery(sSqlRequest);
                rs.next();
                clb = rs.getClob("Xml");
                Reader in = clb.getCharacterStream();
                StringWriter w = new StringWriter();
                IOUtils.copy(in, w);
                String xml = w.toString();
                document = requester.parseDOM(xml);
            } finally {
                if (clb != null) {
                    clb.free();
                }
                if (rs != null) {
                    rs.close();
                }
                if (statement != null) {
                    statement.close();
                }
            }
        } else // Other cases
        // SELECT Xml FROM CacheTable WHERE Id \= {Id}
        {
            Statement statement = null;
            ResultSet rs = null;
            try {
                statement = sqlRequester.connection.createStatement();
                rs = statement.executeQuery(sSqlRequest);
                rs.next();
                String xml = rs.getString("Xml");
                document = requester.parseDOM(xml);
            } finally {
                if (rs != null) {
                    rs.close();
                }
                if (statement != null) {
                    statement.close();
                }
            }
        }
        Engine.logCacheManager.debug("Response built from the cache");
        storeWeakResponse(document, cacheEntry.requestString);
        return document;
    } catch (Exception e) {
        throw new EngineException("Unable to get the response [" + cacheEntry.toString() + "] from the cache !", e);
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) EngineException(com.twinsoft.convertigo.engine.EngineException) Reader(java.io.Reader) PreparedStatement(java.sql.PreparedStatement) Document(org.w3c.dom.Document) IOException(java.io.IOException) SQLException(java.sql.SQLException) EngineException(com.twinsoft.convertigo.engine.EngineException) StringWriter(java.io.StringWriter) ResultSet(java.sql.ResultSet) StringEx(com.twinsoft.util.StringEx)

Aggregations

StringEx (com.twinsoft.util.StringEx)30 EngineException (com.twinsoft.convertigo.engine.EngineException)14 IOException (java.io.IOException)13 Statement (java.sql.Statement)10 SQLException (java.sql.SQLException)9 File (java.io.File)7 PreparedStatement (java.sql.PreparedStatement)7 Document (org.w3c.dom.Document)5 ResultSet (java.sql.ResultSet)4 HttpConnector (com.twinsoft.convertigo.beans.connectors.HttpConnector)3 Element (org.w3c.dom.Element)3 AbstractHttpTransaction (com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction)2 RequestableHttpVariable (com.twinsoft.convertigo.beans.variables.RequestableHttpVariable)2 RequestableVariable (com.twinsoft.convertigo.beans.variables.RequestableVariable)2 StringReader (java.io.StringReader)2 StringWriter (java.io.StringWriter)2 MalformedURLException (java.net.MalformedURLException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2