Search in sources :

Example 11 with PluginFactory

use of org.apache.logging.log4j.plugins.PluginFactory in project logging-log4j2 by apache.

the class MapRewritePolicy method createPolicy.

/**
 * The factory method to create the MapRewritePolicy.
 * @param mode The string representation of the Mode.
 * @param pairs key/value pairs for the new Map keys and values.
 * @return The MapRewritePolicy.
 */
@PluginFactory
public static MapRewritePolicy createPolicy(@PluginAttribute final String mode, @PluginElement("KeyValuePair") final KeyValuePair[] pairs) {
    Mode op = mode == null ? op = Mode.Add : Mode.valueOf(mode);
    if (pairs == null || pairs.length == 0) {
        LOGGER.error("keys and values must be specified for the MapRewritePolicy");
        return null;
    }
    final Map<String, Object> map = new HashMap<>();
    for (final KeyValuePair pair : pairs) {
        final String key = pair.getKey();
        if (key == null) {
            LOGGER.error("A null key is not valid in MapRewritePolicy");
            continue;
        }
        final String value = pair.getValue();
        if (value == null) {
            LOGGER.error("A null value for key " + key + " is not allowed in MapRewritePolicy");
            continue;
        }
        map.put(pair.getKey(), pair.getValue());
    }
    if (map.isEmpty()) {
        LOGGER.error("MapRewritePolicy is not configured with any valid key value pairs");
        return null;
    }
    return new MapRewritePolicy(map, op);
}
Also used : KeyValuePair(org.apache.logging.log4j.core.util.KeyValuePair) HashMap(java.util.HashMap) PluginFactory(org.apache.logging.log4j.plugins.PluginFactory)

Example 12 with PluginFactory

use of org.apache.logging.log4j.plugins.PluginFactory in project logging-log4j2 by apache.

the class FactoryMethodConnectionSource method createConnectionSource.

/**
 * Factory method for creating a connection source within the plugin manager.
 *
 * @param className The name of a public class that contains a static method capable of returning either a
 *                  {@link DataSource} or a {@link Connection}.
 * @param methodName The name of the public static method on the aforementioned class that returns the data source
 *                   or connection. If this method returns a {@link Connection}, it should return a new connection
 *                   every call.
 * @return the created connection source.
 */
@PluginFactory
public static FactoryMethodConnectionSource createConnectionSource(@PluginAttribute("class") final String className, @PluginAttribute("method") final String methodName) {
    if (Strings.isEmpty(className) || Strings.isEmpty(methodName)) {
        LOGGER.error("No class name or method name specified for the connection factory method.");
        return null;
    }
    final Method method;
    try {
        final Class<?> factoryClass = Loader.loadClass(className);
        method = factoryClass.getMethod(methodName);
    } catch (final Exception e) {
        LOGGER.error(e.toString(), e);
        return null;
    }
    final Class<?> returnType = method.getReturnType();
    String returnTypeString = returnType.getName();
    DataSource dataSource;
    if (returnType == DataSource.class) {
        try {
            dataSource = (DataSource) method.invoke(null);
            returnTypeString += "[" + dataSource + ']';
        } catch (final Exception e) {
            LOGGER.error(e.toString(), e);
            return null;
        }
    } else if (returnType == Connection.class) {
        dataSource = new DataSource() {

            @Override
            public Connection getConnection() throws SQLException {
                try {
                    return (Connection) method.invoke(null);
                } catch (final Exception e) {
                    throw new SQLException("Failed to obtain connection from factory method.", e);
                }
            }

            @Override
            public Connection getConnection(final String username, final String password) throws SQLException {
                throw new UnsupportedOperationException();
            }

            @Override
            public int getLoginTimeout() throws SQLException {
                throw new UnsupportedOperationException();
            }

            @Override
            public PrintWriter getLogWriter() throws SQLException {
                throw new UnsupportedOperationException();
            }

            @Override
            @SuppressWarnings("unused")
            public java.util.logging.Logger getParentLogger() {
                throw new UnsupportedOperationException();
            }

            @Override
            public boolean isWrapperFor(final Class<?> iface) throws SQLException {
                return false;
            }

            @Override
            public void setLoginTimeout(final int seconds) throws SQLException {
                throw new UnsupportedOperationException();
            }

            @Override
            public void setLogWriter(final PrintWriter out) throws SQLException {
                throw new UnsupportedOperationException();
            }

            @Override
            public <T> T unwrap(final Class<T> iface) throws SQLException {
                return null;
            }
        };
    } else {
        LOGGER.error("Method [{}.{}()] returns unsupported type [{}].", className, methodName, returnType.getName());
        return null;
    }
    return new FactoryMethodConnectionSource(dataSource, className, methodName, returnTypeString);
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) Method(java.lang.reflect.Method) SQLException(java.sql.SQLException) DataSource(javax.sql.DataSource) PrintWriter(java.io.PrintWriter) PluginFactory(org.apache.logging.log4j.plugins.PluginFactory)

Example 13 with PluginFactory

use of org.apache.logging.log4j.plugins.PluginFactory in project logging-log4j2 by apache.

the class JeroMqAppender method createAppender.

// The ZMQ.Socket class has other set methods that we do not cover because
// they throw unsupported operation exceptions.
@PluginFactory
public static JeroMqAppender createAppender(// @formatter:off
@Required(message = "No name provided for JeroMqAppender") @PluginAttribute final String name, @PluginElement Layout<?> layout, @PluginElement final Filter filter, @PluginElement final Property[] properties, // Super attributes
@PluginAttribute final boolean ignoreExceptions, // ZMQ attributes; defaults picked from zmq.Options.
@PluginAttribute(defaultLong = 0) final long affinity, @PluginAttribute(defaultLong = DEFAULT_BACKLOG) final long backlog, @PluginAttribute final boolean delayAttachOnConnect, @PluginAttribute final byte[] identity, @PluginAttribute(defaultBoolean = true) final boolean ipv4Only, @PluginAttribute(defaultLong = -1) final long linger, @PluginAttribute(defaultLong = -1) final long maxMsgSize, @PluginAttribute(defaultLong = DEFAULT_RCV_HWM) final long rcvHwm, @PluginAttribute(defaultLong = 0) final long receiveBufferSize, @PluginAttribute(defaultLong = -1) final int receiveTimeOut, @PluginAttribute(defaultLong = DEFAULT_IVL) final long reconnectIVL, @PluginAttribute(defaultLong = 0) final long reconnectIVLMax, @PluginAttribute(defaultLong = 0) final long sendBufferSize, @PluginAttribute(defaultLong = -1) final int sendTimeOut, @PluginAttribute(defaultLong = DEFAULT_SND_HWM) final long sndHwm, @PluginAttribute(defaultInt = -1) final int tcpKeepAlive, @PluginAttribute(defaultLong = -1) final long tcpKeepAliveCount, @PluginAttribute(defaultLong = -1) final long tcpKeepAliveIdle, @PluginAttribute(defaultLong = -1) final long tcpKeepAliveInterval, @PluginAttribute final boolean xpubVerbose) // @formatter:on
{
    if (layout == null) {
        layout = PatternLayout.createDefaultLayout();
    }
    List<String> endpoints;
    if (properties == null) {
        endpoints = new ArrayList<>(0);
    } else {
        endpoints = new ArrayList<>(properties.length);
        for (final Property property : properties) {
            if ("endpoint".equalsIgnoreCase(property.getName())) {
                final String value = property.getValue();
                if (Strings.isNotEmpty(value)) {
                    endpoints.add(value);
                }
            }
        }
    }
    LOGGER.debug("Creating JeroMqAppender with name={}, filter={}, layout={}, ignoreExceptions={}, endpoints={}", name, filter, layout, ignoreExceptions, endpoints);
    return new JeroMqAppender(name, filter, layout, ignoreExceptions, endpoints, affinity, backlog, delayAttachOnConnect, identity, ipv4Only, linger, maxMsgSize, rcvHwm, receiveBufferSize, receiveTimeOut, reconnectIVL, reconnectIVLMax, sendBufferSize, sendTimeOut, sndHwm, tcpKeepAlive, tcpKeepAliveCount, tcpKeepAliveIdle, tcpKeepAliveInterval, xpubVerbose, Property.EMPTY_ARRAY);
}
Also used : Property(org.apache.logging.log4j.core.config.Property) PluginFactory(org.apache.logging.log4j.plugins.PluginFactory)

Aggregations

PluginFactory (org.apache.logging.log4j.plugins.PluginFactory)13 HashMap (java.util.HashMap)5 KeyValuePair (org.apache.logging.log4j.core.util.KeyValuePair)5 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Level (org.apache.logging.log4j.Level)3 Method (java.lang.reflect.Method)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 PrintWriter (java.io.PrintWriter)1 Reader (java.io.Reader)1 URI (java.net.URI)1 Charset (java.nio.charset.Charset)1 Path (java.nio.file.Path)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 TimeUnit (java.util.concurrent.TimeUnit)1 DataSource (javax.sql.DataSource)1