001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements. See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache license, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License. You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the license for the specific language governing permissions and
015     * limitations under the license.
016     */
017    package org.apache.logging.log4j.core.appender.db.jdbc;
018    
019    import org.apache.logging.log4j.Logger;
020    import org.apache.logging.log4j.core.config.Configuration;
021    import org.apache.logging.log4j.core.config.plugins.Plugin;
022    import org.apache.logging.log4j.core.config.plugins.PluginAttr;
023    import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
024    import org.apache.logging.log4j.core.config.plugins.PluginFactory;
025    import org.apache.logging.log4j.core.layout.PatternLayout;
026    import org.apache.logging.log4j.status.StatusLogger;
027    
028    /**
029     * A configuration element used to configure which event properties are logged to which columns in the database table.
030     */
031    @Plugin(name = "Column", category = "Core", printObject = true)
032    public final class ColumnConfig {
033        private static final Logger LOGGER = StatusLogger.getLogger();
034    
035        private final String columnName;
036        private final PatternLayout layout;
037        private final String literalValue;
038        private final boolean eventTimestamp;
039        private final boolean unicode;
040        private final boolean clob;
041    
042        private ColumnConfig(final String columnName, final PatternLayout layout, final String literalValue,
043                             final boolean eventDate, final boolean unicode, final boolean clob) {
044            this.columnName = columnName;
045            this.layout = layout;
046            this.literalValue = literalValue;
047            this.eventTimestamp = eventDate;
048            this.unicode = unicode;
049            this.clob = clob;
050        }
051    
052        public String getColumnName() {
053            return this.columnName;
054        }
055    
056        public PatternLayout getLayout() {
057            return this.layout;
058        }
059    
060        public String getLiteralValue() {
061            return this.literalValue;
062        }
063    
064        public boolean isEventTimestamp() {
065            return this.eventTimestamp;
066        }
067    
068        public boolean isUnicode() {
069            return this.unicode;
070        }
071    
072        public boolean isClob() {
073            return this.clob;
074        }
075    
076        @Override
077        public String toString() {
078            return "{ name=" + this.columnName + ", layout=" + this.layout + ", literal=" + this.literalValue
079                    + ", timestamp=" + this.eventTimestamp + " }";
080        }
081    
082        /**
083         * Factory method for creating a column config within the plugin manager.
084         *
085         * @param config The configuration object
086         * @param name The name of the database column as it exists within the database table.
087         * @param pattern The {@link PatternLayout} pattern to insert in this column. Mutually exclusive with
088         *                {@code literalValue!=null} and {@code eventTimestamp=true}
089         * @param literalValue The literal value to insert into the column as-is without any quoting or escaping. Mutually
090         *                     exclusive with {@code pattern!=null} and {@code eventTimestamp=true}.
091         * @param eventTimestamp If {@code "true"}, indicates that this column is a date-time column in which the event
092         *                       timestamp should be inserted. Mutually exclusive with {@code pattern!=null} and
093         *                       {@code literalValue!=null}.
094         * @return the created column config.
095         */
096        @PluginFactory
097        public static ColumnConfig createColumnConfig(@PluginConfiguration final Configuration config,
098                                                      @PluginAttr("name") final String name,
099                                                      @PluginAttr("pattern") final String pattern,
100                                                      @PluginAttr("literal") final String literalValue,
101                                                      @PluginAttr("isEventTimestamp") final String eventTimestamp,
102                                                      @PluginAttr("isUnicode") final String unicode,
103                                                      @PluginAttr("isClob") final String clob) {
104            if (name == null || name.length() == 0) {
105                LOGGER.error("The column config is not valid because it does not contain a column name.");
106                return null;
107            }
108    
109            final boolean isPattern = pattern != null && pattern.length() > 0;
110            final boolean isLiteralValue = literalValue != null && literalValue.length() > 0;
111            final boolean isEventTimestamp = eventTimestamp != null && Boolean.parseBoolean(eventTimestamp);
112            final boolean isUnicode = unicode == null || unicode.length() == 0 || Boolean.parseBoolean(unicode);
113            final boolean isClob = clob != null && Boolean.parseBoolean(clob);
114    
115            if ((isPattern && isLiteralValue) || (isPattern && isEventTimestamp) || (isLiteralValue && isEventTimestamp)) {
116                LOGGER.error("The pattern, literal, and isEventTimestamp attributes are mutually exclusive.");
117                return null;
118            }
119    
120            if (isEventTimestamp) {
121                return new ColumnConfig(name, null, null, true, false, false);
122            }
123            if (isLiteralValue) {
124                return new ColumnConfig(name, null, literalValue, false, false, false);
125            }
126            if (isPattern) {
127                return new ColumnConfig(
128                        name, PatternLayout.createLayout(pattern, config, null, null, "true"), null, false, isUnicode,
129                        isClob
130                );
131            }
132    
133            LOGGER.error("To configure a column you must specify a pattern or literal or set isEventDate to true.");
134            return null;
135        }
136    }