Skip to content

Commit 873ad8c

Browse files
committed
Reorganize work with counters
*Make ICounterFactory responsible for creating and filtering counter instances. *Add new class CounterManager that is responsible for registration of new CounterFactories and retrieval of all counter instances. *Rename Counter to ICounter. DEVSIX-1465
1 parent c544870 commit 873ad8c

File tree

9 files changed

+222
-61
lines changed

9 files changed

+222
-61
lines changed

kernel/src/main/java/com/itextpdf/kernel/log/CounterFactory.java renamed to kernel/src/main/java/com/itextpdf/kernel/log/CounterManager.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,63 +47,63 @@ This file is part of the iText (R) project.
4747
import java.util.List;
4848

4949
/**
50-
* Factory that creates a counter for every reader or writer class.
51-
* You can implement your own counter and declare it like this:
52-
* <code>CounterFactory.getInstance().setCounter(new SysoCounter());</code>
53-
* SysoCounter is just an example of a Counter implementation.
54-
* It writes info about files being read and written to the System.out.
50+
* Manager that works with {@link ICounterFactory}. Create {@link ICounter} for each registered {@link ICounterFactory}
51+
* and send corresponding events on document read and write.
52+
* <br/>
53+
* You can implement your own {@link ICounterFactory} and register them with {@link CounterManager#register(ICounterFactory)}
54+
* Or implement {@link ICounter} and register it with {@link SimpleCounterFactory} like this:
55+
* <code>CounterFactory.getInstance().register(new SimpleCounterFactory(new SystemOutCounter());</code>
56+
* {@link SystemOutCounter} is just an example of a ICounter implementation.
5557
* <p>
5658
* This functionality can be used to create metrics in a SaaS context.
5759
*/
58-
public class CounterFactory {
60+
public class CounterManager {
5961

6062
/**
6163
* The singleton instance.
6264
*/
63-
private static CounterFactory instance;
65+
private static CounterManager instance = new CounterManager();
6466

6567
/**
66-
* The current counter implementation.
68+
* List of all registered factories.
6769
*/
68-
private List<Counter> counters = new ArrayList<>();
70+
private List<ICounterFactory> factories = new ArrayList<>();
6971

70-
static {
71-
instance = new CounterFactory();
72-
}
73-
74-
/**
75-
* The empty constructor.
76-
*/
77-
private CounterFactory() {
78-
registerCounter(new DefaultCounter());
72+
private CounterManager() {
73+
register(new SimpleCounterFactory(new DefaultCounter()));
7974
}
8075

8176
/**
8277
* Returns the singleton instance of the factory.
8378
*/
84-
public static CounterFactory getInstance() {
79+
public static CounterManager getInstance() {
8580
return instance;
8681
}
8782

8883
/**
8984
* Returns a list of registered counters for specific class.
9085
*/
91-
public static List<Counter> getCounters(Class<?> cls) {
92-
ArrayList<Counter> result = new ArrayList<>();
93-
for (Counter counter : getInstance().counters) {
94-
Counter counterInstance = counter.getCounter(cls);
95-
if (counterInstance != null) {
96-
result.add(counterInstance);
86+
public List<ICounter> getCounters(Class<?> cls) {
87+
ArrayList<ICounter> result = new ArrayList<>();
88+
for (ICounterFactory factory : factories) {
89+
ICounter counter = factory.getCounter(cls);
90+
if (counter != null) {
91+
result.add(counter);
9792
}
9893
}
9994
return result;
10095
}
10196

10297
/**
103-
* Register new counter.
98+
* Register new {@link ICounterFactory}.
99+
*
100+
* @param factory {@link ICounterFactory} to be registered
104101
*/
105-
public void registerCounter(Counter counter) {
106-
counters.add(counter);
102+
public void register(ICounterFactory factory) {
103+
if (factory != null) {
104+
factories.add(factory);
105+
}
107106
}
108107

108+
109109
}

kernel/src/main/java/com/itextpdf/kernel/log/DefaultCounter.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ This file is part of the iText (R) project.
4949
import org.slf4j.LoggerFactory;
5050

5151
/**
52-
* Default implementation of the Counter interface that essentially doesn't do anything.
52+
* Default implementation of the ICounter interface that essentially doesn't do anything.
5353
*/
54-
public class DefaultCounter implements Counter {
54+
public class DefaultCounter implements ICounter {
5555

5656
private volatile int count = 0;
5757
private int level = 0;
@@ -91,11 +91,6 @@ public class DefaultCounter implements Counter {
9191
"IGZvcm06IGh0dHA6Ly9pdGV4dHBkZi5jb20vc2FsZXMgb3IgYnkgY29udGFjdGluZ" +
9292
"yBvdXIgc2FsZXMgZGVwYXJ0bWVudC4=");
9393

94-
@Override
95-
public Counter getCounter(Class<?> cls) {
96-
return this;
97-
}
98-
9994
@Override
10095
public void onDocumentRead(long size) {
10196
plusOne();

kernel/src/main/java/com/itextpdf/kernel/log/Counter.java renamed to kernel/src/main/java/com/itextpdf/kernel/log/ICounter.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,7 @@ This file is part of the iText (R) project.
5050
* Implementers may use this method to record actual system usage for licensing purposes
5151
* (e.g. count the number of documents or the volumne in bytes in the context of a SaaS license).
5252
*/
53-
public interface Counter {
54-
55-
/**
56-
* Gets a Counter instance for a specific class.
57-
*/
58-
Counter getCounter(Class<?> cls);
53+
public interface ICounter {
5954

6055
/**
6156
* This method gets triggered if a document is read.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
3+
This file is part of the iText (R) project.
4+
Copyright (c) 1998-2017 iText Group NV
5+
Authors: Bruno Lowagie, Paulo Soares, et al.
6+
7+
This program is free software; you can redistribute it and/or modify
8+
it under the terms of the GNU Affero General Public License version 3
9+
as published by the Free Software Foundation with the addition of the
10+
following permission added to Section 15 as permitted in Section 7(a):
11+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
12+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
13+
OF THIRD PARTY RIGHTS
14+
15+
This program is distributed in the hope that it will be useful, but
16+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17+
or FITNESS FOR A PARTICULAR PURPOSE.
18+
See the GNU Affero General Public License for more details.
19+
You should have received a copy of the GNU Affero General Public License
20+
along with this program; if not, see http://www.gnu.org/licenses or write to
21+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
23+
http://itextpdf.com/terms-of-use/
24+
25+
The interactive user interfaces in modified source and object code versions
26+
of this program must display Appropriate Legal Notices, as required under
27+
Section 5 of the GNU Affero General Public License.
28+
29+
In accordance with Section 7(b) of the GNU Affero General Public License,
30+
a covered work must retain the producer line in every PDF that is created
31+
or manipulated using iText.
32+
33+
You can be released from the requirements of the license by purchasing
34+
a commercial license. Buying such a license is mandatory as soon as you
35+
develop commercial activities involving the iText software without
36+
disclosing the source code of your own applications.
37+
These activities include: offering paid services to customers as an ASP,
38+
serving PDFs on the fly in a web application, shipping iText with a closed
39+
source product.
40+
41+
For more information, please contact iText Software Corp. at this
42+
43+
*/
44+
package com.itextpdf.kernel.log;
45+
46+
/**
47+
* Factory that can be registered in {@link CounterManager} and creates a counter for every reader or writer class.
48+
* <br/>
49+
* You can implement your own counter factory and register it like this:
50+
* <code>CounterManager.getInstance().registerCounter(new SystemOutCounterFactory());</code>
51+
* <br/>
52+
* {@link SystemOutCounterFactory} is just an example of {@link ICounterFactory} implementation.
53+
* It creates {@link SystemOutCounter} that writes info about files being read and written to the {@link System#out}
54+
* <p>
55+
* This functionality can be used to create metrics in a SaaS context.
56+
*/
57+
public interface ICounterFactory {
58+
59+
ICounter getCounter(Class<?> cls);
60+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
3+
This file is part of the iText (R) project.
4+
Copyright (c) 1998-2017 iText Group NV
5+
Authors: Bruno Lowagie, Paulo Soares, et al.
6+
7+
This program is free software; you can redistribute it and/or modify
8+
it under the terms of the GNU Affero General Public License version 3
9+
as published by the Free Software Foundation with the addition of the
10+
following permission added to Section 15 as permitted in Section 7(a):
11+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
12+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
13+
OF THIRD PARTY RIGHTS
14+
15+
This program is distributed in the hope that it will be useful, but
16+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17+
or FITNESS FOR A PARTICULAR PURPOSE.
18+
See the GNU Affero General Public License for more details.
19+
You should have received a copy of the GNU Affero General Public License
20+
along with this program; if not, see http://www.gnu.org/licenses or write to
21+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
23+
http://itextpdf.com/terms-of-use/
24+
25+
The interactive user interfaces in modified source and object code versions
26+
of this program must display Appropriate Legal Notices, as required under
27+
Section 5 of the GNU Affero General Public License.
28+
29+
In accordance with Section 7(b) of the GNU Affero General Public License,
30+
a covered work must retain the producer line in every PDF that is created
31+
or manipulated using iText.
32+
33+
You can be released from the requirements of the license by purchasing
34+
a commercial license. Buying such a license is mandatory as soon as you
35+
develop commercial activities involving the iText software without
36+
disclosing the source code of your own applications.
37+
These activities include: offering paid services to customers as an ASP,
38+
serving PDFs on the fly in a web application, shipping iText with a closed
39+
source product.
40+
41+
For more information, please contact iText Software Corp. at this
42+
43+
*/
44+
package com.itextpdf.kernel.log;
45+
46+
/**
47+
* {@link ICounterFactory} implementation that always returns counter instance passed to it in constructor
48+
*/
49+
public class SimpleCounterFactory implements ICounterFactory {
50+
51+
private ICounter counter;
52+
53+
public SimpleCounterFactory(ICounter counter) {
54+
this.counter = counter;
55+
}
56+
57+
@Override
58+
public ICounter getCounter(Class<?> cls) {
59+
return counter;
60+
}
61+
}

kernel/src/main/java/com/itextpdf/kernel/log/SystemOutCounter.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ This file is part of the iText (R) project.
4646
import com.itextpdf.io.util.MessageFormatUtil;
4747

4848
/**
49-
* A {@link Counter} implementation that outputs information about read and written documents to {@link System#out}
49+
* A {@link ICounter} implementation that outputs information about read and written documents to {@link System#out}
5050
*/
51-
public class SystemOutCounter implements Counter {
51+
public class SystemOutCounter implements ICounter {
5252

5353
/**
54-
* The name of the class for which the Counter was created
54+
* The name of the class for which the ICounter was created
5555
* (or iText if no name is available)
5656
*/
5757
protected String name;
@@ -68,10 +68,6 @@ public SystemOutCounter(Class<?> cls) {
6868
this(cls.getName());
6969
}
7070

71-
@Override
72-
public Counter getCounter(Class<?> cls) {
73-
return new SystemOutCounter(cls);
74-
}
7571

7672
@Override
7773
public void onDocumentRead(long size) {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
3+
This file is part of the iText (R) project.
4+
Copyright (c) 1998-2017 iText Group NV
5+
Authors: Bruno Lowagie, Paulo Soares, et al.
6+
7+
This program is free software; you can redistribute it and/or modify
8+
it under the terms of the GNU Affero General Public License version 3
9+
as published by the Free Software Foundation with the addition of the
10+
following permission added to Section 15 as permitted in Section 7(a):
11+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
12+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
13+
OF THIRD PARTY RIGHTS
14+
15+
This program is distributed in the hope that it will be useful, but
16+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17+
or FITNESS FOR A PARTICULAR PURPOSE.
18+
See the GNU Affero General Public License for more details.
19+
You should have received a copy of the GNU Affero General Public License
20+
along with this program; if not, see http://www.gnu.org/licenses or write to
21+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
23+
http://itextpdf.com/terms-of-use/
24+
25+
The interactive user interfaces in modified source and object code versions
26+
of this program must display Appropriate Legal Notices, as required under
27+
Section 5 of the GNU Affero General Public License.
28+
29+
In accordance with Section 7(b) of the GNU Affero General Public License,
30+
a covered work must retain the producer line in every PDF that is created
31+
or manipulated using iText.
32+
33+
You can be released from the requirements of the license by purchasing
34+
a commercial license. Buying such a license is mandatory as soon as you
35+
develop commercial activities involving the iText software without
36+
disclosing the source code of your own applications.
37+
These activities include: offering paid services to customers as an ASP,
38+
serving PDFs on the fly in a web application, shipping iText with a closed
39+
source product.
40+
41+
For more information, please contact iText Software Corp. at this
42+
43+
*/
44+
package com.itextpdf.kernel.log;
45+
46+
/**
47+
* {@link ICounterFactory} implementation that creates new {@link SystemOutCounter} on each call
48+
*/
49+
public class SystemOutCounterFactory implements ICounterFactory {
50+
51+
@Override
52+
public ICounter getCounter(Class<?> cls) {
53+
return cls != null ? new SystemOutCounter(cls) : new SystemOutCounter();
54+
}
55+
}

kernel/src/main/java/com/itextpdf/kernel/pdf/PdfDocument.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ This file is part of the iText (R) project.
5959
import com.itextpdf.kernel.font.PdfFont;
6060
import com.itextpdf.kernel.font.PdfFontFactory;
6161
import com.itextpdf.kernel.geom.PageSize;
62-
import com.itextpdf.kernel.log.Counter;
63-
import com.itextpdf.kernel.log.CounterFactory;
62+
import com.itextpdf.kernel.log.CounterManager;
63+
import com.itextpdf.kernel.log.ICounter;
6464
import com.itextpdf.kernel.numbering.EnglishAlphabetNumbering;
6565
import com.itextpdf.kernel.numbering.RomanNumbering;
6666
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
@@ -888,7 +888,7 @@ public void close() {
888888

889889
xref.writeXrefTableAndTrailer(this, fileId, crypto);
890890
writer.flush();
891-
for (Counter counter : getCounters()) {
891+
for (ICounter counter : getCounters()) {
892892
counter.onDocumentWritten(writer.getCurrentPos());
893893
}
894894
}
@@ -1680,7 +1680,7 @@ protected void open(PdfVersion newPdfVersion) {
16801680
if (reader != null) {
16811681
reader.pdfDocument = this;
16821682
reader.readPdf();
1683-
for (Counter counter : getCounters()) {
1683+
for (ICounter counter : getCounters()) {
16841684
counter.onDocumentRead(reader.getFileLength());
16851685
}
16861686
pdfVersion = reader.headerPdfVersion;
@@ -1914,12 +1914,11 @@ protected void checkClosingStatus() {
19141914
}
19151915

19161916
/**
1917-
* Gets all {@link Counter} instances.
1918-
*
1919-
* @return list of {@link Counter} instances.
1917+
* Gets all {@link ICounter} instances.
1918+
* @return list of {@link ICounter} instances.
19201919
*/
1921-
protected List<Counter> getCounters() {
1922-
return CounterFactory.getCounters(PdfDocument.class);
1920+
protected List<ICounter> getCounters() {
1921+
return CounterManager.getInstance().getCounters(PdfDocument.class);
19231922
}
19241923

19251924
private void updateProducerInInfoDictionary() {

0 commit comments

Comments
 (0)