Polishing.
Refactor duplicate code in SecretEventPublisher into common methods. Reuse Event instead of calling each listener with a new instance. Add FunctionalInterface to LeaseListeners. See gh-431.
This commit is contained in:
@@ -117,11 +117,7 @@ public class SecretLeaseEventPublisher implements InitializingBean {
|
||||
*/
|
||||
protected void onSecretsObtained(RequestedSecret requestedSecret, Lease lease,
|
||||
Map<String, Object> body) {
|
||||
|
||||
for (LeaseListener leaseListener : leaseListeners) {
|
||||
leaseListener.onLeaseEvent(new SecretLeaseCreatedEvent(requestedSecret,
|
||||
lease, body));
|
||||
}
|
||||
dispatch(new SecretLeaseCreatedEvent(requestedSecret, lease, body));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -133,11 +129,7 @@ public class SecretLeaseEventPublisher implements InitializingBean {
|
||||
* @param lease must not be {@literal null}.
|
||||
*/
|
||||
protected void onAfterLeaseRenewed(RequestedSecret requestedSecret, Lease lease) {
|
||||
|
||||
for (LeaseListener leaseListener : leaseListeners) {
|
||||
leaseListener.onLeaseEvent(new AfterSecretLeaseRenewedEvent(requestedSecret,
|
||||
lease));
|
||||
}
|
||||
dispatch(new AfterSecretLeaseRenewedEvent(requestedSecret, lease));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -149,11 +141,7 @@ public class SecretLeaseEventPublisher implements InitializingBean {
|
||||
* @param lease must not be {@literal null}.
|
||||
*/
|
||||
protected void onBeforeLeaseRevocation(RequestedSecret requestedSecret, Lease lease) {
|
||||
|
||||
for (LeaseListener leaseListener : leaseListeners) {
|
||||
leaseListener.onLeaseEvent(new BeforeSecretLeaseRevocationEvent(
|
||||
requestedSecret, lease));
|
||||
}
|
||||
dispatch(new BeforeSecretLeaseRevocationEvent(requestedSecret, lease));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -165,11 +153,7 @@ public class SecretLeaseEventPublisher implements InitializingBean {
|
||||
* @param lease must not be {@literal null}.
|
||||
*/
|
||||
protected void onAfterLeaseRevocation(RequestedSecret requestedSecret, Lease lease) {
|
||||
|
||||
for (LeaseListener leaseListener : leaseListeners) {
|
||||
leaseListener.onLeaseEvent(new AfterSecretLeaseRevocationEvent(
|
||||
requestedSecret, lease));
|
||||
}
|
||||
dispatch(new AfterSecretLeaseRevocationEvent(requestedSecret, lease));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -181,11 +165,7 @@ public class SecretLeaseEventPublisher implements InitializingBean {
|
||||
* @param lease must not be {@literal null}.
|
||||
*/
|
||||
protected void onLeaseExpired(RequestedSecret requestedSecret, Lease lease) {
|
||||
|
||||
for (LeaseListener leaseListener : leaseListeners) {
|
||||
leaseListener
|
||||
.onLeaseEvent(new SecretLeaseExpiredEvent(requestedSecret, lease));
|
||||
}
|
||||
dispatch(new SecretLeaseExpiredEvent(requestedSecret, lease));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -199,10 +179,30 @@ public class SecretLeaseEventPublisher implements InitializingBean {
|
||||
*/
|
||||
protected void onError(RequestedSecret requestedSecret, @Nullable Lease lease,
|
||||
Exception e) {
|
||||
dispatch(new SecretLeaseErrorEvent(requestedSecret, lease, e));
|
||||
}
|
||||
|
||||
for (LeaseErrorListener leaseErrorListener : leaseErrorListeners) {
|
||||
leaseErrorListener.onLeaseError(new SecretLeaseErrorEvent(requestedSecret,
|
||||
lease, e), e);
|
||||
/**
|
||||
* Dispatch the event to all {@link LeaseListener}s.
|
||||
*
|
||||
* @param leaseEvent the event to dispatch.
|
||||
*/
|
||||
void dispatch(SecretLeaseEvent leaseEvent) {
|
||||
|
||||
for (LeaseListener listener : leaseListeners) {
|
||||
listener.onLeaseEvent(leaseEvent);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatch the event to all {@link LeaseErrorListener}s.
|
||||
*
|
||||
* @param errorEvent the event to dispatch.
|
||||
*/
|
||||
void dispatch(SecretLeaseErrorEvent errorEvent) {
|
||||
|
||||
for (LeaseErrorListener listener : leaseErrorListeners) {
|
||||
listener.onLeaseError(errorEvent, (Exception) errorEvent.getException());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,10 +23,11 @@ package org.springframework.vault.core.lease.event;
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface LeaseErrorListener {
|
||||
|
||||
/**
|
||||
* Callback for a {@link SecretLeaseEvent}
|
||||
* Callback for a {@link SecretLeaseEvent}.
|
||||
*
|
||||
* @param leaseEvent the event object, must not be {@literal null}.
|
||||
* @param exception the thrown {@link Exception}.
|
||||
|
||||
@@ -20,6 +20,7 @@ package org.springframework.vault.core.lease.event;
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface LeaseListener {
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
package org.springframework.vault.core.lease.event;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.vault.core.lease.domain.Lease;
|
||||
@@ -44,7 +44,7 @@ public class SecretLeaseCreatedEvent extends SecretLeaseEvent {
|
||||
Map<String, Object> secrets) {
|
||||
|
||||
super(requestedSecret, lease);
|
||||
this.secrets = Collections.unmodifiableMap(new HashMap<>(secrets));
|
||||
this.secrets = Collections.unmodifiableMap(new LinkedHashMap<>(secrets));
|
||||
}
|
||||
|
||||
public Map<String, Object> getSecrets() {
|
||||
|
||||
@@ -37,7 +37,7 @@ public abstract class SecretLeaseEvent extends ApplicationEvent {
|
||||
private final Lease lease;
|
||||
|
||||
/**
|
||||
* Create a new {@link SecretLeaseExpiredEvent} given {@link RequestedSecret} and
|
||||
* Create a new {@link SecretLeaseEvent} given {@link RequestedSecret} and
|
||||
* {@link Lease}.
|
||||
*
|
||||
* @param requestedSecret must not be {@literal null}.
|
||||
|
||||
Reference in New Issue
Block a user