org.springframework.context.PayloadApplicationEvent Java Examples

The following examples show how to use org.springframework.context.PayloadApplicationEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ApplicationListenerMethodAdapter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Resolve the method arguments to use for the specified {@link ApplicationEvent}.
 * <p>These arguments will be used to invoke the method handled by this instance.
 * Can return {@code null} to indicate that no suitable arguments could be resolved
 * and therefore the method should not be invoked at all for the specified event.
 */
@Nullable
protected Object[] resolveArguments(ApplicationEvent event) {
	ResolvableType declaredEventType = getResolvableType(event);
	if (declaredEventType == null) {
		return null;
	}
	if (this.method.getParameterCount() == 0) {
		return new Object[0];
	}
	Class<?> declaredEventClass = declaredEventType.toClass();
	if (!ApplicationEvent.class.isAssignableFrom(declaredEventClass) &&
			event instanceof PayloadApplicationEvent) {
		Object payload = ((PayloadApplicationEvent<?>) event).getPayload();
		if (declaredEventClass.isInstance(payload)) {
			return new Object[] {payload};
		}
	}
	return new Object[] {event};
}
 
Example #2
Source File: ApplicationListenerMethodAdapter.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Resolve the method arguments to use for the specified {@link ApplicationEvent}.
 * <p>These arguments will be used to invoke the method handled by this instance. Can
 * return {@code null} to indicate that no suitable arguments could be resolved and
 * therefore the method should not be invoked at all for the specified event.
 */
protected Object[] resolveArguments(ApplicationEvent event) {
	ResolvableType declaredEventType = getResolvableType(event);
	if (declaredEventType == null) {
		return null;
	}
	if (this.method.getParameterTypes().length == 0) {
		return new Object[0];
	}
	if (!ApplicationEvent.class.isAssignableFrom(declaredEventType.getRawClass())
			&& event instanceof PayloadApplicationEvent) {
		return new Object[] {((PayloadApplicationEvent) event).getPayload()};
	}
	else {
		return new Object[] {event};
	}
}
 
Example #3
Source File: ApplicationListenerMethodAdapter.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private ResolvableType getResolvableType(ApplicationEvent event) {
	ResolvableType payloadType = null;
	if (event instanceof PayloadApplicationEvent) {
		PayloadApplicationEvent<?> payloadEvent = (PayloadApplicationEvent<?>) event;
		payloadType = payloadEvent.getResolvableType().as(
				PayloadApplicationEvent.class).getGeneric(0);
	}
	for (ResolvableType declaredEventType : this.declaredEventTypes) {
		if (!ApplicationEvent.class.isAssignableFrom(declaredEventType.getRawClass())
				&& payloadType != null) {
			if (declaredEventType.isAssignableFrom(payloadType)) {
				return declaredEventType;
			}
		}
		if (declaredEventType.getRawClass().isAssignableFrom(event.getClass())) {
			return declaredEventType;
		}
	}
	return null;
}
 
Example #4
Source File: ApplicationListenerMethodAdapter.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Resolve the method arguments to use for the specified {@link ApplicationEvent}.
 * <p>These arguments will be used to invoke the method handled by this instance. Can
 * return {@code null} to indicate that no suitable arguments could be resolved and
 * therefore the method should not be invoked at all for the specified event.
 */
@Nullable
protected Object[] resolveArguments(ApplicationEvent event) {
	ResolvableType declaredEventType = getResolvableType(event);
	if (declaredEventType == null) {
		return null;
	}
	if (this.method.getParameterCount() == 0) {
		return new Object[0];
	}
	if (!ApplicationEvent.class.isAssignableFrom(declaredEventType.toClass()) &&
			event instanceof PayloadApplicationEvent) {
		return new Object[] {((PayloadApplicationEvent) event).getPayload()};
	}
	else {
		return new Object[] {event};
	}
}
 
Example #5
Source File: ApplicationListenerMethodAdapter.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Nullable
private ResolvableType getResolvableType(ApplicationEvent event) {
	ResolvableType payloadType = null;
	if (event instanceof PayloadApplicationEvent) {
		PayloadApplicationEvent<?> payloadEvent = (PayloadApplicationEvent<?>) event;
		ResolvableType eventType = payloadEvent.getResolvableType();
		if (eventType != null) {
			payloadType = eventType.as(PayloadApplicationEvent.class).getGeneric();
		}
	}
	for (ResolvableType declaredEventType : this.declaredEventTypes) {
		Class<?> eventClass = declaredEventType.toClass();
		if (!ApplicationEvent.class.isAssignableFrom(eventClass) &&
				payloadType != null && declaredEventType.isAssignableFrom(payloadType)) {
			return declaredEventType;
		}
		if (eventClass.isInstance(event)) {
			return declaredEventType;
		}
	}
	return null;
}
 
Example #6
Source File: ApplicationListenerMethodAdapter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private ResolvableType getResolvableType(ApplicationEvent event) {
	ResolvableType payloadType = null;
	if (event instanceof PayloadApplicationEvent) {
		PayloadApplicationEvent<?> payloadEvent = (PayloadApplicationEvent<?>) event;
		payloadType = payloadEvent.getResolvableType().as(PayloadApplicationEvent.class).getGeneric();
	}
	for (ResolvableType declaredEventType : this.declaredEventTypes) {
		if (!ApplicationEvent.class.isAssignableFrom(declaredEventType.getRawClass()) && payloadType != null) {
			if (declaredEventType.isAssignableFrom(payloadType)) {
				return declaredEventType;
			}
		}
		if (declaredEventType.getRawClass().isInstance(event)) {
			return declaredEventType;
		}
	}
	return null;
}
 
Example #7
Source File: ApplicationListenerMethodAdapter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Resolve the method arguments to use for the specified {@link ApplicationEvent}.
 * <p>These arguments will be used to invoke the method handled by this instance. Can
 * return {@code null} to indicate that no suitable arguments could be resolved and
 * therefore the method should not be invoked at all for the specified event.
 */
protected Object[] resolveArguments(ApplicationEvent event) {
	ResolvableType declaredEventType = getResolvableType(event);
	if (declaredEventType == null) {
		return null;
	}
	if (this.method.getParameterTypes().length == 0) {
		return new Object[0];
	}
	if (!ApplicationEvent.class.isAssignableFrom(declaredEventType.getRawClass()) &&
			event instanceof PayloadApplicationEvent) {
		return new Object[] {((PayloadApplicationEvent) event).getPayload()};
	}
	else {
		return new Object[] {event};
	}
}
 
Example #8
Source File: UiEventListenerMethodAdapter.java    From cuba with Apache License 2.0 6 votes vote down vote up
/**
 * Resolve the method arguments to use for the specified {@link ApplicationEvent}.
 * <p>These arguments will be used to invoke the method handled by this instance. Can
 * return {@code null} to indicate that no suitable arguments could be resolved and
 * therefore the method should not be invoked at all for the specified event.
 */
@Nullable
protected Object[] resolveArguments(ApplicationEvent event) {
    ResolvableType declaredEventType = getResolvableType(event);
    if (declaredEventType == null || declaredEventType.getRawClass() == null) {
        return null;
    }
    if (this.method.getParameterTypes().length == 0) {
        return new Object[0];
    }
    if (!ApplicationEvent.class.isAssignableFrom(declaredEventType.getRawClass()) &&
            event instanceof PayloadApplicationEvent) {
        return new Object[]{((PayloadApplicationEvent) event).getPayload()};
    } else {
        return new Object[]{event};
    }
}
 
Example #9
Source File: ApplicationListenerMethodAdapter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Nullable
private ResolvableType getResolvableType(ApplicationEvent event) {
	ResolvableType payloadType = null;
	if (event instanceof PayloadApplicationEvent) {
		PayloadApplicationEvent<?> payloadEvent = (PayloadApplicationEvent<?>) event;
		ResolvableType eventType = payloadEvent.getResolvableType();
		if (eventType != null) {
			payloadType = eventType.as(PayloadApplicationEvent.class).getGeneric();
		}
	}
	for (ResolvableType declaredEventType : this.declaredEventTypes) {
		Class<?> eventClass = declaredEventType.toClass();
		if (!ApplicationEvent.class.isAssignableFrom(eventClass) &&
				payloadType != null && declaredEventType.isAssignableFrom(payloadType)) {
			return declaredEventType;
		}
		if (eventClass.isInstance(event)) {
			return declaredEventType;
		}
	}
	return null;
}
 
Example #10
Source File: TooManyFailureSmsVerificationAttemptsEventListener.java    From daming with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(PayloadApplicationEvent<TooManyFailureSmsVerificationAttemptsEvent> event) {
    // FIXME, introduce a command handler
    log.debug("Receiving {}", event.getPayload());
    TooManyFailureSmsVerificationAttemptsEvent eventPayload = event.getPayload();
    SmsVerification smsVerification = target.shouldFindBy(eventPayload.getMobile(), eventPayload.getScope());
    target.remove(smsVerification);
    log.info("The [{}] code for [{}] is removed due to too many failure verification attempts",
            smsVerification.getScope(), smsVerification.getMobile());
}
 
Example #11
Source File: SmsVerificationCodeVerifiedEventListener.java    From daming with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(PayloadApplicationEvent<SmsVerificationCodeVerifiedEvent> event) {
    log.debug("Receiving {}", event.getPayload().toString());
    target.on(event.getPayload());
    log.debug("Failure verification attempt count for [{}][{}] is reset", event.getPayload().getMobile(),
            event.getPayload().getScope());
}
 
Example #12
Source File: ApplicationListenerMethodAdapter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean supportsEventType(ResolvableType eventType) {
	for (ResolvableType declaredEventType : this.declaredEventTypes) {
		if (declaredEventType.isAssignableFrom(eventType)) {
			return true;
		}
		else if (PayloadApplicationEvent.class.isAssignableFrom(eventType.getRawClass())) {
			ResolvableType payloadType = eventType.as(PayloadApplicationEvent.class).getGeneric();
			if (declaredEventType.isAssignableFrom(payloadType)) {
				return true;
			}
		}
	}
	return eventType.hasUnresolvableGenerics();
}
 
Example #13
Source File: ApplicationListenerMethodAdapterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void invokeListenerWithSeveralTypes() {
	Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleStringOrInteger");
	PayloadApplicationEvent<String> event = new PayloadApplicationEvent<>(this, "test");
	invokeListener(method, event);
	verify(this.sampleEvents, times(1)).handleStringOrInteger();
	PayloadApplicationEvent<Integer> event2 = new PayloadApplicationEvent<>(this, 123);
	invokeListener(method, event2);
	verify(this.sampleEvents, times(2)).handleStringOrInteger();
	PayloadApplicationEvent<Double> event3 = new PayloadApplicationEvent<>(this, 23.2);
	invokeListener(method, event3);
	verify(this.sampleEvents, times(2)).handleStringOrInteger();
}
 
Example #14
Source File: EventPublication.java    From spring-domain-events with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the event as Spring {@link ApplicationEvent}, effectively wrapping it into a
 * {@link PayloadApplicationEvent} in case it's not one already.
 *
 * @return
 */
default ApplicationEvent getApplicationEvent() {

	Object event = getEvent();

	return PayloadApplicationEvent.class.isInstance(event) //
			? PayloadApplicationEvent.class.cast(event)
			: new PayloadApplicationEvent<>(this, event);
}
 
Example #15
Source File: ApplicationListenerMethodAdapterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void invokeListenerWithAnnotationValueAndParameter() {
	Method method = ReflectionUtils.findMethod(
			SampleEvents.class, "handleStringAnnotationValueAndParameter", String.class);
	PayloadApplicationEvent<String> event = new PayloadApplicationEvent<>(this, "test");
	invokeListener(method, event);
	verify(this.sampleEvents, times(1)).handleStringAnnotationValueAndParameter("test");
}
 
Example #16
Source File: SyncopeLogic.java    From syncope with Apache License 2.0 5 votes vote down vote up
@EventListener
public static void addLoadInstant(final PayloadApplicationEvent<SystemInfo.LoadInstant> event) {
    synchronized (MONITOR) {
        initSystemInfo();
        SYSTEM_INFO.getLoad().add(event.getPayload());
    }
}
 
Example #17
Source File: ApplicationListenerMethodAdapter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public boolean supportsEventType(ResolvableType eventType) {
	for (ResolvableType declaredEventType : this.declaredEventTypes) {
		if (declaredEventType.isAssignableFrom(eventType)) {
			return true;
		}
		else if (PayloadApplicationEvent.class.isAssignableFrom(eventType.getRawClass())) {
			ResolvableType payloadType = eventType.as(PayloadApplicationEvent.class).getGeneric();
			if (declaredEventType.isAssignableFrom(payloadType)) {
				return true;
			}
		}
	}
	return eventType.hasUnresolvableGenerics();
}
 
Example #18
Source File: ApplicationListenerMethodAdapterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void invokeListenerWithAnyGenericPayload() {
	Method method = ReflectionUtils.findMethod(
			SampleEvents.class, "handleGenericAnyPayload", EntityWrapper.class);
	EntityWrapper<String> payload = new EntityWrapper<>("test");
	invokeListener(method, new PayloadApplicationEvent<>(this, payload));
	verify(this.sampleEvents, times(1)).handleGenericAnyPayload(payload);
}
 
Example #19
Source File: ApplicationListenerMethodAdapterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void invokeListenerWithGenericPayload() {
	Method method = ReflectionUtils.findMethod(SampleEvents.class,
			"handleGenericStringPayload", EntityWrapper.class);
	EntityWrapper<String> payload = new EntityWrapper<>("test");
	invokeListener(method, new PayloadApplicationEvent<>(this, payload));
	verify(this.sampleEvents, times(1)).handleGenericStringPayload(payload);
}
 
Example #20
Source File: ApplicationListenerMethodAdapterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void invokeListenerWithWrongGenericPayload() {
	Method method = ReflectionUtils.findMethod(SampleEvents.class,
			"handleGenericStringPayload", EntityWrapper.class);
	EntityWrapper<Integer> payload = new EntityWrapper<>(123);
	invokeListener(method, new PayloadApplicationEvent<>(this, payload));
	verify(this.sampleEvents, times(0)).handleGenericStringPayload(any());
}
 
Example #21
Source File: ApplicationListenerMethodAdapterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void invokeListenerWithAnyGenericPayload() {
	Method method = ReflectionUtils.findMethod(SampleEvents.class,
			"handleGenericAnyPayload", EntityWrapper.class);
	EntityWrapper<String> payload = new EntityWrapper<>("test");
	invokeListener(method, new PayloadApplicationEvent<>(this, payload));
	verify(this.sampleEvents, times(1)).handleGenericAnyPayload(payload);
}
 
Example #22
Source File: ApplicationListenerMethodAdapterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void invokeListenerWithPayload() {
	Method method = ReflectionUtils.findMethod(SampleEvents.class,
			"handleString", String.class);
	PayloadApplicationEvent<String> event = new PayloadApplicationEvent<>(this, "test");
	invokeListener(method, event);
	verify(this.sampleEvents, times(1)).handleString("test");
}
 
Example #23
Source File: ApplicationListenerMethodAdapterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void invokeListenerWithPayloadWrongType() {
	Method method = ReflectionUtils.findMethod(SampleEvents.class,
			"handleString", String.class);
	PayloadApplicationEvent<Long> event = new PayloadApplicationEvent<>(this, 123L);
	invokeListener(method, event);
	verify(this.sampleEvents, never()).handleString(anyString());
}
 
Example #24
Source File: ApplicationListenerMethodAdapterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void invokeListenerWithAnnotationValue() {
	Method method = ReflectionUtils.findMethod(SampleEvents.class,
			"handleStringAnnotationClasses");
	PayloadApplicationEvent<String> event = new PayloadApplicationEvent<>(this, "test");
	invokeListener(method, event);
	verify(this.sampleEvents, times(1)).handleStringAnnotationClasses();
}
 
Example #25
Source File: ApplicationListenerMethodAdapterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void invokeListenerWithAnnotationValueAndParameter() {
	Method method = ReflectionUtils.findMethod(SampleEvents.class,
			"handleStringAnnotationValueAndParameter", String.class);
	PayloadApplicationEvent<String> event = new PayloadApplicationEvent<>(this, "test");
	invokeListener(method, event);
	verify(this.sampleEvents, times(1)).handleStringAnnotationValueAndParameter("test");
}
 
Example #26
Source File: ApplicationListenerMethodAdapterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void invokeListenerWithSeveralTypes() {
	Method method = ReflectionUtils.findMethod(SampleEvents.class,
			"handleStringOrInteger");
	PayloadApplicationEvent<String> event = new PayloadApplicationEvent<>(this, "test");
	invokeListener(method, event);
	verify(this.sampleEvents, times(1)).handleStringOrInteger();
	PayloadApplicationEvent<Integer> event2 = new PayloadApplicationEvent<>(this, 123);
	invokeListener(method, event2);
	verify(this.sampleEvents, times(2)).handleStringOrInteger();
	PayloadApplicationEvent<Double> event3 = new PayloadApplicationEvent<>(this, 23.2);
	invokeListener(method, event3);
	verify(this.sampleEvents, times(2)).handleStringOrInteger();
}
 
Example #27
Source File: UiEventListenerMethodAdapter.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Override
public boolean supportsEventType(@Nonnull ResolvableType eventType) {
    for (ResolvableType declaredEventType : this.declaredEventTypes) {
        if (declaredEventType.isAssignableFrom(eventType)) {
            return true;
        } else if (eventType.getRawClass() != null
                && PayloadApplicationEvent.class.isAssignableFrom(eventType.getRawClass())) {
            ResolvableType payloadType = eventType.as(PayloadApplicationEvent.class).getGeneric();
            if (declaredEventType.isAssignableFrom(payloadType)) {
                return true;
            }
        }
    }
    return eventType.hasUnresolvableGenerics();
}
 
Example #28
Source File: UiEventListenerMethodAdapter.java    From cuba with Apache License 2.0 5 votes vote down vote up
protected void publishEvent(Object instance, Object event) {
    if (event != null) {
        if (event instanceof ApplicationEvent) {
            this.events.publish((ApplicationEvent) event);
        } else {
            this.events.publish(new PayloadApplicationEvent<>(instance, event));
        }
    }
}
 
Example #29
Source File: UiEventListenerMethodAdapter.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Nullable
protected ResolvableType getResolvableType(ApplicationEvent event) {
    ResolvableType payloadType = null;
    if (event instanceof PayloadApplicationEvent) {
        PayloadApplicationEvent<?> payloadEvent = (PayloadApplicationEvent<?>) event;
        ResolvableType resolvableType = payloadEvent.getResolvableType();
        if (resolvableType != null) {
            payloadType = resolvableType.as(PayloadApplicationEvent.class).getGeneric();
        }
    }

    for (ResolvableType declaredEventType : this.declaredEventTypes) {
        Class<?> rawClass = declaredEventType.getRawClass();

        if (rawClass != null) {
            if (!ApplicationEvent.class.isAssignableFrom(rawClass) && payloadType != null) {
                if (declaredEventType.isAssignableFrom(payloadType)) {
                    return declaredEventType;
                }
            }
            if (rawClass.isInstance(event)) {
                return declaredEventType;
            }
        }
    }
    return null;
}
 
Example #30
Source File: ApplicationListenerMethodAdapter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public boolean supportsEventType(ResolvableType eventType) {
	for (ResolvableType declaredEventType : this.declaredEventTypes) {
		if (declaredEventType.isAssignableFrom(eventType)) {
			return true;
		}
		if (PayloadApplicationEvent.class.isAssignableFrom(eventType.toClass())) {
			ResolvableType payloadType = eventType.as(PayloadApplicationEvent.class).getGeneric();
			if (declaredEventType.isAssignableFrom(payloadType)) {
				return true;
			}
		}
	}
	return eventType.hasUnresolvableGenerics();
}