Skip to content

Commit 716eca0

Browse files
committed
Internalize the recycler API utilities (#2016)
1 parent 47ff3fd commit 716eca0

File tree

14 files changed

+69
-36
lines changed

14 files changed

+69
-36
lines changed

log4j-api-test/src/test/java/org/apache/logging/log4j/spi/RecyclerFactoriesTest.java renamed to log4j-api-test/src/test/java/org/apache/logging/log4j/internal/RecyclerFactoriesTest.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.apache.logging.log4j.spi;
17+
package org.apache.logging.log4j.internal;
1818

1919
import java.util.ArrayDeque;
2020
import java.util.concurrent.ArrayBlockingQueue;
21+
import org.apache.logging.log4j.spi.DummyRecyclerFactory;
22+
import org.apache.logging.log4j.spi.Recycler;
23+
import org.apache.logging.log4j.spi.RecyclerFactory;
2124
import org.assertj.core.api.Assertions;
2225
import org.assertj.core.api.InstanceOfAssertFactories;
2326
import org.junit.jupiter.api.Test;
@@ -41,17 +44,22 @@ void ThreadLocalRecyclerFactory_should_work() {
4144

4245
@Test
4346
void ThreadLocalRecyclerFactory_should_work_with_capacity() {
44-
final RecyclerFactory actualThreadLocalRecyclerFactory = RecyclerFactories.ofSpec("threadLocal:capacity=13");
47+
final int capacity = 13;
48+
final RecyclerFactory actualThreadLocalRecyclerFactory =
49+
RecyclerFactories.ofSpec("threadLocal:capacity=" + capacity);
4550
Assertions.assertThat(actualThreadLocalRecyclerFactory)
4651
.asInstanceOf(InstanceOfAssertFactories.type(ThreadLocalRecyclerFactory.class))
4752
.extracting(ThreadLocalRecyclerFactory::getCapacity)
48-
.isEqualTo(13);
53+
.isEqualTo(capacity);
4954
}
5055

5156
@Test
5257
void QueueingRecyclerFactory_should_work() {
5358
final RecyclerFactory actualQueueingRecyclerFactory = RecyclerFactories.ofSpec("queue");
54-
Assertions.assertThat(actualQueueingRecyclerFactory).isInstanceOf(QueueingRecyclerFactory.class);
59+
Assertions.assertThat(actualQueueingRecyclerFactory)
60+
.asInstanceOf(InstanceOfAssertFactories.type(QueueingRecyclerFactory.class))
61+
.extracting(QueueingRecyclerFactory::getCapacity)
62+
.isEqualTo(RecyclerFactories.CAPACITY);
5563
}
5664

5765
@Test
@@ -68,14 +76,19 @@ void QueueingRecyclerFactory_should_work_with_supplier() {
6876

6977
@Test
7078
void QueueingRecyclerFactory_should_work_with_capacity() {
71-
final RecyclerFactory actualQueueingRecyclerFactory = RecyclerFactories.ofSpec("queue:capacity=100");
72-
Assertions.assertThat(actualQueueingRecyclerFactory).isInstanceOf(QueueingRecyclerFactory.class);
79+
final int capacity = 100;
80+
final RecyclerFactory actualQueueingRecyclerFactory = RecyclerFactories.ofSpec("queue:capacity=" + capacity);
81+
Assertions.assertThat(actualQueueingRecyclerFactory)
82+
.asInstanceOf(InstanceOfAssertFactories.type(QueueingRecyclerFactory.class))
83+
.extracting(QueueingRecyclerFactory::getCapacity)
84+
.isEqualTo(capacity);
7385
}
7486

7587
@Test
7688
void QueueingRecyclerFactory_should_work_with_supplier_and_capacity() {
89+
final int capacity = 100;
7790
final RecyclerFactory recyclerFactory = RecyclerFactories.ofSpec(
78-
"queue:" + "supplier=java.util.concurrent.ArrayBlockingQueue.new," + "capacity=100");
91+
"queue:supplier=java.util.concurrent.ArrayBlockingQueue.new,capacity=" + capacity);
7992
Assertions.assertThat(recyclerFactory).isInstanceOf(QueueingRecyclerFactory.class);
8093
final QueueingRecyclerFactory queueingRecyclerFactory = (QueueingRecyclerFactory) recyclerFactory;
8194
final Recycler<Object> recycler = queueingRecyclerFactory.create(Object::new);
@@ -84,6 +97,6 @@ void QueueingRecyclerFactory_should_work_with_supplier_and_capacity() {
8497
(QueueingRecyclerFactory.QueueingRecycler<Object>) recycler;
8598
Assertions.assertThat(queueingRecycler.getQueue()).isInstanceOf(ArrayBlockingQueue.class);
8699
final ArrayBlockingQueue<Object> queue = (ArrayBlockingQueue<Object>) queueingRecycler.getQueue();
87-
Assertions.assertThat(queue.remainingCapacity()).isEqualTo(100);
100+
Assertions.assertThat(queue.remainingCapacity()).isEqualTo(capacity);
88101
}
89102
}

log4j-api-test/src/test/java/org/apache/logging/log4j/util/StringParameterParserTest.java renamed to log4j-api-test/src/test/java/org/apache/logging/log4j/internal/StringParameterParserTest.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,18 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.apache.logging.log4j.util;
18-
19-
import java.util.*;
20-
import org.apache.logging.log4j.util.StringParameterParser.*;
17+
package org.apache.logging.log4j.internal;
18+
19+
import java.util.Collections;
20+
import java.util.LinkedHashMap;
21+
import java.util.LinkedHashSet;
22+
import java.util.Map;
23+
import java.util.Set;
24+
import org.apache.logging.log4j.internal.StringParameterParser.DoubleQuotedStringValue;
25+
import org.apache.logging.log4j.internal.StringParameterParser.NullValue;
26+
import org.apache.logging.log4j.internal.StringParameterParser.StringValue;
27+
import org.apache.logging.log4j.internal.StringParameterParser.Value;
28+
import org.apache.logging.log4j.internal.StringParameterParser.Values;
2129
import org.assertj.core.api.Assertions;
2230
import org.junit.jupiter.api.Test;
2331

log4j-api-test/src/test/java/org/apache/logging/log4j/spi/ThreadLocalRecyclerFactoryTest.java renamed to log4j-api-test/src/test/java/org/apache/logging/log4j/internal/ThreadLocalRecyclerFactoryTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.apache.logging.log4j.spi;
17+
package org.apache.logging.log4j.internal;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
2020

2121
import java.util.List;
2222
import java.util.Queue;
2323
import java.util.stream.Collectors;
2424
import java.util.stream.IntStream;
25+
import org.apache.logging.log4j.spi.Recycler;
2526
import org.junit.jupiter.api.BeforeEach;
2627
import org.junit.jupiter.api.Test;
2728
import org.junit.jupiter.params.ParameterizedTest;

log4j-api-test/src/test/java/org/apache/logging/log4j/message/ReusableMessageFactoryTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import static org.junit.jupiter.api.Assertions.*;
2020

21-
import org.apache.logging.log4j.spi.ThreadLocalRecyclerFactory;
2221
import org.junit.jupiter.api.BeforeEach;
2322
import org.junit.jupiter.api.Test;
2423

@@ -31,7 +30,7 @@ public class ReusableMessageFactoryTest {
3130

3231
@BeforeEach
3332
void setUp() {
34-
factory = new ReusableMessageFactory(new ThreadLocalRecyclerFactory(8));
33+
factory = new ReusableMessageFactory();
3534
}
3635

3736
@Test

log4j-api/src/main/java/org/apache/logging/log4j/util/QueueFactories.java renamed to log4j-api/src/main/java/org/apache/logging/log4j/internal/QueueFactories.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.apache.logging.log4j.util;
17+
package org.apache.logging.log4j.internal;
1818

1919
import java.lang.reflect.Constructor;
2020
import java.lang.reflect.Method;
2121
import java.util.Queue;
2222
import java.util.concurrent.ArrayBlockingQueue;
2323
import java.util.function.Supplier;
24+
import org.apache.logging.log4j.spi.QueueFactory;
25+
import org.apache.logging.log4j.util.Cast;
26+
import org.apache.logging.log4j.util.InternalApi;
27+
import org.apache.logging.log4j.util.LoaderUtil;
2428
import org.jctools.queues.MpmcArrayQueue;
2529
import org.jctools.queues.SpscArrayQueue;
2630

log4j-api/src/main/java/org/apache/logging/log4j/spi/QueueingRecyclerFactory.java renamed to log4j-api/src/main/java/org/apache/logging/log4j/internal/QueueingRecyclerFactory.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,28 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.apache.logging.log4j.spi;
17+
package org.apache.logging.log4j.internal;
1818

1919
import static java.util.Objects.requireNonNull;
2020

2121
import java.util.Queue;
2222
import java.util.function.Consumer;
2323
import java.util.function.Supplier;
24-
import org.apache.logging.log4j.util.QueueFactory;
24+
import org.apache.logging.log4j.spi.AbstractRecycler;
25+
import org.apache.logging.log4j.spi.QueueFactory;
26+
import org.apache.logging.log4j.spi.Recycler;
27+
import org.apache.logging.log4j.spi.RecyclerFactory;
2528

2629
/**
2730
* A {@link RecyclerFactory} pooling objects in a queue created using the provided {@link QueueFactory}.
2831
*/
29-
public class QueueingRecyclerFactory implements RecyclerFactory {
32+
final class QueueingRecyclerFactory implements RecyclerFactory {
3033

3134
private final QueueFactory queueFactory;
3235

3336
private final int capacity;
3437

35-
public QueueingRecyclerFactory(final QueueFactory queueFactory, final int capacity) {
38+
QueueingRecyclerFactory(final QueueFactory queueFactory, final int capacity) {
3639
if (capacity < 1) {
3740
throw new IllegalArgumentException("was expecting `capacity > 0`, found: " + capacity);
3841
}
@@ -43,7 +46,7 @@ public QueueingRecyclerFactory(final QueueFactory queueFactory, final int capaci
4346
/**
4447
* @return the maximum number of objects retained per thread in recyclers created
4548
*/
46-
public int getCapacity() {
49+
int getCapacity() {
4750
return capacity;
4851
}
4952

log4j-api/src/main/java/org/apache/logging/log4j/spi/RecyclerFactories.java renamed to log4j-api/src/main/java/org/apache/logging/log4j/internal/RecyclerFactories.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.apache.logging.log4j.spi;
17+
package org.apache.logging.log4j.internal;
1818

1919
import static java.util.Objects.requireNonNull;
2020
import static org.apache.logging.log4j.util.Constants.isThreadLocalsEnabled;
2121

2222
import java.util.Map;
2323
import java.util.Set;
24+
import org.apache.logging.log4j.spi.DummyRecyclerFactory;
25+
import org.apache.logging.log4j.spi.QueueFactory;
26+
import org.apache.logging.log4j.spi.RecyclerFactory;
2427
import org.apache.logging.log4j.util.InternalApi;
25-
import org.apache.logging.log4j.util.QueueFactories;
26-
import org.apache.logging.log4j.util.QueueFactory;
27-
import org.apache.logging.log4j.util.StringParameterParser;
2828

2929
/**
3030
* Stores the default {@link RecyclerFactory} instance.

log4j-api/src/main/java/org/apache/logging/log4j/util/StringParameterParser.java renamed to log4j-api/src/main/java/org/apache/logging/log4j/internal/StringParameterParser.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.apache.logging.log4j.util;
17+
package org.apache.logging.log4j.internal;
1818

1919
import java.util.*;
2020
import java.util.concurrent.Callable;
21+
import org.apache.logging.log4j.util.InternalApi;
22+
import org.apache.logging.log4j.util.Strings;
2123

2224
/**
2325
* Utility class for parsing string-formatted parameters, e.g., {@code queue:supplier=com.acme.FastestQueue.new,capacity=42}.

log4j-api/src/main/java/org/apache/logging/log4j/spi/ThreadLocalRecyclerFactory.java renamed to log4j-api/src/main/java/org/apache/logging/log4j/internal/ThreadLocalRecyclerFactory.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.apache.logging.log4j.spi;
17+
package org.apache.logging.log4j.internal;
1818

1919
import static java.util.Objects.requireNonNull;
2020

2121
import java.util.Queue;
2222
import java.util.function.Consumer;
2323
import java.util.function.Supplier;
24-
import org.apache.logging.log4j.util.QueueFactories;
24+
import org.apache.logging.log4j.spi.AbstractRecycler;
25+
import org.apache.logging.log4j.spi.Recycler;
26+
import org.apache.logging.log4j.spi.RecyclerFactory;
2527

2628
/**
2729
* A {@link RecyclerFactory} pooling objects in a queue stored in a {@link ThreadLocal}.
@@ -32,7 +34,7 @@
3234
*
3335
* @since 3.0.0
3436
*/
35-
public class ThreadLocalRecyclerFactory implements RecyclerFactory {
37+
final class ThreadLocalRecyclerFactory implements RecyclerFactory {
3638

3739
/**
3840
* Maximum number of objects retained per thread.
@@ -42,7 +44,7 @@ public class ThreadLocalRecyclerFactory implements RecyclerFactory {
4244
*/
4345
private final int capacity;
4446

45-
public ThreadLocalRecyclerFactory(int capacity) {
47+
ThreadLocalRecyclerFactory(int capacity) {
4648
if (capacity < 1) {
4749
throw new IllegalArgumentException("was expecting a `capacity` greater than 1, found: " + capacity);
4850
}
@@ -52,7 +54,7 @@ public ThreadLocalRecyclerFactory(int capacity) {
5254
/**
5355
* @return maximum number of objects retained per thread in recyclers created
5456
*/
55-
public int getCapacity() {
57+
int getCapacity() {
5658
return capacity;
5759
}
5860

log4j-api/src/main/java/org/apache/logging/log4j/spi/LoggingSystem.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.util.stream.Collectors;
4242
import org.apache.logging.log4j.LogManager;
4343
import org.apache.logging.log4j.ThreadContext;
44+
import org.apache.logging.log4j.internal.RecyclerFactories;
4445
import org.apache.logging.log4j.message.DefaultFlowMessageFactory;
4546
import org.apache.logging.log4j.message.FlowMessageFactory;
4647
import org.apache.logging.log4j.message.MessageFactory;

log4j-api/src/main/java/org/apache/logging/log4j/util/QueueFactory.java renamed to log4j-api/src/main/java/org/apache/logging/log4j/spi/QueueFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.apache.logging.log4j.util;
17+
package org.apache.logging.log4j.spi;
1818

1919
import java.util.Queue;
20+
import org.apache.logging.log4j.internal.QueueFactories;
2021

2122
/**
2223
* A {@link Queue} factory contract.

log4j-api/src/main/java/org/apache/logging/log4j/status/StatusLogger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@
2626
import java.util.concurrent.locks.ReentrantReadWriteLock;
2727
import org.apache.logging.log4j.Level;
2828
import org.apache.logging.log4j.Marker;
29+
import org.apache.logging.log4j.internal.QueueFactories;
2930
import org.apache.logging.log4j.message.Message;
3031
import org.apache.logging.log4j.message.ParameterizedNoReferenceMessageFactory;
3132
import org.apache.logging.log4j.simple.SimpleLogger;
3233
import org.apache.logging.log4j.simple.SimpleLoggerContext;
3334
import org.apache.logging.log4j.spi.AbstractLogger;
3435
import org.apache.logging.log4j.spi.LoggingSystemProperty;
3536
import org.apache.logging.log4j.util.LowLevelLogUtil;
36-
import org.apache.logging.log4j.util.QueueFactories;
3737

3838
/**
3939
* Records events that occur in the logging system. By default, only error messages are logged to {@link System#err}.

log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/ThreadLocalRecyclerNestedLoggingTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@
2525
import org.apache.logging.log4j.core.test.appender.ListAppender;
2626
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
2727
import org.apache.logging.log4j.core.test.junit.Named;
28-
import org.apache.logging.log4j.spi.ThreadLocalRecyclerFactory;
2928
import org.assertj.core.api.Assertions;
3029
import org.junit.jupiter.api.Test;
3130

3231
/**
33-
* Tests if logging while trying to encode an event causes {@link ThreadLocalRecyclerFactory.ThreadLocalRecycler} to incorrectly share buffers and end up overriding layout's earlier encoding work.
32+
* Tests if logging while trying to encode an event causes thread local recycler to incorrectly share buffers and end up overriding layout's earlier encoding work.
3433
*
3534
* @see <a href="https://issues.apache.org/jira/browse/LOG4J2-2368">LOG4J2-2368</a>
3635
*/

log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/convert/TypeConverterFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
import java.util.UnknownFormatConversionException;
2424
import java.util.concurrent.ConcurrentHashMap;
2525
import org.apache.logging.log4j.Logger;
26+
import org.apache.logging.log4j.internal.RecyclerFactories;
2627
import org.apache.logging.log4j.plugins.Inject;
2728
import org.apache.logging.log4j.plugins.Singleton;
2829
import org.apache.logging.log4j.plugins.util.TypeUtil;
29-
import org.apache.logging.log4j.spi.RecyclerFactories;
3030
import org.apache.logging.log4j.spi.RecyclerFactory;
3131
import org.apache.logging.log4j.status.StatusLogger;
3232
import org.apache.logging.log4j.util.Cast;

0 commit comments

Comments
 (0)