Skip to content

Commit 2b15aa5

Browse files
Egor Martsynkovskyintrofog
authored andcommitted
Clean up context manager mechanism
Rename some classes related to DEVSIX-5311 DEVSIX-5659
1 parent 3cc23ad commit 2b15aa5

File tree

15 files changed

+59
-103
lines changed

15 files changed

+59
-103
lines changed

commons/src/main/java/com/itextpdf/commons/actions/AbstractContextBasedEventHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ This file is part of the iText (R) project.
2828
/**
2929
* Base class for events handling depending on the context.
3030
*/
31-
public abstract class AbstractContextBasedEventHandler implements IBaseEventHandler {
31+
public abstract class AbstractContextBasedEventHandler implements IEventHandler {
3232
private final IContext defaultContext;
3333

3434
/**
@@ -48,7 +48,7 @@ protected AbstractContextBasedEventHandler(IContext onUnknownContext) {
4848
*
4949
* @param event to handle
5050
*/
51-
public final void onEvent(IBaseEvent event) {
51+
public final void onEvent(IEvent event) {
5252
if (!(event instanceof AbstractContextBasedITextEvent)) {
5353
return;
5454
}

commons/src/main/java/com/itextpdf/commons/actions/AbstractITextEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ This file is part of the iText (R) project.
2828
/**
2929
* Abstract class which defines events only for internal usage.
3030
*/
31-
public abstract class AbstractITextEvent implements IBaseEvent {
31+
public abstract class AbstractITextEvent implements IEvent {
3232
private static final String ONLY_FOR_INTERNAL_USE = "AbstractITextEvent is only for internal usage.";
3333

3434
private static final Map<String, Object> INTERNAL_PACKAGES = new ConcurrentHashMap<>();

commons/src/main/java/com/itextpdf/commons/actions/EventManager.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ This file is part of the iText (R) project.
3636
public final class EventManager {
3737
private static final EventManager INSTANCE = new EventManager();
3838

39-
private final Set<IBaseEventHandler> handlers = new LinkedHashSet<>();
39+
private final Set<IEventHandler> handlers = new LinkedHashSet<>();
4040

4141
private EventManager() {
4242
handlers.add(ProductEventHandler.INSTANCE);
@@ -56,9 +56,9 @@ public static EventManager getInstance() {
5656
*
5757
* @param event to handle
5858
*/
59-
public void onEvent(IBaseEvent event) {
59+
public void onEvent(IEvent event) {
6060
final List<RuntimeException> caughtExceptions = new ArrayList<>();
61-
for (final IBaseEventHandler handler : handlers) {
61+
for (final IEventHandler handler : handlers) {
6262
try {
6363
handler.onEvent(event);
6464
} catch (RuntimeException ex) {
@@ -83,11 +83,11 @@ public void onEvent(IBaseEvent event) {
8383
}
8484

8585
/**
86-
* Add new {@link IBaseEventHandler} to the event handling process.
86+
* Add new {@link IEventHandler} to the event handling process.
8787
*
8888
* @param handler is a handler to add
8989
*/
90-
public void register(IBaseEventHandler handler) {
90+
public void register(IEventHandler handler) {
9191
if (handler != null) {
9292
handlers.add(handler);
9393
}
@@ -99,7 +99,7 @@ public void register(IBaseEventHandler handler) {
9999
* @param handler is a handler to check
100100
* @return true if handler has been already registered and false otherwise
101101
*/
102-
public boolean isRegistered(IBaseEventHandler handler) {
102+
public boolean isRegistered(IEventHandler handler) {
103103
if (handler != null) {
104104
return handlers.contains(handler);
105105
}
@@ -113,7 +113,7 @@ public boolean isRegistered(IBaseEventHandler handler) {
113113
* @return true if the handler had been registered previously and was removed. False if the
114114
* handler was not found among registered handlers
115115
*/
116-
public boolean unregister(IBaseEventHandler handler) {
116+
public boolean unregister(IEventHandler handler) {
117117
if (handler != null) {
118118
return handlers.remove(handler);
119119
}

commons/src/main/java/com/itextpdf/commons/actions/IBaseEvent.java renamed to commons/src/main/java/com/itextpdf/commons/actions/IEvent.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ This file is part of the iText (R) project.
2525
/**
2626
* Base marker interface for any generated event of any source.
2727
*
28-
* <p>TODO: DEVSIX-5311 rename it IEvent when the oldest mechanism is deleted
2928
*/
30-
public interface IBaseEvent {
29+
public interface IEvent {
3130
}

commons/src/main/java/com/itextpdf/commons/actions/IBaseEventHandler.java renamed to commons/src/main/java/com/itextpdf/commons/actions/IEventHandler.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@ This file is part of the iText (R) project.
2525
/**
2626
* The interface for an event handler.
2727
*
28-
* <p>TODO: DEVSIX-5311 rename it IEventHandler when the oldest mechanism is deleted
2928
*/
30-
public interface IBaseEventHandler {
29+
public interface IEventHandler {
3130
/**
3231
* Handles the event.
3332
*
3433
* @param event to handle
3534
*/
36-
void onEvent(IBaseEvent event);
35+
void onEvent(IEvent event);
3736
}

commons/src/main/java/com/itextpdf/commons/actions/NamespaceConstant.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ public final class NamespaceConstant {
6464
public static final String CORE_SVG = ITEXT + ".svg";
6565

6666
//Addons
67-
public static final String PDF_DEBUG = ITEXT + ".pdfdebug";
6867
public static final String PDF_HTML = ITEXT + ".html2pdf";
69-
public static final String PDF_INVOICE = ITEXT + ".zugferd";
7068
public static final String PDF_SWEEP = ITEXT + ".pdfcleanup";
7169
public static final String PDF_OCR = ITEXT + ".pdfocr";
7270
public static final String PDF_OCR_TESSERACT4 = PDF_OCR + ".tesseract4";

commons/src/main/java/com/itextpdf/commons/actions/confirmations/EventConfirmationType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ This file is part of the iText (R) project.
2424

2525
import com.itextpdf.commons.actions.AbstractProductProcessITextEvent;
2626
import com.itextpdf.commons.actions.EventManager;
27-
import com.itextpdf.commons.actions.IBaseEvent;
27+
import com.itextpdf.commons.actions.IEvent;
2828

2929
/**
3030
* Defines the strategy of {@link AbstractProductProcessITextEvent} confirming.
3131
*/
3232
public enum EventConfirmationType {
3333
/**
3434
* The successful execution of the process associated with the event should be confirmed by the
35-
* second invocation of the {@link EventManager#onEvent(IBaseEvent)} method.
35+
* second invocation of the {@link EventManager#onEvent(IEvent)} method.
3636
*/
3737
ON_DEMAND,
3838
/**

commons/src/main/java/com/itextpdf/commons/actions/contexts/AbstractContextManagerConfigurationEvent.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ protected AbstractContextManagerConfigurationEvent() {
3838
}
3939

4040
protected void registerGenericContext(Collection<String> namespaces, Collection<String> products) {
41-
ContextManager.getInstance().registerGenericContextForProducts(namespaces, products);
41+
ContextManager.getInstance().registerGenericContext(namespaces, products);
4242
}
4343

44-
protected void unregisterGenericContext(Collection<String> namespaces) {
45-
ContextManager.getInstance().unregisterGenericContextForProducts(namespaces);
44+
protected void unregisterContext(Collection<String> namespaces) {
45+
ContextManager.getInstance().unregisterContext(namespaces);
4646
}
4747
}

commons/src/main/java/com/itextpdf/commons/actions/contexts/ContextManager.java

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -62,28 +62,16 @@ public class ContextManager {
6262

6363
static {
6464
ContextManager local = new ContextManager();
65-
local.registerGenericContextForProducts(NamespaceConstant.ITEXT_CORE_NAMESPACES,
66-
Collections.singletonList(NamespaceConstant.ITEXT),
65+
local.registerGenericContext(NamespaceConstant.ITEXT_CORE_NAMESPACES,
6766
Collections.singleton(ProductNameConstant.ITEXT_CORE));
6867

69-
local.registerGenericContextForProducts(Collections.singletonList(NamespaceConstant.PDF_DEBUG),
70-
Collections.singletonList(NamespaceConstant.PDF_DEBUG),
71-
Collections.<String>emptyList());
72-
73-
local.registerGenericContextForProducts(Collections.singletonList(NamespaceConstant.PDF_HTML),
74-
Collections.singletonList(NamespaceConstant.PDF_HTML),
68+
local.registerGenericContext(Collections.singletonList(NamespaceConstant.PDF_HTML),
7569
Collections.singleton(ProductNameConstant.PDF_HTML));
7670

77-
local.registerGenericContextForProducts(Collections.singletonList(NamespaceConstant.PDF_INVOICE),
78-
Collections.singletonList(NamespaceConstant.PDF_INVOICE),
79-
Collections.<String>emptyList());
80-
81-
local.registerGenericContextForProducts(Collections.singletonList(NamespaceConstant.PDF_SWEEP),
82-
Collections.singletonList(NamespaceConstant.PDF_SWEEP),
71+
local.registerGenericContext(Collections.singletonList(NamespaceConstant.PDF_SWEEP),
8372
Collections.singleton(ProductNameConstant.PDF_SWEEP));
8473

85-
local.registerGenericContextForProducts(Collections.singletonList(NamespaceConstant.PDF_OCR_TESSERACT4),
86-
Collections.singletonList(NamespaceConstant.PDF_OCR_TESSERACT4),
74+
local.registerGenericContext(Collections.singletonList(NamespaceConstant.PDF_OCR_TESSERACT4),
8775
Collections.singleton(ProductNameConstant.PDF_OCR_TESSERACT4));
8876

8977
INSTANCE = local;
@@ -139,27 +127,9 @@ String getRecognisedNamespace(String className) {
139127
return null;
140128
}
141129

142-
// TODO DEVSIX-5311 consider renaming to be in sync with renamed registerGenericContextForProducts
143-
void unregisterGenericContextForProducts(Collection<String> namespaces) {
144-
for (String namespace : namespaces) {
145-
unregisterContext(namespace);
146-
}
147-
}
148-
149-
// TODO DEVSIX-5311 rename into registerGenericContext (currently we cann't rename it as
150-
// the old method with the same arguments but different logic is used for old mechanism)
151-
void registerGenericContextForProducts(Collection<String> namespaces, Collection<String> products) {
152-
registerGenericContextForProducts(namespaces, Collections.<String>emptyList(), products);
153-
}
154-
155-
// TODO DEVSIX-5311 This method is needed for similar working of new and old license mechanism,
156-
// should be moved to single properly method
157-
private void registerGenericContextForProducts(Collection<String> namespaces, Collection<String> eventIds,
158-
Collection<String> products) {
159-
final GenericContext context = new GenericContext(products);
130+
void unregisterContext(Collection<String> namespaces) {
160131
for (String namespace : namespaces) {
161-
//Conversion to lowercase is done to be compatible with possible changes in case of packages/namespaces
162-
registerContext(namespace.toLowerCase(), context);
132+
contextMappings.remove(namespace);
163133
}
164134
}
165135

@@ -170,17 +140,12 @@ private IContext getNamespaceMapping(String namespace) {
170140
return null;
171141
}
172142

173-
// TODO DEVSIX-5311 This method is used for old logic of license mechanism, will be removed
174-
private void registerGenericContext(Collection<String> namespaces, Collection<String> eventIds) {
175-
registerGenericContextForProducts(namespaces, eventIds, Collections.<String>emptyList());
176-
}
177-
178-
private void registerContext(String namespace, IContext context) {
179-
contextMappings.put(namespace, context);
180-
}
181-
182-
private void unregisterContext(String namespace) {
183-
contextMappings.remove(namespace);
143+
void registerGenericContext(Collection<String> namespaces, Collection<String> products) {
144+
final GenericContext context = new GenericContext(products);
145+
for (String namespace : namespaces) {
146+
//Conversion to lowercase is done to be compatible with possible changes in case of packages/namespaces
147+
contextMappings.put(namespace.toLowerCase(), context);
148+
}
184149
}
185150

186151
private static class LengthComparator implements Comparator<String> {

commons/src/test/java/com/itextpdf/commons/actions/AbstractContextBasedEventHandlerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public boolean wasInvoked() {
8787
}
8888
}
8989

90-
private static class UnknownEvent implements IBaseEvent {
90+
private static class UnknownEvent implements IEvent {
9191

9292
}
9393
}

0 commit comments

Comments
 (0)