Search in sources :

Example 1 with JDBCColumnHandler

use of org.apache.log4j.jdbcplus.JDBCColumnHandler in project jaffa-framework by jaffa-projects.

the class JDBCLoggerWithAttachment method append.

/**
 * Invokes the append method of the base class.
 * Additionally it'll create an attachment, if provided in the MDC.
 * @param event the LoggingEvent to log
 * @param layout layout to use for message
 * @exception Exception Description of Exception
 */
public void append(LoggingEvent event, Layout layout) throws Exception {
    // If attachment is not provided, simply invoke the append() method of the base class and return
    if (event.getMDC(m_attachmentMDCKey) == null) {
        super.append(event, layout);
        return;
    }
    // The following is a copy of the original method, but with the ability to obtain the auto-generated key.
    if (!ready())
        throw new Exception("JDBCLogger::append(), Not ready to append !");
    boolean errorOccurred = false;
    PreparedStatement prepStmt = null;
    try {
        prepareConnectionForAttachment();
        int paramIndex = 1;
        prepStmt = (PreparedStatement) c_stmt.get(this);
        String logId = null;
        for (int i = 0; i < ((Number) c_num.get(this)).intValue(); i++) {
            JDBCLogColumn logcol = (JDBCLogColumn) ((List) c_logcols.get(this)).get(i);
            if (((Boolean) c_ignore.get(logcol)).booleanValue())
                continue;
            int logtype = ((Number) c_logtype.get(logcol)).intValue();
            int sqlType = ((Number) c_sqlType.get(logcol)).intValue();
            Object value = c_value.get(logcol);
            Object parameter = null;
            if (logtype == JDBCLogType.MSG) {
                parameter = event.getRenderedMessage();
                if (parameter == null) {
                    prepStmt.setNull(paramIndex, sqlType);
                } else {
                    prepStmt.setObject(paramIndex, parameter);
                }
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.LAYOUT) {
                parameter = layout.format(event);
                // default: use first pattern
                int layoutIndex = ((Integer) value).intValue();
                // LAYOUT~x. use tokens in layout string
                List tokenList = getTokenList((String) parameter);
                String token = getTokenFromList(tokenList, layoutIndex);
                if (parameter == null) {
                    prepStmt.setNull(paramIndex, sqlType);
                } else {
                    prepStmt.setString(paramIndex, token);
                }
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.PRIO) {
                parameter = event.getLevel().toString();
                if (parameter == null) {
                    prepStmt.setNull(paramIndex, sqlType);
                } else {
                    prepStmt.setObject(paramIndex, parameter);
                }
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.CAT) {
                parameter = event.getLoggerName();
                if (parameter == null) {
                    prepStmt.setNull(paramIndex, sqlType);
                } else {
                    prepStmt.setObject(paramIndex, parameter);
                }
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.THREAD) {
                parameter = event.getThreadName();
                if (parameter == null) {
                    prepStmt.setNull(paramIndex, sqlType);
                } else {
                    prepStmt.setObject(paramIndex, parameter);
                }
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.ID) {
                parameter = ((JDBCIDHandler) c_idhandler.get(logcol)).getID();
                if (parameter == null) {
                    prepStmt.setNull(paramIndex, sqlType);
                } else {
                    prepStmt.setObject(paramIndex, parameter);
                }
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.STATIC) {
                parameter = value;
                if (parameter == null) {
                    prepStmt.setNull(paramIndex, sqlType);
                } else {
                    prepStmt.setObject(paramIndex, parameter);
                }
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.TIMESTAMP) {
                parameter = new Timestamp((new java.util.Date()).getTime());
                if (parameter == null) {
                    prepStmt.setNull(paramIndex, sqlType);
                } else {
                    prepStmt.setObject(paramIndex, parameter);
                }
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.INC) {
                parameter = ((Number) c_inc.get(this));
                c_inc.set(this, ((Number) parameter).longValue() + 1);
                prepStmt.setObject(paramIndex, parameter);
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.DYNAMIC) {
                parameter = ((JDBCColumnHandler) c_columnHandler.get(logcol)).getObject(event, (String) c_table.get(this), (String) c_name.get(logcol));
                if (parameter == null) {
                    prepStmt.setNull(paramIndex, sqlType);
                } else {
                    prepStmt.setObject(paramIndex, parameter);
                }
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.THROWABLE) {
                // extract throwable information from loggingEvent if available
                parameter = (String) c_quotedString.invoke(this, getThrowableRepresentationFromLoggingEvent(event));
                if (parameter == null) {
                    prepStmt.setNull(paramIndex, sqlType);
                } else {
                    prepStmt.setObject(paramIndex, parameter);
                }
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.NDC) {
                parameter = event.getNDC();
                if (parameter == null) {
                    prepStmt.setNull(paramIndex, sqlType);
                } else {
                    prepStmt.setObject(paramIndex, parameter);
                }
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.MDC) {
                Object mdcObject = event.getMDC(value.toString());
                parameter = mdcObject == null ? null : mdcObject.toString();
                if (parameter == null) {
                    prepStmt.setNull(paramIndex, sqlType);
                } else {
                    prepStmt.setObject(paramIndex, parameter);
                }
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.IPRIO) {
                parameter = event.getLevel().toInt();
                prepStmt.setObject(paramIndex, parameter);
                paramIndex = paramIndex + 1;
            } else if (logtype == JDBCLogType.ORACLE_SEQUENCE) {
            // do nothing
            }
            // Obtain the logId
            if (parameter != null && "log_id".equalsIgnoreCase((String) c_name.get(logcol)))
                logId = parameter.toString();
        }
        prepStmt.executeUpdate();
        // Obtain the auto-generated key
        if (logId == null)
            throw new Exception("JDBCLogger::append(), LogId was not determined for the Log. Cannot add the attachment");
        prepStmt.close();
        // Now create the attachment
        Object attachment = event.getMDC(m_attachmentMDCKey);
        prepStmt = m_stmtForAttachment;
        IVoucherGenerator vg = VoucherGeneratorFactory.instance();
        vg.setConnection((Connection) c_con.get(this));
        vg.setDomainClassName(AttachmentMeta.getName());
        vg.setFieldName(AttachmentMeta.ATTACHMENT_ID);
        vg.setLabelToken(AttachmentMeta.META_ATTACHMENT_ID.getLabelToken());
        prepStmt.setString(1, vg.generate());
        prepStmt.setString(2, "org.jaffa.modules.messaging.domain.BusinessEventLog;" + logId);
        prepStmt.setString(3, AttachmentService.createAttachmentName(attachment));
        prepStmt.setString(4, "E");
        prepStmt.setString(5, AttachmentService.createContentType(attachment));
        prepStmt.setTimestamp(6, new Timestamp((new java.util.Date()).getTime()));
        if (SecurityManager.getPrincipal() != null && SecurityManager.getPrincipal().getName() != null)
            prepStmt.setString(7, SecurityManager.getPrincipal().getName());
        else
            prepStmt.setNull(7, Types.VARCHAR);
        byte[] bytes = AttachmentService.createAttachmentData(attachment);
        if ("oracle".equalsIgnoreCase(m_engine)) {
            Blob blob = (Blob) c_createBlob.invoke(null, c_con.get(this), bytes);
            prepStmt.setBlob(8, blob);
        } else {
            InputStream stream = new BufferedInputStream(new ByteArrayInputStream(bytes));
            prepStmt.setBinaryStream(8, stream, bytes.length);
        }
        prepStmt.executeUpdate();
    } catch (Exception e) {
        // e.printStackTrace();
        errorOccurred = true;
        throw (e);
    } finally {
        try {
            if (prepStmt != null) {
                prepStmt.close();
            }
            freeConnection();
        } catch (Exception exception) {
            if (errorOccurred) {
            // consume exception
            } else {
                throw (exception);
            }
        }
    }
}
Also used : JDBCIDHandler(org.apache.log4j.jdbcplus.JDBCIDHandler) Blob(java.sql.Blob) JDBCColumnHandler(org.apache.log4j.jdbcplus.JDBCColumnHandler) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JDBCLogColumn(org.apache.log4j.jdbcplus.JDBCLogColumn) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) IVoucherGenerator(org.jaffa.components.voucher.IVoucherGenerator) SQLException(java.sql.SQLException) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) List(java.util.List)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Blob (java.sql.Blob)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 Timestamp (java.sql.Timestamp)1 List (java.util.List)1 JDBCColumnHandler (org.apache.log4j.jdbcplus.JDBCColumnHandler)1 JDBCIDHandler (org.apache.log4j.jdbcplus.JDBCIDHandler)1 JDBCLogColumn (org.apache.log4j.jdbcplus.JDBCLogColumn)1 IVoucherGenerator (org.jaffa.components.voucher.IVoucherGenerator)1