You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2016/09/07 17:37:50 UTC
[1/7] logging-log4j2 git commit: Properly track resources during
shutdown.
Repository: logging-log4j2
Updated Branches:
refs/heads/master 97e45ed63 -> 78fe32c0d
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/78fe32c0/log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/NoSqlDatabaseManager.java
----------------------------------------------------------------------
diff --git a/log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/NoSqlDatabaseManager.java b/log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/NoSqlDatabaseManager.java
index 448cd04..1f758c9 100644
--- a/log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/NoSqlDatabaseManager.java
+++ b/log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/NoSqlDatabaseManager.java
@@ -1,220 +1,220 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-package org.apache.logging.log4j.nosql.appender;
-
-import java.util.Map;
-
-import org.apache.logging.log4j.Marker;
-import org.apache.logging.log4j.ThreadContext;
-import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.core.appender.AppenderLoggingException;
-import org.apache.logging.log4j.core.appender.ManagerFactory;
-import org.apache.logging.log4j.core.appender.db.AbstractDatabaseManager;
-import org.apache.logging.log4j.core.util.Closer;
-
-/**
- * An {@link AbstractDatabaseManager} implementation for all NoSQL databases.
- *
- * @param <W> A type parameter for reassuring the compiler that all operations are using the same {@link NoSqlObject}.
- */
-public final class NoSqlDatabaseManager<W> extends AbstractDatabaseManager {
- private static final NoSQLDatabaseManagerFactory FACTORY = new NoSQLDatabaseManagerFactory();
-
- private final NoSqlProvider<NoSqlConnection<W, ? extends NoSqlObject<W>>> provider;
-
- private NoSqlConnection<W, ? extends NoSqlObject<W>> connection;
-
- private NoSqlDatabaseManager(final String name, final int bufferSize,
- final NoSqlProvider<NoSqlConnection<W, ? extends NoSqlObject<W>>> provider) {
- super(name, bufferSize);
- this.provider = provider;
- }
-
- @Override
- protected void startupInternal() {
- // nothing to see here
- }
-
- @Override
- protected void shutdownInternal() {
- // NoSQL doesn't use transactions, so all we need to do here is simply close the client
- Closer.closeSilently(this.connection);
- }
-
- @Override
- protected void connectAndStart() {
- try {
- this.connection = this.provider.getConnection();
- } catch (final Exception e) {
- throw new AppenderLoggingException("Failed to get connection from NoSQL connection provider.", e);
- }
- }
-
- @Override
- protected void writeInternal(final LogEvent event) {
- if (!this.isRunning() || this.connection == null || this.connection.isClosed()) {
- throw new AppenderLoggingException(
- "Cannot write logging event; NoSQL manager not connected to the database.");
- }
-
- final NoSqlObject<W> entity = this.connection.createObject();
- entity.set("level", event.getLevel());
- entity.set("loggerName", event.getLoggerName());
- entity.set("message", event.getMessage() == null ? null : event.getMessage().getFormattedMessage());
-
- final StackTraceElement source = event.getSource();
- if (source == null) {
- entity.set("source", (Object) null);
- } else {
- entity.set("source", this.convertStackTraceElement(source));
- }
-
- final Marker marker = event.getMarker();
- if (marker == null) {
- entity.set("marker", (Object) null);
- } else {
- entity.set("marker", buildMarkerEntity(marker));
- }
-
- entity.set("threadId", event.getThreadId());
- entity.set("threadName", event.getThreadName());
- entity.set("threadPriority", event.getThreadPriority());
- entity.set("millis", event.getTimeMillis());
- entity.set("date", new java.util.Date(event.getTimeMillis()));
-
- @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
- Throwable thrown = event.getThrown();
- if (thrown == null) {
- entity.set("thrown", (Object) null);
- } else {
- final NoSqlObject<W> originalExceptionEntity = this.connection.createObject();
- NoSqlObject<W> exceptionEntity = originalExceptionEntity;
- exceptionEntity.set("type", thrown.getClass().getName());
- exceptionEntity.set("message", thrown.getMessage());
- exceptionEntity.set("stackTrace", this.convertStackTrace(thrown.getStackTrace()));
- while (thrown.getCause() != null) {
- thrown = thrown.getCause();
- final NoSqlObject<W> causingExceptionEntity = this.connection.createObject();
- causingExceptionEntity.set("type", thrown.getClass().getName());
- causingExceptionEntity.set("message", thrown.getMessage());
- causingExceptionEntity.set("stackTrace", this.convertStackTrace(thrown.getStackTrace()));
- exceptionEntity.set("cause", causingExceptionEntity);
- exceptionEntity = causingExceptionEntity;
- }
-
- entity.set("thrown", originalExceptionEntity);
- }
-
- final Map<String, String> contextMap = event.getContextMap();
- if (contextMap == null) {
- entity.set("contextMap", (Object) null);
- } else {
- final NoSqlObject<W> contextMapEntity = this.connection.createObject();
- for (final Map.Entry<String, String> entry : contextMap.entrySet()) {
- contextMapEntity.set(entry.getKey(), entry.getValue());
- }
- entity.set("contextMap", contextMapEntity);
- }
-
- final ThreadContext.ContextStack contextStack = event.getContextStack();
- if (contextStack == null) {
- entity.set("contextStack", (Object) null);
- } else {
- entity.set("contextStack", contextStack.asList().toArray());
- }
-
- this.connection.insertObject(entity);
- }
-
- private NoSqlObject<W> buildMarkerEntity(final Marker marker) {
- final NoSqlObject<W> entity = this.connection.createObject();
- entity.set("name", marker.getName());
-
- final Marker[] parents = marker.getParents();
- if (parents != null) {
- @SuppressWarnings("unchecked")
- final NoSqlObject<W>[] parentEntities = new NoSqlObject[parents.length];
- for (int i = 0; i < parents.length; i++) {
- parentEntities[i] = buildMarkerEntity(parents[i]);
- }
- entity.set("parents", parentEntities);
- }
- return entity;
- }
-
- @Override
- protected void commitAndClose() {
- // all NoSQL drivers auto-commit (since NoSQL doesn't generally use the concept of transactions).
- // also, all our NoSQL drivers use internal connection pooling and provide clients, not connections.
- // thus, we should not be closing the client until shutdown as NoSQL is very different from SQL.
- // see LOG4J2-591 and LOG4J2-676
- }
-
- private NoSqlObject<W>[] convertStackTrace(final StackTraceElement[] stackTrace) {
- final NoSqlObject<W>[] stackTraceEntities = this.connection.createList(stackTrace.length);
- for (int i = 0; i < stackTrace.length; i++) {
- stackTraceEntities[i] = this.convertStackTraceElement(stackTrace[i]);
- }
- return stackTraceEntities;
- }
-
- private NoSqlObject<W> convertStackTraceElement(final StackTraceElement element) {
- final NoSqlObject<W> elementEntity = this.connection.createObject();
- elementEntity.set("className", element.getClassName());
- elementEntity.set("methodName", element.getMethodName());
- elementEntity.set("fileName", element.getFileName());
- elementEntity.set("lineNumber", element.getLineNumber());
- return elementEntity;
- }
-
- /**
- * Creates a NoSQL manager for use within the {@link NoSqlAppender}, or returns a suitable one if it already exists.
- *
- * @param name The name of the manager, which should include connection details and hashed passwords where possible.
- * @param bufferSize The size of the log event buffer.
- * @param provider A provider instance which will be used to obtain connections to the chosen NoSQL database.
- * @return a new or existing NoSQL manager as applicable.
- */
- public static NoSqlDatabaseManager<?> getNoSqlDatabaseManager(final String name, final int bufferSize,
- final NoSqlProvider<?> provider) {
- return AbstractDatabaseManager.getManager(name, new FactoryData(bufferSize, provider), FACTORY);
- }
-
- /**
- * Encapsulates data that {@link NoSQLDatabaseManagerFactory} uses to create managers.
- */
- private static final class FactoryData extends AbstractDatabaseManager.AbstractFactoryData {
- private final NoSqlProvider<?> provider;
-
- protected FactoryData(final int bufferSize, final NoSqlProvider<?> provider) {
- super(bufferSize);
- this.provider = provider;
- }
- }
-
- /**
- * Creates managers.
- */
- private static final class NoSQLDatabaseManagerFactory implements
- ManagerFactory<NoSqlDatabaseManager<?>, FactoryData> {
- @Override
- @SuppressWarnings("unchecked")
- public NoSqlDatabaseManager<?> createManager(final String name, final FactoryData data) {
- return new NoSqlDatabaseManager(name, data.getBufferSize(), data.provider);
- }
- }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.nosql.appender;
+
+import java.util.Map;
+
+import org.apache.logging.log4j.Marker;
+import org.apache.logging.log4j.ThreadContext;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.appender.AppenderLoggingException;
+import org.apache.logging.log4j.core.appender.ManagerFactory;
+import org.apache.logging.log4j.core.appender.db.AbstractDatabaseManager;
+import org.apache.logging.log4j.core.util.Closer;
+
+/**
+ * An {@link AbstractDatabaseManager} implementation for all NoSQL databases.
+ *
+ * @param <W> A type parameter for reassuring the compiler that all operations are using the same {@link NoSqlObject}.
+ */
+public final class NoSqlDatabaseManager<W> extends AbstractDatabaseManager {
+ private static final NoSQLDatabaseManagerFactory FACTORY = new NoSQLDatabaseManagerFactory();
+
+ private final NoSqlProvider<NoSqlConnection<W, ? extends NoSqlObject<W>>> provider;
+
+ private NoSqlConnection<W, ? extends NoSqlObject<W>> connection;
+
+ private NoSqlDatabaseManager(final String name, final int bufferSize,
+ final NoSqlProvider<NoSqlConnection<W, ? extends NoSqlObject<W>>> provider) {
+ super(name, bufferSize);
+ this.provider = provider;
+ }
+
+ @Override
+ protected void startupInternal() {
+ // nothing to see here
+ }
+
+ @Override
+ protected boolean shutdownInternal() {
+ // NoSQL doesn't use transactions, so all we need to do here is simply close the client
+ Closer.closeSilently(this.connection);
+ }
+
+ @Override
+ protected void connectAndStart() {
+ try {
+ this.connection = this.provider.getConnection();
+ } catch (final Exception e) {
+ throw new AppenderLoggingException("Failed to get connection from NoSQL connection provider.", e);
+ }
+ }
+
+ @Override
+ protected void writeInternal(final LogEvent event) {
+ if (!this.isRunning() || this.connection == null || this.connection.isClosed()) {
+ throw new AppenderLoggingException(
+ "Cannot write logging event; NoSQL manager not connected to the database.");
+ }
+
+ final NoSqlObject<W> entity = this.connection.createObject();
+ entity.set("level", event.getLevel());
+ entity.set("loggerName", event.getLoggerName());
+ entity.set("message", event.getMessage() == null ? null : event.getMessage().getFormattedMessage());
+
+ final StackTraceElement source = event.getSource();
+ if (source == null) {
+ entity.set("source", (Object) null);
+ } else {
+ entity.set("source", this.convertStackTraceElement(source));
+ }
+
+ final Marker marker = event.getMarker();
+ if (marker == null) {
+ entity.set("marker", (Object) null);
+ } else {
+ entity.set("marker", buildMarkerEntity(marker));
+ }
+
+ entity.set("threadId", event.getThreadId());
+ entity.set("threadName", event.getThreadName());
+ entity.set("threadPriority", event.getThreadPriority());
+ entity.set("millis", event.getTimeMillis());
+ entity.set("date", new java.util.Date(event.getTimeMillis()));
+
+ @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
+ Throwable thrown = event.getThrown();
+ if (thrown == null) {
+ entity.set("thrown", (Object) null);
+ } else {
+ final NoSqlObject<W> originalExceptionEntity = this.connection.createObject();
+ NoSqlObject<W> exceptionEntity = originalExceptionEntity;
+ exceptionEntity.set("type", thrown.getClass().getName());
+ exceptionEntity.set("message", thrown.getMessage());
+ exceptionEntity.set("stackTrace", this.convertStackTrace(thrown.getStackTrace()));
+ while (thrown.getCause() != null) {
+ thrown = thrown.getCause();
+ final NoSqlObject<W> causingExceptionEntity = this.connection.createObject();
+ causingExceptionEntity.set("type", thrown.getClass().getName());
+ causingExceptionEntity.set("message", thrown.getMessage());
+ causingExceptionEntity.set("stackTrace", this.convertStackTrace(thrown.getStackTrace()));
+ exceptionEntity.set("cause", causingExceptionEntity);
+ exceptionEntity = causingExceptionEntity;
+ }
+
+ entity.set("thrown", originalExceptionEntity);
+ }
+
+ final Map<String, String> contextMap = event.getContextMap();
+ if (contextMap == null) {
+ entity.set("contextMap", (Object) null);
+ } else {
+ final NoSqlObject<W> contextMapEntity = this.connection.createObject();
+ for (final Map.Entry<String, String> entry : contextMap.entrySet()) {
+ contextMapEntity.set(entry.getKey(), entry.getValue());
+ }
+ entity.set("contextMap", contextMapEntity);
+ }
+
+ final ThreadContext.ContextStack contextStack = event.getContextStack();
+ if (contextStack == null) {
+ entity.set("contextStack", (Object) null);
+ } else {
+ entity.set("contextStack", contextStack.asList().toArray());
+ }
+
+ this.connection.insertObject(entity);
+ }
+
+ private NoSqlObject<W> buildMarkerEntity(final Marker marker) {
+ final NoSqlObject<W> entity = this.connection.createObject();
+ entity.set("name", marker.getName());
+
+ final Marker[] parents = marker.getParents();
+ if (parents != null) {
+ @SuppressWarnings("unchecked")
+ final NoSqlObject<W>[] parentEntities = new NoSqlObject[parents.length];
+ for (int i = 0; i < parents.length; i++) {
+ parentEntities[i] = buildMarkerEntity(parents[i]);
+ }
+ entity.set("parents", parentEntities);
+ }
+ return entity;
+ }
+
+ @Override
+ protected boolean commitAndClose() {
+ // all NoSQL drivers auto-commit (since NoSQL doesn't generally use the concept of transactions).
+ // also, all our NoSQL drivers use internal connection pooling and provide clients, not connections.
+ // thus, we should not be closing the client until shutdown as NoSQL is very different from SQL.
+ // see LOG4J2-591 and LOG4J2-676
+ }
+
+ private NoSqlObject<W>[] convertStackTrace(final StackTraceElement[] stackTrace) {
+ final NoSqlObject<W>[] stackTraceEntities = this.connection.createList(stackTrace.length);
+ for (int i = 0; i < stackTrace.length; i++) {
+ stackTraceEntities[i] = this.convertStackTraceElement(stackTrace[i]);
+ }
+ return stackTraceEntities;
+ }
+
+ private NoSqlObject<W> convertStackTraceElement(final StackTraceElement element) {
+ final NoSqlObject<W> elementEntity = this.connection.createObject();
+ elementEntity.set("className", element.getClassName());
+ elementEntity.set("methodName", element.getMethodName());
+ elementEntity.set("fileName", element.getFileName());
+ elementEntity.set("lineNumber", element.getLineNumber());
+ return elementEntity;
+ }
+
+ /**
+ * Creates a NoSQL manager for use within the {@link NoSqlAppender}, or returns a suitable one if it already exists.
+ *
+ * @param name The name of the manager, which should include connection details and hashed passwords where possible.
+ * @param bufferSize The size of the log event buffer.
+ * @param provider A provider instance which will be used to obtain connections to the chosen NoSQL database.
+ * @return a new or existing NoSQL manager as applicable.
+ */
+ public static NoSqlDatabaseManager<?> getNoSqlDatabaseManager(final String name, final int bufferSize,
+ final NoSqlProvider<?> provider) {
+ return AbstractDatabaseManager.getManager(name, new FactoryData(bufferSize, provider), FACTORY);
+ }
+
+ /**
+ * Encapsulates data that {@link NoSQLDatabaseManagerFactory} uses to create managers.
+ */
+ private static final class FactoryData extends AbstractDatabaseManager.AbstractFactoryData {
+ private final NoSqlProvider<?> provider;
+
+ protected FactoryData(final int bufferSize, final NoSqlProvider<?> provider) {
+ super(bufferSize);
+ this.provider = provider;
+ }
+ }
+
+ /**
+ * Creates managers.
+ */
+ private static final class NoSQLDatabaseManagerFactory implements
+ ManagerFactory<NoSqlDatabaseManager<?>, FactoryData> {
+ @Override
+ @SuppressWarnings("unchecked")
+ public NoSqlDatabaseManager<?> createManager(final String name, final FactoryData data) {
+ return new NoSqlDatabaseManager(name, data.getBufferSize(), data.provider);
+ }
+ }
+}
[2/7] logging-log4j2 git commit: Properly track resources during
shutdown.
Posted by gg...@apache.org.
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/78fe32c0/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumePersistentManager.java
----------------------------------------------------------------------
diff --git a/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumePersistentManager.java b/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumePersistentManager.java
index ba03629..4711ae0 100644
--- a/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumePersistentManager.java
+++ b/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumePersistentManager.java
@@ -1,848 +1,851 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-package org.apache.logging.log4j.flume.appender;
-
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.DataInputStream;
-import java.io.DataOutputStream;
-import java.io.File;
-import java.nio.charset.Charset;
-import java.nio.charset.StandardCharsets;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.concurrent.Callable;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicLong;
-
-import javax.crypto.Cipher;
-import javax.crypto.SecretKey;
-
-import org.apache.flume.Event;
-import org.apache.flume.event.SimpleEvent;
-import org.apache.logging.log4j.LoggingException;
-import org.apache.logging.log4j.core.appender.ManagerFactory;
-import org.apache.logging.log4j.core.config.Property;
-import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
-import org.apache.logging.log4j.core.config.plugins.util.PluginType;
-import org.apache.logging.log4j.core.util.ExecutorServices;
-import org.apache.logging.log4j.core.util.FileUtils;
-import org.apache.logging.log4j.core.util.Log4jThread;
-import org.apache.logging.log4j.core.util.Log4jThreadFactory;
-import org.apache.logging.log4j.core.util.SecretKeyProvider;
-import org.apache.logging.log4j.util.Strings;
-
-import com.sleepycat.je.Cursor;
-import com.sleepycat.je.CursorConfig;
-import com.sleepycat.je.Database;
-import com.sleepycat.je.DatabaseConfig;
-import com.sleepycat.je.DatabaseEntry;
-import com.sleepycat.je.Environment;
-import com.sleepycat.je.EnvironmentConfig;
-import com.sleepycat.je.LockConflictException;
-import com.sleepycat.je.LockMode;
-import com.sleepycat.je.OperationStatus;
-import com.sleepycat.je.StatsConfig;
-import com.sleepycat.je.Transaction;
-
-/**
- * Manager that persists data to Berkeley DB before passing it on to Flume.
- */
-public class FlumePersistentManager extends FlumeAvroManager {
-
- /** Attribute name for the key provider. */
- public static final String KEY_PROVIDER = "keyProvider";
-
- private static final Charset UTF8 = StandardCharsets.UTF_8;
-
- private static final String DEFAULT_DATA_DIR = ".log4j/flumeData";
-
- private static final long SHUTDOWN_WAIT_MILLIS = 60000;
-
- private static final long LOCK_TIMEOUT_SLEEP_MILLIS = 500;
-
- private static BDBManagerFactory factory = new BDBManagerFactory();
-
- private final Database database;
-
- private final Environment environment;
-
- private final WriterThread worker;
-
- private final Gate gate = new Gate();
-
- private final SecretKey secretKey;
-
- private final int lockTimeoutRetryCount;
-
- private final ExecutorService threadPool;
-
- private final AtomicLong dbCount = new AtomicLong();
-
- /**
- * Constructor
- * @param name The unique name of this manager.
- * @param shortName Original name for the Manager.
- * @param agents An array of Agents.
- * @param batchSize The number of events to include in a batch.
- * @param retries The number of times to retry connecting before giving up.
- * @param connectionTimeout The amount of time to wait for a connection to be established.
- * @param requestTimeout The amount of time to wair for a response to a request.
- * @param delay The amount of time to wait between retries.
- * @param database The database to write to.
- * @param environment The database environment.
- * @param secretKey The SecretKey to use for encryption.
- * @param lockTimeoutRetryCount The number of times to retry a lock timeout.
- */
- protected FlumePersistentManager(final String name, final String shortName, final Agent[] agents,
- final int batchSize, final int retries, final int connectionTimeout,
- final int requestTimeout, final int delay, final Database database,
- final Environment environment, final SecretKey secretKey,
- final int lockTimeoutRetryCount) {
- super(name, shortName, agents, batchSize, delay, retries, connectionTimeout, requestTimeout);
- this.database = database;
- this.environment = environment;
- dbCount.set(database.count());
- this.worker = new WriterThread(database, environment, this, gate, batchSize, secretKey, dbCount,
- lockTimeoutRetryCount);
- this.worker.start();
- this.secretKey = secretKey;
- this.threadPool = Executors.newCachedThreadPool(Log4jThreadFactory.createDaemonThreadFactory("Flume"));
- this.lockTimeoutRetryCount = lockTimeoutRetryCount;
- }
-
-
- /**
- * Returns a FlumeAvroManager.
- * @param name The name of the manager.
- * @param agents The agents to use.
- * @param properties Properties to pass to the Manager.
- * @param batchSize The number of events to include in a batch.
- * @param retries The number of times to retry connecting before giving up.
- * @param connectionTimeout The amount of time to wait to establish a connection.
- * @param requestTimeout The amount of time to wait for a response to a request.
- * @param delayMillis Amount of time to delay before delivering a batch.
- * @param lockTimeoutRetryCount The number of times to retry after a lock timeout.
- * @param dataDir The location of the Berkeley database.
- * @return A FlumeAvroManager.
- */
- public static FlumePersistentManager getManager(final String name, final Agent[] agents,
- final Property[] properties, int batchSize, final int retries,
- final int connectionTimeout, final int requestTimeout,
- final int delayMillis, final int lockTimeoutRetryCount,
- final String dataDir) {
- if (agents == null || agents.length == 0) {
- throw new IllegalArgumentException("At least one agent is required");
- }
-
- if (batchSize <= 0) {
- batchSize = 1;
- }
- final String dataDirectory = Strings.isEmpty(dataDir) ? DEFAULT_DATA_DIR : dataDir;
-
- final StringBuilder sb = new StringBuilder("FlumePersistent[");
- boolean first = true;
- for (final Agent agent : agents) {
- if (!first) {
- sb.append(',');
- }
- sb.append(agent.getHost()).append(':').append(agent.getPort());
- first = false;
- }
- sb.append(']');
- sb.append(' ').append(dataDirectory);
- return getManager(sb.toString(), factory, new FactoryData(name, agents, batchSize, retries,
- connectionTimeout, requestTimeout, delayMillis, lockTimeoutRetryCount, dataDir, properties));
- }
-
- @Override
- public void send(final Event event) {
- if (worker.isShutdown()) {
- throw new LoggingException("Unable to record event");
- }
-
- final Map<String, String> headers = event.getHeaders();
- final byte[] keyData = headers.get(FlumeEvent.GUID).getBytes(UTF8);
- try {
- final ByteArrayOutputStream baos = new ByteArrayOutputStream();
- final DataOutputStream daos = new DataOutputStream(baos);
- daos.writeInt(event.getBody().length);
- daos.write(event.getBody(), 0, event.getBody().length);
- daos.writeInt(event.getHeaders().size());
- for (final Map.Entry<String, String> entry : headers.entrySet()) {
- daos.writeUTF(entry.getKey());
- daos.writeUTF(entry.getValue());
- }
- byte[] eventData = baos.toByteArray();
- if (secretKey != null) {
- final Cipher cipher = Cipher.getInstance("AES");
- cipher.init(Cipher.ENCRYPT_MODE, secretKey);
- eventData = cipher.doFinal(eventData);
- }
- final Future<Integer> future = threadPool.submit(new BDBWriter(keyData, eventData, environment, database,
- gate, dbCount, getBatchSize(), lockTimeoutRetryCount));
- boolean interrupted = false;
- int ieCount = 0;
- do {
- try {
- future.get();
- } catch (final InterruptedException ie) {
- interrupted = true;
- ++ieCount;
- }
- } while (interrupted && ieCount <= 1);
-
- } catch (final Exception ex) {
- throw new LoggingException("Exception occurred writing log event", ex);
- }
- }
-
- @Override
- protected void releaseSub(final long timeout, final TimeUnit timeUnit) {
- LOGGER.debug("Shutting down FlumePersistentManager");
- worker.shutdown();
- final long requestedTimeoutMillis = timeUnit.toMillis(timeout);
- final long shutdownWaitMillis = requestedTimeoutMillis < 0 ? SHUTDOWN_WAIT_MILLIS : requestedTimeoutMillis;
- try {
- worker.join(shutdownWaitMillis);
- } catch (final InterruptedException ie) {
- // Ignore the exception and shutdown.
- }
- ExecutorServices.shutdown(threadPool, shutdownWaitMillis, TimeUnit.MILLISECONDS, toString());
- try {
- worker.join();
- } catch (final InterruptedException ex) {
- logDebug("interrupted while waiting for worker to complete", ex);
- }
- try {
- LOGGER.debug("FlumePersistenceManager dataset status: {}", database.getStats(new StatsConfig()));
- database.close();
- } catch (final Exception ex) {
- logWarn("Failed to close database", ex);
- }
- try {
- environment.cleanLog();
- environment.close();
- } catch (final Exception ex) {
- logWarn("Failed to close environment", ex);
- }
- super.releaseSub(timeout, timeUnit);
- }
-
- private void doSend(final SimpleEvent event) {
- LOGGER.debug("Sending event to Flume");
- super.send(event);
- }
-
- /**
- * Thread for writing to Berkeley DB to avoid having interrupts close the database.
- */
- private static class BDBWriter implements Callable<Integer> {
- private final byte[] eventData;
- private final byte[] keyData;
- private final Environment environment;
- private final Database database;
- private final Gate gate;
- private final AtomicLong dbCount;
- private final long batchSize;
- private final int lockTimeoutRetryCount;
-
- public BDBWriter(final byte[] keyData, final byte[] eventData, final Environment environment,
- final Database database, final Gate gate, final AtomicLong dbCount, final long batchSize,
- final int lockTimeoutRetryCount) {
- this.keyData = keyData;
- this.eventData = eventData;
- this.environment = environment;
- this.database = database;
- this.gate = gate;
- this.dbCount = dbCount;
- this.batchSize = batchSize;
- this.lockTimeoutRetryCount = lockTimeoutRetryCount;
- }
-
- @Override
- public Integer call() throws Exception {
- final DatabaseEntry key = new DatabaseEntry(keyData);
- final DatabaseEntry data = new DatabaseEntry(eventData);
- Exception exception = null;
- for (int retryIndex = 0; retryIndex < lockTimeoutRetryCount; ++retryIndex) {
- Transaction txn = null;
- try {
- txn = environment.beginTransaction(null, null);
- try {
- database.put(txn, key, data);
- txn.commit();
- txn = null;
- if (dbCount.incrementAndGet() >= batchSize) {
- gate.open();
- }
- exception = null;
- break;
- } catch (final LockConflictException lce) {
- exception = lce;
- // Fall through and retry.
- } catch (final Exception ex) {
- if (txn != null) {
- txn.abort();
- }
- throw ex;
- } finally {
- if (txn != null) {
- txn.abort();
- txn = null;
- }
- }
- } catch (final LockConflictException lce) {
- exception = lce;
- if (txn != null) {
- try {
- txn.abort();
- txn = null;
- } catch (final Exception ex) {
- LOGGER.trace("Ignoring exception while aborting transaction during lock conflict.");
- }
- }
-
- }
- try {
- Thread.sleep(LOCK_TIMEOUT_SLEEP_MILLIS);
- } catch (final InterruptedException ie) {
- // Ignore the error
- }
- }
- if (exception != null) {
- throw exception;
- }
- return eventData.length;
- }
- }
-
- /**
- * Factory data.
- */
- private static class FactoryData {
- private final String name;
- private final Agent[] agents;
- private final int batchSize;
- private final String dataDir;
- private final int retries;
- private final int connectionTimeout;
- private final int requestTimeout;
- private final int delayMillis;
- private final int lockTimeoutRetryCount;
- private final Property[] properties;
-
- /**
- * Constructor.
- * @param name The name of the Appender.
- * @param agents The agents.
- * @param batchSize The number of events to include in a batch.
- * @param dataDir The directory for data.
- */
- public FactoryData(final String name, final Agent[] agents, final int batchSize, final int retries,
- final int connectionTimeout, final int requestTimeout, final int delayMillis,
- final int lockTimeoutRetryCount, final String dataDir, final Property[] properties) {
- this.name = name;
- this.agents = agents;
- this.batchSize = batchSize;
- this.dataDir = dataDir;
- this.retries = retries;
- this.connectionTimeout = connectionTimeout;
- this.requestTimeout = requestTimeout;
- this.delayMillis = delayMillis;
- this.lockTimeoutRetryCount = lockTimeoutRetryCount;
- this.properties = properties;
- }
- }
-
- /**
- * Avro Manager Factory.
- */
- private static class BDBManagerFactory implements ManagerFactory<FlumePersistentManager, FactoryData> {
-
- /**
- * Create the FlumeKratiManager.
- * @param name The name of the entity to manage.
- * @param data The data required to create the entity.
- * @return The FlumeKratiManager.
- */
- @Override
- public FlumePersistentManager createManager(final String name, final FactoryData data) {
- SecretKey secretKey = null;
- Database database = null;
- Environment environment = null;
-
- final Map<String, String> properties = new HashMap<>();
- if (data.properties != null) {
- for (final Property property : data.properties) {
- properties.put(property.getName(), property.getValue());
- }
- }
-
- try {
- final File dir = new File(data.dataDir);
- FileUtils.mkdir(dir, true);
- final EnvironmentConfig dbEnvConfig = new EnvironmentConfig();
- dbEnvConfig.setTransactional(true);
- dbEnvConfig.setAllowCreate(true);
- dbEnvConfig.setLockTimeout(5, TimeUnit.SECONDS);
- environment = new Environment(dir, dbEnvConfig);
- final DatabaseConfig dbConfig = new DatabaseConfig();
- dbConfig.setTransactional(true);
- dbConfig.setAllowCreate(true);
- database = environment.openDatabase(null, name, dbConfig);
- } catch (final Exception ex) {
- LOGGER.error("Could not create FlumePersistentManager", ex);
- // For consistency, close database as well as environment even though it should never happen since the
- // database is that last thing in the block above, but this does guard against a future line being
- // inserted at the end that would bomb (like some debug logging).
- if (database != null) {
- database.close();
- database = null;
- }
- if (environment != null) {
- environment.close();
- environment = null;
- }
- return null;
- }
-
- try {
- String key = null;
- for (final Map.Entry<String, String> entry : properties.entrySet()) {
- if (entry.getKey().equalsIgnoreCase(KEY_PROVIDER)) {
- key = entry.getValue();
- break;
- }
- }
- if (key != null) {
- final PluginManager manager = new PluginManager("KeyProvider");
- manager.collectPlugins();
- final Map<String, PluginType<?>> plugins = manager.getPlugins();
- if (plugins != null) {
- boolean found = false;
- for (final Map.Entry<String, PluginType<?>> entry : plugins.entrySet()) {
- if (entry.getKey().equalsIgnoreCase(key)) {
- found = true;
- final Class<?> cl = entry.getValue().getPluginClass();
- try {
- final SecretKeyProvider provider = (SecretKeyProvider) cl.newInstance();
- secretKey = provider.getSecretKey();
- LOGGER.debug("Persisting events using SecretKeyProvider {}", cl.getName());
- } catch (final Exception ex) {
- LOGGER.error("Unable to create SecretKeyProvider {}, encryption will be disabled",
- cl.getName());
- }
- break;
- }
- }
- if (!found) {
- LOGGER.error("Unable to locate SecretKey provider {}, encryption will be disabled", key);
- }
- } else {
- LOGGER.error("Unable to locate SecretKey provider {}, encryption will be disabled", key);
- }
- }
- } catch (final Exception ex) {
- LOGGER.warn("Error setting up encryption - encryption will be disabled", ex);
- }
- return new FlumePersistentManager(name, data.name, data.agents, data.batchSize, data.retries,
- data.connectionTimeout, data.requestTimeout, data.delayMillis, database, environment, secretKey,
- data.lockTimeoutRetryCount);
- }
- }
-
- /**
- * Thread that sends data to Flume and pulls it from Berkeley DB.
- */
- private static class WriterThread extends Log4jThread {
- private volatile boolean shutdown = false;
- private final Database database;
- private final Environment environment;
- private final FlumePersistentManager manager;
- private final Gate gate;
- private final SecretKey secretKey;
- private final int batchSize;
- private final AtomicLong dbCounter;
- private final int lockTimeoutRetryCount;
-
- public WriterThread(final Database database, final Environment environment,
- final FlumePersistentManager manager, final Gate gate, final int batchsize,
- final SecretKey secretKey, final AtomicLong dbCount, final int lockTimeoutRetryCount) {
- super("FlumePersistentManager-Writer");
- this.database = database;
- this.environment = environment;
- this.manager = manager;
- this.gate = gate;
- this.batchSize = batchsize;
- this.secretKey = secretKey;
- this.setDaemon(true);
- this.dbCounter = dbCount;
- this.lockTimeoutRetryCount = lockTimeoutRetryCount;
- }
-
- public void shutdown() {
- LOGGER.debug("Writer thread shutting down");
- this.shutdown = true;
- gate.open();
- }
-
- public boolean isShutdown() {
- return shutdown;
- }
-
- @Override
- public void run() {
- LOGGER.trace("WriterThread started - batch size = " + batchSize + ", delayMillis = " + manager.getDelayMillis());
- long nextBatchMillis = System.currentTimeMillis() + manager.getDelayMillis();
- while (!shutdown) {
- final long nowMillis = System.currentTimeMillis();
- final long dbCount = database.count();
- dbCounter.set(dbCount);
- if (dbCount >= batchSize || dbCount > 0 && nextBatchMillis <= nowMillis) {
- nextBatchMillis = nowMillis + manager.getDelayMillis();
- try {
- boolean errors = false;
- final DatabaseEntry key = new DatabaseEntry();
- final DatabaseEntry data = new DatabaseEntry();
-
- gate.close();
- OperationStatus status;
- if (batchSize > 1) {
- try {
- errors = sendBatch(key, data);
- } catch (final Exception ex) {
- break;
- }
- } else {
- Exception exception = null;
- for (int retryIndex = 0; retryIndex < lockTimeoutRetryCount; ++retryIndex) {
- exception = null;
- Transaction txn = null;
- Cursor cursor = null;
- try {
- txn = environment.beginTransaction(null, null);
- cursor = database.openCursor(txn, null);
- try {
- status = cursor.getFirst(key, data, LockMode.RMW);
- while (status == OperationStatus.SUCCESS) {
- final SimpleEvent event = createEvent(data);
- if (event != null) {
- try {
- manager.doSend(event);
- } catch (final Exception ioe) {
- errors = true;
- LOGGER.error("Error sending event", ioe);
- break;
- }
- try {
- cursor.delete();
- } catch (final Exception ex) {
- LOGGER.error("Unable to delete event", ex);
- }
- }
- status = cursor.getNext(key, data, LockMode.RMW);
- }
- if (cursor != null) {
- cursor.close();
- cursor = null;
- }
- txn.commit();
- txn = null;
- dbCounter.decrementAndGet();
- exception = null;
- break;
- } catch (final LockConflictException lce) {
- exception = lce;
- // Fall through and retry.
- } catch (final Exception ex) {
- LOGGER.error("Error reading or writing to database", ex);
- shutdown = true;
- break;
- } finally {
- if (cursor != null) {
- cursor.close();
- cursor = null;
- }
- if (txn != null) {
- txn.abort();
- txn = null;
- }
- }
- } catch (final LockConflictException lce) {
- exception = lce;
- if (cursor != null) {
- try {
- cursor.close();
- cursor = null;
- } catch (final Exception ex) {
- LOGGER.trace("Ignored exception closing cursor during lock conflict.");
- }
- }
- if (txn != null) {
- try {
- txn.abort();
- txn = null;
- } catch (final Exception ex) {
- LOGGER.trace("Ignored exception aborting tx during lock conflict.");
- }
- }
- }
- try {
- Thread.sleep(LOCK_TIMEOUT_SLEEP_MILLIS);
- } catch (final InterruptedException ie) {
- // Ignore the error
- }
- }
- if (exception != null) {
- LOGGER.error("Unable to read or update data base", exception);
- }
- }
- if (errors) {
- Thread.sleep(manager.getDelayMillis());
- continue;
- }
- } catch (final Exception ex) {
- LOGGER.warn("WriterThread encountered an exception. Continuing.", ex);
- }
- } else {
- if (nextBatchMillis <= nowMillis) {
- nextBatchMillis = nowMillis + manager.getDelayMillis();
- }
- try {
- final long interval = nextBatchMillis - nowMillis;
- gate.waitForOpen(interval);
- } catch (final InterruptedException ie) {
- LOGGER.warn("WriterThread interrupted, continuing");
- } catch (final Exception ex) {
- LOGGER.error("WriterThread encountered an exception waiting for work", ex);
- break;
- }
- }
- }
-
- if (batchSize > 1 && database.count() > 0) {
- final DatabaseEntry key = new DatabaseEntry();
- final DatabaseEntry data = new DatabaseEntry();
- try {
- sendBatch(key, data);
- } catch (final Exception ex) {
- LOGGER.warn("Unable to write final batch");
- }
- }
- LOGGER.trace("WriterThread exiting");
- }
-
- private boolean sendBatch(DatabaseEntry key, final DatabaseEntry data) throws Exception {
- boolean errors = false;
- OperationStatus status;
- Cursor cursor = null;
- try {
- final BatchEvent batch = new BatchEvent();
- for (int retryIndex = 0; retryIndex < lockTimeoutRetryCount; ++retryIndex) {
- try {
- cursor = database.openCursor(null, CursorConfig.DEFAULT);
- status = cursor.getFirst(key, data, null);
-
- for (int i = 0; status == OperationStatus.SUCCESS && i < batchSize; ++i) {
- final SimpleEvent event = createEvent(data);
- if (event != null) {
- batch.addEvent(event);
- }
- status = cursor.getNext(key, data, null);
- }
- break;
- } catch (final LockConflictException lce) {
- if (cursor != null) {
- try {
- cursor.close();
- cursor = null;
- } catch (final Exception ex) {
- LOGGER.trace("Ignored exception closing cursor during lock conflict.");
- }
- }
- }
- }
-
- try {
- manager.send(batch);
- } catch (final Exception ioe) {
- LOGGER.error("Error sending events", ioe);
- errors = true;
- }
- if (!errors) {
- if (cursor != null) {
- cursor.close();
- cursor = null;
- }
- Transaction txn = null;
- Exception exception = null;
- for (int retryIndex = 0; retryIndex < lockTimeoutRetryCount; ++retryIndex) {
- try {
- txn = environment.beginTransaction(null, null);
- try {
- for (final Event event : batch.getEvents()) {
- try {
- final Map<String, String> headers = event.getHeaders();
- key = new DatabaseEntry(headers.get(FlumeEvent.GUID).getBytes(UTF8));
- database.delete(txn, key);
- } catch (final Exception ex) {
- LOGGER.error("Error deleting key from database", ex);
- }
- }
- txn.commit();
- long count = dbCounter.get();
- while (!dbCounter.compareAndSet(count, count - batch.getEvents().size())) {
- count = dbCounter.get();
- }
- exception = null;
- break;
- } catch (final LockConflictException lce) {
- exception = lce;
- if (cursor != null) {
- try {
- cursor.close();
- cursor = null;
- } catch (final Exception ex) {
- LOGGER.trace("Ignored exception closing cursor during lock conflict.");
- }
- }
- if (txn != null) {
- try {
- txn.abort();
- txn = null;
- } catch (final Exception ex) {
- LOGGER.trace("Ignored exception aborting transaction during lock conflict.");
- }
- }
- } catch (final Exception ex) {
- LOGGER.error("Unable to commit transaction", ex);
- if (txn != null) {
- txn.abort();
- }
- }
- } catch (final LockConflictException lce) {
- exception = lce;
- if (cursor != null) {
- try {
- cursor.close();
- cursor = null;
- } catch (final Exception ex) {
- LOGGER.trace("Ignored exception closing cursor during lock conflict.");
- }
- }
- if (txn != null) {
- try {
- txn.abort();
- txn = null;
- } catch (final Exception ex) {
- LOGGER.trace("Ignored exception aborting transaction during lock conflict.");
- }
- }
- } finally {
- if (cursor != null) {
- cursor.close();
- cursor = null;
- }
- if (txn != null) {
- txn.abort();
- txn = null;
- }
- }
- try {
- Thread.sleep(LOCK_TIMEOUT_SLEEP_MILLIS);
- } catch (final InterruptedException ie) {
- // Ignore the error
- }
- }
- if (exception != null) {
- LOGGER.error("Unable to delete events from data base", exception);
- }
- }
- } catch (final Exception ex) {
- LOGGER.error("Error reading database", ex);
- shutdown = true;
- throw ex;
- } finally {
- if (cursor != null) {
- cursor.close();
- }
- }
-
- return errors;
- }
-
- private SimpleEvent createEvent(final DatabaseEntry data) {
- final SimpleEvent event = new SimpleEvent();
- try {
- byte[] eventData = data.getData();
- if (secretKey != null) {
- final Cipher cipher = Cipher.getInstance("AES");
- cipher.init(Cipher.DECRYPT_MODE, secretKey);
- eventData = cipher.doFinal(eventData);
- }
- final ByteArrayInputStream bais = new ByteArrayInputStream(eventData);
- final DataInputStream dais = new DataInputStream(bais);
- int length = dais.readInt();
- final byte[] bytes = new byte[length];
- dais.read(bytes, 0, length);
- event.setBody(bytes);
- length = dais.readInt();
- final Map<String, String> map = new HashMap<>(length);
- for (int i = 0; i < length; ++i) {
- final String headerKey = dais.readUTF();
- final String value = dais.readUTF();
- map.put(headerKey, value);
- }
- event.setHeaders(map);
- return event;
- } catch (final Exception ex) {
- LOGGER.error("Error retrieving event", ex);
- return null;
- }
- }
-
- }
-
- /**
- * An internal class.
- */
- private static class Gate {
-
- private boolean isOpen = false;
-
- public boolean isOpen() {
- return isOpen;
- }
-
- public synchronized void open() {
- isOpen = true;
- notifyAll();
- }
-
- public synchronized void close() {
- isOpen = false;
- }
-
- public synchronized void waitForOpen(final long timeout) throws InterruptedException {
- wait(timeout);
- }
- }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.flume.appender;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.File;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
+
+import javax.crypto.Cipher;
+import javax.crypto.SecretKey;
+
+import org.apache.flume.Event;
+import org.apache.flume.event.SimpleEvent;
+import org.apache.logging.log4j.LoggingException;
+import org.apache.logging.log4j.core.appender.ManagerFactory;
+import org.apache.logging.log4j.core.config.Property;
+import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
+import org.apache.logging.log4j.core.config.plugins.util.PluginType;
+import org.apache.logging.log4j.core.util.ExecutorServices;
+import org.apache.logging.log4j.core.util.FileUtils;
+import org.apache.logging.log4j.core.util.Log4jThread;
+import org.apache.logging.log4j.core.util.Log4jThreadFactory;
+import org.apache.logging.log4j.core.util.SecretKeyProvider;
+import org.apache.logging.log4j.util.Strings;
+
+import com.sleepycat.je.Cursor;
+import com.sleepycat.je.CursorConfig;
+import com.sleepycat.je.Database;
+import com.sleepycat.je.DatabaseConfig;
+import com.sleepycat.je.DatabaseEntry;
+import com.sleepycat.je.Environment;
+import com.sleepycat.je.EnvironmentConfig;
+import com.sleepycat.je.LockConflictException;
+import com.sleepycat.je.LockMode;
+import com.sleepycat.je.OperationStatus;
+import com.sleepycat.je.StatsConfig;
+import com.sleepycat.je.Transaction;
+
+/**
+ * Manager that persists data to Berkeley DB before passing it on to Flume.
+ */
+public class FlumePersistentManager extends FlumeAvroManager {
+
+ /** Attribute name for the key provider. */
+ public static final String KEY_PROVIDER = "keyProvider";
+
+ private static final Charset UTF8 = StandardCharsets.UTF_8;
+
+ private static final String DEFAULT_DATA_DIR = ".log4j/flumeData";
+
+ private static final long SHUTDOWN_WAIT_MILLIS = 60000;
+
+ private static final long LOCK_TIMEOUT_SLEEP_MILLIS = 500;
+
+ private static BDBManagerFactory factory = new BDBManagerFactory();
+
+ private final Database database;
+
+ private final Environment environment;
+
+ private final WriterThread worker;
+
+ private final Gate gate = new Gate();
+
+ private final SecretKey secretKey;
+
+ private final int lockTimeoutRetryCount;
+
+ private final ExecutorService threadPool;
+
+ private final AtomicLong dbCount = new AtomicLong();
+
+ /**
+ * Constructor
+ * @param name The unique name of this manager.
+ * @param shortName Original name for the Manager.
+ * @param agents An array of Agents.
+ * @param batchSize The number of events to include in a batch.
+ * @param retries The number of times to retry connecting before giving up.
+ * @param connectionTimeout The amount of time to wait for a connection to be established.
+ * @param requestTimeout The amount of time to wair for a response to a request.
+ * @param delay The amount of time to wait between retries.
+ * @param database The database to write to.
+ * @param environment The database environment.
+ * @param secretKey The SecretKey to use for encryption.
+ * @param lockTimeoutRetryCount The number of times to retry a lock timeout.
+ */
+ protected FlumePersistentManager(final String name, final String shortName, final Agent[] agents,
+ final int batchSize, final int retries, final int connectionTimeout,
+ final int requestTimeout, final int delay, final Database database,
+ final Environment environment, final SecretKey secretKey,
+ final int lockTimeoutRetryCount) {
+ super(name, shortName, agents, batchSize, delay, retries, connectionTimeout, requestTimeout);
+ this.database = database;
+ this.environment = environment;
+ dbCount.set(database.count());
+ this.worker = new WriterThread(database, environment, this, gate, batchSize, secretKey, dbCount,
+ lockTimeoutRetryCount);
+ this.worker.start();
+ this.secretKey = secretKey;
+ this.threadPool = Executors.newCachedThreadPool(Log4jThreadFactory.createDaemonThreadFactory("Flume"));
+ this.lockTimeoutRetryCount = lockTimeoutRetryCount;
+ }
+
+
+ /**
+ * Returns a FlumeAvroManager.
+ * @param name The name of the manager.
+ * @param agents The agents to use.
+ * @param properties Properties to pass to the Manager.
+ * @param batchSize The number of events to include in a batch.
+ * @param retries The number of times to retry connecting before giving up.
+ * @param connectionTimeout The amount of time to wait to establish a connection.
+ * @param requestTimeout The amount of time to wait for a response to a request.
+ * @param delayMillis Amount of time to delay before delivering a batch.
+ * @param lockTimeoutRetryCount The number of times to retry after a lock timeout.
+ * @param dataDir The location of the Berkeley database.
+ * @return A FlumeAvroManager.
+ */
+ public static FlumePersistentManager getManager(final String name, final Agent[] agents,
+ final Property[] properties, int batchSize, final int retries,
+ final int connectionTimeout, final int requestTimeout,
+ final int delayMillis, final int lockTimeoutRetryCount,
+ final String dataDir) {
+ if (agents == null || agents.length == 0) {
+ throw new IllegalArgumentException("At least one agent is required");
+ }
+
+ if (batchSize <= 0) {
+ batchSize = 1;
+ }
+ final String dataDirectory = Strings.isEmpty(dataDir) ? DEFAULT_DATA_DIR : dataDir;
+
+ final StringBuilder sb = new StringBuilder("FlumePersistent[");
+ boolean first = true;
+ for (final Agent agent : agents) {
+ if (!first) {
+ sb.append(',');
+ }
+ sb.append(agent.getHost()).append(':').append(agent.getPort());
+ first = false;
+ }
+ sb.append(']');
+ sb.append(' ').append(dataDirectory);
+ return getManager(sb.toString(), factory, new FactoryData(name, agents, batchSize, retries,
+ connectionTimeout, requestTimeout, delayMillis, lockTimeoutRetryCount, dataDir, properties));
+ }
+
+ @Override
+ public void send(final Event event) {
+ if (worker.isShutdown()) {
+ throw new LoggingException("Unable to record event");
+ }
+
+ final Map<String, String> headers = event.getHeaders();
+ final byte[] keyData = headers.get(FlumeEvent.GUID).getBytes(UTF8);
+ try {
+ final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ final DataOutputStream daos = new DataOutputStream(baos);
+ daos.writeInt(event.getBody().length);
+ daos.write(event.getBody(), 0, event.getBody().length);
+ daos.writeInt(event.getHeaders().size());
+ for (final Map.Entry<String, String> entry : headers.entrySet()) {
+ daos.writeUTF(entry.getKey());
+ daos.writeUTF(entry.getValue());
+ }
+ byte[] eventData = baos.toByteArray();
+ if (secretKey != null) {
+ final Cipher cipher = Cipher.getInstance("AES");
+ cipher.init(Cipher.ENCRYPT_MODE, secretKey);
+ eventData = cipher.doFinal(eventData);
+ }
+ final Future<Integer> future = threadPool.submit(new BDBWriter(keyData, eventData, environment, database,
+ gate, dbCount, getBatchSize(), lockTimeoutRetryCount));
+ boolean interrupted = false;
+ int ieCount = 0;
+ do {
+ try {
+ future.get();
+ } catch (final InterruptedException ie) {
+ interrupted = true;
+ ++ieCount;
+ }
+ } while (interrupted && ieCount <= 1);
+
+ } catch (final Exception ex) {
+ throw new LoggingException("Exception occurred writing log event", ex);
+ }
+ }
+
+ @Override
+ protected boolean releaseSub(final long timeout, final TimeUnit timeUnit) {
+ boolean closed = true;
+ LOGGER.debug("Shutting down FlumePersistentManager");
+ worker.shutdown();
+ final long requestedTimeoutMillis = timeUnit.toMillis(timeout);
+ final long shutdownWaitMillis = requestedTimeoutMillis < 0 ? SHUTDOWN_WAIT_MILLIS : requestedTimeoutMillis;
+ try {
+ worker.join(shutdownWaitMillis);
+ } catch (final InterruptedException ie) {
+ // Ignore the exception and shutdown.
+ }
+ ExecutorServices.shutdown(threadPool, shutdownWaitMillis, TimeUnit.MILLISECONDS, toString());
+ try {
+ worker.join();
+ } catch (final InterruptedException ex) {
+ logDebug("interrupted while waiting for worker to complete", ex);
+ }
+ try {
+ LOGGER.debug("FlumePersistenceManager dataset status: {}", database.getStats(new StatsConfig()));
+ database.close();
+ } catch (final Exception ex) {
+ logWarn("Failed to close database", ex);
+ closed = false;
+ }
+ try {
+ environment.cleanLog();
+ environment.close();
+ } catch (final Exception ex) {
+ logWarn("Failed to close environment", ex);
+ closed = false;
+ }
+ return closed && super.releaseSub(timeout, timeUnit);
+ }
+
+ private void doSend(final SimpleEvent event) {
+ LOGGER.debug("Sending event to Flume");
+ super.send(event);
+ }
+
+ /**
+ * Thread for writing to Berkeley DB to avoid having interrupts close the database.
+ */
+ private static class BDBWriter implements Callable<Integer> {
+ private final byte[] eventData;
+ private final byte[] keyData;
+ private final Environment environment;
+ private final Database database;
+ private final Gate gate;
+ private final AtomicLong dbCount;
+ private final long batchSize;
+ private final int lockTimeoutRetryCount;
+
+ public BDBWriter(final byte[] keyData, final byte[] eventData, final Environment environment,
+ final Database database, final Gate gate, final AtomicLong dbCount, final long batchSize,
+ final int lockTimeoutRetryCount) {
+ this.keyData = keyData;
+ this.eventData = eventData;
+ this.environment = environment;
+ this.database = database;
+ this.gate = gate;
+ this.dbCount = dbCount;
+ this.batchSize = batchSize;
+ this.lockTimeoutRetryCount = lockTimeoutRetryCount;
+ }
+
+ @Override
+ public Integer call() throws Exception {
+ final DatabaseEntry key = new DatabaseEntry(keyData);
+ final DatabaseEntry data = new DatabaseEntry(eventData);
+ Exception exception = null;
+ for (int retryIndex = 0; retryIndex < lockTimeoutRetryCount; ++retryIndex) {
+ Transaction txn = null;
+ try {
+ txn = environment.beginTransaction(null, null);
+ try {
+ database.put(txn, key, data);
+ txn.commit();
+ txn = null;
+ if (dbCount.incrementAndGet() >= batchSize) {
+ gate.open();
+ }
+ exception = null;
+ break;
+ } catch (final LockConflictException lce) {
+ exception = lce;
+ // Fall through and retry.
+ } catch (final Exception ex) {
+ if (txn != null) {
+ txn.abort();
+ }
+ throw ex;
+ } finally {
+ if (txn != null) {
+ txn.abort();
+ txn = null;
+ }
+ }
+ } catch (final LockConflictException lce) {
+ exception = lce;
+ if (txn != null) {
+ try {
+ txn.abort();
+ txn = null;
+ } catch (final Exception ex) {
+ LOGGER.trace("Ignoring exception while aborting transaction during lock conflict.");
+ }
+ }
+
+ }
+ try {
+ Thread.sleep(LOCK_TIMEOUT_SLEEP_MILLIS);
+ } catch (final InterruptedException ie) {
+ // Ignore the error
+ }
+ }
+ if (exception != null) {
+ throw exception;
+ }
+ return eventData.length;
+ }
+ }
+
+ /**
+ * Factory data.
+ */
+ private static class FactoryData {
+ private final String name;
+ private final Agent[] agents;
+ private final int batchSize;
+ private final String dataDir;
+ private final int retries;
+ private final int connectionTimeout;
+ private final int requestTimeout;
+ private final int delayMillis;
+ private final int lockTimeoutRetryCount;
+ private final Property[] properties;
+
+ /**
+ * Constructor.
+ * @param name The name of the Appender.
+ * @param agents The agents.
+ * @param batchSize The number of events to include in a batch.
+ * @param dataDir The directory for data.
+ */
+ public FactoryData(final String name, final Agent[] agents, final int batchSize, final int retries,
+ final int connectionTimeout, final int requestTimeout, final int delayMillis,
+ final int lockTimeoutRetryCount, final String dataDir, final Property[] properties) {
+ this.name = name;
+ this.agents = agents;
+ this.batchSize = batchSize;
+ this.dataDir = dataDir;
+ this.retries = retries;
+ this.connectionTimeout = connectionTimeout;
+ this.requestTimeout = requestTimeout;
+ this.delayMillis = delayMillis;
+ this.lockTimeoutRetryCount = lockTimeoutRetryCount;
+ this.properties = properties;
+ }
+ }
+
+ /**
+ * Avro Manager Factory.
+ */
+ private static class BDBManagerFactory implements ManagerFactory<FlumePersistentManager, FactoryData> {
+
+ /**
+ * Create the FlumeKratiManager.
+ * @param name The name of the entity to manage.
+ * @param data The data required to create the entity.
+ * @return The FlumeKratiManager.
+ */
+ @Override
+ public FlumePersistentManager createManager(final String name, final FactoryData data) {
+ SecretKey secretKey = null;
+ Database database = null;
+ Environment environment = null;
+
+ final Map<String, String> properties = new HashMap<>();
+ if (data.properties != null) {
+ for (final Property property : data.properties) {
+ properties.put(property.getName(), property.getValue());
+ }
+ }
+
+ try {
+ final File dir = new File(data.dataDir);
+ FileUtils.mkdir(dir, true);
+ final EnvironmentConfig dbEnvConfig = new EnvironmentConfig();
+ dbEnvConfig.setTransactional(true);
+ dbEnvConfig.setAllowCreate(true);
+ dbEnvConfig.setLockTimeout(5, TimeUnit.SECONDS);
+ environment = new Environment(dir, dbEnvConfig);
+ final DatabaseConfig dbConfig = new DatabaseConfig();
+ dbConfig.setTransactional(true);
+ dbConfig.setAllowCreate(true);
+ database = environment.openDatabase(null, name, dbConfig);
+ } catch (final Exception ex) {
+ LOGGER.error("Could not create FlumePersistentManager", ex);
+ // For consistency, close database as well as environment even though it should never happen since the
+ // database is that last thing in the block above, but this does guard against a future line being
+ // inserted at the end that would bomb (like some debug logging).
+ if (database != null) {
+ database.close();
+ database = null;
+ }
+ if (environment != null) {
+ environment.close();
+ environment = null;
+ }
+ return null;
+ }
+
+ try {
+ String key = null;
+ for (final Map.Entry<String, String> entry : properties.entrySet()) {
+ if (entry.getKey().equalsIgnoreCase(KEY_PROVIDER)) {
+ key = entry.getValue();
+ break;
+ }
+ }
+ if (key != null) {
+ final PluginManager manager = new PluginManager("KeyProvider");
+ manager.collectPlugins();
+ final Map<String, PluginType<?>> plugins = manager.getPlugins();
+ if (plugins != null) {
+ boolean found = false;
+ for (final Map.Entry<String, PluginType<?>> entry : plugins.entrySet()) {
+ if (entry.getKey().equalsIgnoreCase(key)) {
+ found = true;
+ final Class<?> cl = entry.getValue().getPluginClass();
+ try {
+ final SecretKeyProvider provider = (SecretKeyProvider) cl.newInstance();
+ secretKey = provider.getSecretKey();
+ LOGGER.debug("Persisting events using SecretKeyProvider {}", cl.getName());
+ } catch (final Exception ex) {
+ LOGGER.error("Unable to create SecretKeyProvider {}, encryption will be disabled",
+ cl.getName());
+ }
+ break;
+ }
+ }
+ if (!found) {
+ LOGGER.error("Unable to locate SecretKey provider {}, encryption will be disabled", key);
+ }
+ } else {
+ LOGGER.error("Unable to locate SecretKey provider {}, encryption will be disabled", key);
+ }
+ }
+ } catch (final Exception ex) {
+ LOGGER.warn("Error setting up encryption - encryption will be disabled", ex);
+ }
+ return new FlumePersistentManager(name, data.name, data.agents, data.batchSize, data.retries,
+ data.connectionTimeout, data.requestTimeout, data.delayMillis, database, environment, secretKey,
+ data.lockTimeoutRetryCount);
+ }
+ }
+
+ /**
+ * Thread that sends data to Flume and pulls it from Berkeley DB.
+ */
+ private static class WriterThread extends Log4jThread {
+ private volatile boolean shutdown = false;
+ private final Database database;
+ private final Environment environment;
+ private final FlumePersistentManager manager;
+ private final Gate gate;
+ private final SecretKey secretKey;
+ private final int batchSize;
+ private final AtomicLong dbCounter;
+ private final int lockTimeoutRetryCount;
+
+ public WriterThread(final Database database, final Environment environment,
+ final FlumePersistentManager manager, final Gate gate, final int batchsize,
+ final SecretKey secretKey, final AtomicLong dbCount, final int lockTimeoutRetryCount) {
+ super("FlumePersistentManager-Writer");
+ this.database = database;
+ this.environment = environment;
+ this.manager = manager;
+ this.gate = gate;
+ this.batchSize = batchsize;
+ this.secretKey = secretKey;
+ this.setDaemon(true);
+ this.dbCounter = dbCount;
+ this.lockTimeoutRetryCount = lockTimeoutRetryCount;
+ }
+
+ public void shutdown() {
+ LOGGER.debug("Writer thread shutting down");
+ this.shutdown = true;
+ gate.open();
+ }
+
+ public boolean isShutdown() {
+ return shutdown;
+ }
+
+ @Override
+ public void run() {
+ LOGGER.trace("WriterThread started - batch size = " + batchSize + ", delayMillis = " + manager.getDelayMillis());
+ long nextBatchMillis = System.currentTimeMillis() + manager.getDelayMillis();
+ while (!shutdown) {
+ final long nowMillis = System.currentTimeMillis();
+ final long dbCount = database.count();
+ dbCounter.set(dbCount);
+ if (dbCount >= batchSize || dbCount > 0 && nextBatchMillis <= nowMillis) {
+ nextBatchMillis = nowMillis + manager.getDelayMillis();
+ try {
+ boolean errors = false;
+ final DatabaseEntry key = new DatabaseEntry();
+ final DatabaseEntry data = new DatabaseEntry();
+
+ gate.close();
+ OperationStatus status;
+ if (batchSize > 1) {
+ try {
+ errors = sendBatch(key, data);
+ } catch (final Exception ex) {
+ break;
+ }
+ } else {
+ Exception exception = null;
+ for (int retryIndex = 0; retryIndex < lockTimeoutRetryCount; ++retryIndex) {
+ exception = null;
+ Transaction txn = null;
+ Cursor cursor = null;
+ try {
+ txn = environment.beginTransaction(null, null);
+ cursor = database.openCursor(txn, null);
+ try {
+ status = cursor.getFirst(key, data, LockMode.RMW);
+ while (status == OperationStatus.SUCCESS) {
+ final SimpleEvent event = createEvent(data);
+ if (event != null) {
+ try {
+ manager.doSend(event);
+ } catch (final Exception ioe) {
+ errors = true;
+ LOGGER.error("Error sending event", ioe);
+ break;
+ }
+ try {
+ cursor.delete();
+ } catch (final Exception ex) {
+ LOGGER.error("Unable to delete event", ex);
+ }
+ }
+ status = cursor.getNext(key, data, LockMode.RMW);
+ }
+ if (cursor != null) {
+ cursor.close();
+ cursor = null;
+ }
+ txn.commit();
+ txn = null;
+ dbCounter.decrementAndGet();
+ exception = null;
+ break;
+ } catch (final LockConflictException lce) {
+ exception = lce;
+ // Fall through and retry.
+ } catch (final Exception ex) {
+ LOGGER.error("Error reading or writing to database", ex);
+ shutdown = true;
+ break;
+ } finally {
+ if (cursor != null) {
+ cursor.close();
+ cursor = null;
+ }
+ if (txn != null) {
+ txn.abort();
+ txn = null;
+ }
+ }
+ } catch (final LockConflictException lce) {
+ exception = lce;
+ if (cursor != null) {
+ try {
+ cursor.close();
+ cursor = null;
+ } catch (final Exception ex) {
+ LOGGER.trace("Ignored exception closing cursor during lock conflict.");
+ }
+ }
+ if (txn != null) {
+ try {
+ txn.abort();
+ txn = null;
+ } catch (final Exception ex) {
+ LOGGER.trace("Ignored exception aborting tx during lock conflict.");
+ }
+ }
+ }
+ try {
+ Thread.sleep(LOCK_TIMEOUT_SLEEP_MILLIS);
+ } catch (final InterruptedException ie) {
+ // Ignore the error
+ }
+ }
+ if (exception != null) {
+ LOGGER.error("Unable to read or update data base", exception);
+ }
+ }
+ if (errors) {
+ Thread.sleep(manager.getDelayMillis());
+ continue;
+ }
+ } catch (final Exception ex) {
+ LOGGER.warn("WriterThread encountered an exception. Continuing.", ex);
+ }
+ } else {
+ if (nextBatchMillis <= nowMillis) {
+ nextBatchMillis = nowMillis + manager.getDelayMillis();
+ }
+ try {
+ final long interval = nextBatchMillis - nowMillis;
+ gate.waitForOpen(interval);
+ } catch (final InterruptedException ie) {
+ LOGGER.warn("WriterThread interrupted, continuing");
+ } catch (final Exception ex) {
+ LOGGER.error("WriterThread encountered an exception waiting for work", ex);
+ break;
+ }
+ }
+ }
+
+ if (batchSize > 1 && database.count() > 0) {
+ final DatabaseEntry key = new DatabaseEntry();
+ final DatabaseEntry data = new DatabaseEntry();
+ try {
+ sendBatch(key, data);
+ } catch (final Exception ex) {
+ LOGGER.warn("Unable to write final batch");
+ }
+ }
+ LOGGER.trace("WriterThread exiting");
+ }
+
+ private boolean sendBatch(DatabaseEntry key, final DatabaseEntry data) throws Exception {
+ boolean errors = false;
+ OperationStatus status;
+ Cursor cursor = null;
+ try {
+ final BatchEvent batch = new BatchEvent();
+ for (int retryIndex = 0; retryIndex < lockTimeoutRetryCount; ++retryIndex) {
+ try {
+ cursor = database.openCursor(null, CursorConfig.DEFAULT);
+ status = cursor.getFirst(key, data, null);
+
+ for (int i = 0; status == OperationStatus.SUCCESS && i < batchSize; ++i) {
+ final SimpleEvent event = createEvent(data);
+ if (event != null) {
+ batch.addEvent(event);
+ }
+ status = cursor.getNext(key, data, null);
+ }
+ break;
+ } catch (final LockConflictException lce) {
+ if (cursor != null) {
+ try {
+ cursor.close();
+ cursor = null;
+ } catch (final Exception ex) {
+ LOGGER.trace("Ignored exception closing cursor during lock conflict.");
+ }
+ }
+ }
+ }
+
+ try {
+ manager.send(batch);
+ } catch (final Exception ioe) {
+ LOGGER.error("Error sending events", ioe);
+ errors = true;
+ }
+ if (!errors) {
+ if (cursor != null) {
+ cursor.close();
+ cursor = null;
+ }
+ Transaction txn = null;
+ Exception exception = null;
+ for (int retryIndex = 0; retryIndex < lockTimeoutRetryCount; ++retryIndex) {
+ try {
+ txn = environment.beginTransaction(null, null);
+ try {
+ for (final Event event : batch.getEvents()) {
+ try {
+ final Map<String, String> headers = event.getHeaders();
+ key = new DatabaseEntry(headers.get(FlumeEvent.GUID).getBytes(UTF8));
+ database.delete(txn, key);
+ } catch (final Exception ex) {
+ LOGGER.error("Error deleting key from database", ex);
+ }
+ }
+ txn.commit();
+ long count = dbCounter.get();
+ while (!dbCounter.compareAndSet(count, count - batch.getEvents().size())) {
+ count = dbCounter.get();
+ }
+ exception = null;
+ break;
+ } catch (final LockConflictException lce) {
+ exception = lce;
+ if (cursor != null) {
+ try {
+ cursor.close();
+ cursor = null;
+ } catch (final Exception ex) {
+ LOGGER.trace("Ignored exception closing cursor during lock conflict.");
+ }
+ }
+ if (txn != null) {
+ try {
+ txn.abort();
+ txn = null;
+ } catch (final Exception ex) {
+ LOGGER.trace("Ignored exception aborting transaction during lock conflict.");
+ }
+ }
+ } catch (final Exception ex) {
+ LOGGER.error("Unable to commit transaction", ex);
+ if (txn != null) {
+ txn.abort();
+ }
+ }
+ } catch (final LockConflictException lce) {
+ exception = lce;
+ if (cursor != null) {
+ try {
+ cursor.close();
+ cursor = null;
+ } catch (final Exception ex) {
+ LOGGER.trace("Ignored exception closing cursor during lock conflict.");
+ }
+ }
+ if (txn != null) {
+ try {
+ txn.abort();
+ txn = null;
+ } catch (final Exception ex) {
+ LOGGER.trace("Ignored exception aborting transaction during lock conflict.");
+ }
+ }
+ } finally {
+ if (cursor != null) {
+ cursor.close();
+ cursor = null;
+ }
+ if (txn != null) {
+ txn.abort();
+ txn = null;
+ }
+ }
+ try {
+ Thread.sleep(LOCK_TIMEOUT_SLEEP_MILLIS);
+ } catch (final InterruptedException ie) {
+ // Ignore the error
+ }
+ }
+ if (exception != null) {
+ LOGGER.error("Unable to delete events from data base", exception);
+ }
+ }
+ } catch (final Exception ex) {
+ LOGGER.error("Error reading database", ex);
+ shutdown = true;
+ throw ex;
+ } finally {
+ if (cursor != null) {
+ cursor.close();
+ }
+ }
+
+ return errors;
+ }
+
+ private SimpleEvent createEvent(final DatabaseEntry data) {
+ final SimpleEvent event = new SimpleEvent();
+ try {
+ byte[] eventData = data.getData();
+ if (secretKey != null) {
+ final Cipher cipher = Cipher.getInstance("AES");
+ cipher.init(Cipher.DECRYPT_MODE, secretKey);
+ eventData = cipher.doFinal(eventData);
+ }
+ final ByteArrayInputStream bais = new ByteArrayInputStream(eventData);
+ final DataInputStream dais = new DataInputStream(bais);
+ int length = dais.readInt();
+ final byte[] bytes = new byte[length];
+ dais.read(bytes, 0, length);
+ event.setBody(bytes);
+ length = dais.readInt();
+ final Map<String, String> map = new HashMap<>(length);
+ for (int i = 0; i < length; ++i) {
+ final String headerKey = dais.readUTF();
+ final String value = dais.readUTF();
+ map.put(headerKey, value);
+ }
+ event.setHeaders(map);
+ return event;
+ } catch (final Exception ex) {
+ LOGGER.error("Error retrieving event", ex);
+ return null;
+ }
+ }
+
+ }
+
+ /**
+ * An internal class.
+ */
+ private static class Gate {
+
+ private boolean isOpen = false;
+
+ public boolean isOpen() {
+ return isOpen;
+ }
+
+ public synchronized void open() {
+ isOpen = true;
+ notifyAll();
+ }
+
+ public synchronized void close() {
+ isOpen = false;
+ }
+
+ public synchronized void waitForOpen(final long timeout) throws InterruptedException {
+ wait(timeout);
+ }
+ }
+}
[5/7] logging-log4j2 git commit: Properly track resources during
shutdown.
Posted by gg...@apache.org.
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/78fe32c0/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/jeromq/JeroMqAppender.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/jeromq/JeroMqAppender.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/jeromq/JeroMqAppender.java
index 5a60653..96b2f57 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/jeromq/JeroMqAppender.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/jeromq/JeroMqAppender.java
@@ -1,185 +1,185 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-
-package org.apache.logging.log4j.core.appender.mom.jeromq;
-
-import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.concurrent.TimeUnit;
-
-import org.apache.logging.log4j.core.Appender;
-import org.apache.logging.log4j.core.Filter;
-import org.apache.logging.log4j.core.Layout;
-import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.core.appender.AbstractAppender;
-import org.apache.logging.log4j.core.config.Node;
-import org.apache.logging.log4j.core.config.Property;
-import org.apache.logging.log4j.core.config.plugins.Plugin;
-import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
-import org.apache.logging.log4j.core.config.plugins.PluginElement;
-import org.apache.logging.log4j.core.config.plugins.PluginFactory;
-import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
-import org.apache.logging.log4j.core.layout.PatternLayout;
-import org.apache.logging.log4j.util.Strings;
-
-/**
- * Sends log events to one or more ZeroMQ (JeroMQ) endpoints.
- * <p>
- * Requires the JeroMQ jar (LGPL as of 0.3.5)
- * </p>
- */
-// TODO
-// Some methods are synchronized because a ZMQ.Socket is not thread-safe
-// Using a ThreadLocal for the publisher hangs tests on shutdown. There must be
-// some issue on threads owning certain resources as opposed to others.
-@Plugin(name = "JeroMQ", category = Node.CATEGORY, elementType = Appender.ELEMENT_TYPE, printObject = true)
-public final class JeroMqAppender extends AbstractAppender {
-
- private static final int DEFAULT_BACKLOG = 100;
-
- private static final int DEFAULT_IVL = 100;
-
- private static final int DEFAULT_RCV_HWM = 1000;
-
- private static final int DEFAULT_SND_HWM = 1000;
-
- private final JeroMqManager manager;
- private final List<String> endpoints;
- private int sendRcFalse;
- private int sendRcTrue;
-
- private JeroMqAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout,
- final boolean ignoreExceptions, final List<String> endpoints, final long affinity, final long backlog,
- final boolean delayAttachOnConnect, final byte[] identity, final boolean ipv4Only, final long linger,
- final long maxMsgSize, final long rcvHwm, final long receiveBufferSize, final int receiveTimeOut,
- final long reconnectIVL, final long reconnectIVLMax, final long sendBufferSize, final int sendTimeOut,
- final long sndHWM, final int tcpKeepAlive, final long tcpKeepAliveCount, final long tcpKeepAliveIdle,
- final long tcpKeepAliveInterval, final boolean xpubVerbose) {
- super(name, filter, layout, ignoreExceptions);
- this.manager = JeroMqManager.getJeroMqManager(name, affinity, backlog, delayAttachOnConnect, identity, ipv4Only,
- linger, maxMsgSize, rcvHwm, receiveBufferSize, receiveTimeOut, reconnectIVL, reconnectIVLMax,
- sendBufferSize, sendTimeOut, sndHWM, tcpKeepAlive, tcpKeepAliveCount, tcpKeepAliveIdle,
- tcpKeepAliveInterval, xpubVerbose, endpoints);
- this.endpoints = endpoints;
- }
-
- // 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("name") final String name,
- @PluginElement("Layout") Layout<?> layout,
- @PluginElement("Filter") final Filter filter,
- @PluginElement("Properties") final Property[] properties,
- // Super attributes
- @PluginAttribute("ignoreExceptions") final boolean ignoreExceptions,
- // ZMQ attributes; defaults picked from zmq.Options.
- @PluginAttribute(value = "affinity", defaultLong = 0) final long affinity,
- @PluginAttribute(value = "backlog", defaultLong = DEFAULT_BACKLOG) final long backlog,
- @PluginAttribute(value = "delayAttachOnConnect", defaultBoolean = false) final boolean delayAttachOnConnect,
- @PluginAttribute(value = "identity") final byte[] identity,
- @PluginAttribute(value = "ipv4Only", defaultBoolean = true) final boolean ipv4Only,
- @PluginAttribute(value = "linger", defaultLong = -1) final long linger,
- @PluginAttribute(value = "maxMsgSize", defaultLong = -1) final long maxMsgSize,
- @PluginAttribute(value = "rcvHwm", defaultLong = DEFAULT_RCV_HWM) final long rcvHwm,
- @PluginAttribute(value = "receiveBufferSize", defaultLong = 0) final long receiveBufferSize,
- @PluginAttribute(value = "receiveTimeOut", defaultLong = -1) final int receiveTimeOut,
- @PluginAttribute(value = "reconnectIVL", defaultLong = DEFAULT_IVL) final long reconnectIVL,
- @PluginAttribute(value = "reconnectIVLMax", defaultLong = 0) final long reconnectIVLMax,
- @PluginAttribute(value = "sendBufferSize", defaultLong = 0) final long sendBufferSize,
- @PluginAttribute(value = "sendTimeOut", defaultLong = -1) final int sendTimeOut,
- @PluginAttribute(value = "sndHwm", defaultLong = DEFAULT_SND_HWM) final long sndHwm,
- @PluginAttribute(value = "tcpKeepAlive", defaultInt = -1) final int tcpKeepAlive,
- @PluginAttribute(value = "tcpKeepAliveCount", defaultLong = -1) final long tcpKeepAliveCount,
- @PluginAttribute(value = "tcpKeepAliveIdle", defaultLong = -1) final long tcpKeepAliveIdle,
- @PluginAttribute(value = "tcpKeepAliveInterval", defaultLong = -1) final long tcpKeepAliveInterval,
- @PluginAttribute(value = "xpubVerbose", defaultBoolean = false) 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);
- }
-
- @Override
- public synchronized void append(final LogEvent event) {
- final Layout<? extends Serializable> layout = getLayout();
- final byte[] formattedMessage = layout.toByteArray(event);
- if (manager.send(getLayout().toByteArray(event))) {
- sendRcTrue++;
- } else {
- sendRcFalse++;
- LOGGER.error("Appender {} could not send message {} to JeroMQ {}", getName(), sendRcFalse, formattedMessage);
- }
- }
-
- @Override
- public boolean stop(final long timeout, final TimeUnit timeUnit) {
- setStopping();
- super.stop(timeout, timeUnit, false);
- manager.stop(timeout, timeUnit);
- setStopped();
- return true;
- }
-
- // not public, handy for testing
- int getSendRcFalse() {
- return sendRcFalse;
- }
-
- // not public, handy for testing
- int getSendRcTrue() {
- return sendRcTrue;
- }
-
- // not public, handy for testing
- void resetSendRcs() {
- sendRcTrue = sendRcFalse = 0;
- }
-
- @Override
- public String toString() {
- return "JeroMqAppender{" +
- "name=" + getName() +
- ", state=" + getState() +
- ", manager=" + manager +
- ", endpoints=" + endpoints +
- '}';
- }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+package org.apache.logging.log4j.core.appender.mom.jeromq;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.logging.log4j.core.Appender;
+import org.apache.logging.log4j.core.Filter;
+import org.apache.logging.log4j.core.Layout;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.appender.AbstractAppender;
+import org.apache.logging.log4j.core.config.Node;
+import org.apache.logging.log4j.core.config.Property;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
+import org.apache.logging.log4j.core.config.plugins.PluginElement;
+import org.apache.logging.log4j.core.config.plugins.PluginFactory;
+import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
+import org.apache.logging.log4j.core.layout.PatternLayout;
+import org.apache.logging.log4j.util.Strings;
+
+/**
+ * Sends log events to one or more ZeroMQ (JeroMQ) endpoints.
+ * <p>
+ * Requires the JeroMQ jar (LGPL as of 0.3.5)
+ * </p>
+ */
+// TODO
+// Some methods are synchronized because a ZMQ.Socket is not thread-safe
+// Using a ThreadLocal for the publisher hangs tests on shutdown. There must be
+// some issue on threads owning certain resources as opposed to others.
+@Plugin(name = "JeroMQ", category = Node.CATEGORY, elementType = Appender.ELEMENT_TYPE, printObject = true)
+public final class JeroMqAppender extends AbstractAppender {
+
+ private static final int DEFAULT_BACKLOG = 100;
+
+ private static final int DEFAULT_IVL = 100;
+
+ private static final int DEFAULT_RCV_HWM = 1000;
+
+ private static final int DEFAULT_SND_HWM = 1000;
+
+ private final JeroMqManager manager;
+ private final List<String> endpoints;
+ private int sendRcFalse;
+ private int sendRcTrue;
+
+ private JeroMqAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout,
+ final boolean ignoreExceptions, final List<String> endpoints, final long affinity, final long backlog,
+ final boolean delayAttachOnConnect, final byte[] identity, final boolean ipv4Only, final long linger,
+ final long maxMsgSize, final long rcvHwm, final long receiveBufferSize, final int receiveTimeOut,
+ final long reconnectIVL, final long reconnectIVLMax, final long sendBufferSize, final int sendTimeOut,
+ final long sndHWM, final int tcpKeepAlive, final long tcpKeepAliveCount, final long tcpKeepAliveIdle,
+ final long tcpKeepAliveInterval, final boolean xpubVerbose) {
+ super(name, filter, layout, ignoreExceptions);
+ this.manager = JeroMqManager.getJeroMqManager(name, affinity, backlog, delayAttachOnConnect, identity, ipv4Only,
+ linger, maxMsgSize, rcvHwm, receiveBufferSize, receiveTimeOut, reconnectIVL, reconnectIVLMax,
+ sendBufferSize, sendTimeOut, sndHWM, tcpKeepAlive, tcpKeepAliveCount, tcpKeepAliveIdle,
+ tcpKeepAliveInterval, xpubVerbose, endpoints);
+ this.endpoints = endpoints;
+ }
+
+ // 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("name") final String name,
+ @PluginElement("Layout") Layout<?> layout,
+ @PluginElement("Filter") final Filter filter,
+ @PluginElement("Properties") final Property[] properties,
+ // Super attributes
+ @PluginAttribute("ignoreExceptions") final boolean ignoreExceptions,
+ // ZMQ attributes; defaults picked from zmq.Options.
+ @PluginAttribute(value = "affinity", defaultLong = 0) final long affinity,
+ @PluginAttribute(value = "backlog", defaultLong = DEFAULT_BACKLOG) final long backlog,
+ @PluginAttribute(value = "delayAttachOnConnect", defaultBoolean = false) final boolean delayAttachOnConnect,
+ @PluginAttribute(value = "identity") final byte[] identity,
+ @PluginAttribute(value = "ipv4Only", defaultBoolean = true) final boolean ipv4Only,
+ @PluginAttribute(value = "linger", defaultLong = -1) final long linger,
+ @PluginAttribute(value = "maxMsgSize", defaultLong = -1) final long maxMsgSize,
+ @PluginAttribute(value = "rcvHwm", defaultLong = DEFAULT_RCV_HWM) final long rcvHwm,
+ @PluginAttribute(value = "receiveBufferSize", defaultLong = 0) final long receiveBufferSize,
+ @PluginAttribute(value = "receiveTimeOut", defaultLong = -1) final int receiveTimeOut,
+ @PluginAttribute(value = "reconnectIVL", defaultLong = DEFAULT_IVL) final long reconnectIVL,
+ @PluginAttribute(value = "reconnectIVLMax", defaultLong = 0) final long reconnectIVLMax,
+ @PluginAttribute(value = "sendBufferSize", defaultLong = 0) final long sendBufferSize,
+ @PluginAttribute(value = "sendTimeOut", defaultLong = -1) final int sendTimeOut,
+ @PluginAttribute(value = "sndHwm", defaultLong = DEFAULT_SND_HWM) final long sndHwm,
+ @PluginAttribute(value = "tcpKeepAlive", defaultInt = -1) final int tcpKeepAlive,
+ @PluginAttribute(value = "tcpKeepAliveCount", defaultLong = -1) final long tcpKeepAliveCount,
+ @PluginAttribute(value = "tcpKeepAliveIdle", defaultLong = -1) final long tcpKeepAliveIdle,
+ @PluginAttribute(value = "tcpKeepAliveInterval", defaultLong = -1) final long tcpKeepAliveInterval,
+ @PluginAttribute(value = "xpubVerbose", defaultBoolean = false) 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);
+ }
+
+ @Override
+ public synchronized void append(final LogEvent event) {
+ final Layout<? extends Serializable> layout = getLayout();
+ final byte[] formattedMessage = layout.toByteArray(event);
+ if (manager.send(getLayout().toByteArray(event))) {
+ sendRcTrue++;
+ } else {
+ sendRcFalse++;
+ LOGGER.error("Appender {} could not send message {} to JeroMQ {}", getName(), sendRcFalse, formattedMessage);
+ }
+ }
+
+ @Override
+ public boolean stop(final long timeout, final TimeUnit timeUnit) {
+ setStopping();
+ boolean stopped = super.stop(timeout, timeUnit, false);
+ stopped &= manager.stop(timeout, timeUnit);
+ setStopped();
+ return stopped;
+ }
+
+ // not public, handy for testing
+ int getSendRcFalse() {
+ return sendRcFalse;
+ }
+
+ // not public, handy for testing
+ int getSendRcTrue() {
+ return sendRcTrue;
+ }
+
+ // not public, handy for testing
+ void resetSendRcs() {
+ sendRcTrue = sendRcFalse = 0;
+ }
+
+ @Override
+ public String toString() {
+ return "JeroMqAppender{" +
+ "name=" + getName() +
+ ", state=" + getState() +
+ ", manager=" + manager +
+ ", endpoints=" + endpoints +
+ '}';
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/78fe32c0/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/jeromq/JeroMqManager.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/jeromq/JeroMqManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/jeromq/JeroMqManager.java
index f731d61..a438faf 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/jeromq/JeroMqManager.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/jeromq/JeroMqManager.java
@@ -1,221 +1,222 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-
-package org.apache.logging.log4j.core.appender.mom.jeromq;
-
-import java.util.Arrays;
-import java.util.List;
-import java.util.concurrent.TimeUnit;
-
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.core.appender.AbstractManager;
-import org.apache.logging.log4j.core.appender.ManagerFactory;
-import org.apache.logging.log4j.core.util.ShutdownCallbackRegistry;
-import org.apache.logging.log4j.util.PropertiesUtil;
-import org.zeromq.ZMQ;
-
-/**
- * Manager for publishing messages via JeroMq.
- *
- * @since 2.6
- */
-public class JeroMqManager extends AbstractManager {
-
- /**
- * System property to enable shutdown hook.
- */
- public static final String SYS_PROPERTY_ENABLE_SHUTDOWN_HOOK = "log4j.jeromq.enableShutdownHook";
-
- /**
- * System property to control JeroMQ I/O thread count.
- */
- public static final String SYS_PROPERTY_IO_THREADS = "log4j.jeromq.ioThreads";
-
- private static final JeroMqManagerFactory FACTORY = new JeroMqManagerFactory();
- private static final ZMQ.Context CONTEXT;
-
- static {
- LOGGER.trace("JeroMqManager using ZMQ version {}", ZMQ.getVersionString());
-
- final int ioThreads = PropertiesUtil.getProperties().getIntegerProperty(SYS_PROPERTY_IO_THREADS, 1);
- LOGGER.trace("JeroMqManager creating ZMQ context with ioThreads = {}", ioThreads);
- CONTEXT = ZMQ.context(ioThreads);
-
- final boolean enableShutdownHook = PropertiesUtil.getProperties().getBooleanProperty(
- SYS_PROPERTY_ENABLE_SHUTDOWN_HOOK, true);
- if (enableShutdownHook) {
- ((ShutdownCallbackRegistry) LogManager.getFactory()).addShutdownCallback(new Runnable() {
- @Override
- public void run() {
- CONTEXT.close();
- }
- });
- }
- }
-
- private final ZMQ.Socket publisher;
-
- private JeroMqManager(final String name, final JeroMqConfiguration config) {
- super(null, name);
- publisher = CONTEXT.socket(ZMQ.PUB);
- publisher.setAffinity(config.affinity);
- publisher.setBacklog(config.backlog);
- publisher.setDelayAttachOnConnect(config.delayAttachOnConnect);
- if (config.identity != null) {
- publisher.setIdentity(config.identity);
- }
- publisher.setIPv4Only(config.ipv4Only);
- publisher.setLinger(config.linger);
- publisher.setMaxMsgSize(config.maxMsgSize);
- publisher.setRcvHWM(config.rcvHwm);
- publisher.setReceiveBufferSize(config.receiveBufferSize);
- publisher.setReceiveTimeOut(config.receiveTimeOut);
- publisher.setReconnectIVL(config.reconnectIVL);
- publisher.setReconnectIVLMax(config.reconnectIVLMax);
- publisher.setSendBufferSize(config.sendBufferSize);
- publisher.setSendTimeOut(config.sendTimeOut);
- publisher.setSndHWM(config.sndHwm);
- publisher.setTCPKeepAlive(config.tcpKeepAlive);
- publisher.setTCPKeepAliveCount(config.tcpKeepAliveCount);
- publisher.setTCPKeepAliveIdle(config.tcpKeepAliveIdle);
- publisher.setTCPKeepAliveInterval(config.tcpKeepAliveInterval);
- publisher.setXpubVerbose(config.xpubVerbose);
- for (final String endpoint : config.endpoints) {
- publisher.bind(endpoint);
- }
- LOGGER.debug("Created JeroMqManager with {}", config);
- }
-
- public boolean send(final byte[] data) {
- return publisher.send(data);
- }
-
- @Override
- protected void releaseSub(final long timeout, final TimeUnit timeUnit) {
- publisher.close();
- }
-
- public static JeroMqManager getJeroMqManager(final String name, final long affinity, final long backlog,
- final boolean delayAttachOnConnect, final byte[] identity,
- final boolean ipv4Only, final long linger, final long maxMsgSize,
- final long rcvHwm, final long receiveBufferSize,
- final int receiveTimeOut, final long reconnectIVL,
- final long reconnectIVLMax, final long sendBufferSize,
- final int sendTimeOut, final long sndHwm, final int tcpKeepAlive,
- final long tcpKeepAliveCount, final long tcpKeepAliveIdle,
- final long tcpKeepAliveInterval, final boolean xpubVerbose,
- final List<String> endpoints) {
- return getManager(name, FACTORY,
- new JeroMqConfiguration(affinity, backlog, delayAttachOnConnect, identity, ipv4Only, linger, maxMsgSize,
- rcvHwm, receiveBufferSize, receiveTimeOut, reconnectIVL, reconnectIVLMax, sendBufferSize, sendTimeOut,
- sndHwm, tcpKeepAlive, tcpKeepAliveCount, tcpKeepAliveIdle, tcpKeepAliveInterval, xpubVerbose,
- endpoints));
- }
-
- public static ZMQ.Context getContext() {
- return CONTEXT;
- }
-
- private static class JeroMqConfiguration {
- private final long affinity;
- private final long backlog;
- private final boolean delayAttachOnConnect;
- private final byte[] identity;
- private final boolean ipv4Only;
- private final long linger;
- private final long maxMsgSize;
- private final long rcvHwm;
- private final long receiveBufferSize;
- private final int receiveTimeOut;
- private final long reconnectIVL;
- private final long reconnectIVLMax;
- private final long sendBufferSize;
- private final int sendTimeOut;
- private final long sndHwm;
- private final int tcpKeepAlive;
- private final long tcpKeepAliveCount;
- private final long tcpKeepAliveIdle;
- private final long tcpKeepAliveInterval;
- private final boolean xpubVerbose;
- private final List<String> endpoints;
-
- private JeroMqConfiguration(final long affinity, final long backlog, final boolean delayAttachOnConnect,
- final byte[] identity, final boolean ipv4Only, final long linger,
- final long maxMsgSize, final long rcvHwm, final long receiveBufferSize,
- final int receiveTimeOut, final long reconnectIVL, final long reconnectIVLMax,
- final long sendBufferSize, final int sendTimeOut, final long sndHwm,
- final int tcpKeepAlive, final long tcpKeepAliveCount, final long tcpKeepAliveIdle,
- final long tcpKeepAliveInterval, final boolean xpubVerbose,
- final List<String> endpoints) {
- this.affinity = affinity;
- this.backlog = backlog;
- this.delayAttachOnConnect = delayAttachOnConnect;
- this.identity = identity;
- this.ipv4Only = ipv4Only;
- this.linger = linger;
- this.maxMsgSize = maxMsgSize;
- this.rcvHwm = rcvHwm;
- this.receiveBufferSize = receiveBufferSize;
- this.receiveTimeOut = receiveTimeOut;
- this.reconnectIVL = reconnectIVL;
- this.reconnectIVLMax = reconnectIVLMax;
- this.sendBufferSize = sendBufferSize;
- this.sendTimeOut = sendTimeOut;
- this.sndHwm = sndHwm;
- this.tcpKeepAlive = tcpKeepAlive;
- this.tcpKeepAliveCount = tcpKeepAliveCount;
- this.tcpKeepAliveIdle = tcpKeepAliveIdle;
- this.tcpKeepAliveInterval = tcpKeepAliveInterval;
- this.xpubVerbose = xpubVerbose;
- this.endpoints = endpoints;
- }
-
- @Override
- public String toString() {
- return "JeroMqConfiguration{" +
- "affinity=" + affinity +
- ", backlog=" + backlog +
- ", delayAttachOnConnect=" + delayAttachOnConnect +
- ", identity=" + Arrays.toString(identity) +
- ", ipv4Only=" + ipv4Only +
- ", linger=" + linger +
- ", maxMsgSize=" + maxMsgSize +
- ", rcvHwm=" + rcvHwm +
- ", receiveBufferSize=" + receiveBufferSize +
- ", receiveTimeOut=" + receiveTimeOut +
- ", reconnectIVL=" + reconnectIVL +
- ", reconnectIVLMax=" + reconnectIVLMax +
- ", sendBufferSize=" + sendBufferSize +
- ", sendTimeOut=" + sendTimeOut +
- ", sndHwm=" + sndHwm +
- ", tcpKeepAlive=" + tcpKeepAlive +
- ", tcpKeepAliveCount=" + tcpKeepAliveCount +
- ", tcpKeepAliveIdle=" + tcpKeepAliveIdle +
- ", tcpKeepAliveInterval=" + tcpKeepAliveInterval +
- ", xpubVerbose=" + xpubVerbose +
- ", endpoints=" + endpoints +
- '}';
- }
- }
-
- private static class JeroMqManagerFactory implements ManagerFactory<JeroMqManager, JeroMqConfiguration> {
- @Override
- public JeroMqManager createManager(final String name, final JeroMqConfiguration data) {
- return new JeroMqManager(name, data);
- }
- }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+package org.apache.logging.log4j.core.appender.mom.jeromq;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.appender.AbstractManager;
+import org.apache.logging.log4j.core.appender.ManagerFactory;
+import org.apache.logging.log4j.core.util.ShutdownCallbackRegistry;
+import org.apache.logging.log4j.util.PropertiesUtil;
+import org.zeromq.ZMQ;
+
+/**
+ * Manager for publishing messages via JeroMq.
+ *
+ * @since 2.6
+ */
+public class JeroMqManager extends AbstractManager {
+
+ /**
+ * System property to enable shutdown hook.
+ */
+ public static final String SYS_PROPERTY_ENABLE_SHUTDOWN_HOOK = "log4j.jeromq.enableShutdownHook";
+
+ /**
+ * System property to control JeroMQ I/O thread count.
+ */
+ public static final String SYS_PROPERTY_IO_THREADS = "log4j.jeromq.ioThreads";
+
+ private static final JeroMqManagerFactory FACTORY = new JeroMqManagerFactory();
+ private static final ZMQ.Context CONTEXT;
+
+ static {
+ LOGGER.trace("JeroMqManager using ZMQ version {}", ZMQ.getVersionString());
+
+ final int ioThreads = PropertiesUtil.getProperties().getIntegerProperty(SYS_PROPERTY_IO_THREADS, 1);
+ LOGGER.trace("JeroMqManager creating ZMQ context with ioThreads = {}", ioThreads);
+ CONTEXT = ZMQ.context(ioThreads);
+
+ final boolean enableShutdownHook = PropertiesUtil.getProperties().getBooleanProperty(
+ SYS_PROPERTY_ENABLE_SHUTDOWN_HOOK, true);
+ if (enableShutdownHook) {
+ ((ShutdownCallbackRegistry) LogManager.getFactory()).addShutdownCallback(new Runnable() {
+ @Override
+ public void run() {
+ CONTEXT.close();
+ }
+ });
+ }
+ }
+
+ private final ZMQ.Socket publisher;
+
+ private JeroMqManager(final String name, final JeroMqConfiguration config) {
+ super(null, name);
+ publisher = CONTEXT.socket(ZMQ.PUB);
+ publisher.setAffinity(config.affinity);
+ publisher.setBacklog(config.backlog);
+ publisher.setDelayAttachOnConnect(config.delayAttachOnConnect);
+ if (config.identity != null) {
+ publisher.setIdentity(config.identity);
+ }
+ publisher.setIPv4Only(config.ipv4Only);
+ publisher.setLinger(config.linger);
+ publisher.setMaxMsgSize(config.maxMsgSize);
+ publisher.setRcvHWM(config.rcvHwm);
+ publisher.setReceiveBufferSize(config.receiveBufferSize);
+ publisher.setReceiveTimeOut(config.receiveTimeOut);
+ publisher.setReconnectIVL(config.reconnectIVL);
+ publisher.setReconnectIVLMax(config.reconnectIVLMax);
+ publisher.setSendBufferSize(config.sendBufferSize);
+ publisher.setSendTimeOut(config.sendTimeOut);
+ publisher.setSndHWM(config.sndHwm);
+ publisher.setTCPKeepAlive(config.tcpKeepAlive);
+ publisher.setTCPKeepAliveCount(config.tcpKeepAliveCount);
+ publisher.setTCPKeepAliveIdle(config.tcpKeepAliveIdle);
+ publisher.setTCPKeepAliveInterval(config.tcpKeepAliveInterval);
+ publisher.setXpubVerbose(config.xpubVerbose);
+ for (final String endpoint : config.endpoints) {
+ publisher.bind(endpoint);
+ }
+ LOGGER.debug("Created JeroMqManager with {}", config);
+ }
+
+ public boolean send(final byte[] data) {
+ return publisher.send(data);
+ }
+
+ @Override
+ protected boolean releaseSub(final long timeout, final TimeUnit timeUnit) {
+ publisher.close();
+ return true;
+ }
+
+ public static JeroMqManager getJeroMqManager(final String name, final long affinity, final long backlog,
+ final boolean delayAttachOnConnect, final byte[] identity,
+ final boolean ipv4Only, final long linger, final long maxMsgSize,
+ final long rcvHwm, final long receiveBufferSize,
+ final int receiveTimeOut, final long reconnectIVL,
+ final long reconnectIVLMax, final long sendBufferSize,
+ final int sendTimeOut, final long sndHwm, final int tcpKeepAlive,
+ final long tcpKeepAliveCount, final long tcpKeepAliveIdle,
+ final long tcpKeepAliveInterval, final boolean xpubVerbose,
+ final List<String> endpoints) {
+ return getManager(name, FACTORY,
+ new JeroMqConfiguration(affinity, backlog, delayAttachOnConnect, identity, ipv4Only, linger, maxMsgSize,
+ rcvHwm, receiveBufferSize, receiveTimeOut, reconnectIVL, reconnectIVLMax, sendBufferSize, sendTimeOut,
+ sndHwm, tcpKeepAlive, tcpKeepAliveCount, tcpKeepAliveIdle, tcpKeepAliveInterval, xpubVerbose,
+ endpoints));
+ }
+
+ public static ZMQ.Context getContext() {
+ return CONTEXT;
+ }
+
+ private static class JeroMqConfiguration {
+ private final long affinity;
+ private final long backlog;
+ private final boolean delayAttachOnConnect;
+ private final byte[] identity;
+ private final boolean ipv4Only;
+ private final long linger;
+ private final long maxMsgSize;
+ private final long rcvHwm;
+ private final long receiveBufferSize;
+ private final int receiveTimeOut;
+ private final long reconnectIVL;
+ private final long reconnectIVLMax;
+ private final long sendBufferSize;
+ private final int sendTimeOut;
+ private final long sndHwm;
+ private final int tcpKeepAlive;
+ private final long tcpKeepAliveCount;
+ private final long tcpKeepAliveIdle;
+ private final long tcpKeepAliveInterval;
+ private final boolean xpubVerbose;
+ private final List<String> endpoints;
+
+ private JeroMqConfiguration(final long affinity, final long backlog, final boolean delayAttachOnConnect,
+ final byte[] identity, final boolean ipv4Only, final long linger,
+ final long maxMsgSize, final long rcvHwm, final long receiveBufferSize,
+ final int receiveTimeOut, final long reconnectIVL, final long reconnectIVLMax,
+ final long sendBufferSize, final int sendTimeOut, final long sndHwm,
+ final int tcpKeepAlive, final long tcpKeepAliveCount, final long tcpKeepAliveIdle,
+ final long tcpKeepAliveInterval, final boolean xpubVerbose,
+ final List<String> endpoints) {
+ this.affinity = affinity;
+ this.backlog = backlog;
+ this.delayAttachOnConnect = delayAttachOnConnect;
+ this.identity = identity;
+ this.ipv4Only = ipv4Only;
+ this.linger = linger;
+ this.maxMsgSize = maxMsgSize;
+ this.rcvHwm = rcvHwm;
+ this.receiveBufferSize = receiveBufferSize;
+ this.receiveTimeOut = receiveTimeOut;
+ this.reconnectIVL = reconnectIVL;
+ this.reconnectIVLMax = reconnectIVLMax;
+ this.sendBufferSize = sendBufferSize;
+ this.sendTimeOut = sendTimeOut;
+ this.sndHwm = sndHwm;
+ this.tcpKeepAlive = tcpKeepAlive;
+ this.tcpKeepAliveCount = tcpKeepAliveCount;
+ this.tcpKeepAliveIdle = tcpKeepAliveIdle;
+ this.tcpKeepAliveInterval = tcpKeepAliveInterval;
+ this.xpubVerbose = xpubVerbose;
+ this.endpoints = endpoints;
+ }
+
+ @Override
+ public String toString() {
+ return "JeroMqConfiguration{" +
+ "affinity=" + affinity +
+ ", backlog=" + backlog +
+ ", delayAttachOnConnect=" + delayAttachOnConnect +
+ ", identity=" + Arrays.toString(identity) +
+ ", ipv4Only=" + ipv4Only +
+ ", linger=" + linger +
+ ", maxMsgSize=" + maxMsgSize +
+ ", rcvHwm=" + rcvHwm +
+ ", receiveBufferSize=" + receiveBufferSize +
+ ", receiveTimeOut=" + receiveTimeOut +
+ ", reconnectIVL=" + reconnectIVL +
+ ", reconnectIVLMax=" + reconnectIVLMax +
+ ", sendBufferSize=" + sendBufferSize +
+ ", sendTimeOut=" + sendTimeOut +
+ ", sndHwm=" + sndHwm +
+ ", tcpKeepAlive=" + tcpKeepAlive +
+ ", tcpKeepAliveCount=" + tcpKeepAliveCount +
+ ", tcpKeepAliveIdle=" + tcpKeepAliveIdle +
+ ", tcpKeepAliveInterval=" + tcpKeepAliveInterval +
+ ", xpubVerbose=" + xpubVerbose +
+ ", endpoints=" + endpoints +
+ '}';
+ }
+ }
+
+ private static class JeroMqManagerFactory implements ManagerFactory<JeroMqManager, JeroMqConfiguration> {
+ @Override
+ public JeroMqManager createManager(final String name, final JeroMqConfiguration data) {
+ return new JeroMqManager(name, data);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/78fe32c0/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/kafka/KafkaAppender.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/kafka/KafkaAppender.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/kafka/KafkaAppender.java
index bee9437..787f655 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/kafka/KafkaAppender.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/kafka/KafkaAppender.java
@@ -1,120 +1,120 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-
-package org.apache.logging.log4j.core.appender.mom.kafka;
-
-import java.io.Serializable;
-import java.nio.charset.StandardCharsets;
-import java.util.concurrent.TimeUnit;
-
-import org.apache.logging.log4j.core.Appender;
-import org.apache.logging.log4j.core.Filter;
-import org.apache.logging.log4j.core.Layout;
-import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.core.appender.AbstractAppender;
-import org.apache.logging.log4j.core.appender.AppenderLoggingException;
-import org.apache.logging.log4j.core.config.Configuration;
-import org.apache.logging.log4j.core.config.Node;
-import org.apache.logging.log4j.core.config.Property;
-import org.apache.logging.log4j.core.config.plugins.Plugin;
-import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
-import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
-import org.apache.logging.log4j.core.config.plugins.PluginElement;
-import org.apache.logging.log4j.core.config.plugins.PluginFactory;
-import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
-import org.apache.logging.log4j.core.layout.SerializedLayout;
-import org.apache.logging.log4j.core.util.StringEncoder;
-
-/**
- * Sends log events to an Apache Kafka topic.
- */
-@Plugin(name = "Kafka", category = Node.CATEGORY, elementType = Appender.ELEMENT_TYPE, printObject = true)
-public final class KafkaAppender extends AbstractAppender {
-
- @PluginFactory
- public static KafkaAppender createAppender(
- @PluginElement("Layout") final Layout<? extends Serializable> layout,
- @PluginElement("Filter") final Filter filter,
- @Required(message = "No name provided for KafkaAppender") @PluginAttribute("name") final String name,
- @PluginAttribute(value = "ignoreExceptions", defaultBoolean = true) final boolean ignoreExceptions,
- @Required(message = "No topic provided for KafkaAppender") @PluginAttribute("topic") final String topic,
- @PluginElement("Properties") final Property[] properties,
- @PluginConfiguration final Configuration configuration) {
- final KafkaManager kafkaManager = new KafkaManager(configuration.getLoggerContext(), name, topic, properties);
- return new KafkaAppender(name, layout, filter, ignoreExceptions, kafkaManager);
- }
-
- private final KafkaManager manager;
-
- private KafkaAppender(final String name, final Layout<? extends Serializable> layout, final Filter filter, final boolean ignoreExceptions, final KafkaManager manager) {
- super(name, filter, layout, ignoreExceptions);
- this.manager = manager;
- }
-
- @Override
- public void append(final LogEvent event) {
- if (event.getLoggerName().startsWith("org.apache.kafka")) {
- LOGGER.warn("Recursive logging from [{}] for appender [{}].", event.getLoggerName(), getName());
- } else {
- try {
- final Layout<? extends Serializable> layout = getLayout();
- byte[] data;
- if (layout != null) {
- if (layout instanceof SerializedLayout) {
- final byte[] header = layout.getHeader();
- final byte[] body = layout.toByteArray(event);
- data = new byte[header.length + body.length];
- System.arraycopy(header, 0, data, 0, header.length);
- System.arraycopy(body, 0, data, header.length, body.length);
- } else {
- data = layout.toByteArray(event);
- }
- } else {
- data = StringEncoder.toBytes(event.getMessage().getFormattedMessage(), StandardCharsets.UTF_8);
- }
- manager.send(data);
- } catch (final Exception e) {
- LOGGER.error("Unable to write to Kafka [{}] for appender [{}].", manager.getName(), getName(), e);
- throw new AppenderLoggingException("Unable to write to Kafka in appender: " + e.getMessage(), e);
- }
- }
- }
-
- @Override
- public void start() {
- super.start();
- manager.startup();
- }
-
- @Override
- public boolean stop(final long timeout, final TimeUnit timeUnit) {
- setStopping();
- super.stop(timeout, timeUnit, false);
- manager.stop(timeout, timeUnit);
- setStopped();
- return true;
- }
-
- @Override
- public String toString() {
- return "KafkaAppender{" +
- "name=" + getName() +
- ", state=" + getState() +
- ", topic=" + manager.getTopic() +
- '}';
- }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+package org.apache.logging.log4j.core.appender.mom.kafka;
+
+import java.io.Serializable;
+import java.nio.charset.StandardCharsets;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.logging.log4j.core.Appender;
+import org.apache.logging.log4j.core.Filter;
+import org.apache.logging.log4j.core.Layout;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.appender.AbstractAppender;
+import org.apache.logging.log4j.core.appender.AppenderLoggingException;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.Node;
+import org.apache.logging.log4j.core.config.Property;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
+import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
+import org.apache.logging.log4j.core.config.plugins.PluginElement;
+import org.apache.logging.log4j.core.config.plugins.PluginFactory;
+import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
+import org.apache.logging.log4j.core.layout.SerializedLayout;
+import org.apache.logging.log4j.core.util.StringEncoder;
+
+/**
+ * Sends log events to an Apache Kafka topic.
+ */
+@Plugin(name = "Kafka", category = Node.CATEGORY, elementType = Appender.ELEMENT_TYPE, printObject = true)
+public final class KafkaAppender extends AbstractAppender {
+
+ @PluginFactory
+ public static KafkaAppender createAppender(
+ @PluginElement("Layout") final Layout<? extends Serializable> layout,
+ @PluginElement("Filter") final Filter filter,
+ @Required(message = "No name provided for KafkaAppender") @PluginAttribute("name") final String name,
+ @PluginAttribute(value = "ignoreExceptions", defaultBoolean = true) final boolean ignoreExceptions,
+ @Required(message = "No topic provided for KafkaAppender") @PluginAttribute("topic") final String topic,
+ @PluginElement("Properties") final Property[] properties,
+ @PluginConfiguration final Configuration configuration) {
+ final KafkaManager kafkaManager = new KafkaManager(configuration.getLoggerContext(), name, topic, properties);
+ return new KafkaAppender(name, layout, filter, ignoreExceptions, kafkaManager);
+ }
+
+ private final KafkaManager manager;
+
+ private KafkaAppender(final String name, final Layout<? extends Serializable> layout, final Filter filter, final boolean ignoreExceptions, final KafkaManager manager) {
+ super(name, filter, layout, ignoreExceptions);
+ this.manager = manager;
+ }
+
+ @Override
+ public void append(final LogEvent event) {
+ if (event.getLoggerName().startsWith("org.apache.kafka")) {
+ LOGGER.warn("Recursive logging from [{}] for appender [{}].", event.getLoggerName(), getName());
+ } else {
+ try {
+ final Layout<? extends Serializable> layout = getLayout();
+ byte[] data;
+ if (layout != null) {
+ if (layout instanceof SerializedLayout) {
+ final byte[] header = layout.getHeader();
+ final byte[] body = layout.toByteArray(event);
+ data = new byte[header.length + body.length];
+ System.arraycopy(header, 0, data, 0, header.length);
+ System.arraycopy(body, 0, data, header.length, body.length);
+ } else {
+ data = layout.toByteArray(event);
+ }
+ } else {
+ data = StringEncoder.toBytes(event.getMessage().getFormattedMessage(), StandardCharsets.UTF_8);
+ }
+ manager.send(data);
+ } catch (final Exception e) {
+ LOGGER.error("Unable to write to Kafka [{}] for appender [{}].", manager.getName(), getName(), e);
+ throw new AppenderLoggingException("Unable to write to Kafka in appender: " + e.getMessage(), e);
+ }
+ }
+ }
+
+ @Override
+ public void start() {
+ super.start();
+ manager.startup();
+ }
+
+ @Override
+ public boolean stop(final long timeout, final TimeUnit timeUnit) {
+ setStopping();
+ boolean stopped = super.stop(timeout, timeUnit, false);
+ stopped &= manager.stop(timeout, timeUnit);
+ setStopped();
+ return stopped;
+ }
+
+ @Override
+ public String toString() {
+ return "KafkaAppender{" +
+ "name=" + getName() +
+ ", state=" + getState() +
+ ", topic=" + manager.getTopic() +
+ '}';
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/78fe32c0/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/kafka/KafkaManager.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/kafka/KafkaManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/kafka/KafkaManager.java
index d991d7e..c5347a2 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/kafka/KafkaManager.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/kafka/KafkaManager.java
@@ -1,79 +1,80 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-
-package org.apache.logging.log4j.core.appender.mom.kafka;
-
-import java.util.Properties;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
-
-import org.apache.kafka.clients.producer.Producer;
-import org.apache.kafka.clients.producer.ProducerRecord;
-import org.apache.logging.log4j.core.LoggerContext;
-import org.apache.logging.log4j.core.appender.AbstractManager;
-import org.apache.logging.log4j.core.config.Property;
-
-public class KafkaManager extends AbstractManager {
-
- public static final String DEFAULT_TIMEOUT_MILLIS = "30000";
-
- /**
- * package-private access for testing.
- */
- static KafkaProducerFactory producerFactory = new DefaultKafkaProducerFactory();
-
- private final Properties config = new Properties();
- private Producer<byte[], byte[]> producer;
- private final int timeoutMillis;
-
- private final String topic;
-
- public KafkaManager(final LoggerContext loggerContext, final String name, final String topic, final Property[] properties) {
- super(loggerContext, name);
- this.topic = topic;
- config.setProperty("key.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer");
- config.setProperty("value.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer");
- config.setProperty("batch.size", "0");
- for (final Property property : properties) {
- config.setProperty(property.getName(), property.getValue());
- }
- this.timeoutMillis = Integer.parseInt(config.getProperty("timeout.ms", DEFAULT_TIMEOUT_MILLIS));
- }
-
- @Override
- public void releaseSub(final long timeout, final TimeUnit timeUnit) {
- if (producer != null) {
- producer.close(timeout, timeUnit);
- }
- }
-
- public void send(final byte[] msg) throws ExecutionException, InterruptedException, TimeoutException {
- if (producer != null) {
- producer.send(new ProducerRecord<byte[], byte[]>(topic, msg)).get(timeoutMillis, TimeUnit.MILLISECONDS);
- }
- }
-
- public void startup() {
- producer = producerFactory.newKafkaProducer(config);
- }
-
- public String getTopic() {
- return topic;
- }
-
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+package org.apache.logging.log4j.core.appender.mom.kafka;
+
+import java.util.Properties;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+import org.apache.kafka.clients.producer.Producer;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.appender.AbstractManager;
+import org.apache.logging.log4j.core.config.Property;
+
+public class KafkaManager extends AbstractManager {
+
+ public static final String DEFAULT_TIMEOUT_MILLIS = "30000";
+
+ /**
+ * package-private access for testing.
+ */
+ static KafkaProducerFactory producerFactory = new DefaultKafkaProducerFactory();
+
+ private final Properties config = new Properties();
+ private Producer<byte[], byte[]> producer;
+ private final int timeoutMillis;
+
+ private final String topic;
+
+ public KafkaManager(final LoggerContext loggerContext, final String name, final String topic, final Property[] properties) {
+ super(loggerContext, name);
+ this.topic = topic;
+ config.setProperty("key.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer");
+ config.setProperty("value.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer");
+ config.setProperty("batch.size", "0");
+ for (final Property property : properties) {
+ config.setProperty(property.getName(), property.getValue());
+ }
+ this.timeoutMillis = Integer.parseInt(config.getProperty("timeout.ms", DEFAULT_TIMEOUT_MILLIS));
+ }
+
+ @Override
+ public boolean releaseSub(final long timeout, final TimeUnit timeUnit) {
+ if (producer != null) {
+ producer.close(timeout, timeUnit);
+ }
+ return true;
+ }
+
+ public void send(final byte[] msg) throws ExecutionException, InterruptedException, TimeoutException {
+ if (producer != null) {
+ producer.send(new ProducerRecord<byte[], byte[]>(topic, msg)).get(timeoutMillis, TimeUnit.MILLISECONDS);
+ }
+ }
+
+ public void startup() {
+ producer = producerFactory.newKafkaProducer(config);
+ }
+
+ public String getTopic() {
+ return topic;
+ }
+
+}
[3/7] logging-log4j2 git commit: Properly track resources during
shutdown.
Posted by gg...@apache.org.
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/78fe32c0/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumeAvroManager.java
----------------------------------------------------------------------
diff --git a/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumeAvroManager.java b/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumeAvroManager.java
index 61b2392..c1dd288 100644
--- a/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumeAvroManager.java
+++ b/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumeAvroManager.java
@@ -1,333 +1,337 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-package org.apache.logging.log4j.flume.appender;
-
-import java.util.Properties;
-import java.util.concurrent.TimeUnit;
-
-import org.apache.flume.Event;
-import org.apache.flume.api.RpcClient;
-import org.apache.flume.api.RpcClientFactory;
-import org.apache.logging.log4j.core.appender.AppenderLoggingException;
-import org.apache.logging.log4j.core.appender.ManagerFactory;
-
-/**
- * Manager for FlumeAvroAppenders.
- */
-public class FlumeAvroManager extends AbstractFlumeManager {
-
- private static final int MAX_RECONNECTS = 3;
- private static final int MINIMUM_TIMEOUT = 1000;
-
- private static AvroManagerFactory factory = new AvroManagerFactory();
-
- private final Agent[] agents;
-
- private final int batchSize;
-
- private final long delayNanos;
- private final int delayMillis;
-
- private final int retries;
-
- private final int connectTimeoutMillis;
-
- private final int requestTimeoutMillis;
-
- private final int current = 0;
-
- private RpcClient rpcClient = null;
-
- private BatchEvent batchEvent = new BatchEvent();
- private long nextSend = 0;
-
- /**
- * Constructor
- * @param name The unique name of this manager.
- * @param agents An array of Agents.
- * @param batchSize The number of events to include in a batch.
- * @param retries The number of times to retry connecting before giving up.
- * @param connectTimeout The connection timeout in ms.
- * @param requestTimeout The request timeout in ms.
- *
- */
- protected FlumeAvroManager(final String name, final String shortName, final Agent[] agents, final int batchSize,
- final int delayMillis, final int retries, final int connectTimeout, final int requestTimeout) {
- super(name);
- this.agents = agents;
- this.batchSize = batchSize;
- this.delayMillis = delayMillis;
- this.delayNanos = TimeUnit.MILLISECONDS.toNanos(delayMillis);
- this.retries = retries;
- this.connectTimeoutMillis = connectTimeout;
- this.requestTimeoutMillis = requestTimeout;
- this.rpcClient = connect(agents, retries, connectTimeout, requestTimeout);
- }
-
- /**
- * Returns a FlumeAvroManager.
- * @param name The name of the manager.
- * @param agents The agents to use.
- * @param batchSize The number of events to include in a batch.
- * @param delayMillis The number of milliseconds to wait before sending an incomplete batch.
- * @param retries The number of times to retry connecting before giving up.
- * @param connectTimeoutMillis The connection timeout in ms.
- * @param requestTimeoutMillis The request timeout in ms.
- * @return A FlumeAvroManager.
- */
- public static FlumeAvroManager getManager(final String name, final Agent[] agents, int batchSize, final int delayMillis,
- final int retries, final int connectTimeoutMillis, final int requestTimeoutMillis) {
- if (agents == null || agents.length == 0) {
- throw new IllegalArgumentException("At least one agent is required");
- }
-
- if (batchSize <= 0) {
- batchSize = 1;
- }
-
- final StringBuilder sb = new StringBuilder("FlumeAvro[");
- boolean first = true;
- for (final Agent agent : agents) {
- if (!first) {
- sb.append(',');
- }
- sb.append(agent.getHost()).append(':').append(agent.getPort());
- first = false;
- }
- sb.append(']');
- return getManager(sb.toString(), factory,
- new FactoryData(name, agents, batchSize, delayMillis, retries, connectTimeoutMillis, requestTimeoutMillis));
- }
-
- /**
- * Returns the agents.
- * @return The agent array.
- */
- public Agent[] getAgents() {
- return agents;
- }
-
- /**
- * Returns the index of the current agent.
- * @return The index for the current agent.
- */
- public int getCurrent() {
- return current;
- }
-
- public int getRetries() {
- return retries;
- }
-
- public int getConnectTimeoutMillis() {
- return connectTimeoutMillis;
- }
-
- public int getRequestTimeoutMillis() {
- return requestTimeoutMillis;
- }
-
- public int getBatchSize() {
- return batchSize;
- }
-
- public int getDelayMillis() {
- return delayMillis;
- }
-
- public synchronized void send(final BatchEvent events) {
- if (rpcClient == null) {
- rpcClient = connect(agents, retries, connectTimeoutMillis, requestTimeoutMillis);
- }
-
- if (rpcClient != null) {
- try {
- LOGGER.trace("Sending batch of {} events", events.getEvents().size());
- rpcClient.appendBatch(events.getEvents());
- } catch (final Exception ex) {
- rpcClient.close();
- rpcClient = null;
- final String msg = "Unable to write to " + getName() + " at " + agents[current].getHost() + ':' +
- agents[current].getPort();
- LOGGER.warn(msg, ex);
- throw new AppenderLoggingException("No Flume agents are available");
- }
- } else {
- final String msg = "Unable to write to " + getName() + " at " + agents[current].getHost() + ':' +
- agents[current].getPort();
- LOGGER.warn(msg);
- throw new AppenderLoggingException("No Flume agents are available");
- }
- }
-
- @Override
- public synchronized void send(final Event event) {
- if (batchSize == 1) {
- if (rpcClient == null) {
- rpcClient = connect(agents, retries, connectTimeoutMillis, requestTimeoutMillis);
- }
-
- if (rpcClient != null) {
- try {
- rpcClient.append(event);
- } catch (final Exception ex) {
- rpcClient.close();
- rpcClient = null;
- final String msg = "Unable to write to " + getName() + " at " + agents[current].getHost() + ':' +
- agents[current].getPort();
- LOGGER.warn(msg, ex);
- throw new AppenderLoggingException("No Flume agents are available");
- }
- } else {
- final String msg = "Unable to write to " + getName() + " at " + agents[current].getHost() + ':' +
- agents[current].getPort();
- LOGGER.warn(msg);
- throw new AppenderLoggingException("No Flume agents are available");
- }
- } else {
- batchEvent.addEvent(event);
- final int eventCount = batchEvent.getEvents().size();
- if (eventCount == 1) {
- nextSend = System.nanoTime() + delayNanos;
- }
- if (eventCount >= batchSize || System.nanoTime() >= nextSend) {
- send(batchEvent);
- batchEvent = new BatchEvent();
- }
- }
- }
-
- /**
- * There is a very good chance that this will always return the first agent even if it isn't available.
- * @param agents The list of agents to choose from
- * @return The FlumeEventAvroServer.
- */
- private RpcClient connect(final Agent[] agents, int retries, final int connectTimeoutMillis, final int requestTimeoutMillis) {
- try {
- final Properties props = new Properties();
-
- props.put("client.type", "default_failover");
-
- int agentCount = 1;
- final StringBuilder sb = new StringBuilder();
- for (final Agent agent : agents) {
- if (sb.length() > 0) {
- sb.append(' ');
- }
- final String hostName = "host" + agentCount++;
- props.put("hosts." + hostName, agent.getHost() + ':' + agent.getPort());
- sb.append(hostName);
- }
- props.put("hosts", sb.toString());
- if (batchSize > 0) {
- props.put("batch-size", Integer.toString(batchSize));
- }
- if (retries > 1) {
- if (retries > MAX_RECONNECTS) {
- retries = MAX_RECONNECTS;
- }
- props.put("max-attempts", Integer.toString(retries * agents.length));
- }
- if (requestTimeoutMillis >= MINIMUM_TIMEOUT) {
- props.put("request-timeout", Integer.toString(requestTimeoutMillis));
- }
- if (connectTimeoutMillis >= MINIMUM_TIMEOUT) {
- props.put("connect-timeout", Integer.toString(connectTimeoutMillis));
- }
- return RpcClientFactory.getInstance(props);
- } catch (final Exception ex) {
- LOGGER.error("Unable to create Flume RPCClient: {}", ex.getMessage());
- return null;
- }
- }
-
- @Override
- protected void releaseSub(final long timeout, final TimeUnit timeUnit) {
- if (rpcClient != null) {
- try {
- synchronized(this) {
- try {
- if (batchSize > 1 && batchEvent.getEvents().size() > 0) {
- send(batchEvent);
- }
- } catch (final Exception ex) {
- LOGGER.error("Error sending final batch: {}", ex.getMessage());
- }
- }
- rpcClient.close();
- } catch (final Exception ex) {
- LOGGER.error("Attempt to close RPC client failed", ex);
- }
- }
- rpcClient = null;
- }
-
- /**
- * Factory data.
- */
- private static class FactoryData {
- private final String name;
- private final Agent[] agents;
- private final int batchSize;
- private final int delayMillis;
- private final int retries;
- private final int conntectTimeoutMillis;
- private final int requestTimeoutMillis;
-
- /**
- * Constructor.
- * @param name The name of the Appender.
- * @param agents The agents.
- * @param batchSize The number of events to include in a batch.
- */
- public FactoryData(final String name, final Agent[] agents, final int batchSize, final int delayMillis,
- final int retries, final int connectTimeoutMillis, final int requestTimeoutMillis) {
- this.name = name;
- this.agents = agents;
- this.batchSize = batchSize;
- this.delayMillis = delayMillis;
- this.retries = retries;
- this.conntectTimeoutMillis = connectTimeoutMillis;
- this.requestTimeoutMillis = requestTimeoutMillis;
- }
- }
-
- /**
- * Avro Manager Factory.
- */
- private static class AvroManagerFactory implements ManagerFactory<FlumeAvroManager, FactoryData> {
-
- /**
- * Create the FlumeAvroManager.
- * @param name The name of the entity to manage.
- * @param data The data required to create the entity.
- * @return The FlumeAvroManager.
- */
- @Override
- public FlumeAvroManager createManager(final String name, final FactoryData data) {
- try {
-
- return new FlumeAvroManager(name, data.name, data.agents, data.batchSize, data.delayMillis,
- data.retries, data.conntectTimeoutMillis, data.requestTimeoutMillis);
- } catch (final Exception ex) {
- LOGGER.error("Could not create FlumeAvroManager", ex);
- }
- return null;
- }
- }
-
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.flume.appender;
+
+import java.util.Properties;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.flume.Event;
+import org.apache.flume.api.RpcClient;
+import org.apache.flume.api.RpcClientFactory;
+import org.apache.logging.log4j.core.appender.AppenderLoggingException;
+import org.apache.logging.log4j.core.appender.ManagerFactory;
+
+/**
+ * Manager for FlumeAvroAppenders.
+ */
+public class FlumeAvroManager extends AbstractFlumeManager {
+
+ private static final int MAX_RECONNECTS = 3;
+ private static final int MINIMUM_TIMEOUT = 1000;
+
+ private static AvroManagerFactory factory = new AvroManagerFactory();
+
+ private final Agent[] agents;
+
+ private final int batchSize;
+
+ private final long delayNanos;
+ private final int delayMillis;
+
+ private final int retries;
+
+ private final int connectTimeoutMillis;
+
+ private final int requestTimeoutMillis;
+
+ private final int current = 0;
+
+ private RpcClient rpcClient = null;
+
+ private BatchEvent batchEvent = new BatchEvent();
+ private long nextSend = 0;
+
+ /**
+ * Constructor
+ * @param name The unique name of this manager.
+ * @param agents An array of Agents.
+ * @param batchSize The number of events to include in a batch.
+ * @param retries The number of times to retry connecting before giving up.
+ * @param connectTimeout The connection timeout in ms.
+ * @param requestTimeout The request timeout in ms.
+ *
+ */
+ protected FlumeAvroManager(final String name, final String shortName, final Agent[] agents, final int batchSize,
+ final int delayMillis, final int retries, final int connectTimeout, final int requestTimeout) {
+ super(name);
+ this.agents = agents;
+ this.batchSize = batchSize;
+ this.delayMillis = delayMillis;
+ this.delayNanos = TimeUnit.MILLISECONDS.toNanos(delayMillis);
+ this.retries = retries;
+ this.connectTimeoutMillis = connectTimeout;
+ this.requestTimeoutMillis = requestTimeout;
+ this.rpcClient = connect(agents, retries, connectTimeout, requestTimeout);
+ }
+
+ /**
+ * Returns a FlumeAvroManager.
+ * @param name The name of the manager.
+ * @param agents The agents to use.
+ * @param batchSize The number of events to include in a batch.
+ * @param delayMillis The number of milliseconds to wait before sending an incomplete batch.
+ * @param retries The number of times to retry connecting before giving up.
+ * @param connectTimeoutMillis The connection timeout in ms.
+ * @param requestTimeoutMillis The request timeout in ms.
+ * @return A FlumeAvroManager.
+ */
+ public static FlumeAvroManager getManager(final String name, final Agent[] agents, int batchSize, final int delayMillis,
+ final int retries, final int connectTimeoutMillis, final int requestTimeoutMillis) {
+ if (agents == null || agents.length == 0) {
+ throw new IllegalArgumentException("At least one agent is required");
+ }
+
+ if (batchSize <= 0) {
+ batchSize = 1;
+ }
+
+ final StringBuilder sb = new StringBuilder("FlumeAvro[");
+ boolean first = true;
+ for (final Agent agent : agents) {
+ if (!first) {
+ sb.append(',');
+ }
+ sb.append(agent.getHost()).append(':').append(agent.getPort());
+ first = false;
+ }
+ sb.append(']');
+ return getManager(sb.toString(), factory,
+ new FactoryData(name, agents, batchSize, delayMillis, retries, connectTimeoutMillis, requestTimeoutMillis));
+ }
+
+ /**
+ * Returns the agents.
+ * @return The agent array.
+ */
+ public Agent[] getAgents() {
+ return agents;
+ }
+
+ /**
+ * Returns the index of the current agent.
+ * @return The index for the current agent.
+ */
+ public int getCurrent() {
+ return current;
+ }
+
+ public int getRetries() {
+ return retries;
+ }
+
+ public int getConnectTimeoutMillis() {
+ return connectTimeoutMillis;
+ }
+
+ public int getRequestTimeoutMillis() {
+ return requestTimeoutMillis;
+ }
+
+ public int getBatchSize() {
+ return batchSize;
+ }
+
+ public int getDelayMillis() {
+ return delayMillis;
+ }
+
+ public synchronized void send(final BatchEvent events) {
+ if (rpcClient == null) {
+ rpcClient = connect(agents, retries, connectTimeoutMillis, requestTimeoutMillis);
+ }
+
+ if (rpcClient != null) {
+ try {
+ LOGGER.trace("Sending batch of {} events", events.getEvents().size());
+ rpcClient.appendBatch(events.getEvents());
+ } catch (final Exception ex) {
+ rpcClient.close();
+ rpcClient = null;
+ final String msg = "Unable to write to " + getName() + " at " + agents[current].getHost() + ':' +
+ agents[current].getPort();
+ LOGGER.warn(msg, ex);
+ throw new AppenderLoggingException("No Flume agents are available");
+ }
+ } else {
+ final String msg = "Unable to write to " + getName() + " at " + agents[current].getHost() + ':' +
+ agents[current].getPort();
+ LOGGER.warn(msg);
+ throw new AppenderLoggingException("No Flume agents are available");
+ }
+ }
+
+ @Override
+ public synchronized void send(final Event event) {
+ if (batchSize == 1) {
+ if (rpcClient == null) {
+ rpcClient = connect(agents, retries, connectTimeoutMillis, requestTimeoutMillis);
+ }
+
+ if (rpcClient != null) {
+ try {
+ rpcClient.append(event);
+ } catch (final Exception ex) {
+ rpcClient.close();
+ rpcClient = null;
+ final String msg = "Unable to write to " + getName() + " at " + agents[current].getHost() + ':' +
+ agents[current].getPort();
+ LOGGER.warn(msg, ex);
+ throw new AppenderLoggingException("No Flume agents are available");
+ }
+ } else {
+ final String msg = "Unable to write to " + getName() + " at " + agents[current].getHost() + ':' +
+ agents[current].getPort();
+ LOGGER.warn(msg);
+ throw new AppenderLoggingException("No Flume agents are available");
+ }
+ } else {
+ batchEvent.addEvent(event);
+ final int eventCount = batchEvent.getEvents().size();
+ if (eventCount == 1) {
+ nextSend = System.nanoTime() + delayNanos;
+ }
+ if (eventCount >= batchSize || System.nanoTime() >= nextSend) {
+ send(batchEvent);
+ batchEvent = new BatchEvent();
+ }
+ }
+ }
+
+ /**
+ * There is a very good chance that this will always return the first agent even if it isn't available.
+ * @param agents The list of agents to choose from
+ * @return The FlumeEventAvroServer.
+ */
+ private RpcClient connect(final Agent[] agents, int retries, final int connectTimeoutMillis, final int requestTimeoutMillis) {
+ try {
+ final Properties props = new Properties();
+
+ props.put("client.type", "default_failover");
+
+ int agentCount = 1;
+ final StringBuilder sb = new StringBuilder();
+ for (final Agent agent : agents) {
+ if (sb.length() > 0) {
+ sb.append(' ');
+ }
+ final String hostName = "host" + agentCount++;
+ props.put("hosts." + hostName, agent.getHost() + ':' + agent.getPort());
+ sb.append(hostName);
+ }
+ props.put("hosts", sb.toString());
+ if (batchSize > 0) {
+ props.put("batch-size", Integer.toString(batchSize));
+ }
+ if (retries > 1) {
+ if (retries > MAX_RECONNECTS) {
+ retries = MAX_RECONNECTS;
+ }
+ props.put("max-attempts", Integer.toString(retries * agents.length));
+ }
+ if (requestTimeoutMillis >= MINIMUM_TIMEOUT) {
+ props.put("request-timeout", Integer.toString(requestTimeoutMillis));
+ }
+ if (connectTimeoutMillis >= MINIMUM_TIMEOUT) {
+ props.put("connect-timeout", Integer.toString(connectTimeoutMillis));
+ }
+ return RpcClientFactory.getInstance(props);
+ } catch (final Exception ex) {
+ LOGGER.error("Unable to create Flume RPCClient: {}", ex.getMessage());
+ return null;
+ }
+ }
+
+ @Override
+ protected boolean releaseSub(final long timeout, final TimeUnit timeUnit) {
+ boolean closed = true;
+ if (rpcClient != null) {
+ try {
+ synchronized(this) {
+ try {
+ if (batchSize > 1 && batchEvent.getEvents().size() > 0) {
+ send(batchEvent);
+ }
+ } catch (final Exception ex) {
+ LOGGER.error("Error sending final batch: {}", ex.getMessage());
+ closed = false;
+ }
+ }
+ rpcClient.close();
+ } catch (final Exception ex) {
+ LOGGER.error("Attempt to close RPC client failed", ex);
+ closed = false;
+ }
+ }
+ rpcClient = null;
+ return closed;
+ }
+
+ /**
+ * Factory data.
+ */
+ private static class FactoryData {
+ private final String name;
+ private final Agent[] agents;
+ private final int batchSize;
+ private final int delayMillis;
+ private final int retries;
+ private final int conntectTimeoutMillis;
+ private final int requestTimeoutMillis;
+
+ /**
+ * Constructor.
+ * @param name The name of the Appender.
+ * @param agents The agents.
+ * @param batchSize The number of events to include in a batch.
+ */
+ public FactoryData(final String name, final Agent[] agents, final int batchSize, final int delayMillis,
+ final int retries, final int connectTimeoutMillis, final int requestTimeoutMillis) {
+ this.name = name;
+ this.agents = agents;
+ this.batchSize = batchSize;
+ this.delayMillis = delayMillis;
+ this.retries = retries;
+ this.conntectTimeoutMillis = connectTimeoutMillis;
+ this.requestTimeoutMillis = requestTimeoutMillis;
+ }
+ }
+
+ /**
+ * Avro Manager Factory.
+ */
+ private static class AvroManagerFactory implements ManagerFactory<FlumeAvroManager, FactoryData> {
+
+ /**
+ * Create the FlumeAvroManager.
+ * @param name The name of the entity to manage.
+ * @param data The data required to create the entity.
+ * @return The FlumeAvroManager.
+ */
+ @Override
+ public FlumeAvroManager createManager(final String name, final FactoryData data) {
+ try {
+
+ return new FlumeAvroManager(name, data.name, data.agents, data.batchSize, data.delayMillis,
+ data.retries, data.conntectTimeoutMillis, data.requestTimeoutMillis);
+ } catch (final Exception ex) {
+ LOGGER.error("Could not create FlumeAvroManager", ex);
+ }
+ return null;
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/78fe32c0/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumeEmbeddedManager.java
----------------------------------------------------------------------
diff --git a/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumeEmbeddedManager.java b/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumeEmbeddedManager.java
index ec5ee19..7c6840a 100644
--- a/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumeEmbeddedManager.java
+++ b/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumeEmbeddedManager.java
@@ -1,277 +1,278 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-package org.apache.logging.log4j.flume.appender;
-
-import java.util.HashMap;
-import java.util.Locale;
-import java.util.Map;
-import java.util.concurrent.TimeUnit;
-
-import org.apache.flume.Event;
-import org.apache.flume.EventDeliveryException;
-import org.apache.flume.agent.embedded.EmbeddedAgent;
-import org.apache.logging.log4j.LoggingException;
-import org.apache.logging.log4j.core.appender.ManagerFactory;
-import org.apache.logging.log4j.core.config.ConfigurationException;
-import org.apache.logging.log4j.core.config.Property;
-import org.apache.logging.log4j.core.util.NameUtil;
-import org.apache.logging.log4j.util.PropertiesUtil;
-import org.apache.logging.log4j.util.Strings;
-
-/**
- *
- */
-public class FlumeEmbeddedManager extends AbstractFlumeManager {
-
- private static final String FILE_SEP = PropertiesUtil.getProperties().getStringProperty("file.separator");
-
- private static final String IN_MEMORY = "InMemory";
-
- private static FlumeManagerFactory factory = new FlumeManagerFactory();
-
- private final EmbeddedAgent agent;
-
- private final String shortName;
-
-
- /**
- * Constructor
- * @param name The unique name of this manager.
- * @param shortName The short version of the agent name.
- * @param agent The embedded agent.
- */
- protected FlumeEmbeddedManager(final String name, final String shortName, final EmbeddedAgent agent) {
- super(name);
- this.agent = agent;
- this.shortName = shortName;
- }
-
- /**
- * Returns a FlumeEmbeddedManager.
- * @param name The name of the manager.
- * @param agents The agents to use.
- * @param properties Properties for the embedded manager.
- * @param batchSize The number of events to include in a batch.
- * @param dataDir The directory where the Flume FileChannel should write to.
- * @return A FlumeAvroManager.
- */
- public static FlumeEmbeddedManager getManager(final String name, final Agent[] agents, final Property[] properties,
- int batchSize, final String dataDir) {
-
- if (batchSize <= 0) {
- batchSize = 1;
- }
-
- if ((agents == null || agents.length == 0) && (properties == null || properties.length == 0)) {
- throw new IllegalArgumentException("Either an Agent or properties are required");
- } else if (agents != null && agents.length > 0 && properties != null && properties.length > 0) {
- throw new IllegalArgumentException("Cannot configure both Agents and Properties.");
- }
-
- final StringBuilder sb = new StringBuilder();
- boolean first = true;
-
- if (agents != null && agents.length > 0) {
- sb.append(name).append('[');
- for (final Agent agent : agents) {
- if (!first) {
- sb.append('_');
- }
- sb.append(agent.getHost()).append('-').append(agent.getPort());
- first = false;
- }
- sb.append(']');
- } else {
- String sep = Strings.EMPTY;
- sb.append(name).append('-');
- final StringBuilder props = new StringBuilder();
- for (final Property prop : properties) {
- props.append(sep);
- props.append(prop.getName()).append('=').append(prop.getValue());
- sep = "_";
- }
- sb.append(NameUtil.md5(props.toString()));
- }
- return getManager(sb.toString(), factory,
- new FactoryData(name, agents, properties, batchSize, dataDir));
- }
-
- @Override
- public void send(final Event event) {
- try {
- agent.put(event);
- } catch (final EventDeliveryException ex) {
- throw new LoggingException("Unable to deliver event to Flume Appender " + shortName, ex);
- }
- }
-
- @Override
- protected void releaseSub(final long timeout, final TimeUnit timeUnit) {
- agent.stop();
- }
-
- /**
- * Factory data.
- */
- private static class FactoryData {
- private final Agent[] agents;
- private final Property[] properties;
- private final int batchSize;
- private final String dataDir;
- private final String name;
-
- /**
- * Constructor.
- * @param name The name of the Appender.
- * @param agents The agents.
- * @param properties The Flume configuration properties.
- * @param batchSize The number of events to include in a batch.
- * @param dataDir The directory where Flume should write to.
- */
- public FactoryData(final String name, final Agent[] agents, final Property[] properties, final int batchSize,
- final String dataDir) {
- this.name = name;
- this.agents = agents;
- this.batchSize = batchSize;
- this.properties = properties;
- this.dataDir = dataDir;
- }
- }
-
- /**
- * Avro Manager Factory.
- */
- private static class FlumeManagerFactory implements ManagerFactory<FlumeEmbeddedManager, FactoryData> {
-
- /**
- * Create the FlumeAvroManager.
- * @param name The name of the entity to manage.
- * @param data The data required to create the entity.
- * @return The FlumeAvroManager.
- */
- @Override
- public FlumeEmbeddedManager createManager(final String name, final FactoryData data) {
- try {
- final Map<String, String> props = createProperties(data.name, data.agents, data.properties,
- data.batchSize, data.dataDir);
- final EmbeddedAgent agent = new EmbeddedAgent(name);
- agent.configure(props);
- agent.start();
- LOGGER.debug("Created Agent " + name);
- return new FlumeEmbeddedManager(name, data.name, agent);
- } catch (final Exception ex) {
- LOGGER.error("Could not create FlumeEmbeddedManager", ex);
- }
- return null;
- }
-
- private Map<String, String> createProperties(final String name, final Agent[] agents,
- final Property[] properties, final int batchSize, String dataDir) {
- final Map<String, String> props = new HashMap<>();
-
- if ((agents == null || agents.length == 0) && (properties == null || properties.length == 0)) {
- LOGGER.error("No Flume configuration provided");
- throw new ConfigurationException("No Flume configuration provided");
- }
-
- if (agents != null && agents.length > 0 && properties != null && properties.length > 0) {
- LOGGER.error("Agents and Flume configuration cannot both be specified");
- throw new ConfigurationException("Agents and Flume configuration cannot both be specified");
- }
-
- if (agents != null && agents.length > 0) {
-
- if (Strings.isNotEmpty(dataDir)) {
- if (dataDir.equals(IN_MEMORY)) {
- props.put("channel.type", "memory");
- } else {
- props.put("channel.type", "file");
-
- if (!dataDir.endsWith(FILE_SEP)) {
- dataDir = dataDir + FILE_SEP;
- }
-
- props.put("channel.checkpointDir", dataDir + "checkpoint");
- props.put("channel.dataDirs", dataDir + "data");
- }
-
- } else {
- props.put("channel.type", "file");
- }
-
- final StringBuilder sb = new StringBuilder();
- String leading = Strings.EMPTY;
- final int priority = agents.length;
- for (int i = 0; i < priority; ++i) {
- sb.append(leading).append("agent").append(i);
- leading = " ";
- final String prefix = "agent" + i;
- props.put(prefix + ".type", "avro");
- props.put(prefix + ".hostname", agents[i].getHost());
- props.put(prefix + ".port", Integer.toString(agents[i].getPort()));
- props.put(prefix + ".batch-size", Integer.toString(batchSize));
- props.put("processor.priority." + prefix, Integer.toString(agents.length - i));
- }
- props.put("sinks", sb.toString());
- props.put("processor.type", "failover");
- } else {
- String[] sinks = null;
-
- for (final Property property : properties) {
- final String key = property.getName();
-
- if (Strings.isEmpty(key)) {
- final String msg = "A property name must be provided";
- LOGGER.error(msg);
- throw new ConfigurationException(msg);
- }
-
- final String upperKey = key.toUpperCase(Locale.ENGLISH);
-
- if (upperKey.startsWith(name.toUpperCase(Locale.ENGLISH))) {
- final String msg =
- "Specification of the agent name is not allowed in Flume Appender configuration: " + key;
- LOGGER.error(msg);
- throw new ConfigurationException(msg);
- }
-
- final String value = property.getValue();
- if (Strings.isEmpty(value)) {
- final String msg = "A value for property " + key + " must be provided";
- LOGGER.error(msg);
- throw new ConfigurationException(msg);
- }
-
- if (upperKey.equals("SINKS")) {
- sinks = value.trim().split(" ");
- }
-
- props.put(key, value);
- }
-
- if (sinks == null || sinks.length == 0) {
- final String msg = "At least one Sink must be specified";
- LOGGER.error(msg);
- throw new ConfigurationException(msg);
- }
- }
- return props;
- }
-
- }
-
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.flume.appender;
+
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.flume.Event;
+import org.apache.flume.EventDeliveryException;
+import org.apache.flume.agent.embedded.EmbeddedAgent;
+import org.apache.logging.log4j.LoggingException;
+import org.apache.logging.log4j.core.appender.ManagerFactory;
+import org.apache.logging.log4j.core.config.ConfigurationException;
+import org.apache.logging.log4j.core.config.Property;
+import org.apache.logging.log4j.core.util.NameUtil;
+import org.apache.logging.log4j.util.PropertiesUtil;
+import org.apache.logging.log4j.util.Strings;
+
+/**
+ *
+ */
+public class FlumeEmbeddedManager extends AbstractFlumeManager {
+
+ private static final String FILE_SEP = PropertiesUtil.getProperties().getStringProperty("file.separator");
+
+ private static final String IN_MEMORY = "InMemory";
+
+ private static FlumeManagerFactory factory = new FlumeManagerFactory();
+
+ private final EmbeddedAgent agent;
+
+ private final String shortName;
+
+
+ /**
+ * Constructor
+ * @param name The unique name of this manager.
+ * @param shortName The short version of the agent name.
+ * @param agent The embedded agent.
+ */
+ protected FlumeEmbeddedManager(final String name, final String shortName, final EmbeddedAgent agent) {
+ super(name);
+ this.agent = agent;
+ this.shortName = shortName;
+ }
+
+ /**
+ * Returns a FlumeEmbeddedManager.
+ * @param name The name of the manager.
+ * @param agents The agents to use.
+ * @param properties Properties for the embedded manager.
+ * @param batchSize The number of events to include in a batch.
+ * @param dataDir The directory where the Flume FileChannel should write to.
+ * @return A FlumeAvroManager.
+ */
+ public static FlumeEmbeddedManager getManager(final String name, final Agent[] agents, final Property[] properties,
+ int batchSize, final String dataDir) {
+
+ if (batchSize <= 0) {
+ batchSize = 1;
+ }
+
+ if ((agents == null || agents.length == 0) && (properties == null || properties.length == 0)) {
+ throw new IllegalArgumentException("Either an Agent or properties are required");
+ } else if (agents != null && agents.length > 0 && properties != null && properties.length > 0) {
+ throw new IllegalArgumentException("Cannot configure both Agents and Properties.");
+ }
+
+ final StringBuilder sb = new StringBuilder();
+ boolean first = true;
+
+ if (agents != null && agents.length > 0) {
+ sb.append(name).append('[');
+ for (final Agent agent : agents) {
+ if (!first) {
+ sb.append('_');
+ }
+ sb.append(agent.getHost()).append('-').append(agent.getPort());
+ first = false;
+ }
+ sb.append(']');
+ } else {
+ String sep = Strings.EMPTY;
+ sb.append(name).append('-');
+ final StringBuilder props = new StringBuilder();
+ for (final Property prop : properties) {
+ props.append(sep);
+ props.append(prop.getName()).append('=').append(prop.getValue());
+ sep = "_";
+ }
+ sb.append(NameUtil.md5(props.toString()));
+ }
+ return getManager(sb.toString(), factory,
+ new FactoryData(name, agents, properties, batchSize, dataDir));
+ }
+
+ @Override
+ public void send(final Event event) {
+ try {
+ agent.put(event);
+ } catch (final EventDeliveryException ex) {
+ throw new LoggingException("Unable to deliver event to Flume Appender " + shortName, ex);
+ }
+ }
+
+ @Override
+ protected boolean releaseSub(final long timeout, final TimeUnit timeUnit) {
+ agent.stop();
+ return true;
+ }
+
+ /**
+ * Factory data.
+ */
+ private static class FactoryData {
+ private final Agent[] agents;
+ private final Property[] properties;
+ private final int batchSize;
+ private final String dataDir;
+ private final String name;
+
+ /**
+ * Constructor.
+ * @param name The name of the Appender.
+ * @param agents The agents.
+ * @param properties The Flume configuration properties.
+ * @param batchSize The number of events to include in a batch.
+ * @param dataDir The directory where Flume should write to.
+ */
+ public FactoryData(final String name, final Agent[] agents, final Property[] properties, final int batchSize,
+ final String dataDir) {
+ this.name = name;
+ this.agents = agents;
+ this.batchSize = batchSize;
+ this.properties = properties;
+ this.dataDir = dataDir;
+ }
+ }
+
+ /**
+ * Avro Manager Factory.
+ */
+ private static class FlumeManagerFactory implements ManagerFactory<FlumeEmbeddedManager, FactoryData> {
+
+ /**
+ * Create the FlumeAvroManager.
+ * @param name The name of the entity to manage.
+ * @param data The data required to create the entity.
+ * @return The FlumeAvroManager.
+ */
+ @Override
+ public FlumeEmbeddedManager createManager(final String name, final FactoryData data) {
+ try {
+ final Map<String, String> props = createProperties(data.name, data.agents, data.properties,
+ data.batchSize, data.dataDir);
+ final EmbeddedAgent agent = new EmbeddedAgent(name);
+ agent.configure(props);
+ agent.start();
+ LOGGER.debug("Created Agent " + name);
+ return new FlumeEmbeddedManager(name, data.name, agent);
+ } catch (final Exception ex) {
+ LOGGER.error("Could not create FlumeEmbeddedManager", ex);
+ }
+ return null;
+ }
+
+ private Map<String, String> createProperties(final String name, final Agent[] agents,
+ final Property[] properties, final int batchSize, String dataDir) {
+ final Map<String, String> props = new HashMap<>();
+
+ if ((agents == null || agents.length == 0) && (properties == null || properties.length == 0)) {
+ LOGGER.error("No Flume configuration provided");
+ throw new ConfigurationException("No Flume configuration provided");
+ }
+
+ if (agents != null && agents.length > 0 && properties != null && properties.length > 0) {
+ LOGGER.error("Agents and Flume configuration cannot both be specified");
+ throw new ConfigurationException("Agents and Flume configuration cannot both be specified");
+ }
+
+ if (agents != null && agents.length > 0) {
+
+ if (Strings.isNotEmpty(dataDir)) {
+ if (dataDir.equals(IN_MEMORY)) {
+ props.put("channel.type", "memory");
+ } else {
+ props.put("channel.type", "file");
+
+ if (!dataDir.endsWith(FILE_SEP)) {
+ dataDir = dataDir + FILE_SEP;
+ }
+
+ props.put("channel.checkpointDir", dataDir + "checkpoint");
+ props.put("channel.dataDirs", dataDir + "data");
+ }
+
+ } else {
+ props.put("channel.type", "file");
+ }
+
+ final StringBuilder sb = new StringBuilder();
+ String leading = Strings.EMPTY;
+ final int priority = agents.length;
+ for (int i = 0; i < priority; ++i) {
+ sb.append(leading).append("agent").append(i);
+ leading = " ";
+ final String prefix = "agent" + i;
+ props.put(prefix + ".type", "avro");
+ props.put(prefix + ".hostname", agents[i].getHost());
+ props.put(prefix + ".port", Integer.toString(agents[i].getPort()));
+ props.put(prefix + ".batch-size", Integer.toString(batchSize));
+ props.put("processor.priority." + prefix, Integer.toString(agents.length - i));
+ }
+ props.put("sinks", sb.toString());
+ props.put("processor.type", "failover");
+ } else {
+ String[] sinks = null;
+
+ for (final Property property : properties) {
+ final String key = property.getName();
+
+ if (Strings.isEmpty(key)) {
+ final String msg = "A property name must be provided";
+ LOGGER.error(msg);
+ throw new ConfigurationException(msg);
+ }
+
+ final String upperKey = key.toUpperCase(Locale.ENGLISH);
+
+ if (upperKey.startsWith(name.toUpperCase(Locale.ENGLISH))) {
+ final String msg =
+ "Specification of the agent name is not allowed in Flume Appender configuration: " + key;
+ LOGGER.error(msg);
+ throw new ConfigurationException(msg);
+ }
+
+ final String value = property.getValue();
+ if (Strings.isEmpty(value)) {
+ final String msg = "A value for property " + key + " must be provided";
+ LOGGER.error(msg);
+ throw new ConfigurationException(msg);
+ }
+
+ if (upperKey.equals("SINKS")) {
+ sinks = value.trim().split(" ");
+ }
+
+ props.put(key, value);
+ }
+
+ if (sinks == null || sinks.length == 0) {
+ final String msg = "At least one Sink must be specified";
+ LOGGER.error(msg);
+ throw new ConfigurationException(msg);
+ }
+ }
+ return props;
+ }
+
+ }
+
+}
[4/7] logging-log4j2 git commit: Properly track resources during
shutdown.
Posted by gg...@apache.org.
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/78fe32c0/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java
index 1fc08a0..db6e9ae 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java
@@ -1,479 +1,479 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-package org.apache.logging.log4j.core.appender.rolling;
-
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.io.Serializable;
-import java.nio.ByteBuffer;
-import java.util.concurrent.Future;
-import java.util.concurrent.Semaphore;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
-
-import org.apache.logging.log4j.core.Layout;
-import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.core.LoggerContext;
-import org.apache.logging.log4j.core.appender.ConfigurationFactoryData;
-import org.apache.logging.log4j.core.appender.FileManager;
-import org.apache.logging.log4j.core.appender.ManagerFactory;
-import org.apache.logging.log4j.core.appender.rolling.action.AbstractAction;
-import org.apache.logging.log4j.core.appender.rolling.action.Action;
-import org.apache.logging.log4j.core.config.Configuration;
-import org.apache.logging.log4j.core.util.Constants;
-
-/**
- * The Rolling File Manager.
- */
-public class RollingFileManager extends FileManager {
-
- private static RollingFileManagerFactory factory = new RollingFileManagerFactory();
-
- protected long size;
- private long initialTime;
- private final PatternProcessor patternProcessor;
- private final Semaphore semaphore = new Semaphore(1);
- private volatile TriggeringPolicy triggeringPolicy;
- private volatile RolloverStrategy rolloverStrategy;
- private volatile boolean renameEmptyFiles = false;
-
- private static final AtomicReferenceFieldUpdater<RollingFileManager, TriggeringPolicy> triggeringPolicyUpdater =
- AtomicReferenceFieldUpdater.newUpdater(RollingFileManager.class, TriggeringPolicy.class, "triggeringPolicy");
-
- private static final AtomicReferenceFieldUpdater<RollingFileManager, RolloverStrategy> rolloverStrategyUpdater =
- AtomicReferenceFieldUpdater.newUpdater(RollingFileManager.class, RolloverStrategy.class, "rolloverStrategy");
-
- @Deprecated
- protected RollingFileManager(final String fileName, final String pattern, final OutputStream os,
- final boolean append, final long size, final long time, final TriggeringPolicy triggeringPolicy,
- final RolloverStrategy rolloverStrategy, final String advertiseURI,
- final Layout<? extends Serializable> layout, final int bufferSize, final boolean writeHeader) {
- this(fileName, pattern, os, append, size, time, triggeringPolicy, rolloverStrategy, advertiseURI, layout,
- writeHeader, ByteBuffer.wrap(new byte[Constants.ENCODER_BYTE_BUFFER_SIZE]));
- }
-
- @Deprecated
- protected RollingFileManager(final String fileName, final String pattern, final OutputStream os,
- final boolean append, final long size, final long time, final TriggeringPolicy triggeringPolicy,
- final RolloverStrategy rolloverStrategy, final String advertiseURI,
- final Layout<? extends Serializable> layout, final boolean writeHeader, final ByteBuffer buffer) {
- super(fileName, os, append, false, advertiseURI, layout, writeHeader, buffer);
- this.size = size;
- this.initialTime = time;
- this.triggeringPolicy = triggeringPolicy;
- this.rolloverStrategy = rolloverStrategy;
- this.patternProcessor = new PatternProcessor(pattern);
- this.patternProcessor.setPrevFileTime(time);
- }
-
- /**
- * @since 2.7
- */
- protected RollingFileManager(final LoggerContext loggerContext, final String fileName, final String pattern, final OutputStream os,
- final boolean append, final boolean createOnDemand, final long size, final long time,
- final TriggeringPolicy triggeringPolicy, final RolloverStrategy rolloverStrategy,
- final String advertiseURI, final Layout<? extends Serializable> layout, final boolean writeHeader, final ByteBuffer buffer) {
- super(loggerContext, fileName, os, append, false, createOnDemand, advertiseURI, layout, writeHeader, buffer);
- this.size = size;
- this.initialTime = time;
- this.triggeringPolicy = triggeringPolicy;
- this.rolloverStrategy = rolloverStrategy;
- this.patternProcessor = new PatternProcessor(pattern);
- this.patternProcessor.setPrevFileTime(time);
- }
-
- public void initialize() {
- triggeringPolicy.initialize(this);
- }
-
- /**
- * Returns a RollingFileManager.
- * @param fileName The file name.
- * @param pattern The pattern for rolling file.
- * @param append true if the file should be appended to.
- * @param bufferedIO true if data should be buffered.
- * @param policy The TriggeringPolicy.
- * @param strategy The RolloverStrategy.
- * @param advertiseURI the URI to use when advertising the file
- * @param layout The Layout.
- * @param bufferSize buffer size to use if bufferedIO is true
- * @param immediateFlush flush on every write or not
- * @param createOnDemand true if you want to lazy-create the file (a.k.a. on-demand.)
- * @param configuration The configuration.
- * @return A RollingFileManager.
- */
- public static RollingFileManager getFileManager(final String fileName, final String pattern, final boolean append,
- final boolean bufferedIO, final TriggeringPolicy policy, final RolloverStrategy strategy,
- final String advertiseURI, final Layout<? extends Serializable> layout, final int bufferSize,
- final boolean immediateFlush, final boolean createOnDemand, final Configuration configuration) {
-
- return (RollingFileManager) getManager(fileName, new FactoryData(pattern, append,
- bufferedIO, policy, strategy, advertiseURI, layout, bufferSize, immediateFlush, createOnDemand, configuration), factory);
- }
-
- // override to make visible for unit tests
- @Override
- protected synchronized void write(final byte[] bytes, final int offset, final int length,
- final boolean immediateFlush) {
- super.write(bytes, offset, length, immediateFlush);
- }
-
- @Override
- protected synchronized void writeToDestination(final byte[] bytes, final int offset, final int length) {
- size += length;
- super.writeToDestination(bytes, offset, length);
- }
-
- public boolean isRenameEmptyFiles() {
- return renameEmptyFiles;
- }
-
- public void setRenameEmptyFiles(final boolean renameEmptyFiles) {
- this.renameEmptyFiles = renameEmptyFiles;
- }
-
- /**
- * Returns the current size of the file.
- * @return The size of the file in bytes.
- */
- public long getFileSize() {
- return size + byteBuffer.position();
- }
-
- /**
- * Returns the time the file was created.
- * @return The time the file was created.
- */
- public long getFileTime() {
- return initialTime;
- }
-
- /**
- * Determines if a rollover should occur.
- * @param event The LogEvent.
- */
- public synchronized void checkRollover(final LogEvent event) {
- if (triggeringPolicy.isTriggeringEvent(event)) {
- rollover();
- }
- }
-
- @Override
- public void releaseSub(final long timeout, final TimeUnit timeUnit) {
- triggeringPolicy.stop(timeout, timeUnit);
- super.releaseSub(timeout, timeUnit);
- }
-
- public synchronized void rollover() {
- if (rollover(rolloverStrategy)) {
- try {
- size = 0;
- initialTime = System.currentTimeMillis();
- createFileAfterRollover();
- } catch (final IOException e) {
- logError("Failed to create file after rollover", e);
- }
- }
- }
-
- protected void createFileAfterRollover() throws IOException {
- setOutputStream(new FileOutputStream(getFileName(), isAppend()));
- }
-
- /**
- * Returns the pattern processor.
- * @return The PatternProcessor.
- */
- public PatternProcessor getPatternProcessor() {
- return patternProcessor;
- }
-
- public void setTriggeringPolicy(final TriggeringPolicy triggeringPolicy) {
- triggeringPolicy.initialize(this);
- triggeringPolicyUpdater.compareAndSet(this, this.triggeringPolicy, triggeringPolicy);
- }
-
- public void setRolloverStrategy(final RolloverStrategy rolloverStrategy) {
- rolloverStrategyUpdater.compareAndSet(this, this.rolloverStrategy, rolloverStrategy);
- }
-
- /**
- * Returns the triggering policy.
- * @param <T> TriggeringPolicy type
- * @return The TriggeringPolicy
- */
- @SuppressWarnings("unchecked")
- public <T extends TriggeringPolicy> T getTriggeringPolicy() {
- // TODO We could parameterize this class with a TriggeringPolicy instead of type casting here.
- return (T) this.triggeringPolicy;
- }
-
- /**
- * Returns the rollover strategy.
- * @return The RolloverStrategy
- */
- public RolloverStrategy getRolloverStrategy() {
- return this.rolloverStrategy;
- }
-
- private boolean rollover(final RolloverStrategy strategy) {
-
- try {
- // Block until the asynchronous operation is completed.
- semaphore.acquire();
- } catch (final InterruptedException e) {
- logError("Thread interrupted while attempting to check rollover", e);
- return false;
- }
-
- boolean success = false;
- Future<?> future = null;
-
- try {
- final RolloverDescription descriptor = strategy.rollover(this);
- if (descriptor != null) {
- writeFooter();
- closeOutputStream();
- if (descriptor.getSynchronous() != null) {
- LOGGER.debug("RollingFileManager executing synchronous {}", descriptor.getSynchronous());
- try {
- success = descriptor.getSynchronous().execute();
- } catch (final Exception ex) {
- logError("Caught error in synchronous task", ex);
- }
- }
-
- if (success && descriptor.getAsynchronous() != null) {
- LOGGER.debug("RollingFileManager executing async {}", descriptor.getAsynchronous());
- future = LoggerContext.getContext(false).submit(new AsyncAction(descriptor.getAsynchronous(), this));
- }
- return true;
- }
- return false;
- } finally {
- if (future == null || future.isDone() || future.isCancelled()) {
- semaphore.release();
- }
- }
-
- }
-
- /**
- * Performs actions asynchronously.
- */
- private static class AsyncAction extends AbstractAction {
-
- private final Action action;
- private final RollingFileManager manager;
-
- /**
- * Constructor.
- * @param act The action to perform.
- * @param manager The manager.
- */
- public AsyncAction(final Action act, final RollingFileManager manager) {
- this.action = act;
- this.manager = manager;
- }
-
- /**
- * Executes an action.
- *
- * @return true if action was successful. A return value of false will cause
- * the rollover to be aborted if possible.
- * @throws java.io.IOException if IO error, a thrown exception will cause the rollover
- * to be aborted if possible.
- */
- @Override
- public boolean execute() throws IOException {
- try {
- return action.execute();
- } finally {
- manager.semaphore.release();
- }
- }
-
- /**
- * Cancels the action if not already initialized or waits till completion.
- */
- @Override
- public void close() {
- action.close();
- }
-
- /**
- * Determines if action has been completed.
- *
- * @return true if action is complete.
- */
- @Override
- public boolean isComplete() {
- return action.isComplete();
- }
-
- @Override
- public String toString() {
- final StringBuilder builder = new StringBuilder();
- builder.append(super.toString());
- builder.append("[action=");
- builder.append(action);
- builder.append(", manager=");
- builder.append(manager);
- builder.append(", isComplete()=");
- builder.append(isComplete());
- builder.append(", isInterrupted()=");
- builder.append(isInterrupted());
- builder.append("]");
- return builder.toString();
- }
- }
-
- /**
- * Factory data.
- */
- private static class FactoryData extends ConfigurationFactoryData {
- private final String pattern;
- private final boolean append;
- private final boolean bufferedIO;
- private final int bufferSize;
- private final boolean immediateFlush;
- private final boolean createOnDemand;
- private final TriggeringPolicy policy;
- private final RolloverStrategy strategy;
- private final String advertiseURI;
- private final Layout<? extends Serializable> layout;
-
- /**
- * Creates the data for the factory.
- * @param pattern The pattern.
- * @param append The append flag.
- * @param bufferedIO The bufferedIO flag.
- * @param advertiseURI
- * @param layout The Layout.
- * @param bufferSize the buffer size
- * @param immediateFlush flush on every write or not
- * @param createOnDemand true if you want to lazy-create the file (a.k.a. on-demand.)
- * @param configuration The configuration
- */
- public FactoryData(final String pattern, final boolean append, final boolean bufferedIO,
- final TriggeringPolicy policy, final RolloverStrategy strategy, final String advertiseURI,
- final Layout<? extends Serializable> layout, final int bufferSize, final boolean immediateFlush,
- final boolean createOnDemand, final Configuration configuration) {
- super(configuration);
- this.pattern = pattern;
- this.append = append;
- this.bufferedIO = bufferedIO;
- this.bufferSize = bufferSize;
- this.policy = policy;
- this.strategy = strategy;
- this.advertiseURI = advertiseURI;
- this.layout = layout;
- this.immediateFlush = immediateFlush;
- this.createOnDemand = createOnDemand;
- }
-
- public TriggeringPolicy getTriggeringPolicy()
- {
- return this.policy;
- }
-
- public RolloverStrategy getRolloverStrategy()
- {
- return this.strategy;
- }
-
- @Override
- public String toString() {
- final StringBuilder builder = new StringBuilder();
- builder.append(super.toString());
- builder.append("[pattern=");
- builder.append(pattern);
- builder.append(", append=");
- builder.append(append);
- builder.append(", bufferedIO=");
- builder.append(bufferedIO);
- builder.append(", bufferSize=");
- builder.append(bufferSize);
- builder.append(", policy=");
- builder.append(policy);
- builder.append(", strategy=");
- builder.append(strategy);
- builder.append(", advertiseURI=");
- builder.append(advertiseURI);
- builder.append(", layout=");
- builder.append(layout);
- builder.append("]");
- return builder.toString();
- }
- }
-
- @Override
- public void updateData(final Object data)
- {
- final FactoryData factoryData = (FactoryData) data;
- setRolloverStrategy(factoryData.getRolloverStrategy());
- setTriggeringPolicy(factoryData.getTriggeringPolicy());
- }
-
- /**
- * Factory to create a RollingFileManager.
- */
- private static class RollingFileManagerFactory implements ManagerFactory<RollingFileManager, FactoryData> {
-
- /**
- * Creates a RollingFileManager.
- * @param name The name of the entity to manage.
- * @param data The data required to create the entity.
- * @return a RollingFileManager.
- */
- @Override
- public RollingFileManager createManager(final String name, final FactoryData data) {
- final File file = new File(name);
- final File parent = file.getParentFile();
- if (null != parent && !parent.exists()) {
- parent.mkdirs();
- }
- // LOG4J2-1140: check writeHeader before creating the file
- final boolean writeHeader = !data.append || !file.exists();
- try {
- final boolean created = data.createOnDemand ? false : file.createNewFile();
- LOGGER.trace("New file '{}' created = {}", name, created);
- } catch (final IOException ioe) {
- LOGGER.error("Unable to create file " + name, ioe);
- return null;
- }
- final long size = data.append ? file.length() : 0;
-
- try {
- final int actualSize = data.bufferedIO ? data.bufferSize : Constants.ENCODER_BYTE_BUFFER_SIZE;
- final ByteBuffer buffer = ByteBuffer.wrap(new byte[actualSize]);
- final OutputStream os = data.createOnDemand ? null : new FileOutputStream(name, data.append);
- final long time = data.createOnDemand? System.currentTimeMillis() : file.lastModified(); // LOG4J2-531 create file first so time has valid value
-
- return new RollingFileManager(data.getLoggerContext(), name, data.pattern, os,
- data.append, data.createOnDemand, size, time, data.policy, data.strategy, data.advertiseURI,
- data.layout, writeHeader, buffer);
- } catch (final IOException ex) {
- LOGGER.error("RollingFileManager (" + name + ") " + ex, ex);
- }
- return null;
- }
- }
-
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core.appender.rolling;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.util.concurrent.Future;
+import java.util.concurrent.Semaphore;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
+
+import org.apache.logging.log4j.core.Layout;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.appender.ConfigurationFactoryData;
+import org.apache.logging.log4j.core.appender.FileManager;
+import org.apache.logging.log4j.core.appender.ManagerFactory;
+import org.apache.logging.log4j.core.appender.rolling.action.AbstractAction;
+import org.apache.logging.log4j.core.appender.rolling.action.Action;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.util.Constants;
+
+/**
+ * The Rolling File Manager.
+ */
+public class RollingFileManager extends FileManager {
+
+ private static RollingFileManagerFactory factory = new RollingFileManagerFactory();
+
+ protected long size;
+ private long initialTime;
+ private final PatternProcessor patternProcessor;
+ private final Semaphore semaphore = new Semaphore(1);
+ private volatile TriggeringPolicy triggeringPolicy;
+ private volatile RolloverStrategy rolloverStrategy;
+ private volatile boolean renameEmptyFiles = false;
+
+ private static final AtomicReferenceFieldUpdater<RollingFileManager, TriggeringPolicy> triggeringPolicyUpdater =
+ AtomicReferenceFieldUpdater.newUpdater(RollingFileManager.class, TriggeringPolicy.class, "triggeringPolicy");
+
+ private static final AtomicReferenceFieldUpdater<RollingFileManager, RolloverStrategy> rolloverStrategyUpdater =
+ AtomicReferenceFieldUpdater.newUpdater(RollingFileManager.class, RolloverStrategy.class, "rolloverStrategy");
+
+ @Deprecated
+ protected RollingFileManager(final String fileName, final String pattern, final OutputStream os,
+ final boolean append, final long size, final long time, final TriggeringPolicy triggeringPolicy,
+ final RolloverStrategy rolloverStrategy, final String advertiseURI,
+ final Layout<? extends Serializable> layout, final int bufferSize, final boolean writeHeader) {
+ this(fileName, pattern, os, append, size, time, triggeringPolicy, rolloverStrategy, advertiseURI, layout,
+ writeHeader, ByteBuffer.wrap(new byte[Constants.ENCODER_BYTE_BUFFER_SIZE]));
+ }
+
+ @Deprecated
+ protected RollingFileManager(final String fileName, final String pattern, final OutputStream os,
+ final boolean append, final long size, final long time, final TriggeringPolicy triggeringPolicy,
+ final RolloverStrategy rolloverStrategy, final String advertiseURI,
+ final Layout<? extends Serializable> layout, final boolean writeHeader, final ByteBuffer buffer) {
+ super(fileName, os, append, false, advertiseURI, layout, writeHeader, buffer);
+ this.size = size;
+ this.initialTime = time;
+ this.triggeringPolicy = triggeringPolicy;
+ this.rolloverStrategy = rolloverStrategy;
+ this.patternProcessor = new PatternProcessor(pattern);
+ this.patternProcessor.setPrevFileTime(time);
+ }
+
+ /**
+ * @since 2.7
+ */
+ protected RollingFileManager(final LoggerContext loggerContext, final String fileName, final String pattern, final OutputStream os,
+ final boolean append, final boolean createOnDemand, final long size, final long time,
+ final TriggeringPolicy triggeringPolicy, final RolloverStrategy rolloverStrategy,
+ final String advertiseURI, final Layout<? extends Serializable> layout, final boolean writeHeader, final ByteBuffer buffer) {
+ super(loggerContext, fileName, os, append, false, createOnDemand, advertiseURI, layout, writeHeader, buffer);
+ this.size = size;
+ this.initialTime = time;
+ this.triggeringPolicy = triggeringPolicy;
+ this.rolloverStrategy = rolloverStrategy;
+ this.patternProcessor = new PatternProcessor(pattern);
+ this.patternProcessor.setPrevFileTime(time);
+ }
+
+ public void initialize() {
+ triggeringPolicy.initialize(this);
+ }
+
+ /**
+ * Returns a RollingFileManager.
+ * @param fileName The file name.
+ * @param pattern The pattern for rolling file.
+ * @param append true if the file should be appended to.
+ * @param bufferedIO true if data should be buffered.
+ * @param policy The TriggeringPolicy.
+ * @param strategy The RolloverStrategy.
+ * @param advertiseURI the URI to use when advertising the file
+ * @param layout The Layout.
+ * @param bufferSize buffer size to use if bufferedIO is true
+ * @param immediateFlush flush on every write or not
+ * @param createOnDemand true if you want to lazy-create the file (a.k.a. on-demand.)
+ * @param configuration The configuration.
+ * @return A RollingFileManager.
+ */
+ public static RollingFileManager getFileManager(final String fileName, final String pattern, final boolean append,
+ final boolean bufferedIO, final TriggeringPolicy policy, final RolloverStrategy strategy,
+ final String advertiseURI, final Layout<? extends Serializable> layout, final int bufferSize,
+ final boolean immediateFlush, final boolean createOnDemand, final Configuration configuration) {
+
+ return (RollingFileManager) getManager(fileName, new FactoryData(pattern, append,
+ bufferedIO, policy, strategy, advertiseURI, layout, bufferSize, immediateFlush, createOnDemand, configuration), factory);
+ }
+
+ // override to make visible for unit tests
+ @Override
+ protected synchronized void write(final byte[] bytes, final int offset, final int length,
+ final boolean immediateFlush) {
+ super.write(bytes, offset, length, immediateFlush);
+ }
+
+ @Override
+ protected synchronized void writeToDestination(final byte[] bytes, final int offset, final int length) {
+ size += length;
+ super.writeToDestination(bytes, offset, length);
+ }
+
+ public boolean isRenameEmptyFiles() {
+ return renameEmptyFiles;
+ }
+
+ public void setRenameEmptyFiles(final boolean renameEmptyFiles) {
+ this.renameEmptyFiles = renameEmptyFiles;
+ }
+
+ /**
+ * Returns the current size of the file.
+ * @return The size of the file in bytes.
+ */
+ public long getFileSize() {
+ return size + byteBuffer.position();
+ }
+
+ /**
+ * Returns the time the file was created.
+ * @return The time the file was created.
+ */
+ public long getFileTime() {
+ return initialTime;
+ }
+
+ /**
+ * Determines if a rollover should occur.
+ * @param event The LogEvent.
+ */
+ public synchronized void checkRollover(final LogEvent event) {
+ if (triggeringPolicy.isTriggeringEvent(event)) {
+ rollover();
+ }
+ }
+
+ @Override
+ public boolean releaseSub(final long timeout, final TimeUnit timeUnit) {
+ boolean stopped = triggeringPolicy.stop(timeout, timeUnit);
+ return stopped && super.releaseSub(timeout, timeUnit);
+ }
+
+ public synchronized void rollover() {
+ if (rollover(rolloverStrategy)) {
+ try {
+ size = 0;
+ initialTime = System.currentTimeMillis();
+ createFileAfterRollover();
+ } catch (final IOException e) {
+ logError("Failed to create file after rollover", e);
+ }
+ }
+ }
+
+ protected void createFileAfterRollover() throws IOException {
+ setOutputStream(new FileOutputStream(getFileName(), isAppend()));
+ }
+
+ /**
+ * Returns the pattern processor.
+ * @return The PatternProcessor.
+ */
+ public PatternProcessor getPatternProcessor() {
+ return patternProcessor;
+ }
+
+ public void setTriggeringPolicy(final TriggeringPolicy triggeringPolicy) {
+ triggeringPolicy.initialize(this);
+ triggeringPolicyUpdater.compareAndSet(this, this.triggeringPolicy, triggeringPolicy);
+ }
+
+ public void setRolloverStrategy(final RolloverStrategy rolloverStrategy) {
+ rolloverStrategyUpdater.compareAndSet(this, this.rolloverStrategy, rolloverStrategy);
+ }
+
+ /**
+ * Returns the triggering policy.
+ * @param <T> TriggeringPolicy type
+ * @return The TriggeringPolicy
+ */
+ @SuppressWarnings("unchecked")
+ public <T extends TriggeringPolicy> T getTriggeringPolicy() {
+ // TODO We could parameterize this class with a TriggeringPolicy instead of type casting here.
+ return (T) this.triggeringPolicy;
+ }
+
+ /**
+ * Returns the rollover strategy.
+ * @return The RolloverStrategy
+ */
+ public RolloverStrategy getRolloverStrategy() {
+ return this.rolloverStrategy;
+ }
+
+ private boolean rollover(final RolloverStrategy strategy) {
+
+ try {
+ // Block until the asynchronous operation is completed.
+ semaphore.acquire();
+ } catch (final InterruptedException e) {
+ logError("Thread interrupted while attempting to check rollover", e);
+ return false;
+ }
+
+ boolean success = false;
+ Future<?> future = null;
+
+ try {
+ final RolloverDescription descriptor = strategy.rollover(this);
+ if (descriptor != null) {
+ writeFooter();
+ closeOutputStream();
+ if (descriptor.getSynchronous() != null) {
+ LOGGER.debug("RollingFileManager executing synchronous {}", descriptor.getSynchronous());
+ try {
+ success = descriptor.getSynchronous().execute();
+ } catch (final Exception ex) {
+ logError("Caught error in synchronous task", ex);
+ }
+ }
+
+ if (success && descriptor.getAsynchronous() != null) {
+ LOGGER.debug("RollingFileManager executing async {}", descriptor.getAsynchronous());
+ future = LoggerContext.getContext(false).submit(new AsyncAction(descriptor.getAsynchronous(), this));
+ }
+ return true;
+ }
+ return false;
+ } finally {
+ if (future == null || future.isDone() || future.isCancelled()) {
+ semaphore.release();
+ }
+ }
+
+ }
+
+ /**
+ * Performs actions asynchronously.
+ */
+ private static class AsyncAction extends AbstractAction {
+
+ private final Action action;
+ private final RollingFileManager manager;
+
+ /**
+ * Constructor.
+ * @param act The action to perform.
+ * @param manager The manager.
+ */
+ public AsyncAction(final Action act, final RollingFileManager manager) {
+ this.action = act;
+ this.manager = manager;
+ }
+
+ /**
+ * Executes an action.
+ *
+ * @return true if action was successful. A return value of false will cause
+ * the rollover to be aborted if possible.
+ * @throws java.io.IOException if IO error, a thrown exception will cause the rollover
+ * to be aborted if possible.
+ */
+ @Override
+ public boolean execute() throws IOException {
+ try {
+ return action.execute();
+ } finally {
+ manager.semaphore.release();
+ }
+ }
+
+ /**
+ * Cancels the action if not already initialized or waits till completion.
+ */
+ @Override
+ public void close() {
+ action.close();
+ }
+
+ /**
+ * Determines if action has been completed.
+ *
+ * @return true if action is complete.
+ */
+ @Override
+ public boolean isComplete() {
+ return action.isComplete();
+ }
+
+ @Override
+ public String toString() {
+ final StringBuilder builder = new StringBuilder();
+ builder.append(super.toString());
+ builder.append("[action=");
+ builder.append(action);
+ builder.append(", manager=");
+ builder.append(manager);
+ builder.append(", isComplete()=");
+ builder.append(isComplete());
+ builder.append(", isInterrupted()=");
+ builder.append(isInterrupted());
+ builder.append("]");
+ return builder.toString();
+ }
+ }
+
+ /**
+ * Factory data.
+ */
+ private static class FactoryData extends ConfigurationFactoryData {
+ private final String pattern;
+ private final boolean append;
+ private final boolean bufferedIO;
+ private final int bufferSize;
+ private final boolean immediateFlush;
+ private final boolean createOnDemand;
+ private final TriggeringPolicy policy;
+ private final RolloverStrategy strategy;
+ private final String advertiseURI;
+ private final Layout<? extends Serializable> layout;
+
+ /**
+ * Creates the data for the factory.
+ * @param pattern The pattern.
+ * @param append The append flag.
+ * @param bufferedIO The bufferedIO flag.
+ * @param advertiseURI
+ * @param layout The Layout.
+ * @param bufferSize the buffer size
+ * @param immediateFlush flush on every write or not
+ * @param createOnDemand true if you want to lazy-create the file (a.k.a. on-demand.)
+ * @param configuration The configuration
+ */
+ public FactoryData(final String pattern, final boolean append, final boolean bufferedIO,
+ final TriggeringPolicy policy, final RolloverStrategy strategy, final String advertiseURI,
+ final Layout<? extends Serializable> layout, final int bufferSize, final boolean immediateFlush,
+ final boolean createOnDemand, final Configuration configuration) {
+ super(configuration);
+ this.pattern = pattern;
+ this.append = append;
+ this.bufferedIO = bufferedIO;
+ this.bufferSize = bufferSize;
+ this.policy = policy;
+ this.strategy = strategy;
+ this.advertiseURI = advertiseURI;
+ this.layout = layout;
+ this.immediateFlush = immediateFlush;
+ this.createOnDemand = createOnDemand;
+ }
+
+ public TriggeringPolicy getTriggeringPolicy()
+ {
+ return this.policy;
+ }
+
+ public RolloverStrategy getRolloverStrategy()
+ {
+ return this.strategy;
+ }
+
+ @Override
+ public String toString() {
+ final StringBuilder builder = new StringBuilder();
+ builder.append(super.toString());
+ builder.append("[pattern=");
+ builder.append(pattern);
+ builder.append(", append=");
+ builder.append(append);
+ builder.append(", bufferedIO=");
+ builder.append(bufferedIO);
+ builder.append(", bufferSize=");
+ builder.append(bufferSize);
+ builder.append(", policy=");
+ builder.append(policy);
+ builder.append(", strategy=");
+ builder.append(strategy);
+ builder.append(", advertiseURI=");
+ builder.append(advertiseURI);
+ builder.append(", layout=");
+ builder.append(layout);
+ builder.append("]");
+ return builder.toString();
+ }
+ }
+
+ @Override
+ public void updateData(final Object data)
+ {
+ final FactoryData factoryData = (FactoryData) data;
+ setRolloverStrategy(factoryData.getRolloverStrategy());
+ setTriggeringPolicy(factoryData.getTriggeringPolicy());
+ }
+
+ /**
+ * Factory to create a RollingFileManager.
+ */
+ private static class RollingFileManagerFactory implements ManagerFactory<RollingFileManager, FactoryData> {
+
+ /**
+ * Creates a RollingFileManager.
+ * @param name The name of the entity to manage.
+ * @param data The data required to create the entity.
+ * @return a RollingFileManager.
+ */
+ @Override
+ public RollingFileManager createManager(final String name, final FactoryData data) {
+ final File file = new File(name);
+ final File parent = file.getParentFile();
+ if (null != parent && !parent.exists()) {
+ parent.mkdirs();
+ }
+ // LOG4J2-1140: check writeHeader before creating the file
+ final boolean writeHeader = !data.append || !file.exists();
+ try {
+ final boolean created = data.createOnDemand ? false : file.createNewFile();
+ LOGGER.trace("New file '{}' created = {}", name, created);
+ } catch (final IOException ioe) {
+ LOGGER.error("Unable to create file " + name, ioe);
+ return null;
+ }
+ final long size = data.append ? file.length() : 0;
+
+ try {
+ final int actualSize = data.bufferedIO ? data.bufferSize : Constants.ENCODER_BYTE_BUFFER_SIZE;
+ final ByteBuffer buffer = ByteBuffer.wrap(new byte[actualSize]);
+ final OutputStream os = data.createOnDemand ? null : new FileOutputStream(name, data.append);
+ final long time = data.createOnDemand? System.currentTimeMillis() : file.lastModified(); // LOG4J2-531 create file first so time has valid value
+
+ return new RollingFileManager(data.getLoggerContext(), name, data.pattern, os,
+ data.append, data.createOnDemand, size, time, data.policy, data.strategy, data.advertiseURI,
+ data.layout, writeHeader, buffer);
+ } catch (final IOException ex) {
+ LOGGER.error("RollingFileManager (" + name + ") " + ex, ex);
+ }
+ return null;
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/78fe32c0/log4j-core/src/main/java/org/apache/logging/log4j/core/net/JndiManager.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/net/JndiManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/net/JndiManager.java
index a52921d..abeb5ee 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/net/JndiManager.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/net/JndiManager.java
@@ -1,144 +1,144 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-
-package org.apache.logging.log4j.core.net;
-
-import java.util.Properties;
-import java.util.concurrent.TimeUnit;
-
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-
-import org.apache.logging.log4j.core.appender.AbstractManager;
-import org.apache.logging.log4j.core.appender.ManagerFactory;
-import org.apache.logging.log4j.core.util.JndiCloser;
-
-/**
- * JNDI {@link javax.naming.Context} manager.
- *
- * @since 2.1
- */
-public class JndiManager extends AbstractManager {
-
- private static final JndiManagerFactory FACTORY = new JndiManagerFactory();
-
- private final Context context;
-
- private JndiManager(final String name, final Context context) {
- super(null, name);
- this.context = context;
- }
-
- /**
- * Gets the default JndiManager using the default {@link javax.naming.InitialContext}.
- *
- * @return the default JndiManager
- */
- public static JndiManager getDefaultManager() {
- return getManager(JndiManager.class.getName(), FACTORY, null);
- }
-
- /**
- * Gets a named JndiManager using the default {@link javax.naming.InitialContext}.
- * @param name the name of the JndiManager instance to create or use if available
- * @return a default JndiManager
- */
- public static JndiManager getDefaultManager(final String name) {
- return getManager(name, FACTORY, null);
- }
-
- /**
- * Gets a JndiManager with the provided configuration information.
- *
- * @param initialContextFactoryName Fully qualified class name of an implementation of
- * {@link javax.naming.spi.InitialContextFactory}.
- * @param providerURL The provider URL to use for the JNDI connection (specific to the above factory).
- * @param urlPkgPrefixes A colon-separated list of package prefixes for the class name of the factory
- * class that will create a URL context factory
- * @param securityPrincipal The name of the identity of the Principal.
- * @param securityCredentials The security credentials of the Principal.
- * @param additionalProperties Any additional JNDI environment properties to set or {@code null} for none.
- * @return the JndiManager for the provided parameters.
- */
- public static JndiManager getJndiManager(final String initialContextFactoryName,
- final String providerURL,
- final String urlPkgPrefixes,
- final String securityPrincipal,
- final String securityCredentials,
- final Properties additionalProperties) {
- final String name = JndiManager.class.getName() + '@' + JndiManager.class.hashCode();
- if (initialContextFactoryName == null) {
- return getManager(name, FACTORY, null);
- }
- final Properties properties = new Properties();
- properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, initialContextFactoryName);
- if (providerURL != null) {
- properties.setProperty(Context.PROVIDER_URL, providerURL);
- } else {
- LOGGER.warn("The JNDI InitialContextFactory class name [{}] was provided, but there was no associated " +
- "provider URL. This is likely to cause problems.", initialContextFactoryName);
- }
- if (urlPkgPrefixes != null) {
- properties.setProperty(Context.URL_PKG_PREFIXES, urlPkgPrefixes);
- }
- if (securityPrincipal != null) {
- properties.setProperty(Context.SECURITY_PRINCIPAL, securityPrincipal);
- if (securityCredentials != null) {
- properties.setProperty(Context.SECURITY_CREDENTIALS, securityCredentials);
- } else {
- LOGGER.warn("A security principal [{}] was provided, but with no corresponding security credentials.",
- securityPrincipal);
- }
- }
- if (additionalProperties != null) {
- properties.putAll(additionalProperties);
- }
- return getManager(name, FACTORY, properties);
- }
-
- @Override
- protected void releaseSub(final long timeout, final TimeUnit timeUnit) {
- JndiCloser.closeSilently(this.context);
- }
-
- /**
- * Looks up a named object through this JNDI context.
- *
- * @param name name of the object to look up.
- * @param <T> the type of the object.
- * @return the named object if it could be located.
- * @throws NamingException
- */
- @SuppressWarnings("unchecked")
- public <T> T lookup(final String name) throws NamingException {
- return (T) this.context.lookup(name);
- }
-
- private static class JndiManagerFactory implements ManagerFactory<JndiManager, Properties> {
-
- @Override
- public JndiManager createManager(final String name, final Properties data) {
- try {
- return new JndiManager(name, new InitialContext(data));
- } catch (final NamingException e) {
- LOGGER.error("Error creating JNDI InitialContext.", e);
- return null;
- }
- }
- }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+package org.apache.logging.log4j.core.net;
+
+import java.util.Properties;
+import java.util.concurrent.TimeUnit;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+
+import org.apache.logging.log4j.core.appender.AbstractManager;
+import org.apache.logging.log4j.core.appender.ManagerFactory;
+import org.apache.logging.log4j.core.util.JndiCloser;
+
+/**
+ * JNDI {@link javax.naming.Context} manager.
+ *
+ * @since 2.1
+ */
+public class JndiManager extends AbstractManager {
+
+ private static final JndiManagerFactory FACTORY = new JndiManagerFactory();
+
+ private final Context context;
+
+ private JndiManager(final String name, final Context context) {
+ super(null, name);
+ this.context = context;
+ }
+
+ /**
+ * Gets the default JndiManager using the default {@link javax.naming.InitialContext}.
+ *
+ * @return the default JndiManager
+ */
+ public static JndiManager getDefaultManager() {
+ return getManager(JndiManager.class.getName(), FACTORY, null);
+ }
+
+ /**
+ * Gets a named JndiManager using the default {@link javax.naming.InitialContext}.
+ * @param name the name of the JndiManager instance to create or use if available
+ * @return a default JndiManager
+ */
+ public static JndiManager getDefaultManager(final String name) {
+ return getManager(name, FACTORY, null);
+ }
+
+ /**
+ * Gets a JndiManager with the provided configuration information.
+ *
+ * @param initialContextFactoryName Fully qualified class name of an implementation of
+ * {@link javax.naming.spi.InitialContextFactory}.
+ * @param providerURL The provider URL to use for the JNDI connection (specific to the above factory).
+ * @param urlPkgPrefixes A colon-separated list of package prefixes for the class name of the factory
+ * class that will create a URL context factory
+ * @param securityPrincipal The name of the identity of the Principal.
+ * @param securityCredentials The security credentials of the Principal.
+ * @param additionalProperties Any additional JNDI environment properties to set or {@code null} for none.
+ * @return the JndiManager for the provided parameters.
+ */
+ public static JndiManager getJndiManager(final String initialContextFactoryName,
+ final String providerURL,
+ final String urlPkgPrefixes,
+ final String securityPrincipal,
+ final String securityCredentials,
+ final Properties additionalProperties) {
+ final String name = JndiManager.class.getName() + '@' + JndiManager.class.hashCode();
+ if (initialContextFactoryName == null) {
+ return getManager(name, FACTORY, null);
+ }
+ final Properties properties = new Properties();
+ properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, initialContextFactoryName);
+ if (providerURL != null) {
+ properties.setProperty(Context.PROVIDER_URL, providerURL);
+ } else {
+ LOGGER.warn("The JNDI InitialContextFactory class name [{}] was provided, but there was no associated " +
+ "provider URL. This is likely to cause problems.", initialContextFactoryName);
+ }
+ if (urlPkgPrefixes != null) {
+ properties.setProperty(Context.URL_PKG_PREFIXES, urlPkgPrefixes);
+ }
+ if (securityPrincipal != null) {
+ properties.setProperty(Context.SECURITY_PRINCIPAL, securityPrincipal);
+ if (securityCredentials != null) {
+ properties.setProperty(Context.SECURITY_CREDENTIALS, securityCredentials);
+ } else {
+ LOGGER.warn("A security principal [{}] was provided, but with no corresponding security credentials.",
+ securityPrincipal);
+ }
+ }
+ if (additionalProperties != null) {
+ properties.putAll(additionalProperties);
+ }
+ return getManager(name, FACTORY, properties);
+ }
+
+ @Override
+ protected boolean releaseSub(final long timeout, final TimeUnit timeUnit) {
+ return JndiCloser.closeSilently(this.context);
+ }
+
+ /**
+ * Looks up a named object through this JNDI context.
+ *
+ * @param name name of the object to look up.
+ * @param <T> the type of the object.
+ * @return the named object if it could be located.
+ * @throws NamingException
+ */
+ @SuppressWarnings("unchecked")
+ public <T> T lookup(final String name) throws NamingException {
+ return (T) this.context.lookup(name);
+ }
+
+ private static class JndiManagerFactory implements ManagerFactory<JndiManager, Properties> {
+
+ @Override
+ public JndiManager createManager(final String name, final Properties data) {
+ try {
+ return new JndiManager(name, new InitialContext(data));
+ } catch (final NamingException e) {
+ LOGGER.error("Error creating JNDI InitialContext.", e);
+ return null;
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/78fe32c0/log4j-core/src/main/java/org/apache/logging/log4j/core/net/server/JmsServer.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/net/server/JmsServer.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/net/server/JmsServer.java
index 15e0a3f..730e778 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/net/server/JmsServer.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/net/server/JmsServer.java
@@ -1,147 +1,148 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-
-package org.apache.logging.log4j.core.net.server;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.nio.charset.Charset;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicReference;
-import javax.jms.JMSException;
-import javax.jms.Message;
-import javax.jms.MessageConsumer;
-import javax.jms.MessageListener;
-import javax.jms.ObjectMessage;
-
-import org.apache.logging.log4j.LoggingException;
-import org.apache.logging.log4j.core.AbstractLifeCycle;
-import org.apache.logging.log4j.core.LifeCycle;
-import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.core.LogEventListener;
-import org.apache.logging.log4j.core.appender.mom.JmsManager;
-import org.apache.logging.log4j.core.net.JndiManager;
-
-/**
- * LogEventListener server that receives LogEvents over a JMS {@link javax.jms.Destination}.
- *
- * @since 2.1
- */
-public class JmsServer extends LogEventListener implements MessageListener, LifeCycle {
-
- private final AtomicReference<State> state = new AtomicReference<>(State.INITIALIZED);
- private final JmsManager jmsManager;
- private MessageConsumer messageConsumer;
-
- public JmsServer(final String connectionFactoryBindingName,
- final String destinationBindingName,
- final String username,
- final String password) {
- final String managerName = JmsServer.class.getName() + '@' + JmsServer.class.hashCode();
- final JndiManager jndiManager = JndiManager.getDefaultManager(managerName);
- jmsManager = JmsManager.getJmsManager(managerName, jndiManager, connectionFactoryBindingName,
- destinationBindingName, username, password);
- }
-
- @Override
- public State getState() {
- return state.get();
- }
-
- @Override
- public void onMessage(final Message message) {
- try {
- if (message instanceof ObjectMessage) {
- final Object body = ((ObjectMessage) message).getObject();
- if (body instanceof LogEvent) {
- log((LogEvent) body);
- } else {
- LOGGER.warn("Expected ObjectMessage to contain LogEvent. Got type {} instead.", body.getClass());
- }
- } else {
- LOGGER.warn("Received message of type {} and JMSType {} which cannot be handled.", message.getClass(),
- message.getJMSType());
- }
- } catch (final JMSException e) {
- LOGGER.catching(e);
- }
- }
-
- @Override
- public void initialize() {
- }
-
- @Override
- public void start() {
- if (state.compareAndSet(State.INITIALIZED, State.STARTING)) {
- try {
- messageConsumer = jmsManager.createMessageConsumer();
- messageConsumer.setMessageListener(this);
- } catch (final JMSException e) {
- throw new LoggingException(e);
- }
- }
- }
-
- @Override
- public void stop() {
- stop(AbstractLifeCycle.DEFAULT_STOP_TIMEOUT, AbstractLifeCycle.DEFAULT_STOP_TIMEUNIT);
- }
-
- @Override
- public boolean stop(final long timeout, final TimeUnit timeUnit) {
- try {
- messageConsumer.close();
- } catch (final JMSException e) {
- LOGGER.debug("Exception closing {}", messageConsumer, e);
- }
- jmsManager.stop(timeout, timeUnit);
- return true;
- }
-
- @Override
- public boolean isStarted() {
- return state.get() == State.STARTED;
- }
-
- @Override
- public boolean isStopped() {
- return state.get() == State.STOPPED;
- }
-
- /**
- * Starts and runs this server until the user types "exit" into standard input.
- *
- * @throws IOException
- * @since 2.6
- */
- public void run() throws IOException {
- this.start();
- System.out.println("Type \"exit\" to quit.");
- final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
- while (true) {
- final String line = stdin.readLine();
- if (line == null || line.equalsIgnoreCase("exit")) {
- System.out.println("Exiting. Kill the application if it does not exit due to daemon threads.");
- this.stop();
- return;
- }
- }
- }
-
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+package org.apache.logging.log4j.core.net.server;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.nio.charset.Charset;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageListener;
+import javax.jms.ObjectMessage;
+
+import org.apache.logging.log4j.LoggingException;
+import org.apache.logging.log4j.core.AbstractLifeCycle;
+import org.apache.logging.log4j.core.LifeCycle;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.LogEventListener;
+import org.apache.logging.log4j.core.appender.mom.JmsManager;
+import org.apache.logging.log4j.core.net.JndiManager;
+
+/**
+ * LogEventListener server that receives LogEvents over a JMS {@link javax.jms.Destination}.
+ *
+ * @since 2.1
+ */
+public class JmsServer extends LogEventListener implements MessageListener, LifeCycle {
+
+ private final AtomicReference<State> state = new AtomicReference<>(State.INITIALIZED);
+ private final JmsManager jmsManager;
+ private MessageConsumer messageConsumer;
+
+ public JmsServer(final String connectionFactoryBindingName,
+ final String destinationBindingName,
+ final String username,
+ final String password) {
+ final String managerName = JmsServer.class.getName() + '@' + JmsServer.class.hashCode();
+ final JndiManager jndiManager = JndiManager.getDefaultManager(managerName);
+ jmsManager = JmsManager.getJmsManager(managerName, jndiManager, connectionFactoryBindingName,
+ destinationBindingName, username, password);
+ }
+
+ @Override
+ public State getState() {
+ return state.get();
+ }
+
+ @Override
+ public void onMessage(final Message message) {
+ try {
+ if (message instanceof ObjectMessage) {
+ final Object body = ((ObjectMessage) message).getObject();
+ if (body instanceof LogEvent) {
+ log((LogEvent) body);
+ } else {
+ LOGGER.warn("Expected ObjectMessage to contain LogEvent. Got type {} instead.", body.getClass());
+ }
+ } else {
+ LOGGER.warn("Received message of type {} and JMSType {} which cannot be handled.", message.getClass(),
+ message.getJMSType());
+ }
+ } catch (final JMSException e) {
+ LOGGER.catching(e);
+ }
+ }
+
+ @Override
+ public void initialize() {
+ }
+
+ @Override
+ public void start() {
+ if (state.compareAndSet(State.INITIALIZED, State.STARTING)) {
+ try {
+ messageConsumer = jmsManager.createMessageConsumer();
+ messageConsumer.setMessageListener(this);
+ } catch (final JMSException e) {
+ throw new LoggingException(e);
+ }
+ }
+ }
+
+ @Override
+ public void stop() {
+ stop(AbstractLifeCycle.DEFAULT_STOP_TIMEOUT, AbstractLifeCycle.DEFAULT_STOP_TIMEUNIT);
+ }
+
+ @Override
+ public boolean stop(final long timeout, final TimeUnit timeUnit) {
+ boolean stopped = true;
+ try {
+ messageConsumer.close();
+ } catch (final JMSException e) {
+ LOGGER.debug("Exception closing {}", messageConsumer, e);
+ stopped = false;
+ }
+ return stopped && jmsManager.stop(timeout, timeUnit);
+ }
+
+ @Override
+ public boolean isStarted() {
+ return state.get() == State.STARTED;
+ }
+
+ @Override
+ public boolean isStopped() {
+ return state.get() == State.STOPPED;
+ }
+
+ /**
+ * Starts and runs this server until the user types "exit" into standard input.
+ *
+ * @throws IOException
+ * @since 2.6
+ */
+ public void run() throws IOException {
+ this.start();
+ System.out.println("Type \"exit\" to quit.");
+ final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
+ while (true) {
+ final String line = stdin.readLine();
+ if (line == null || line.equalsIgnoreCase("exit")) {
+ System.out.println("Exiting. Kill the application if it does not exit due to daemon threads.");
+ this.stop();
+ return;
+ }
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/78fe32c0/log4j-core/src/main/java/org/apache/logging/log4j/core/util/JndiCloser.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/JndiCloser.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/JndiCloser.java
index 671c922..2114b40 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/JndiCloser.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/JndiCloser.java
@@ -1,58 +1,60 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-
-package org.apache.logging.log4j.core.util;
-
-import javax.naming.Context;
-import javax.naming.NamingException;
-
-/**
- * Helper class for closing JNDI resources.
- *
- * This class is separate from {@link Closer} because JNDI is not in Android.
- */
-public final class JndiCloser {
-
- private JndiCloser() {
- }
-
- /**
- * Closes the specified {@code Context}.
- *
- * @param context the JNDI Context to close, may be {@code null}
- * @throws NamingException if a problem occurred closing the specified JNDI Context
- */
- public static void close(final Context context) throws NamingException {
- if (context != null) {
- context.close();
- }
- }
-
- /**
- * Closes the specified {@code Context}, ignoring any exceptions thrown by the close operation.
- *
- * @param context the JNDI Context to close, may be {@code null}
- */
- public static void closeSilently(final Context context) {
- try {
- close(context);
- } catch (final NamingException ignored) {
- // ignored
- }
- }
-
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+package org.apache.logging.log4j.core.util;
+
+import javax.naming.Context;
+import javax.naming.NamingException;
+
+/**
+ * Helper class for closing JNDI resources.
+ *
+ * This class is separate from {@link Closer} because JNDI is not in Android.
+ */
+public final class JndiCloser {
+
+ private JndiCloser() {
+ }
+
+ /**
+ * Closes the specified {@code Context}.
+ *
+ * @param context the JNDI Context to close, may be {@code null}
+ * @throws NamingException if a problem occurred closing the specified JNDI Context
+ */
+ public static void close(final Context context) throws NamingException {
+ if (context != null) {
+ context.close();
+ }
+ }
+
+ /**
+ * Closes the specified {@code Context}, ignoring any exceptions thrown by the close operation.
+ *
+ * @param context the JNDI Context to close, may be {@code null}
+ */
+ public static boolean closeSilently(final Context context) {
+ try {
+ close(context);
+ return true;
+ } catch (final NamingException ignored) {
+ // ignored
+ return false;
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/78fe32c0/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumeAppender.java
----------------------------------------------------------------------
diff --git a/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumeAppender.java b/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumeAppender.java
index 1df6bed..a29df92 100644
--- a/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumeAppender.java
+++ b/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumeAppender.java
@@ -109,10 +109,10 @@ public final class FlumeAppender extends AbstractAppender implements FlumeEventF
@Override
public boolean stop(final long timeout, final TimeUnit timeUnit) {
setStopping();
- super.stop(timeout, timeUnit, false);
- manager.stop(timeout, timeUnit);
+ boolean stopped = super.stop(timeout, timeUnit, false);
+ stopped &= manager.stop(timeout, timeUnit);
setStopped();
- return true;
+ return stopped;
}
/**
[6/7] logging-log4j2 git commit: Properly track resources during
shutdown.
Posted by gg...@apache.org.
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/78fe32c0/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jdbc/JdbcDatabaseManager.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jdbc/JdbcDatabaseManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jdbc/JdbcDatabaseManager.java
index 76e1901..6f9c66a 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jdbc/JdbcDatabaseManager.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jdbc/JdbcDatabaseManager.java
@@ -1,255 +1,260 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-package org.apache.logging.log4j.core.appender.db.jdbc;
-
-import java.io.StringReader;
-import java.sql.Connection;
-import java.sql.DatabaseMetaData;
-import java.sql.PreparedStatement;
-import java.sql.SQLException;
-import java.sql.Timestamp;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.core.appender.AppenderLoggingException;
-import org.apache.logging.log4j.core.appender.ManagerFactory;
-import org.apache.logging.log4j.core.appender.db.AbstractDatabaseManager;
-import org.apache.logging.log4j.core.layout.PatternLayout;
-import org.apache.logging.log4j.core.util.Closer;
-
-/**
- * An {@link AbstractDatabaseManager} implementation for relational databases accessed via JDBC.
- */
-public final class JdbcDatabaseManager extends AbstractDatabaseManager {
-
- private static final JdbcDatabaseManagerFactory INSTANCE = new JdbcDatabaseManagerFactory();
-
- private final List<Column> columns;
- private final ConnectionSource connectionSource;
- private final String sqlStatement;
-
- private Connection connection;
- private PreparedStatement statement;
- private boolean isBatchSupported;
-
- private JdbcDatabaseManager(final String name, final int bufferSize, final ConnectionSource connectionSource,
- final String sqlStatement, final List<Column> columns) {
- super(name, bufferSize);
- this.connectionSource = connectionSource;
- this.sqlStatement = sqlStatement;
- this.columns = columns;
- }
-
- @Override
- protected void startupInternal() throws Exception {
- this.connection = this.connectionSource.getConnection();
- final DatabaseMetaData metaData = this.connection.getMetaData();
- this.isBatchSupported = metaData.supportsBatchUpdates();
- Closer.closeSilently(this.connection);
- }
-
- @Override
- protected void shutdownInternal() {
- if (this.connection != null || this.statement != null) {
- this.commitAndClose();
- }
- }
-
- @Override
- protected void connectAndStart() {
- try {
- this.connection = this.connectionSource.getConnection();
- this.connection.setAutoCommit(false);
- this.statement = this.connection.prepareStatement(this.sqlStatement);
- } catch (final SQLException e) {
- throw new AppenderLoggingException(
- "Cannot write logging event or flush buffer; JDBC manager cannot connect to the database.", e
- );
- }
- }
-
- @Override
- protected void writeInternal(final LogEvent event) {
- StringReader reader = null;
- try {
- if (!this.isRunning() || this.connection == null || this.connection.isClosed() || this.statement == null
- || this.statement.isClosed()) {
- throw new AppenderLoggingException(
- "Cannot write logging event; JDBC manager not connected to the database.");
- }
-
- int i = 1;
- for (final Column column : this.columns) {
- if (column.isEventTimestamp) {
- this.statement.setTimestamp(i++, new Timestamp(event.getTimeMillis()));
- } else {
- if (column.isClob) {
- reader = new StringReader(column.layout.toSerializable(event));
- if (column.isUnicode) {
- this.statement.setNClob(i++, reader);
- } else {
- this.statement.setClob(i++, reader);
- }
- } else {
- if (column.isUnicode) {
- this.statement.setNString(i++, column.layout.toSerializable(event));
- } else {
- this.statement.setString(i++, column.layout.toSerializable(event));
- }
- }
- }
- }
-
- if (this.isBatchSupported) {
- this.statement.addBatch();
- } else if (this.statement.executeUpdate() == 0) {
- throw new AppenderLoggingException(
- "No records inserted in database table for log event in JDBC manager.");
- }
- } catch (final SQLException e) {
- throw new AppenderLoggingException("Failed to insert record for log event in JDBC manager: " +
- e.getMessage(), e);
- } finally {
- Closer.closeSilently(reader);
- }
- }
-
- @Override
- protected void commitAndClose() {
- try {
- if (this.connection != null && !this.connection.isClosed()) {
- if (this.isBatchSupported) {
- this.statement.executeBatch();
- }
- this.connection.commit();
- }
- } catch (final SQLException e) {
- throw new AppenderLoggingException("Failed to commit transaction logging event or flushing buffer.", e);
- } finally {
- try {
- Closer.close(this.statement);
- } catch (final Exception e) {
- logWarn("Failed to close SQL statement logging event or flushing buffer", e);
- } finally {
- this.statement = null;
- }
-
- try {
- Closer.close(this.connection);
- } catch (final Exception e) {
- logWarn("Failed to close database connection logging event or flushing buffer", e);
- } finally {
- this.connection = null;
- }
- }
- }
-
- /**
- * Creates a JDBC manager for use within the {@link JdbcAppender}, or returns a suitable one if it already exists.
- *
- * @param name The name of the manager, which should include connection details and hashed passwords where possible.
- * @param bufferSize The size of the log event buffer.
- * @param connectionSource The source for connections to the database.
- * @param tableName The name of the database table to insert log events into.
- * @param columnConfigs Configuration information about the log table columns.
- * @return a new or existing JDBC manager as applicable.
- */
- public static JdbcDatabaseManager getJDBCDatabaseManager(final String name, final int bufferSize,
- final ConnectionSource connectionSource,
- final String tableName,
- final ColumnConfig[] columnConfigs) {
-
- return AbstractDatabaseManager.getManager(
- name, new FactoryData(bufferSize, connectionSource, tableName, columnConfigs), getFactory()
- );
- }
-
- private static JdbcDatabaseManagerFactory getFactory() {
- return INSTANCE;
- }
-
- /**
- * Encapsulates data that {@link JdbcDatabaseManagerFactory} uses to create managers.
- */
- private static final class FactoryData extends AbstractDatabaseManager.AbstractFactoryData {
- private final ColumnConfig[] columnConfigs;
- private final ConnectionSource connectionSource;
- private final String tableName;
-
- protected FactoryData(final int bufferSize, final ConnectionSource connectionSource, final String tableName,
- final ColumnConfig[] columnConfigs) {
- super(bufferSize);
- this.connectionSource = connectionSource;
- this.tableName = tableName;
- this.columnConfigs = columnConfigs;
- }
- }
-
- /**
- * Creates managers.
- */
- private static final class JdbcDatabaseManagerFactory implements ManagerFactory<JdbcDatabaseManager, FactoryData> {
- @Override
- public JdbcDatabaseManager createManager(final String name, final FactoryData data) {
- final StringBuilder columnPart = new StringBuilder();
- final StringBuilder valuePart = new StringBuilder();
- final List<Column> columns = new ArrayList<>();
- int i = 0;
- for (final ColumnConfig config : data.columnConfigs) {
- if (i++ > 0) {
- columnPart.append(',');
- valuePart.append(',');
- }
-
- columnPart.append(config.getColumnName());
-
- if (config.getLiteralValue() != null) {
- valuePart.append(config.getLiteralValue());
- } else {
- columns.add(new Column(
- config.getLayout(), config.isEventTimestamp(), config.isUnicode(), config.isClob()
- ));
- valuePart.append('?');
- }
- }
-
- final String sqlStatement = "INSERT INTO " + data.tableName + " (" + columnPart + ") VALUES (" +
- valuePart + ')';
-
- return new JdbcDatabaseManager(name, data.getBufferSize(), data.connectionSource, sqlStatement, columns);
- }
- }
-
- /**
- * Encapsulates information about a database column and how to persist data to it.
- */
- private static final class Column {
- private final PatternLayout layout;
- private final boolean isEventTimestamp;
- private final boolean isUnicode;
- private final boolean isClob;
-
- private Column(final PatternLayout layout, final boolean isEventDate, final boolean isUnicode,
- final boolean isClob) {
- this.layout = layout;
- this.isEventTimestamp = isEventDate;
- this.isUnicode = isUnicode;
- this.isClob = isClob;
- }
- }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core.appender.db.jdbc;
+
+import java.io.StringReader;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.sql.Timestamp;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.appender.AppenderLoggingException;
+import org.apache.logging.log4j.core.appender.ManagerFactory;
+import org.apache.logging.log4j.core.appender.db.AbstractDatabaseManager;
+import org.apache.logging.log4j.core.layout.PatternLayout;
+import org.apache.logging.log4j.core.util.Closer;
+
+/**
+ * An {@link AbstractDatabaseManager} implementation for relational databases accessed via JDBC.
+ */
+public final class JdbcDatabaseManager extends AbstractDatabaseManager {
+
+ private static final JdbcDatabaseManagerFactory INSTANCE = new JdbcDatabaseManagerFactory();
+
+ private final List<Column> columns;
+ private final ConnectionSource connectionSource;
+ private final String sqlStatement;
+
+ private Connection connection;
+ private PreparedStatement statement;
+ private boolean isBatchSupported;
+
+ private JdbcDatabaseManager(final String name, final int bufferSize, final ConnectionSource connectionSource,
+ final String sqlStatement, final List<Column> columns) {
+ super(name, bufferSize);
+ this.connectionSource = connectionSource;
+ this.sqlStatement = sqlStatement;
+ this.columns = columns;
+ }
+
+ @Override
+ protected void startupInternal() throws Exception {
+ this.connection = this.connectionSource.getConnection();
+ final DatabaseMetaData metaData = this.connection.getMetaData();
+ this.isBatchSupported = metaData.supportsBatchUpdates();
+ Closer.closeSilently(this.connection);
+ }
+
+ @Override
+ protected boolean shutdownInternal() {
+ if (this.connection != null || this.statement != null) {
+ return this.commitAndClose();
+ }
+ return true;
+ }
+
+ @Override
+ protected void connectAndStart() {
+ try {
+ this.connection = this.connectionSource.getConnection();
+ this.connection.setAutoCommit(false);
+ this.statement = this.connection.prepareStatement(this.sqlStatement);
+ } catch (final SQLException e) {
+ throw new AppenderLoggingException(
+ "Cannot write logging event or flush buffer; JDBC manager cannot connect to the database.", e
+ );
+ }
+ }
+
+ @Override
+ protected void writeInternal(final LogEvent event) {
+ StringReader reader = null;
+ try {
+ if (!this.isRunning() || this.connection == null || this.connection.isClosed() || this.statement == null
+ || this.statement.isClosed()) {
+ throw new AppenderLoggingException(
+ "Cannot write logging event; JDBC manager not connected to the database.");
+ }
+
+ int i = 1;
+ for (final Column column : this.columns) {
+ if (column.isEventTimestamp) {
+ this.statement.setTimestamp(i++, new Timestamp(event.getTimeMillis()));
+ } else {
+ if (column.isClob) {
+ reader = new StringReader(column.layout.toSerializable(event));
+ if (column.isUnicode) {
+ this.statement.setNClob(i++, reader);
+ } else {
+ this.statement.setClob(i++, reader);
+ }
+ } else {
+ if (column.isUnicode) {
+ this.statement.setNString(i++, column.layout.toSerializable(event));
+ } else {
+ this.statement.setString(i++, column.layout.toSerializable(event));
+ }
+ }
+ }
+ }
+
+ if (this.isBatchSupported) {
+ this.statement.addBatch();
+ } else if (this.statement.executeUpdate() == 0) {
+ throw new AppenderLoggingException(
+ "No records inserted in database table for log event in JDBC manager.");
+ }
+ } catch (final SQLException e) {
+ throw new AppenderLoggingException("Failed to insert record for log event in JDBC manager: " +
+ e.getMessage(), e);
+ } finally {
+ Closer.closeSilently(reader);
+ }
+ }
+
+ @Override
+ protected boolean commitAndClose() {
+ boolean closed = true;
+ try {
+ if (this.connection != null && !this.connection.isClosed()) {
+ if (this.isBatchSupported) {
+ this.statement.executeBatch();
+ }
+ this.connection.commit();
+ }
+ } catch (final SQLException e) {
+ throw new AppenderLoggingException("Failed to commit transaction logging event or flushing buffer.", e);
+ } finally {
+ try {
+ Closer.close(this.statement);
+ } catch (final Exception e) {
+ logWarn("Failed to close SQL statement logging event or flushing buffer", e);
+ closed = false;
+ } finally {
+ this.statement = null;
+ }
+
+ try {
+ Closer.close(this.connection);
+ } catch (final Exception e) {
+ logWarn("Failed to close database connection logging event or flushing buffer", e);
+ closed = false;
+ } finally {
+ this.connection = null;
+ }
+ }
+ return closed;
+ }
+
+ /**
+ * Creates a JDBC manager for use within the {@link JdbcAppender}, or returns a suitable one if it already exists.
+ *
+ * @param name The name of the manager, which should include connection details and hashed passwords where possible.
+ * @param bufferSize The size of the log event buffer.
+ * @param connectionSource The source for connections to the database.
+ * @param tableName The name of the database table to insert log events into.
+ * @param columnConfigs Configuration information about the log table columns.
+ * @return a new or existing JDBC manager as applicable.
+ */
+ public static JdbcDatabaseManager getJDBCDatabaseManager(final String name, final int bufferSize,
+ final ConnectionSource connectionSource,
+ final String tableName,
+ final ColumnConfig[] columnConfigs) {
+
+ return AbstractDatabaseManager.getManager(
+ name, new FactoryData(bufferSize, connectionSource, tableName, columnConfigs), getFactory()
+ );
+ }
+
+ private static JdbcDatabaseManagerFactory getFactory() {
+ return INSTANCE;
+ }
+
+ /**
+ * Encapsulates data that {@link JdbcDatabaseManagerFactory} uses to create managers.
+ */
+ private static final class FactoryData extends AbstractDatabaseManager.AbstractFactoryData {
+ private final ColumnConfig[] columnConfigs;
+ private final ConnectionSource connectionSource;
+ private final String tableName;
+
+ protected FactoryData(final int bufferSize, final ConnectionSource connectionSource, final String tableName,
+ final ColumnConfig[] columnConfigs) {
+ super(bufferSize);
+ this.connectionSource = connectionSource;
+ this.tableName = tableName;
+ this.columnConfigs = columnConfigs;
+ }
+ }
+
+ /**
+ * Creates managers.
+ */
+ private static final class JdbcDatabaseManagerFactory implements ManagerFactory<JdbcDatabaseManager, FactoryData> {
+ @Override
+ public JdbcDatabaseManager createManager(final String name, final FactoryData data) {
+ final StringBuilder columnPart = new StringBuilder();
+ final StringBuilder valuePart = new StringBuilder();
+ final List<Column> columns = new ArrayList<>();
+ int i = 0;
+ for (final ColumnConfig config : data.columnConfigs) {
+ if (i++ > 0) {
+ columnPart.append(',');
+ valuePart.append(',');
+ }
+
+ columnPart.append(config.getColumnName());
+
+ if (config.getLiteralValue() != null) {
+ valuePart.append(config.getLiteralValue());
+ } else {
+ columns.add(new Column(
+ config.getLayout(), config.isEventTimestamp(), config.isUnicode(), config.isClob()
+ ));
+ valuePart.append('?');
+ }
+ }
+
+ final String sqlStatement = "INSERT INTO " + data.tableName + " (" + columnPart + ") VALUES (" +
+ valuePart + ')';
+
+ return new JdbcDatabaseManager(name, data.getBufferSize(), data.connectionSource, sqlStatement, columns);
+ }
+ }
+
+ /**
+ * Encapsulates information about a database column and how to persist data to it.
+ */
+ private static final class Column {
+ private final PatternLayout layout;
+ private final boolean isEventTimestamp;
+ private final boolean isUnicode;
+ private final boolean isClob;
+
+ private Column(final PatternLayout layout, final boolean isEventDate, final boolean isUnicode,
+ final boolean isClob) {
+ this.layout = layout;
+ this.isEventTimestamp = isEventDate;
+ this.isUnicode = isUnicode;
+ this.isClob = isClob;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/78fe32c0/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jpa/JpaDatabaseManager.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jpa/JpaDatabaseManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jpa/JpaDatabaseManager.java
index 17c5642..d5fc922 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jpa/JpaDatabaseManager.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jpa/JpaDatabaseManager.java
@@ -1,188 +1,193 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-package org.apache.logging.log4j.core.appender.db.jpa;
-
-import java.lang.reflect.Constructor;
-
-import javax.persistence.EntityManager;
-import javax.persistence.EntityManagerFactory;
-import javax.persistence.EntityTransaction;
-import javax.persistence.Persistence;
-
-import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.core.appender.AppenderLoggingException;
-import org.apache.logging.log4j.core.appender.ManagerFactory;
-import org.apache.logging.log4j.core.appender.db.AbstractDatabaseManager;
-
-/**
- * An {@link AbstractDatabaseManager} implementation for relational databases accessed via JPA.
- */
-public final class JpaDatabaseManager extends AbstractDatabaseManager {
- private static final JPADatabaseManagerFactory FACTORY = new JPADatabaseManagerFactory();
-
- private final String entityClassName;
- private final Constructor<? extends AbstractLogEventWrapperEntity> entityConstructor;
- private final String persistenceUnitName;
-
- private EntityManagerFactory entityManagerFactory;
-
- private EntityManager entityManager;
- private EntityTransaction transaction;
-
- private JpaDatabaseManager(final String name, final int bufferSize,
- final Class<? extends AbstractLogEventWrapperEntity> entityClass,
- final Constructor<? extends AbstractLogEventWrapperEntity> entityConstructor,
- final String persistenceUnitName) {
- super(name, bufferSize);
- this.entityClassName = entityClass.getName();
- this.entityConstructor = entityConstructor;
- this.persistenceUnitName = persistenceUnitName;
- }
-
- @Override
- protected void startupInternal() {
- this.entityManagerFactory = Persistence.createEntityManagerFactory(this.persistenceUnitName);
- }
-
- @Override
- protected void shutdownInternal() {
- if (this.entityManager != null || this.transaction != null) {
- this.commitAndClose();
- }
- if (this.entityManagerFactory != null && this.entityManagerFactory.isOpen()) {
- this.entityManagerFactory.close();
- }
- }
-
- @Override
- protected void connectAndStart() {
- try {
- this.entityManager = this.entityManagerFactory.createEntityManager();
- this.transaction = this.entityManager.getTransaction();
- this.transaction.begin();
- } catch (final Exception e) {
- throw new AppenderLoggingException(
- "Cannot write logging event or flush buffer; manager cannot create EntityManager or transaction.", e
- );
- }
- }
-
- @Override
- protected void writeInternal(final LogEvent event) {
- if (!this.isRunning() || this.entityManagerFactory == null || this.entityManager == null
- || this.transaction == null) {
- throw new AppenderLoggingException(
- "Cannot write logging event; JPA manager not connected to the database.");
- }
-
- AbstractLogEventWrapperEntity entity;
- try {
- entity = this.entityConstructor.newInstance(event);
- } catch (final Exception e) {
- throw new AppenderLoggingException("Failed to instantiate entity class [" + this.entityClassName + "].", e);
- }
-
- try {
- this.entityManager.persist(entity);
- } catch (final Exception e) {
- if (this.transaction != null && this.transaction.isActive()) {
- this.transaction.rollback();
- this.transaction = null;
- }
- throw new AppenderLoggingException("Failed to insert record for log event in JPA manager: " +
- e.getMessage(), e);
- }
- }
-
- @Override
- protected void commitAndClose() {
- try {
- if (this.transaction != null && this.transaction.isActive()) {
- this.transaction.commit();
- }
- } catch (final Exception e) {
- if (this.transaction != null && this.transaction.isActive()) {
- this.transaction.rollback();
- }
- } finally {
- this.transaction = null;
- try {
- if (this.entityManager != null && this.entityManager.isOpen()) {
- this.entityManager.close();
- }
- } catch (final Exception e) {
- logWarn("Failed to close entity manager while logging event or flushing buffer", e);
- } finally {
- this.entityManager = null;
- }
- }
- }
-
- /**
- * Creates a JPA manager for use within the {@link JpaAppender}, or returns a suitable one if it already exists.
- *
- * @param name The name of the manager, which should include connection details, entity class name, etc.
- * @param bufferSize The size of the log event buffer.
- * @param entityClass The fully-qualified class name of the {@link AbstractLogEventWrapperEntity} concrete
- * implementation.
- * @param entityConstructor The one-arg {@link LogEvent} constructor for the concrete entity class.
- * @param persistenceUnitName The name of the JPA persistence unit that should be used for persisting log events.
- * @return a new or existing JPA manager as applicable.
- */
- public static JpaDatabaseManager getJPADatabaseManager(final String name, final int bufferSize,
- final Class<? extends AbstractLogEventWrapperEntity>
- entityClass,
- final Constructor<? extends AbstractLogEventWrapperEntity>
- entityConstructor,
- final String persistenceUnitName) {
-
- return AbstractDatabaseManager.getManager(
- name, new FactoryData(bufferSize, entityClass, entityConstructor, persistenceUnitName), FACTORY
- );
- }
-
- /**
- * Encapsulates data that {@link JPADatabaseManagerFactory} uses to create managers.
- */
- private static final class FactoryData extends AbstractDatabaseManager.AbstractFactoryData {
- private final Class<? extends AbstractLogEventWrapperEntity> entityClass;
- private final Constructor<? extends AbstractLogEventWrapperEntity> entityConstructor;
- private final String persistenceUnitName;
-
- protected FactoryData(final int bufferSize, final Class<? extends AbstractLogEventWrapperEntity> entityClass,
- final Constructor<? extends AbstractLogEventWrapperEntity> entityConstructor,
- final String persistenceUnitName) {
- super(bufferSize);
-
- this.entityClass = entityClass;
- this.entityConstructor = entityConstructor;
- this.persistenceUnitName = persistenceUnitName;
- }
- }
-
- /**
- * Creates managers.
- */
- private static final class JPADatabaseManagerFactory implements ManagerFactory<JpaDatabaseManager, FactoryData> {
- @Override
- public JpaDatabaseManager createManager(final String name, final FactoryData data) {
- return new JpaDatabaseManager(
- name, data.getBufferSize(), data.entityClass, data.entityConstructor, data.persistenceUnitName
- );
- }
- }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core.appender.db.jpa;
+
+import java.lang.reflect.Constructor;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityTransaction;
+import javax.persistence.Persistence;
+
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.appender.AppenderLoggingException;
+import org.apache.logging.log4j.core.appender.ManagerFactory;
+import org.apache.logging.log4j.core.appender.db.AbstractDatabaseManager;
+
+/**
+ * An {@link AbstractDatabaseManager} implementation for relational databases accessed via JPA.
+ */
+public final class JpaDatabaseManager extends AbstractDatabaseManager {
+ private static final JPADatabaseManagerFactory FACTORY = new JPADatabaseManagerFactory();
+
+ private final String entityClassName;
+ private final Constructor<? extends AbstractLogEventWrapperEntity> entityConstructor;
+ private final String persistenceUnitName;
+
+ private EntityManagerFactory entityManagerFactory;
+
+ private EntityManager entityManager;
+ private EntityTransaction transaction;
+
+ private JpaDatabaseManager(final String name, final int bufferSize,
+ final Class<? extends AbstractLogEventWrapperEntity> entityClass,
+ final Constructor<? extends AbstractLogEventWrapperEntity> entityConstructor,
+ final String persistenceUnitName) {
+ super(name, bufferSize);
+ this.entityClassName = entityClass.getName();
+ this.entityConstructor = entityConstructor;
+ this.persistenceUnitName = persistenceUnitName;
+ }
+
+ @Override
+ protected void startupInternal() {
+ this.entityManagerFactory = Persistence.createEntityManagerFactory(this.persistenceUnitName);
+ }
+
+ @Override
+ protected boolean shutdownInternal() {
+ boolean closed = true;
+ if (this.entityManager != null || this.transaction != null) {
+ closed &= this.commitAndClose();
+ }
+ if (this.entityManagerFactory != null && this.entityManagerFactory.isOpen()) {
+ this.entityManagerFactory.close();
+ }
+ return closed;
+ }
+
+ @Override
+ protected void connectAndStart() {
+ try {
+ this.entityManager = this.entityManagerFactory.createEntityManager();
+ this.transaction = this.entityManager.getTransaction();
+ this.transaction.begin();
+ } catch (final Exception e) {
+ throw new AppenderLoggingException(
+ "Cannot write logging event or flush buffer; manager cannot create EntityManager or transaction.", e
+ );
+ }
+ }
+
+ @Override
+ protected void writeInternal(final LogEvent event) {
+ if (!this.isRunning() || this.entityManagerFactory == null || this.entityManager == null
+ || this.transaction == null) {
+ throw new AppenderLoggingException(
+ "Cannot write logging event; JPA manager not connected to the database.");
+ }
+
+ AbstractLogEventWrapperEntity entity;
+ try {
+ entity = this.entityConstructor.newInstance(event);
+ } catch (final Exception e) {
+ throw new AppenderLoggingException("Failed to instantiate entity class [" + this.entityClassName + "].", e);
+ }
+
+ try {
+ this.entityManager.persist(entity);
+ } catch (final Exception e) {
+ if (this.transaction != null && this.transaction.isActive()) {
+ this.transaction.rollback();
+ this.transaction = null;
+ }
+ throw new AppenderLoggingException("Failed to insert record for log event in JPA manager: " +
+ e.getMessage(), e);
+ }
+ }
+
+ @Override
+ protected boolean commitAndClose() {
+ boolean closed = true;
+ try {
+ if (this.transaction != null && this.transaction.isActive()) {
+ this.transaction.commit();
+ }
+ } catch (final Exception e) {
+ if (this.transaction != null && this.transaction.isActive()) {
+ this.transaction.rollback();
+ }
+ } finally {
+ this.transaction = null;
+ try {
+ if (this.entityManager != null && this.entityManager.isOpen()) {
+ this.entityManager.close();
+ }
+ } catch (final Exception e) {
+ logWarn("Failed to close entity manager while logging event or flushing buffer", e);
+ closed = false;
+ } finally {
+ this.entityManager = null;
+ }
+ }
+ return closed;
+ }
+
+ /**
+ * Creates a JPA manager for use within the {@link JpaAppender}, or returns a suitable one if it already exists.
+ *
+ * @param name The name of the manager, which should include connection details, entity class name, etc.
+ * @param bufferSize The size of the log event buffer.
+ * @param entityClass The fully-qualified class name of the {@link AbstractLogEventWrapperEntity} concrete
+ * implementation.
+ * @param entityConstructor The one-arg {@link LogEvent} constructor for the concrete entity class.
+ * @param persistenceUnitName The name of the JPA persistence unit that should be used for persisting log events.
+ * @return a new or existing JPA manager as applicable.
+ */
+ public static JpaDatabaseManager getJPADatabaseManager(final String name, final int bufferSize,
+ final Class<? extends AbstractLogEventWrapperEntity>
+ entityClass,
+ final Constructor<? extends AbstractLogEventWrapperEntity>
+ entityConstructor,
+ final String persistenceUnitName) {
+
+ return AbstractDatabaseManager.getManager(
+ name, new FactoryData(bufferSize, entityClass, entityConstructor, persistenceUnitName), FACTORY
+ );
+ }
+
+ /**
+ * Encapsulates data that {@link JPADatabaseManagerFactory} uses to create managers.
+ */
+ private static final class FactoryData extends AbstractDatabaseManager.AbstractFactoryData {
+ private final Class<? extends AbstractLogEventWrapperEntity> entityClass;
+ private final Constructor<? extends AbstractLogEventWrapperEntity> entityConstructor;
+ private final String persistenceUnitName;
+
+ protected FactoryData(final int bufferSize, final Class<? extends AbstractLogEventWrapperEntity> entityClass,
+ final Constructor<? extends AbstractLogEventWrapperEntity> entityConstructor,
+ final String persistenceUnitName) {
+ super(bufferSize);
+
+ this.entityClass = entityClass;
+ this.entityConstructor = entityConstructor;
+ this.persistenceUnitName = persistenceUnitName;
+ }
+ }
+
+ /**
+ * Creates managers.
+ */
+ private static final class JPADatabaseManagerFactory implements ManagerFactory<JpaDatabaseManager, FactoryData> {
+ @Override
+ public JpaDatabaseManager createManager(final String name, final FactoryData data) {
+ return new JpaDatabaseManager(
+ name, data.getBufferSize(), data.entityClass, data.entityConstructor, data.persistenceUnitName
+ );
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/78fe32c0/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/JmsAppender.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/JmsAppender.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/JmsAppender.java
index 8e21de8..7794b0d 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/JmsAppender.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/JmsAppender.java
@@ -1,215 +1,215 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-
-package org.apache.logging.log4j.core.appender.mom;
-
-import java.io.Serializable;
-import java.util.concurrent.TimeUnit;
-
-import javax.jms.JMSException;
-import javax.jms.Message;
-import javax.jms.MessageProducer;
-
-import org.apache.logging.log4j.core.Appender;
-import org.apache.logging.log4j.core.Filter;
-import org.apache.logging.log4j.core.Layout;
-import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.core.appender.AbstractAppender;
-import org.apache.logging.log4j.core.appender.AppenderLoggingException;
-import org.apache.logging.log4j.core.config.Node;
-import org.apache.logging.log4j.core.config.plugins.Plugin;
-import org.apache.logging.log4j.core.config.plugins.PluginAliases;
-import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
-import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
-import org.apache.logging.log4j.core.config.plugins.PluginElement;
-import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
-import org.apache.logging.log4j.core.layout.SerializedLayout;
-import org.apache.logging.log4j.core.net.JndiManager;
-
-/**
- * Generic JMS Appender plugin for both queues and topics. This Appender replaces the previous split ones. However,
- * configurations set up for the 2.0 version of the JMS appenders will still work.
- */
-@Plugin(name = "JMS", category = Node.CATEGORY, elementType = Appender.ELEMENT_TYPE, printObject = true)
-@PluginAliases({"JMSQueue", "JMSTopic"})
-public class JmsAppender extends AbstractAppender {
-
- private final JmsManager manager;
- private final MessageProducer producer;
-
- protected JmsAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout,
- final boolean ignoreExceptions, final JmsManager manager)
- throws JMSException {
- super(name, filter, layout, ignoreExceptions);
- this.manager = manager;
- this.producer = this.manager.createMessageProducer();
- }
-
- @Override
- public void append(final LogEvent event) {
- try {
- final Message message = this.manager.createMessage(getLayout().toSerializable(event));
- message.setJMSTimestamp(event.getTimeMillis());
- this.producer.send(message);
- } catch (final JMSException e) {
- throw new AppenderLoggingException(e);
- }
- }
-
- @Override
- public boolean stop(final long timeout, final TimeUnit timeUnit) {
- setStopping();
- super.stop(timeout, timeUnit, false);
- this.manager.stop(timeout, timeUnit);
- setStopped();
- return true;
- }
-
- @PluginBuilderFactory
- public static Builder newBuilder() {
- return new Builder();
- }
-
- public static class Builder implements org.apache.logging.log4j.core.util.Builder<JmsAppender> {
-
- @PluginBuilderAttribute
- @Required(message = "A name for the JmsAppender must be specified")
- private String name;
-
- @PluginBuilderAttribute
- private String factoryName;
-
- @PluginBuilderAttribute
- private String providerUrl;
-
- @PluginBuilderAttribute
- private String urlPkgPrefixes;
-
- @PluginBuilderAttribute
- private String securityPrincipalName;
-
- @PluginBuilderAttribute(sensitive = true)
- private String securityCredentials;
-
- @PluginBuilderAttribute
- @Required(message = "A javax.jms.ConnectionFactory JNDI name must be specified")
- private String factoryBindingName;
-
- @PluginBuilderAttribute
- @PluginAliases({"queueBindingName", "topicBindingName"})
- @Required(message = "A javax.jms.Destination JNDI name must be specified")
- private String destinationBindingName;
-
- @PluginBuilderAttribute
- private String username;
-
- @PluginBuilderAttribute(sensitive = true)
- private String password;
-
- @PluginElement("Layout")
- private Layout<? extends Serializable> layout = SerializedLayout.createLayout();
-
- @PluginElement("Filter")
- private Filter filter;
-
- @PluginBuilderAttribute
- private boolean ignoreExceptions = true;
-
- private Builder() {
- }
-
- public Builder setName(final String name) {
- this.name = name;
- return this;
- }
-
- public Builder setFactoryName(final String factoryName) {
- this.factoryName = factoryName;
- return this;
- }
-
- public Builder setProviderUrl(final String providerUrl) {
- this.providerUrl = providerUrl;
- return this;
- }
-
- public Builder setUrlPkgPrefixes(final String urlPkgPrefixes) {
- this.urlPkgPrefixes = urlPkgPrefixes;
- return this;
- }
-
- public Builder setSecurityPrincipalName(final String securityPrincipalName) {
- this.securityPrincipalName = securityPrincipalName;
- return this;
- }
-
- public Builder setSecurityCredentials(final String securityCredentials) {
- this.securityCredentials = securityCredentials;
- return this;
- }
-
- public Builder setFactoryBindingName(final String factoryBindingName) {
- this.factoryBindingName = factoryBindingName;
- return this;
- }
-
- public Builder setDestinationBindingName(final String destinationBindingName) {
- this.destinationBindingName = destinationBindingName;
- return this;
- }
-
- public Builder setUsername(final String username) {
- this.username = username;
- return this;
- }
-
- public Builder setPassword(final String password) {
- this.password = password;
- return this;
- }
-
- public Builder setLayout(final Layout<? extends Serializable> layout) {
- this.layout = layout;
- return this;
- }
-
- public Builder setFilter(final Filter filter) {
- this.filter = filter;
- return this;
- }
-
- public Builder setIgnoreExceptions(final boolean ignoreExceptions) {
- this.ignoreExceptions = ignoreExceptions;
- return this;
- }
-
- @Override
- public JmsAppender build() {
- final JndiManager jndiManager = JndiManager.getJndiManager(factoryName, providerUrl, urlPkgPrefixes,
- securityPrincipalName, securityCredentials, null);
- final JmsManager jmsManager = JmsManager.getJmsManager(name, jndiManager, factoryBindingName,
- destinationBindingName, username, password);
- try {
- return new JmsAppender(name, filter, layout, ignoreExceptions, jmsManager);
- } catch (final JMSException e) {
- LOGGER.error("Error creating JmsAppender [{}].", name, e);
- return null;
- }
- }
- }
-
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+package org.apache.logging.log4j.core.appender.mom;
+
+import java.io.Serializable;
+import java.util.concurrent.TimeUnit;
+
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageProducer;
+
+import org.apache.logging.log4j.core.Appender;
+import org.apache.logging.log4j.core.Filter;
+import org.apache.logging.log4j.core.Layout;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.appender.AbstractAppender;
+import org.apache.logging.log4j.core.appender.AppenderLoggingException;
+import org.apache.logging.log4j.core.config.Node;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.config.plugins.PluginAliases;
+import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
+import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
+import org.apache.logging.log4j.core.config.plugins.PluginElement;
+import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
+import org.apache.logging.log4j.core.layout.SerializedLayout;
+import org.apache.logging.log4j.core.net.JndiManager;
+
+/**
+ * Generic JMS Appender plugin for both queues and topics. This Appender replaces the previous split ones. However,
+ * configurations set up for the 2.0 version of the JMS appenders will still work.
+ */
+@Plugin(name = "JMS", category = Node.CATEGORY, elementType = Appender.ELEMENT_TYPE, printObject = true)
+@PluginAliases({"JMSQueue", "JMSTopic"})
+public class JmsAppender extends AbstractAppender {
+
+ private final JmsManager manager;
+ private final MessageProducer producer;
+
+ protected JmsAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout,
+ final boolean ignoreExceptions, final JmsManager manager)
+ throws JMSException {
+ super(name, filter, layout, ignoreExceptions);
+ this.manager = manager;
+ this.producer = this.manager.createMessageProducer();
+ }
+
+ @Override
+ public void append(final LogEvent event) {
+ try {
+ final Message message = this.manager.createMessage(getLayout().toSerializable(event));
+ message.setJMSTimestamp(event.getTimeMillis());
+ this.producer.send(message);
+ } catch (final JMSException e) {
+ throw new AppenderLoggingException(e);
+ }
+ }
+
+ @Override
+ public boolean stop(final long timeout, final TimeUnit timeUnit) {
+ setStopping();
+ boolean stopped = super.stop(timeout, timeUnit, false);
+ stopped &= this.manager.stop(timeout, timeUnit);
+ setStopped();
+ return stopped;
+ }
+
+ @PluginBuilderFactory
+ public static Builder newBuilder() {
+ return new Builder();
+ }
+
+ public static class Builder implements org.apache.logging.log4j.core.util.Builder<JmsAppender> {
+
+ @PluginBuilderAttribute
+ @Required(message = "A name for the JmsAppender must be specified")
+ private String name;
+
+ @PluginBuilderAttribute
+ private String factoryName;
+
+ @PluginBuilderAttribute
+ private String providerUrl;
+
+ @PluginBuilderAttribute
+ private String urlPkgPrefixes;
+
+ @PluginBuilderAttribute
+ private String securityPrincipalName;
+
+ @PluginBuilderAttribute(sensitive = true)
+ private String securityCredentials;
+
+ @PluginBuilderAttribute
+ @Required(message = "A javax.jms.ConnectionFactory JNDI name must be specified")
+ private String factoryBindingName;
+
+ @PluginBuilderAttribute
+ @PluginAliases({"queueBindingName", "topicBindingName"})
+ @Required(message = "A javax.jms.Destination JNDI name must be specified")
+ private String destinationBindingName;
+
+ @PluginBuilderAttribute
+ private String username;
+
+ @PluginBuilderAttribute(sensitive = true)
+ private String password;
+
+ @PluginElement("Layout")
+ private Layout<? extends Serializable> layout = SerializedLayout.createLayout();
+
+ @PluginElement("Filter")
+ private Filter filter;
+
+ @PluginBuilderAttribute
+ private boolean ignoreExceptions = true;
+
+ private Builder() {
+ }
+
+ public Builder setName(final String name) {
+ this.name = name;
+ return this;
+ }
+
+ public Builder setFactoryName(final String factoryName) {
+ this.factoryName = factoryName;
+ return this;
+ }
+
+ public Builder setProviderUrl(final String providerUrl) {
+ this.providerUrl = providerUrl;
+ return this;
+ }
+
+ public Builder setUrlPkgPrefixes(final String urlPkgPrefixes) {
+ this.urlPkgPrefixes = urlPkgPrefixes;
+ return this;
+ }
+
+ public Builder setSecurityPrincipalName(final String securityPrincipalName) {
+ this.securityPrincipalName = securityPrincipalName;
+ return this;
+ }
+
+ public Builder setSecurityCredentials(final String securityCredentials) {
+ this.securityCredentials = securityCredentials;
+ return this;
+ }
+
+ public Builder setFactoryBindingName(final String factoryBindingName) {
+ this.factoryBindingName = factoryBindingName;
+ return this;
+ }
+
+ public Builder setDestinationBindingName(final String destinationBindingName) {
+ this.destinationBindingName = destinationBindingName;
+ return this;
+ }
+
+ public Builder setUsername(final String username) {
+ this.username = username;
+ return this;
+ }
+
+ public Builder setPassword(final String password) {
+ this.password = password;
+ return this;
+ }
+
+ public Builder setLayout(final Layout<? extends Serializable> layout) {
+ this.layout = layout;
+ return this;
+ }
+
+ public Builder setFilter(final Filter filter) {
+ this.filter = filter;
+ return this;
+ }
+
+ public Builder setIgnoreExceptions(final boolean ignoreExceptions) {
+ this.ignoreExceptions = ignoreExceptions;
+ return this;
+ }
+
+ @Override
+ public JmsAppender build() {
+ final JndiManager jndiManager = JndiManager.getJndiManager(factoryName, providerUrl, urlPkgPrefixes,
+ securityPrincipalName, securityCredentials, null);
+ final JmsManager jmsManager = JmsManager.getJmsManager(name, jndiManager, factoryBindingName,
+ destinationBindingName, username, password);
+ try {
+ return new JmsAppender(name, filter, layout, ignoreExceptions, jmsManager);
+ } catch (final JMSException e) {
+ LOGGER.error("Error creating JmsAppender [{}].", name, e);
+ return null;
+ }
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/78fe32c0/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/JmsManager.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/JmsManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/JmsManager.java
index a36d3fb..8d70000 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/JmsManager.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/JmsManager.java
@@ -1,174 +1,177 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-
-package org.apache.logging.log4j.core.appender.mom;
-
-import java.io.Serializable;
-import java.util.concurrent.TimeUnit;
-
-import javax.jms.Connection;
-import javax.jms.ConnectionFactory;
-import javax.jms.Destination;
-import javax.jms.JMSException;
-import javax.jms.Message;
-import javax.jms.MessageConsumer;
-import javax.jms.MessageProducer;
-import javax.jms.Session;
-import javax.naming.NamingException;
-
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.core.appender.AbstractManager;
-import org.apache.logging.log4j.core.appender.ManagerFactory;
-import org.apache.logging.log4j.core.net.JndiManager;
-import org.apache.logging.log4j.status.StatusLogger;
-
-/**
- * JMS connection and session manager. Can be used to access MessageProducer, MessageConsumer, and Message objects
- * involving a configured ConnectionFactory and Destination.
- */
-public class JmsManager extends AbstractManager {
-
- private static final Logger LOGGER = StatusLogger.getLogger();
-
- private static final JmsManagerFactory FACTORY = new JmsManagerFactory();
-
- private final JndiManager jndiManager;
- private final Connection connection;
- private final Session session;
- private final Destination destination;
-
- private JmsManager(final String name, final JndiManager jndiManager, final String connectionFactoryName,
- final String destinationName, final String username, final String password)
- throws NamingException, JMSException {
- super(null, name);
- this.jndiManager = jndiManager;
- final ConnectionFactory connectionFactory = this.jndiManager.lookup(connectionFactoryName);
- if (username != null && password != null) {
- this.connection = connectionFactory.createConnection(username, password);
- } else {
- this.connection = connectionFactory.createConnection();
- }
- this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- this.destination = this.jndiManager.lookup(destinationName);
- this.connection.start();
- }
-
- /**
- * Gets a JmsManager using the specified configuration parameters.
- *
- * @param name The name to use for this JmsManager.
- * @param jndiManager The JndiManager to look up JMS information through.
- * @param connectionFactoryName The binding name for the {@link javax.jms.ConnectionFactory}.
- * @param destinationName The binding name for the {@link javax.jms.Destination}.
- * @param username The username to connect with or {@code null} for no authentication.
- * @param password The password to use with the given username or {@code null} for no authentication.
- * @return The JmsManager as configured.
- */
- public static JmsManager getJmsManager(final String name, final JndiManager jndiManager,
- final String connectionFactoryName, final String destinationName,
- final String username, final String password) {
- final JmsConfiguration configuration = new JmsConfiguration(jndiManager, connectionFactoryName, destinationName,
- username, password);
- return getManager(name, FACTORY, configuration);
- }
-
- /**
- * Creates a MessageConsumer on this Destination using the current Session.
- *
- * @return A MessageConsumer on this Destination.
- * @throws JMSException
- */
- public MessageConsumer createMessageConsumer() throws JMSException {
- return this.session.createConsumer(this.destination);
- }
-
- /**
- * Creates a MessageProducer on this Destination using the current Session.
- *
- * @return A MessageProducer on this Destination.
- * @throws JMSException
- */
- public MessageProducer createMessageProducer() throws JMSException {
- return this.session.createProducer(this.destination);
- }
-
- /**
- * Creates a TextMessage or ObjectMessage from a Serializable object. For instance, when using a text-based
- * {@link org.apache.logging.log4j.core.Layout} such as {@link org.apache.logging.log4j.core.layout.PatternLayout},
- * the {@link org.apache.logging.log4j.core.LogEvent} message will be serialized to a String. When using a
- * layout such as {@link org.apache.logging.log4j.core.layout.SerializedLayout}, the LogEvent message will be
- * serialized as a Java object.
- *
- * @param object The LogEvent or String message to wrap.
- * @return A new JMS message containing the provided object.
- * @throws JMSException
- */
- public Message createMessage(final Serializable object) throws JMSException {
- if (object instanceof String) {
- return this.session.createTextMessage((String) object);
- }
- return this.session.createObjectMessage(object);
- }
-
- @Override
- protected void releaseSub(final long timeout, final TimeUnit timeUnit) {
- try {
- this.session.close();
- } catch (final JMSException ignored) {
- // ignore
- }
- try {
- this.connection.close();
- } catch (final JMSException ignored) {
- // ignore
- }
- this.jndiManager.stop(timeout, timeUnit);
- }
-
- private static class JmsConfiguration {
- private final JndiManager jndiManager;
- private final String connectionFactoryName;
- private final String destinationName;
- private final String username;
- private final String password;
-
- private JmsConfiguration(final JndiManager jndiManager, final String connectionFactoryName, final String destinationName,
- final String username, final String password) {
- this.jndiManager = jndiManager;
- this.connectionFactoryName = connectionFactoryName;
- this.destinationName = destinationName;
- this.username = username;
- this.password = password;
- }
- }
-
- private static class JmsManagerFactory implements ManagerFactory<JmsManager, JmsConfiguration> {
-
- @Override
- public JmsManager createManager(final String name, final JmsConfiguration data) {
- try {
- return new JmsManager(name, data.jndiManager, data.connectionFactoryName, data.destinationName,
- data.username, data.password);
- } catch (final Exception e) {
- LOGGER.error("Error creating JmsManager using ConnectionFactory [{}] and Destination [{}].",
- data.connectionFactoryName, data.destinationName, e);
- return null;
- }
- }
- }
-
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+package org.apache.logging.log4j.core.appender.mom;
+
+import java.io.Serializable;
+import java.util.concurrent.TimeUnit;
+
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.Destination;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.Session;
+import javax.naming.NamingException;
+
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.appender.AbstractManager;
+import org.apache.logging.log4j.core.appender.ManagerFactory;
+import org.apache.logging.log4j.core.net.JndiManager;
+import org.apache.logging.log4j.status.StatusLogger;
+
+/**
+ * JMS connection and session manager. Can be used to access MessageProducer, MessageConsumer, and Message objects
+ * involving a configured ConnectionFactory and Destination.
+ */
+public class JmsManager extends AbstractManager {
+
+ private static final Logger LOGGER = StatusLogger.getLogger();
+
+ private static final JmsManagerFactory FACTORY = new JmsManagerFactory();
+
+ private final JndiManager jndiManager;
+ private final Connection connection;
+ private final Session session;
+ private final Destination destination;
+
+ private JmsManager(final String name, final JndiManager jndiManager, final String connectionFactoryName,
+ final String destinationName, final String username, final String password)
+ throws NamingException, JMSException {
+ super(null, name);
+ this.jndiManager = jndiManager;
+ final ConnectionFactory connectionFactory = this.jndiManager.lookup(connectionFactoryName);
+ if (username != null && password != null) {
+ this.connection = connectionFactory.createConnection(username, password);
+ } else {
+ this.connection = connectionFactory.createConnection();
+ }
+ this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ this.destination = this.jndiManager.lookup(destinationName);
+ this.connection.start();
+ }
+
+ /**
+ * Gets a JmsManager using the specified configuration parameters.
+ *
+ * @param name The name to use for this JmsManager.
+ * @param jndiManager The JndiManager to look up JMS information through.
+ * @param connectionFactoryName The binding name for the {@link javax.jms.ConnectionFactory}.
+ * @param destinationName The binding name for the {@link javax.jms.Destination}.
+ * @param username The username to connect with or {@code null} for no authentication.
+ * @param password The password to use with the given username or {@code null} for no authentication.
+ * @return The JmsManager as configured.
+ */
+ public static JmsManager getJmsManager(final String name, final JndiManager jndiManager,
+ final String connectionFactoryName, final String destinationName,
+ final String username, final String password) {
+ final JmsConfiguration configuration = new JmsConfiguration(jndiManager, connectionFactoryName, destinationName,
+ username, password);
+ return getManager(name, FACTORY, configuration);
+ }
+
+ /**
+ * Creates a MessageConsumer on this Destination using the current Session.
+ *
+ * @return A MessageConsumer on this Destination.
+ * @throws JMSException
+ */
+ public MessageConsumer createMessageConsumer() throws JMSException {
+ return this.session.createConsumer(this.destination);
+ }
+
+ /**
+ * Creates a MessageProducer on this Destination using the current Session.
+ *
+ * @return A MessageProducer on this Destination.
+ * @throws JMSException
+ */
+ public MessageProducer createMessageProducer() throws JMSException {
+ return this.session.createProducer(this.destination);
+ }
+
+ /**
+ * Creates a TextMessage or ObjectMessage from a Serializable object. For instance, when using a text-based
+ * {@link org.apache.logging.log4j.core.Layout} such as {@link org.apache.logging.log4j.core.layout.PatternLayout},
+ * the {@link org.apache.logging.log4j.core.LogEvent} message will be serialized to a String. When using a
+ * layout such as {@link org.apache.logging.log4j.core.layout.SerializedLayout}, the LogEvent message will be
+ * serialized as a Java object.
+ *
+ * @param object The LogEvent or String message to wrap.
+ * @return A new JMS message containing the provided object.
+ * @throws JMSException
+ */
+ public Message createMessage(final Serializable object) throws JMSException {
+ if (object instanceof String) {
+ return this.session.createTextMessage((String) object);
+ }
+ return this.session.createObjectMessage(object);
+ }
+
+ @Override
+ protected boolean releaseSub(final long timeout, final TimeUnit timeUnit) {
+ boolean closed = true;
+ try {
+ this.session.close();
+ } catch (final JMSException ignored) {
+ // ignore
+ closed = false;
+ }
+ try {
+ this.connection.close();
+ } catch (final JMSException ignored) {
+ // ignore
+ closed = false;
+ }
+ return closed && this.jndiManager.stop(timeout, timeUnit);
+ }
+
+ private static class JmsConfiguration {
+ private final JndiManager jndiManager;
+ private final String connectionFactoryName;
+ private final String destinationName;
+ private final String username;
+ private final String password;
+
+ private JmsConfiguration(final JndiManager jndiManager, final String connectionFactoryName, final String destinationName,
+ final String username, final String password) {
+ this.jndiManager = jndiManager;
+ this.connectionFactoryName = connectionFactoryName;
+ this.destinationName = destinationName;
+ this.username = username;
+ this.password = password;
+ }
+ }
+
+ private static class JmsManagerFactory implements ManagerFactory<JmsManager, JmsConfiguration> {
+
+ @Override
+ public JmsManager createManager(final String name, final JmsConfiguration data) {
+ try {
+ return new JmsManager(name, data.jndiManager, data.connectionFactoryName, data.destinationName,
+ data.username, data.password);
+ } catch (final Exception e) {
+ LOGGER.error("Error creating JmsManager using ConnectionFactory [{}] and Destination [{}].",
+ data.connectionFactoryName, data.destinationName, e);
+ return null;
+ }
+ }
+ }
+
+}
[7/7] logging-log4j2 git commit: Properly track resources during
shutdown.
Posted by gg...@apache.org.
Properly track resources during shutdown.
Detect if an a problem occurred during shutdown.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/78fe32c0
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/78fe32c0
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/78fe32c0
Branch: refs/heads/master
Commit: 78fe32c0d4643e6692bbcdc3ed754be90f647183
Parents: 97e45ed
Author: ggregory <gg...@US-L-GG02.rocketsoftware.com>
Authored: Wed Sep 7 13:37:42 2016 -0400
Committer: ggregory <gg...@US-L-GG02.rocketsoftware.com>
Committed: Wed Sep 7 13:37:42 2016 -0400
----------------------------------------------------------------------
.../log4j/core/appender/AbstractManager.java | 11 +-
.../appender/AbstractOutputStreamAppender.java | 2 +-
.../core/appender/AbstractWriterAppender.java | 252 +--
.../core/appender/OutputStreamManager.java | 4 +-
.../core/appender/RollingFileAppender.java | 748 ++++----
.../log4j/core/appender/WriterManager.java | 301 ++--
.../appender/db/AbstractDatabaseAppender.java | 284 +--
.../appender/db/AbstractDatabaseManager.java | 458 ++---
.../appender/db/jdbc/JdbcDatabaseManager.java | 515 +++---
.../appender/db/jpa/JpaDatabaseManager.java | 381 ++--
.../log4j/core/appender/mom/JmsAppender.java | 430 ++---
.../log4j/core/appender/mom/JmsManager.java | 351 ++--
.../appender/mom/jeromq/JeroMqAppender.java | 370 ++--
.../core/appender/mom/jeromq/JeroMqManager.java | 443 ++---
.../core/appender/mom/kafka/KafkaAppender.java | 240 +--
.../core/appender/mom/kafka/KafkaManager.java | 159 +-
.../appender/rolling/RollingFileManager.java | 958 +++++-----
.../logging/log4j/core/net/JndiManager.java | 288 +--
.../log4j/core/net/server/JmsServer.java | 295 +--
.../logging/log4j/core/util/JndiCloser.java | 118 +-
.../log4j/flume/appender/FlumeAppender.java | 6 +-
.../log4j/flume/appender/FlumeAvroManager.java | 670 +++----
.../flume/appender/FlumeEmbeddedManager.java | 555 +++---
.../flume/appender/FlumePersistentManager.java | 1699 +++++++++---------
.../nosql/appender/NoSqlDatabaseManager.java | 440 ++---
25 files changed, 5008 insertions(+), 4970 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/78fe32c0/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AbstractManager.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AbstractManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AbstractManager.java
index acd2759..a08ec75 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AbstractManager.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AbstractManager.java
@@ -74,18 +74,21 @@ public abstract class AbstractManager implements AutoCloseable {
stop(AbstractLifeCycle.DEFAULT_STOP_TIMEOUT, AbstractLifeCycle.DEFAULT_STOP_TIMEUNIT);
}
- public void stop(final long timeout, final TimeUnit timeUnit) {
+ public boolean stop(final long timeout, final TimeUnit timeUnit) {
+ boolean stopped = true;
LOCK.lock();
try {
--count;
if (count <= 0) {
MAP.remove(name);
LOGGER.debug("Shutting down {} {}", this.getClass().getSimpleName(), getName());
- releaseSub(timeout, timeUnit);
+ stopped = releaseSub(timeout, timeUnit);
+ LOGGER.debug("Shut down {} {}, all resources released: {}", this.getClass().getSimpleName(), getName(), stopped);
}
} finally {
LOCK.unlock();
}
+ return stopped;
}
/**
@@ -143,9 +146,11 @@ public abstract class AbstractManager implements AutoCloseable {
* lock is held. A timeout is passed for implementors to use as they see fit.
* @param timeout timeout
* @param timeUnit timeout time unit
+ * @return true if all resources were closed normally, false otherwise.
*/
- protected void releaseSub(final long timeout, final TimeUnit timeUnit) {
+ protected boolean releaseSub(final long timeout, final TimeUnit timeUnit) {
// This default implementation does nothing.
+ return true;
}
protected int getCount() {
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/78fe32c0/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AbstractOutputStreamAppender.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AbstractOutputStreamAppender.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AbstractOutputStreamAppender.java
index d4ad3f7..f7e86b4 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AbstractOutputStreamAppender.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AbstractOutputStreamAppender.java
@@ -139,7 +139,7 @@ public abstract class AbstractOutputStreamAppender<M extends OutputStreamManager
@Override
protected boolean stop(final long timeout, final TimeUnit timeUnit, final boolean changeLifeCycleState) {
boolean stopped = super.stop(timeout, timeUnit, changeLifeCycleState);
- manager.stop(timeout, timeUnit);
+ stopped &= manager.stop(timeout, timeUnit);
if (changeLifeCycleState) {
setStopped();
}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/78fe32c0/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AbstractWriterAppender.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AbstractWriterAppender.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AbstractWriterAppender.java
index 7e1d46b..17a0bb4 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AbstractWriterAppender.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AbstractWriterAppender.java
@@ -1,126 +1,126 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-package org.apache.logging.log4j.core.appender;
-
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReadWriteLock;
-import java.util.concurrent.locks.ReentrantReadWriteLock;
-
-import org.apache.logging.log4j.core.Filter;
-import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.core.StringLayout;
-
-/**
- * Appends log events as strings to a writer.
- *
- * @param <M>
- * The kind of {@link WriterManager} under management
- */
-public abstract class AbstractWriterAppender<M extends WriterManager> extends AbstractAppender {
-
- /**
- * Immediate flush means that the underlying writer will be flushed at the
- * end of each append operation. Immediate flush is slower but ensures that
- * each append request is actually written. If <code>immediateFlush</code>
- * is set to {@code false}, then there is a good chance that the last few
- * logs events are not actually written to persistent media if and when the
- * application crashes.
- */
- protected final boolean immediateFlush;
- private final M manager;
- private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
- private final Lock readLock = readWriteLock.readLock();
-
- /**
- * Instantiates.
- *
- * @param name
- * The name of the Appender.
- * @param layout
- * The layout to format the message.
- * @param manager
- * The OutputStreamManager.
- */
- protected AbstractWriterAppender(final String name, final StringLayout layout, final Filter filter,
- final boolean ignoreExceptions, final boolean immediateFlush, final M manager) {
- super(name, filter, layout, ignoreExceptions);
- this.manager = manager;
- this.immediateFlush = immediateFlush;
- }
-
- /**
- * Actual writing occurs here.
- * <p>
- * Most subclasses will need to override this method.
- * </p>
- *
- * @param event
- * The LogEvent.
- */
- @Override
- public void append(final LogEvent event) {
- readLock.lock();
- try {
- final String str = getStringLayout().toSerializable(event);
- if (str.length() > 0) {
- manager.write(str);
- if (this.immediateFlush || event.isEndOfBatch()) {
- manager.flush();
- }
- }
- } catch (final AppenderLoggingException ex) {
- error("Unable to write " + manager.getName() + " for appender " + getName() + ": " + ex);
- throw ex;
- } finally {
- readLock.unlock();
- }
- }
-
- /**
- * Gets the manager.
- *
- * @return the manager.
- */
- public M getManager() {
- return manager;
- }
-
- public StringLayout getStringLayout() {
- return (StringLayout) getLayout();
- }
-
- @Override
- public void start() {
- if (getLayout() == null) {
- LOGGER.error("No layout set for the appender named [{}].", getName());
- }
- if (manager == null) {
- LOGGER.error("No OutputStreamManager set for the appender named [{}].", getName());
- }
- super.start();
- }
-
- @Override
- public boolean stop(final long timeout, final TimeUnit timeUnit) {
- setStopping();
- super.stop(timeout, timeUnit, false);
- manager.stop(timeout, timeUnit);
- setStopped();
- return true;
- }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core.appender;
+
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+import org.apache.logging.log4j.core.Filter;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.StringLayout;
+
+/**
+ * Appends log events as strings to a writer.
+ *
+ * @param <M>
+ * The kind of {@link WriterManager} under management
+ */
+public abstract class AbstractWriterAppender<M extends WriterManager> extends AbstractAppender {
+
+ /**
+ * Immediate flush means that the underlying writer will be flushed at the
+ * end of each append operation. Immediate flush is slower but ensures that
+ * each append request is actually written. If <code>immediateFlush</code>
+ * is set to {@code false}, then there is a good chance that the last few
+ * logs events are not actually written to persistent media if and when the
+ * application crashes.
+ */
+ protected final boolean immediateFlush;
+ private final M manager;
+ private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
+ private final Lock readLock = readWriteLock.readLock();
+
+ /**
+ * Instantiates.
+ *
+ * @param name
+ * The name of the Appender.
+ * @param layout
+ * The layout to format the message.
+ * @param manager
+ * The OutputStreamManager.
+ */
+ protected AbstractWriterAppender(final String name, final StringLayout layout, final Filter filter,
+ final boolean ignoreExceptions, final boolean immediateFlush, final M manager) {
+ super(name, filter, layout, ignoreExceptions);
+ this.manager = manager;
+ this.immediateFlush = immediateFlush;
+ }
+
+ /**
+ * Actual writing occurs here.
+ * <p>
+ * Most subclasses will need to override this method.
+ * </p>
+ *
+ * @param event
+ * The LogEvent.
+ */
+ @Override
+ public void append(final LogEvent event) {
+ readLock.lock();
+ try {
+ final String str = getStringLayout().toSerializable(event);
+ if (str.length() > 0) {
+ manager.write(str);
+ if (this.immediateFlush || event.isEndOfBatch()) {
+ manager.flush();
+ }
+ }
+ } catch (final AppenderLoggingException ex) {
+ error("Unable to write " + manager.getName() + " for appender " + getName() + ": " + ex);
+ throw ex;
+ } finally {
+ readLock.unlock();
+ }
+ }
+
+ /**
+ * Gets the manager.
+ *
+ * @return the manager.
+ */
+ public M getManager() {
+ return manager;
+ }
+
+ public StringLayout getStringLayout() {
+ return (StringLayout) getLayout();
+ }
+
+ @Override
+ public void start() {
+ if (getLayout() == null) {
+ LOGGER.error("No layout set for the appender named [{}].", getName());
+ }
+ if (manager == null) {
+ LOGGER.error("No OutputStreamManager set for the appender named [{}].", getName());
+ }
+ super.start();
+ }
+
+ @Override
+ public boolean stop(final long timeout, final TimeUnit timeUnit) {
+ setStopping();
+ boolean stopped = super.stop(timeout, timeUnit, false);
+ stopped &= manager.stop(timeout, timeUnit);
+ setStopped();
+ return stopped;
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/78fe32c0/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamManager.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamManager.java
index c5b7a45..d7a2627 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamManager.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamManager.java
@@ -131,9 +131,9 @@ public class OutputStreamManager extends AbstractManager implements ByteBufferDe
* Default hook to write footer during close.
*/
@Override
- public void releaseSub(final long timeout, final TimeUnit timeUnit) {
+ public boolean releaseSub(final long timeout, final TimeUnit timeUnit) {
writeFooter();
- closeOutputStream();
+ return closeOutputStream();
}
/**
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/78fe32c0/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/RollingFileAppender.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/RollingFileAppender.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/RollingFileAppender.java
index 726c2d2..b799d1c 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/RollingFileAppender.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/RollingFileAppender.java
@@ -1,374 +1,374 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-package org.apache.logging.log4j.core.appender;
-
-import java.io.Serializable;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.concurrent.TimeUnit;
-import java.util.zip.Deflater;
-
-import org.apache.logging.log4j.core.Filter;
-import org.apache.logging.log4j.core.Layout;
-import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy;
-import org.apache.logging.log4j.core.appender.rolling.RollingFileManager;
-import org.apache.logging.log4j.core.appender.rolling.RolloverStrategy;
-import org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy;
-import org.apache.logging.log4j.core.config.Configuration;
-import org.apache.logging.log4j.core.config.plugins.Plugin;
-import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
-import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
-import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
-import org.apache.logging.log4j.core.config.plugins.PluginElement;
-import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
-import org.apache.logging.log4j.core.net.Advertiser;
-import org.apache.logging.log4j.core.util.Booleans;
-import org.apache.logging.log4j.core.util.Integers;
-
-/**
- * An appender that writes to files and can roll over at intervals.
- */
-@Plugin(name = "RollingFile", category = "Core", elementType = "appender", printObject = true)
-public final class RollingFileAppender extends AbstractOutputStreamAppender<RollingFileManager> {
-
- /**
- * Builds FileAppender instances.
- *
- * @param <B>
- * This builder class
- */
- public static class Builder<B extends Builder<B>> extends AbstractOutputStreamAppender.Builder<B>
- implements org.apache.logging.log4j.core.util.Builder<RollingFileAppender> {
-
- @PluginBuilderAttribute
- @Required
- private String fileName;
-
- @PluginBuilderAttribute
- @Required
- private String filePattern;
-
- @PluginBuilderAttribute
- private boolean append = true;
-
- @PluginBuilderAttribute
- private boolean locking;
-
- @PluginElement("Policy")
- @Required
- private TriggeringPolicy policy;
-
- @PluginElement("Strategy")
- private RolloverStrategy strategy;
-
- @PluginBuilderAttribute
- private boolean advertise;
-
- @PluginBuilderAttribute
- private String advertiseUri;
-
- @PluginBuilderAttribute
- private boolean createOnDemand;
-
- @PluginConfiguration
- private Configuration configuration;
-
- @Override
- public RollingFileAppender build() {
- // Even though some variables may be annotated with @Required, we must still perform validation here for
- // call sites that build builders programmatically.
- final boolean isBufferedIo = isBufferedIo();
- final int bufferSize = getBufferSize();
- if (getName() == null) {
- LOGGER.error("RollingFileAppender '{}': No name provided.", getName());
- return null;
- }
-
- if (!isBufferedIo && bufferSize > 0) {
- LOGGER.warn("RollingFileAppender '{}': The bufferSize is set to {} but bufferedIO is not true", getName(), bufferSize);
- }
-
- if (fileName == null) {
- LOGGER.error("RollingFileAppender '{}': No file name provided.", getName());
- return null;
- }
-
- if (filePattern == null) {
- LOGGER.error("RollingFileAppender '{}': No file name pattern provided.", getName());
- return null;
- }
-
- if (policy == null) {
- LOGGER.error("RollingFileAppender '{}': No TriggeringPolicy provided.", getName());
- return null;
- }
-
- if (strategy == null) {
- strategy = DefaultRolloverStrategy.createStrategy(null, null, null,
- String.valueOf(Deflater.DEFAULT_COMPRESSION), null, true, configuration);
- }
-
- if (strategy == null) {
- strategy = DefaultRolloverStrategy.createStrategy(null, null, null,
- String.valueOf(Deflater.DEFAULT_COMPRESSION), null, true, configuration);
- }
-
- final RollingFileManager manager = RollingFileManager.getFileManager(fileName, filePattern, append,
- isBufferedIo, policy, strategy, advertiseUri, getLayout(), bufferSize, isImmediateFlush(),
- createOnDemand, configuration);
- if (manager == null) {
- return null;
- }
-
- manager.initialize();
-
- return new RollingFileAppender(getName(), getLayout(), getFilter(), manager, fileName, filePattern,
- isIgnoreExceptions(), isImmediateFlush(), advertise ? configuration.getAdvertiser() : null);
- }
-
- public String getAdvertiseUri() {
- return advertiseUri;
- }
-
- public Configuration getConfiguration() {
- return configuration;
- }
-
- public String getFileName() {
- return fileName;
- }
-
- public boolean isAdvertise() {
- return advertise;
- }
-
- public boolean isAppend() {
- return append;
- }
-
- public boolean isCreateOnDemand() {
- return createOnDemand;
- }
-
- public boolean isLocking() {
- return locking;
- }
-
- public B withAdvertise(final boolean advertise) {
- this.advertise = advertise;
- return asBuilder();
- }
-
- public B withAdvertiseUri(final String advertiseUri) {
- this.advertiseUri = advertiseUri;
- return asBuilder();
- }
-
- public B withAppend(final boolean append) {
- this.append = append;
- return asBuilder();
- }
-
- public B withConfiguration(final Configuration config) {
- this.configuration = config;
- return asBuilder();
- }
-
- public B withFileName(final String fileName) {
- this.fileName = fileName;
- return asBuilder();
- }
-
- public B withCreateOnDemand(final boolean createOnDemand) {
- this.createOnDemand = createOnDemand;
- return asBuilder();
- }
-
- public B withLocking(final boolean locking) {
- this.locking = locking;
- return asBuilder();
- }
-
- public String getFilePattern() {
- return filePattern;
- }
-
- public TriggeringPolicy getPolicy() {
- return policy;
- }
-
- public RolloverStrategy getStrategy() {
- return strategy;
- }
-
- public B withFilePattern(final String filePattern) {
- this.filePattern = filePattern;
- return asBuilder();
- }
-
- public B withPolicy(final TriggeringPolicy policy) {
- this.policy = policy;
- return asBuilder();
- }
-
- public B withStrategy(final RolloverStrategy strategy) {
- this.strategy = strategy;
- return asBuilder();
- }
-
- }
-
- private static final int DEFAULT_BUFFER_SIZE = 8192;
-
- private final String fileName;
- private final String filePattern;
- private Object advertisement;
- private final Advertiser advertiser;
-
- private RollingFileAppender(final String name, final Layout<? extends Serializable> layout, final Filter filter,
- final RollingFileManager manager, final String fileName, final String filePattern,
- final boolean ignoreExceptions, final boolean immediateFlush, final Advertiser advertiser) {
- super(name, layout, filter, ignoreExceptions, immediateFlush, manager);
- if (advertiser != null) {
- final Map<String, String> configuration = new HashMap<>(layout.getContentFormat());
- configuration.put("contentType", layout.getContentType());
- configuration.put("name", name);
- advertisement = advertiser.advertise(configuration);
- }
- this.fileName = fileName;
- this.filePattern = filePattern;
- this.advertiser = advertiser;
- }
-
- @Override
- public boolean stop(final long timeout, final TimeUnit timeUnit) {
- setStopping();
- super.stop(timeout, timeUnit, false);
- if (advertiser != null) {
- advertiser.unadvertise(advertisement);
- }
- setStopped();
- return true;
- }
-
- /**
- * Writes the log entry rolling over the file when required.
-
- * @param event The LogEvent.
- */
- @Override
- public void append(final LogEvent event) {
- getManager().checkRollover(event);
- super.append(event);
- }
-
- /**
- * Returns the File name for the Appender.
- * @return The file name.
- */
- public String getFileName() {
- return fileName;
- }
-
- /**
- * Returns the file pattern used when rolling over.
- * @return The file pattern.
- */
- public String getFilePattern() {
- return filePattern;
- }
-
- /**
- * Returns the triggering policy.
- * @param <T> TriggeringPolicy type
- * @return The TriggeringPolicy
- */
- public <T extends TriggeringPolicy> T getTriggeringPolicy() {
- return getManager().getTriggeringPolicy();
- }
-
- /**
- * Creates a RollingFileAppender.
- * @param fileName The name of the file that is actively written to. (required).
- * @param filePattern The pattern of the file name to use on rollover. (required).
- * @param append If true, events are appended to the file. If false, the file
- * is overwritten when opened. Defaults to "true"
- * @param name The name of the Appender (required).
- * @param bufferedIO When true, I/O will be buffered. Defaults to "true".
- * @param bufferSizeStr buffer size for buffered IO (default is 8192).
- * @param immediateFlush When true, events are immediately flushed. Defaults to "true".
- * @param policy The triggering policy. (required).
- * @param strategy The rollover strategy. Defaults to DefaultRolloverStrategy.
- * @param layout The layout to use (defaults to the default PatternLayout).
- * @param filter The Filter or null.
- * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
- * they are propagated to the caller.
- * @param advertise "true" if the appender configuration should be advertised, "false" otherwise.
- * @param advertiseUri The advertised URI which can be used to retrieve the file contents.
- * @param config The Configuration.
- * @return A RollingFileAppender.
- * @deprecated Use {@link #newBuilder()}.
- */
- @Deprecated
- public static RollingFileAppender createAppender(
- // @formatter:off
- final String fileName,
- final String filePattern,
- final String append,
- final String name,
- final String bufferedIO,
- final String bufferSizeStr,
- final String immediateFlush,
- final TriggeringPolicy policy,
- final RolloverStrategy strategy,
- final Layout<? extends Serializable> layout,
- final Filter filter,
- final String ignore,
- final String advertise,
- final String advertiseUri,
- final Configuration config) {
- // @formatter:on
- final int bufferSize = Integers.parseInt(bufferSizeStr, DEFAULT_BUFFER_SIZE);
- // @formatter:off
- return newBuilder()
- .withAdvertise(Boolean.parseBoolean(advertise))
- .withAdvertiseUri(advertiseUri)
- .withAppend(Booleans.parseBoolean(append, true))
- .withBufferedIo(Booleans.parseBoolean(bufferedIO, true))
- .withBufferSize(bufferSize)
- .withConfiguration(config)
- .withFileName(fileName)
- .withFilePattern(filePattern)
- .withFilter(filter)
- .withIgnoreExceptions(Booleans.parseBoolean(ignore, true))
- .withImmediateFlush(Booleans.parseBoolean(immediateFlush, true))
- .withLayout(layout)
- .withCreateOnDemand(false)
- .withLocking(false)
- .withName(name)
- .withPolicy(policy)
- .withStrategy(strategy)
- .build();
- // @formatter:on
- }
-
- @PluginBuilderFactory
- public static <B extends Builder<B>> B newBuilder() {
- return new Builder<B>().asBuilder();
- }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core.appender;
+
+import java.io.Serializable;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+import java.util.zip.Deflater;
+
+import org.apache.logging.log4j.core.Filter;
+import org.apache.logging.log4j.core.Layout;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy;
+import org.apache.logging.log4j.core.appender.rolling.RollingFileManager;
+import org.apache.logging.log4j.core.appender.rolling.RolloverStrategy;
+import org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
+import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
+import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
+import org.apache.logging.log4j.core.config.plugins.PluginElement;
+import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
+import org.apache.logging.log4j.core.net.Advertiser;
+import org.apache.logging.log4j.core.util.Booleans;
+import org.apache.logging.log4j.core.util.Integers;
+
+/**
+ * An appender that writes to files and can roll over at intervals.
+ */
+@Plugin(name = "RollingFile", category = "Core", elementType = "appender", printObject = true)
+public final class RollingFileAppender extends AbstractOutputStreamAppender<RollingFileManager> {
+
+ /**
+ * Builds FileAppender instances.
+ *
+ * @param <B>
+ * This builder class
+ */
+ public static class Builder<B extends Builder<B>> extends AbstractOutputStreamAppender.Builder<B>
+ implements org.apache.logging.log4j.core.util.Builder<RollingFileAppender> {
+
+ @PluginBuilderAttribute
+ @Required
+ private String fileName;
+
+ @PluginBuilderAttribute
+ @Required
+ private String filePattern;
+
+ @PluginBuilderAttribute
+ private boolean append = true;
+
+ @PluginBuilderAttribute
+ private boolean locking;
+
+ @PluginElement("Policy")
+ @Required
+ private TriggeringPolicy policy;
+
+ @PluginElement("Strategy")
+ private RolloverStrategy strategy;
+
+ @PluginBuilderAttribute
+ private boolean advertise;
+
+ @PluginBuilderAttribute
+ private String advertiseUri;
+
+ @PluginBuilderAttribute
+ private boolean createOnDemand;
+
+ @PluginConfiguration
+ private Configuration configuration;
+
+ @Override
+ public RollingFileAppender build() {
+ // Even though some variables may be annotated with @Required, we must still perform validation here for
+ // call sites that build builders programmatically.
+ final boolean isBufferedIo = isBufferedIo();
+ final int bufferSize = getBufferSize();
+ if (getName() == null) {
+ LOGGER.error("RollingFileAppender '{}': No name provided.", getName());
+ return null;
+ }
+
+ if (!isBufferedIo && bufferSize > 0) {
+ LOGGER.warn("RollingFileAppender '{}': The bufferSize is set to {} but bufferedIO is not true", getName(), bufferSize);
+ }
+
+ if (fileName == null) {
+ LOGGER.error("RollingFileAppender '{}': No file name provided.", getName());
+ return null;
+ }
+
+ if (filePattern == null) {
+ LOGGER.error("RollingFileAppender '{}': No file name pattern provided.", getName());
+ return null;
+ }
+
+ if (policy == null) {
+ LOGGER.error("RollingFileAppender '{}': No TriggeringPolicy provided.", getName());
+ return null;
+ }
+
+ if (strategy == null) {
+ strategy = DefaultRolloverStrategy.createStrategy(null, null, null,
+ String.valueOf(Deflater.DEFAULT_COMPRESSION), null, true, configuration);
+ }
+
+ if (strategy == null) {
+ strategy = DefaultRolloverStrategy.createStrategy(null, null, null,
+ String.valueOf(Deflater.DEFAULT_COMPRESSION), null, true, configuration);
+ }
+
+ final RollingFileManager manager = RollingFileManager.getFileManager(fileName, filePattern, append,
+ isBufferedIo, policy, strategy, advertiseUri, getLayout(), bufferSize, isImmediateFlush(),
+ createOnDemand, configuration);
+ if (manager == null) {
+ return null;
+ }
+
+ manager.initialize();
+
+ return new RollingFileAppender(getName(), getLayout(), getFilter(), manager, fileName, filePattern,
+ isIgnoreExceptions(), isImmediateFlush(), advertise ? configuration.getAdvertiser() : null);
+ }
+
+ public String getAdvertiseUri() {
+ return advertiseUri;
+ }
+
+ public Configuration getConfiguration() {
+ return configuration;
+ }
+
+ public String getFileName() {
+ return fileName;
+ }
+
+ public boolean isAdvertise() {
+ return advertise;
+ }
+
+ public boolean isAppend() {
+ return append;
+ }
+
+ public boolean isCreateOnDemand() {
+ return createOnDemand;
+ }
+
+ public boolean isLocking() {
+ return locking;
+ }
+
+ public B withAdvertise(final boolean advertise) {
+ this.advertise = advertise;
+ return asBuilder();
+ }
+
+ public B withAdvertiseUri(final String advertiseUri) {
+ this.advertiseUri = advertiseUri;
+ return asBuilder();
+ }
+
+ public B withAppend(final boolean append) {
+ this.append = append;
+ return asBuilder();
+ }
+
+ public B withConfiguration(final Configuration config) {
+ this.configuration = config;
+ return asBuilder();
+ }
+
+ public B withFileName(final String fileName) {
+ this.fileName = fileName;
+ return asBuilder();
+ }
+
+ public B withCreateOnDemand(final boolean createOnDemand) {
+ this.createOnDemand = createOnDemand;
+ return asBuilder();
+ }
+
+ public B withLocking(final boolean locking) {
+ this.locking = locking;
+ return asBuilder();
+ }
+
+ public String getFilePattern() {
+ return filePattern;
+ }
+
+ public TriggeringPolicy getPolicy() {
+ return policy;
+ }
+
+ public RolloverStrategy getStrategy() {
+ return strategy;
+ }
+
+ public B withFilePattern(final String filePattern) {
+ this.filePattern = filePattern;
+ return asBuilder();
+ }
+
+ public B withPolicy(final TriggeringPolicy policy) {
+ this.policy = policy;
+ return asBuilder();
+ }
+
+ public B withStrategy(final RolloverStrategy strategy) {
+ this.strategy = strategy;
+ return asBuilder();
+ }
+
+ }
+
+ private static final int DEFAULT_BUFFER_SIZE = 8192;
+
+ private final String fileName;
+ private final String filePattern;
+ private Object advertisement;
+ private final Advertiser advertiser;
+
+ private RollingFileAppender(final String name, final Layout<? extends Serializable> layout, final Filter filter,
+ final RollingFileManager manager, final String fileName, final String filePattern,
+ final boolean ignoreExceptions, final boolean immediateFlush, final Advertiser advertiser) {
+ super(name, layout, filter, ignoreExceptions, immediateFlush, manager);
+ if (advertiser != null) {
+ final Map<String, String> configuration = new HashMap<>(layout.getContentFormat());
+ configuration.put("contentType", layout.getContentType());
+ configuration.put("name", name);
+ advertisement = advertiser.advertise(configuration);
+ }
+ this.fileName = fileName;
+ this.filePattern = filePattern;
+ this.advertiser = advertiser;
+ }
+
+ @Override
+ public boolean stop(final long timeout, final TimeUnit timeUnit) {
+ setStopping();
+ final boolean stopped = super.stop(timeout, timeUnit, false);
+ if (advertiser != null) {
+ advertiser.unadvertise(advertisement);
+ }
+ setStopped();
+ return stopped;
+ }
+
+ /**
+ * Writes the log entry rolling over the file when required.
+
+ * @param event The LogEvent.
+ */
+ @Override
+ public void append(final LogEvent event) {
+ getManager().checkRollover(event);
+ super.append(event);
+ }
+
+ /**
+ * Returns the File name for the Appender.
+ * @return The file name.
+ */
+ public String getFileName() {
+ return fileName;
+ }
+
+ /**
+ * Returns the file pattern used when rolling over.
+ * @return The file pattern.
+ */
+ public String getFilePattern() {
+ return filePattern;
+ }
+
+ /**
+ * Returns the triggering policy.
+ * @param <T> TriggeringPolicy type
+ * @return The TriggeringPolicy
+ */
+ public <T extends TriggeringPolicy> T getTriggeringPolicy() {
+ return getManager().getTriggeringPolicy();
+ }
+
+ /**
+ * Creates a RollingFileAppender.
+ * @param fileName The name of the file that is actively written to. (required).
+ * @param filePattern The pattern of the file name to use on rollover. (required).
+ * @param append If true, events are appended to the file. If false, the file
+ * is overwritten when opened. Defaults to "true"
+ * @param name The name of the Appender (required).
+ * @param bufferedIO When true, I/O will be buffered. Defaults to "true".
+ * @param bufferSizeStr buffer size for buffered IO (default is 8192).
+ * @param immediateFlush When true, events are immediately flushed. Defaults to "true".
+ * @param policy The triggering policy. (required).
+ * @param strategy The rollover strategy. Defaults to DefaultRolloverStrategy.
+ * @param layout The layout to use (defaults to the default PatternLayout).
+ * @param filter The Filter or null.
+ * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
+ * they are propagated to the caller.
+ * @param advertise "true" if the appender configuration should be advertised, "false" otherwise.
+ * @param advertiseUri The advertised URI which can be used to retrieve the file contents.
+ * @param config The Configuration.
+ * @return A RollingFileAppender.
+ * @deprecated Use {@link #newBuilder()}.
+ */
+ @Deprecated
+ public static RollingFileAppender createAppender(
+ // @formatter:off
+ final String fileName,
+ final String filePattern,
+ final String append,
+ final String name,
+ final String bufferedIO,
+ final String bufferSizeStr,
+ final String immediateFlush,
+ final TriggeringPolicy policy,
+ final RolloverStrategy strategy,
+ final Layout<? extends Serializable> layout,
+ final Filter filter,
+ final String ignore,
+ final String advertise,
+ final String advertiseUri,
+ final Configuration config) {
+ // @formatter:on
+ final int bufferSize = Integers.parseInt(bufferSizeStr, DEFAULT_BUFFER_SIZE);
+ // @formatter:off
+ return newBuilder()
+ .withAdvertise(Boolean.parseBoolean(advertise))
+ .withAdvertiseUri(advertiseUri)
+ .withAppend(Booleans.parseBoolean(append, true))
+ .withBufferedIo(Booleans.parseBoolean(bufferedIO, true))
+ .withBufferSize(bufferSize)
+ .withConfiguration(config)
+ .withFileName(fileName)
+ .withFilePattern(filePattern)
+ .withFilter(filter)
+ .withIgnoreExceptions(Booleans.parseBoolean(ignore, true))
+ .withImmediateFlush(Booleans.parseBoolean(immediateFlush, true))
+ .withLayout(layout)
+ .withCreateOnDemand(false)
+ .withLocking(false)
+ .withName(name)
+ .withPolicy(policy)
+ .withStrategy(strategy)
+ .build();
+ // @formatter:on
+ }
+
+ @PluginBuilderFactory
+ public static <B extends Builder<B>> B newBuilder() {
+ return new Builder<B>().asBuilder();
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/78fe32c0/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/WriterManager.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/WriterManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/WriterManager.java
index 323a9fe..c2e7572 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/WriterManager.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/WriterManager.java
@@ -1,150 +1,151 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-package org.apache.logging.log4j.core.appender;
-
-import java.io.IOException;
-import java.io.Writer;
-import java.util.concurrent.TimeUnit;
-
-import org.apache.logging.log4j.core.StringLayout;
-
-/**
- * Manages a Writer so that it can be shared by multiple Appenders and will
- * allow appenders to reconfigure without requiring a new writer.
- */
-public class WriterManager extends AbstractManager {
-
- /**
- * Creates a Manager.
- *
- * @param name The name of the stream to manage.
- * @param data The data to pass to the Manager.
- * @param factory The factory to use to create the Manager.
- * @param <T> The type of the WriterManager.
- * @return A WriterManager.
- */
- public static <T> WriterManager getManager(final String name, final T data,
- final ManagerFactory<? extends WriterManager, T> factory) {
- return AbstractManager.getManager(name, factory, data);
- }
- protected final StringLayout layout;
-
- private volatile Writer writer;
-
- public WriterManager(final Writer writer, final String streamName, final StringLayout layout,
- final boolean writeHeader) {
- super(null, streamName);
- this.writer = writer;
- this.layout = layout;
- if (writeHeader && layout != null) {
- final byte[] header = layout.getHeader();
- if (header != null) {
- try {
- this.writer.write(new String(header, layout.getCharset()));
- } catch (final IOException e) {
- logError("Unable to write header", e);
- }
- }
- }
- }
-
- protected synchronized void closeWriter() {
- final Writer w = writer; // access volatile field only once per method
- try {
- w.close();
- } catch (final IOException ex) {
- logError("Unable to close stream", ex);
- }
- }
-
- /**
- * Flushes any buffers.
- */
- public synchronized void flush() {
- try {
- writer.flush();
- } catch (final IOException ex) {
- final String msg = "Error flushing stream " + getName();
- throw new AppenderLoggingException(msg, ex);
- }
- }
-
- protected Writer getWriter() {
- return writer;
- }
-
- /**
- * Returns the status of the stream.
- * @return true if the stream is open, false if it is not.
- */
- public boolean isOpen() {
- return getCount() > 0;
- }
-
- /**
- * Default hook to write footer during close.
- */
- @Override
- public void releaseSub(final long timeout, final TimeUnit timeUnit) {
- writeFooter();
- closeWriter();
- }
-
- protected void setWriter(final Writer writer) {
- final byte[] header = layout.getHeader();
- if (header != null) {
- try {
- writer.write(new String(header, layout.getCharset()));
- this.writer = writer; // only update field if writer.write() succeeded
- } catch (final IOException ioe) {
- logError("Unable to write header", ioe);
- }
- } else {
- this.writer = writer;
- }
- }
-
- /**
- * Some output streams synchronize writes while others do not. Synchronizing here insures that
- * log events won't be intertwined.
- * @param bytes The serialized Log event.
- * @param offset The offset into the byte array.
- * @param length The number of bytes to write.
- * @throws AppenderLoggingException if an error occurs.
- */
- protected synchronized void write(final String str) {
- try {
- writer.write(str);
- } catch (final IOException ex) {
- final String msg = "Error writing to stream " + getName();
- throw new AppenderLoggingException(msg, ex);
- }
- }
-
- /**
- * Writes the footer.
- */
- protected void writeFooter() {
- if (layout == null) {
- return;
- }
- final byte[] footer = layout.getFooter();
- if (footer != null && footer.length > 0) {
- write(new String(footer, layout.getCharset()));
- }
- }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core.appender;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.logging.log4j.core.StringLayout;
+
+/**
+ * Manages a Writer so that it can be shared by multiple Appenders and will
+ * allow appenders to reconfigure without requiring a new writer.
+ */
+public class WriterManager extends AbstractManager {
+
+ /**
+ * Creates a Manager.
+ *
+ * @param name The name of the stream to manage.
+ * @param data The data to pass to the Manager.
+ * @param factory The factory to use to create the Manager.
+ * @param <T> The type of the WriterManager.
+ * @return A WriterManager.
+ */
+ public static <T> WriterManager getManager(final String name, final T data,
+ final ManagerFactory<? extends WriterManager, T> factory) {
+ return AbstractManager.getManager(name, factory, data);
+ }
+ protected final StringLayout layout;
+
+ private volatile Writer writer;
+
+ public WriterManager(final Writer writer, final String streamName, final StringLayout layout,
+ final boolean writeHeader) {
+ super(null, streamName);
+ this.writer = writer;
+ this.layout = layout;
+ if (writeHeader && layout != null) {
+ final byte[] header = layout.getHeader();
+ if (header != null) {
+ try {
+ this.writer.write(new String(header, layout.getCharset()));
+ } catch (final IOException e) {
+ logError("Unable to write header", e);
+ }
+ }
+ }
+ }
+
+ protected synchronized void closeWriter() {
+ final Writer w = writer; // access volatile field only once per method
+ try {
+ w.close();
+ } catch (final IOException ex) {
+ logError("Unable to close stream", ex);
+ }
+ }
+
+ /**
+ * Flushes any buffers.
+ */
+ public synchronized void flush() {
+ try {
+ writer.flush();
+ } catch (final IOException ex) {
+ final String msg = "Error flushing stream " + getName();
+ throw new AppenderLoggingException(msg, ex);
+ }
+ }
+
+ protected Writer getWriter() {
+ return writer;
+ }
+
+ /**
+ * Returns the status of the stream.
+ * @return true if the stream is open, false if it is not.
+ */
+ public boolean isOpen() {
+ return getCount() > 0;
+ }
+
+ /**
+ * Default hook to write footer during close.
+ */
+ @Override
+ public boolean releaseSub(final long timeout, final TimeUnit timeUnit) {
+ writeFooter();
+ closeWriter();
+ return true;
+ }
+
+ protected void setWriter(final Writer writer) {
+ final byte[] header = layout.getHeader();
+ if (header != null) {
+ try {
+ writer.write(new String(header, layout.getCharset()));
+ this.writer = writer; // only update field if writer.write() succeeded
+ } catch (final IOException ioe) {
+ logError("Unable to write header", ioe);
+ }
+ } else {
+ this.writer = writer;
+ }
+ }
+
+ /**
+ * Some output streams synchronize writes while others do not. Synchronizing here insures that
+ * log events won't be intertwined.
+ * @param bytes The serialized Log event.
+ * @param offset The offset into the byte array.
+ * @param length The number of bytes to write.
+ * @throws AppenderLoggingException if an error occurs.
+ */
+ protected synchronized void write(final String str) {
+ try {
+ writer.write(str);
+ } catch (final IOException ex) {
+ final String msg = "Error writing to stream " + getName();
+ throw new AppenderLoggingException(msg, ex);
+ }
+ }
+
+ /**
+ * Writes the footer.
+ */
+ protected void writeFooter() {
+ if (layout == null) {
+ return;
+ }
+ final byte[] footer = layout.getFooter();
+ if (footer != null && footer.length > 0) {
+ write(new String(footer, layout.getCharset()));
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/78fe32c0/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseAppender.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseAppender.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseAppender.java
index 85a3a67..538b644 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseAppender.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseAppender.java
@@ -1,142 +1,142 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-package org.apache.logging.log4j.core.appender.db;
-
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReadWriteLock;
-import java.util.concurrent.locks.ReentrantReadWriteLock;
-
-import org.apache.logging.log4j.LoggingException;
-import org.apache.logging.log4j.core.Filter;
-import org.apache.logging.log4j.core.Layout;
-import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.core.appender.AbstractAppender;
-import org.apache.logging.log4j.core.appender.AppenderLoggingException;
-
-/**
- * An abstract Appender for writing events to a database of some type, be it relational or NoSQL. All database appenders
- * should inherit from this base appender. Three implementations are currently provided:
- * {@link org.apache.logging.log4j.core.appender.db.jdbc JDBC}, {@link org.apache.logging.log4j.core.appender.db.jpa
- * JPA}, and <a href="/log4j/2.x/log4j-nosql/apidocs/">NoSQL</a>.
- *
- * @param <T> Specifies which type of {@link AbstractDatabaseManager} this Appender requires.
- */
-public abstract class AbstractDatabaseAppender<T extends AbstractDatabaseManager> extends AbstractAppender {
-
- private final ReadWriteLock lock = new ReentrantReadWriteLock();
- private final Lock readLock = lock.readLock();
- private final Lock writeLock = lock.writeLock();
-
- private T manager;
-
- /**
- * Instantiates the base appender.
- *
- * @param name The appender name.
- * @param filter The filter, if any, to use.
- * @param ignoreExceptions If {@code true} exceptions encountered when appending events are logged; otherwise
- * they are propagated to the caller.
- * @param manager The matching {@link AbstractDatabaseManager} implementation.
- */
- protected AbstractDatabaseAppender(final String name, final Filter filter, final boolean ignoreExceptions,
- final T manager) {
- super(name, filter, null, ignoreExceptions);
- this.manager = manager;
- }
-
- /**
- * This always returns {@code null}, as database appenders do not use a single layout. The JPA and NoSQL appenders
- * do not use a layout at all. The JDBC appender has a layout-per-column pattern.
- *
- * @return {@code null}.
- */
- @Override
- public final Layout<LogEvent> getLayout() {
- return null;
- }
-
- /**
- * Returns the underlying manager in use within this appender.
- *
- * @return the manager.
- */
- public final T getManager() {
- return this.manager;
- }
-
- @Override
- public final void start() {
- if (this.getManager() == null) {
- LOGGER.error("No AbstractDatabaseManager set for the appender named [{}].", this.getName());
- }
- super.start();
- if (this.getManager() != null) {
- this.getManager().startup();
- }
- }
-
- @Override
- public boolean stop(final long timeout, final TimeUnit timeUnit) {
- setStopping();
- super.stop(timeout, timeUnit, false);
- if (this.getManager() != null) {
- this.getManager().stop(timeout, timeUnit);
- }
- setStopped();
- return true;
- }
-
- @Override
- public final void append(final LogEvent event) {
- this.readLock.lock();
- try {
- this.getManager().write(event);
- } catch (final LoggingException e) {
- LOGGER.error("Unable to write to database [{}] for appender [{}].", this.getManager().getName(),
- this.getName(), e);
- throw e;
- } catch (final Exception e) {
- LOGGER.error("Unable to write to database [{}] for appender [{}].", this.getManager().getName(),
- this.getName(), e);
- throw new AppenderLoggingException("Unable to write to database in appender: " + e.getMessage(), e);
- } finally {
- this.readLock.unlock();
- }
- }
-
- /**
- * Replaces the underlying manager in use within this appender. This can be useful for manually changing the way log
- * events are written to the database without losing buffered or in-progress events. The existing manager is
- * released only after the new manager has been installed. This method is thread-safe.
- *
- * @param manager The new manager to install.
- */
- protected final void replaceManager(final T manager) {
- this.writeLock.lock();
- try {
- final T old = this.getManager();
- if (!manager.isRunning()) {
- manager.startup();
- }
- this.manager = manager;
- old.close();
- } finally {
- this.writeLock.unlock();
- }
- }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core.appender.db;
+
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+import org.apache.logging.log4j.LoggingException;
+import org.apache.logging.log4j.core.Filter;
+import org.apache.logging.log4j.core.Layout;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.appender.AbstractAppender;
+import org.apache.logging.log4j.core.appender.AppenderLoggingException;
+
+/**
+ * An abstract Appender for writing events to a database of some type, be it relational or NoSQL. All database appenders
+ * should inherit from this base appender. Three implementations are currently provided:
+ * {@link org.apache.logging.log4j.core.appender.db.jdbc JDBC}, {@link org.apache.logging.log4j.core.appender.db.jpa
+ * JPA}, and <a href="/log4j/2.x/log4j-nosql/apidocs/">NoSQL</a>.
+ *
+ * @param <T> Specifies which type of {@link AbstractDatabaseManager} this Appender requires.
+ */
+public abstract class AbstractDatabaseAppender<T extends AbstractDatabaseManager> extends AbstractAppender {
+
+ private final ReadWriteLock lock = new ReentrantReadWriteLock();
+ private final Lock readLock = lock.readLock();
+ private final Lock writeLock = lock.writeLock();
+
+ private T manager;
+
+ /**
+ * Instantiates the base appender.
+ *
+ * @param name The appender name.
+ * @param filter The filter, if any, to use.
+ * @param ignoreExceptions If {@code true} exceptions encountered when appending events are logged; otherwise
+ * they are propagated to the caller.
+ * @param manager The matching {@link AbstractDatabaseManager} implementation.
+ */
+ protected AbstractDatabaseAppender(final String name, final Filter filter, final boolean ignoreExceptions,
+ final T manager) {
+ super(name, filter, null, ignoreExceptions);
+ this.manager = manager;
+ }
+
+ /**
+ * This always returns {@code null}, as database appenders do not use a single layout. The JPA and NoSQL appenders
+ * do not use a layout at all. The JDBC appender has a layout-per-column pattern.
+ *
+ * @return {@code null}.
+ */
+ @Override
+ public final Layout<LogEvent> getLayout() {
+ return null;
+ }
+
+ /**
+ * Returns the underlying manager in use within this appender.
+ *
+ * @return the manager.
+ */
+ public final T getManager() {
+ return this.manager;
+ }
+
+ @Override
+ public final void start() {
+ if (this.getManager() == null) {
+ LOGGER.error("No AbstractDatabaseManager set for the appender named [{}].", this.getName());
+ }
+ super.start();
+ if (this.getManager() != null) {
+ this.getManager().startup();
+ }
+ }
+
+ @Override
+ public boolean stop(final long timeout, final TimeUnit timeUnit) {
+ setStopping();
+ boolean stopped = super.stop(timeout, timeUnit, false);
+ if (this.getManager() != null) {
+ stopped &= this.getManager().stop(timeout, timeUnit);
+ }
+ setStopped();
+ return stopped;
+ }
+
+ @Override
+ public final void append(final LogEvent event) {
+ this.readLock.lock();
+ try {
+ this.getManager().write(event);
+ } catch (final LoggingException e) {
+ LOGGER.error("Unable to write to database [{}] for appender [{}].", this.getManager().getName(),
+ this.getName(), e);
+ throw e;
+ } catch (final Exception e) {
+ LOGGER.error("Unable to write to database [{}] for appender [{}].", this.getManager().getName(),
+ this.getName(), e);
+ throw new AppenderLoggingException("Unable to write to database in appender: " + e.getMessage(), e);
+ } finally {
+ this.readLock.unlock();
+ }
+ }
+
+ /**
+ * Replaces the underlying manager in use within this appender. This can be useful for manually changing the way log
+ * events are written to the database without losing buffered or in-progress events. The existing manager is
+ * released only after the new manager has been installed. This method is thread-safe.
+ *
+ * @param manager The new manager to install.
+ */
+ protected final void replaceManager(final T manager) {
+ this.writeLock.lock();
+ try {
+ final T old = this.getManager();
+ if (!manager.isRunning()) {
+ manager.startup();
+ }
+ this.manager = manager;
+ old.close();
+ } finally {
+ this.writeLock.unlock();
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/78fe32c0/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseManager.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseManager.java
index 5f46bf4..8400adb 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseManager.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseManager.java
@@ -1,226 +1,232 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-
-package org.apache.logging.log4j.core.appender.db;
-
-import java.io.Flushable;
-import java.util.ArrayList;
-import java.util.concurrent.TimeUnit;
-
-import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.core.appender.AbstractManager;
-import org.apache.logging.log4j.core.appender.ManagerFactory;
-
-/**
- * Manager that allows database appenders to have their configuration reloaded without losing events.
- */
-public abstract class AbstractDatabaseManager extends AbstractManager implements Flushable {
- private final ArrayList<LogEvent> buffer;
- private final int bufferSize;
-
- private boolean running = false;
-
- /**
- * Instantiates the base manager.
- *
- * @param name The manager name, which should include any configuration details that one might want to be able to
- * reconfigure at runtime, such as database name, username, (hashed) password, etc.
- * @param bufferSize The size of the log event buffer.
- */
- protected AbstractDatabaseManager(final String name, final int bufferSize) {
- super(null, name);
- this.bufferSize = bufferSize;
- this.buffer = new ArrayList<>(bufferSize + 1);
- }
-
- /**
- * Implementations should implement this method to perform any proprietary startup operations. This method will
- * never be called twice on the same instance. It is safe to throw any exceptions from this method. This method
- * does not necessarily connect to the database, as it is generally unreliable to connect once and use the same
- * connection for hours.
- */
- protected abstract void startupInternal() throws Exception;
-
- /**
- * This method is called within the appender when the appender is started. If it has not already been called, it
- * calls {@link #startupInternal()} and catches any exceptions it might throw.
- */
- public final synchronized void startup() {
- if (!this.isRunning()) {
- try {
- this.startupInternal();
- this.running = true;
- } catch (final Exception e) {
- logError("Could not perform database startup operations", e);
- }
- }
- }
-
- /**
- * Implementations should implement this method to perform any proprietary disconnection / shutdown operations. This
- * method will never be called twice on the same instance, and it will only be called <em>after</em>
- * {@link #startupInternal()}. It is safe to throw any exceptions from this method. This method does not
- * necessarily disconnect from the database for the same reasons outlined in {@link #startupInternal()}.
- */
- protected abstract void shutdownInternal() throws Exception;
-
- /**
- * This method is called from the {@link #close()} method when the appender is stopped or the appender's manager
- * is replaced. If it has not already been called, it calls {@link #shutdownInternal()} and catches any exceptions
- * it might throw.
- */
- public final synchronized void shutdown() {
- this.flush();
- if (this.isRunning()) {
- try {
- this.shutdownInternal();
- } catch (final Exception e) {
- logWarn("Caught exception while performing database shutdown operations", e);
- } finally {
- this.running = false;
- }
- }
- }
-
- /**
- * Indicates whether the manager is currently connected {@link #startup()} has been called and {@link #shutdown()}
- * has not been called).
- *
- * @return {@code true} if the manager is connected.
- */
- public final boolean isRunning() {
- return this.running;
- }
-
- /**
- * Connects to the database and starts a transaction (if applicable). With buffering enabled, this is called when
- * flushing the buffer begins, before the first call to {@link #writeInternal}. With buffering disabled, this is
- * called immediately before every invocation of {@link #writeInternal}.
- */
- protected abstract void connectAndStart();
-
- /**
- * Performs the actual writing of the event in an implementation-specific way. This method is called immediately
- * from {@link #write(LogEvent)} if buffering is off, or from {@link #flush()} if the buffer has reached its limit.
- *
- * @param event The event to write to the database.
- */
- protected abstract void writeInternal(LogEvent event);
-
- /**
- * Commits any active transaction (if applicable) and disconnects from the database (returns the connection to the
- * connection pool). With buffering enabled, this is called when flushing the buffer completes, after the last call
- * to {@link #writeInternal}. With buffering disabled, this is called immediately after every invocation of
- * {@link #writeInternal}.
- */
- protected abstract void commitAndClose();
-
- /**
- * This method is called automatically when the buffer size reaches its maximum or at the beginning of a call to
- * {@link #shutdown()}. It can also be called manually to flush events to the database.
- */
- @Override
- public final synchronized void flush() {
- if (this.isRunning() && this.buffer.size() > 0) {
- this.connectAndStart();
- try {
- for (final LogEvent event : this.buffer) {
- this.writeInternal(event);
- }
- } finally {
- this.commitAndClose();
- // not sure if this should be done when writing the events failed
- this.buffer.clear();
- }
- }
- }
-
- /**
- * This method manages buffering and writing of events.
- *
- * @param event The event to write to the database.
- */
- public final synchronized void write(final LogEvent event) {
- if (this.bufferSize > 0) {
- this.buffer.add(event);
- if (this.buffer.size() >= this.bufferSize || event.isEndOfBatch()) {
- this.flush();
- }
- } else {
- this.connectAndStart();
- try {
- this.writeInternal(event);
- } finally {
- this.commitAndClose();
- }
- }
- }
-
- @Override
- public final void releaseSub(final long timeout, final TimeUnit timeUnit) {
- this.shutdown();
- }
-
- @Override
- public final String toString() {
- return this.getName();
- }
-
- /**
- * Implementations should define their own getManager method and call this method from that to create or get
- * existing managers.
- *
- * @param name The manager name, which should include any configuration details that one might want to be able to
- * reconfigure at runtime, such as database name, username, (hashed) password, etc.
- * @param data The concrete instance of {@link AbstractFactoryData} appropriate for the given manager.
- * @param factory A factory instance for creating the appropriate manager.
- * @param <M> The concrete manager type.
- * @param <T> The concrete {@link AbstractFactoryData} type.
- * @return a new or existing manager of the specified type and name.
- */
- protected static <M extends AbstractDatabaseManager, T extends AbstractFactoryData> M getManager(
- final String name, final T data, final ManagerFactory<M, T> factory
- ) {
- return AbstractManager.getManager(name, factory, data);
- }
-
- /**
- * Implementations should extend this class for passing data between the getManager method and the manager factory
- * class.
- */
- protected abstract static class AbstractFactoryData {
- private final int bufferSize;
-
- /**
- * Constructs the base factory data.
- *
- * @param bufferSize The size of the buffer.
- */
- protected AbstractFactoryData(final int bufferSize) {
- this.bufferSize = bufferSize;
- }
-
- /**
- * Gets the buffer size.
- *
- * @return the buffer size.
- */
- public int getBufferSize() {
- return bufferSize;
- }
- }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+package org.apache.logging.log4j.core.appender.db;
+
+import java.io.Flushable;
+import java.util.ArrayList;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.appender.AbstractManager;
+import org.apache.logging.log4j.core.appender.ManagerFactory;
+
+/**
+ * Manager that allows database appenders to have their configuration reloaded without losing events.
+ */
+public abstract class AbstractDatabaseManager extends AbstractManager implements Flushable {
+ private final ArrayList<LogEvent> buffer;
+ private final int bufferSize;
+
+ private boolean running = false;
+
+ /**
+ * Instantiates the base manager.
+ *
+ * @param name The manager name, which should include any configuration details that one might want to be able to
+ * reconfigure at runtime, such as database name, username, (hashed) password, etc.
+ * @param bufferSize The size of the log event buffer.
+ */
+ protected AbstractDatabaseManager(final String name, final int bufferSize) {
+ super(null, name);
+ this.bufferSize = bufferSize;
+ this.buffer = new ArrayList<>(bufferSize + 1);
+ }
+
+ /**
+ * Implementations should implement this method to perform any proprietary startup operations. This method will
+ * never be called twice on the same instance. It is safe to throw any exceptions from this method. This method
+ * does not necessarily connect to the database, as it is generally unreliable to connect once and use the same
+ * connection for hours.
+ */
+ protected abstract void startupInternal() throws Exception;
+
+ /**
+ * This method is called within the appender when the appender is started. If it has not already been called, it
+ * calls {@link #startupInternal()} and catches any exceptions it might throw.
+ */
+ public final synchronized void startup() {
+ if (!this.isRunning()) {
+ try {
+ this.startupInternal();
+ this.running = true;
+ } catch (final Exception e) {
+ logError("Could not perform database startup operations", e);
+ }
+ }
+ }
+
+ /**
+ * Implementations should implement this method to perform any proprietary disconnection / shutdown operations. This
+ * method will never be called twice on the same instance, and it will only be called <em>after</em>
+ * {@link #startupInternal()}. It is safe to throw any exceptions from this method. This method does not
+ * necessarily disconnect from the database for the same reasons outlined in {@link #startupInternal()}.
+ * @return true if all resources were closed normally, false otherwise.
+ */
+ protected abstract boolean shutdownInternal() throws Exception;
+
+ /**
+ * This method is called from the {@link #close()} method when the appender is stopped or the appender's manager
+ * is replaced. If it has not already been called, it calls {@link #shutdownInternal()} and catches any exceptions
+ * it might throw.
+ * @return true if all resources were closed normally, false otherwise.
+ */
+ public final synchronized boolean shutdown() {
+ boolean closed = true;
+ this.flush();
+ if (this.isRunning()) {
+ try {
+ this.shutdownInternal();
+ } catch (final Exception e) {
+ logWarn("Caught exception while performing database shutdown operations", e);
+ closed = false;
+ } finally {
+ this.running = false;
+ }
+ }
+ return closed;
+ }
+
+ /**
+ * Indicates whether the manager is currently connected {@link #startup()} has been called and {@link #shutdown()}
+ * has not been called).
+ *
+ * @return {@code true} if the manager is connected.
+ */
+ public final boolean isRunning() {
+ return this.running;
+ }
+
+ /**
+ * Connects to the database and starts a transaction (if applicable). With buffering enabled, this is called when
+ * flushing the buffer begins, before the first call to {@link #writeInternal}. With buffering disabled, this is
+ * called immediately before every invocation of {@link #writeInternal}.
+ */
+ protected abstract void connectAndStart();
+
+ /**
+ * Performs the actual writing of the event in an implementation-specific way. This method is called immediately
+ * from {@link #write(LogEvent)} if buffering is off, or from {@link #flush()} if the buffer has reached its limit.
+ *
+ * @param event The event to write to the database.
+ */
+ protected abstract void writeInternal(LogEvent event);
+
+ /**
+ * Commits any active transaction (if applicable) and disconnects from the database (returns the connection to the
+ * connection pool). With buffering enabled, this is called when flushing the buffer completes, after the last call
+ * to {@link #writeInternal}. With buffering disabled, this is called immediately after every invocation of
+ * {@link #writeInternal}.
+ * @return true if all resources were closed normally, false otherwise.
+ */
+ protected abstract boolean commitAndClose();
+
+ /**
+ * This method is called automatically when the buffer size reaches its maximum or at the beginning of a call to
+ * {@link #shutdown()}. It can also be called manually to flush events to the database.
+ */
+ @Override
+ public final synchronized void flush() {
+ if (this.isRunning() && this.buffer.size() > 0) {
+ this.connectAndStart();
+ try {
+ for (final LogEvent event : this.buffer) {
+ this.writeInternal(event);
+ }
+ } finally {
+ this.commitAndClose();
+ // not sure if this should be done when writing the events failed
+ this.buffer.clear();
+ }
+ }
+ }
+
+ /**
+ * This method manages buffering and writing of events.
+ *
+ * @param event The event to write to the database.
+ */
+ public final synchronized void write(final LogEvent event) {
+ if (this.bufferSize > 0) {
+ this.buffer.add(event);
+ if (this.buffer.size() >= this.bufferSize || event.isEndOfBatch()) {
+ this.flush();
+ }
+ } else {
+ this.connectAndStart();
+ try {
+ this.writeInternal(event);
+ } finally {
+ this.commitAndClose();
+ }
+ }
+ }
+
+ @Override
+ public final boolean releaseSub(final long timeout, final TimeUnit timeUnit) {
+ return this.shutdown();
+ }
+
+ @Override
+ public final String toString() {
+ return this.getName();
+ }
+
+ /**
+ * Implementations should define their own getManager method and call this method from that to create or get
+ * existing managers.
+ *
+ * @param name The manager name, which should include any configuration details that one might want to be able to
+ * reconfigure at runtime, such as database name, username, (hashed) password, etc.
+ * @param data The concrete instance of {@link AbstractFactoryData} appropriate for the given manager.
+ * @param factory A factory instance for creating the appropriate manager.
+ * @param <M> The concrete manager type.
+ * @param <T> The concrete {@link AbstractFactoryData} type.
+ * @return a new or existing manager of the specified type and name.
+ */
+ protected static <M extends AbstractDatabaseManager, T extends AbstractFactoryData> M getManager(
+ final String name, final T data, final ManagerFactory<M, T> factory
+ ) {
+ return AbstractManager.getManager(name, factory, data);
+ }
+
+ /**
+ * Implementations should extend this class for passing data between the getManager method and the manager factory
+ * class.
+ */
+ protected abstract static class AbstractFactoryData {
+ private final int bufferSize;
+
+ /**
+ * Constructs the base factory data.
+ *
+ * @param bufferSize The size of the buffer.
+ */
+ protected AbstractFactoryData(final int bufferSize) {
+ this.bufferSize = bufferSize;
+ }
+
+ /**
+ * Gets the buffer size.
+ *
+ * @return the buffer size.
+ */
+ public int getBufferSize() {
+ return bufferSize;
+ }
+ }
+}