Java Code Examples for org.springframework.context.ApplicationEvent#getSource()

The following examples show how to use org.springframework.context.ApplicationEvent#getSource() . 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: SpringBus.java    From cxf with Apache License 2.0 6 votes vote down vote up
public void onApplicationEvent(ApplicationEvent event) {
    if (ctx == null) {
        return;
    }
    boolean doIt = false;
    ApplicationContext ac = ctx;
    while (ac != null) {
        if (event.getSource() == ac) {
            doIt = true;
            break;
        }
        ac = ac.getParent();
    }
    if (doIt) {
        if (event instanceof ContextRefreshedEvent) {
            if (getState() != BusState.RUNNING) {
                initialize();
            }
        } else if (event instanceof ContextClosedEvent && getState() == BusState.RUNNING) {
            // The bus could be create by using SpringBusFactory.createBus("/cxf.xml");
            // Just to make sure the shutdown is called rightly
            shutdown();
        }
    }
}
 
Example 2
Source File: AbstractApplicationEventMulticaster.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Return a Collection of ApplicationListeners matching the given
 * event type. Non-matching listeners get excluded early.
 * @param event the event to be propagated. Allows for excluding
 * non-matching listeners early, based on cached matching information.
 * @param eventType the event type
 * @return a Collection of ApplicationListeners
 * @see org.springframework.context.ApplicationListener
 */
protected Collection<ApplicationListener<?>> getApplicationListeners(
		ApplicationEvent event, ResolvableType eventType) {

	Object source = event.getSource();
	Class<?> sourceType = (source != null ? source.getClass() : null);
	ListenerCacheKey cacheKey = new ListenerCacheKey(eventType, sourceType);

	// Quick check for existing entry on ConcurrentHashMap...
	ListenerRetriever retriever = this.retrieverCache.get(cacheKey);
	if (retriever != null) {
		return retriever.getApplicationListeners();
	}

	if (this.beanClassLoader == null ||
			(ClassUtils.isCacheSafe(event.getClass(), this.beanClassLoader) &&
					(sourceType == null || ClassUtils.isCacheSafe(sourceType, this.beanClassLoader)))) {
		// Fully synchronized building and caching of a ListenerRetriever
		synchronized (this.retrievalMutex) {
			retriever = this.retrieverCache.get(cacheKey);
			if (retriever != null) {
				return retriever.getApplicationListeners();
			}
			retriever = new ListenerRetriever(true);
			Collection<ApplicationListener<?>> listeners =
					retrieveApplicationListeners(eventType, sourceType, retriever);
			this.retrieverCache.put(cacheKey, retriever);
			return listeners;
		}
	}
	else {
		// No ListenerRetriever caching -> no synchronization necessary
		return retrieveApplicationListeners(eventType, sourceType, null);
	}
}
 
Example 3
Source File: LogEventListener.java    From magic-starter with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private <T extends AbstractLogModel> T wrapperLogModel(ApplicationEvent event) {
	Map<String, Object> source = (Map<String, Object>) event.getSource();
	T model = (T) source.get(LogConstants.EVENT_LOG_KEY);
	HttpServletRequest request = (HttpServletRequest) source.get(LogConstants.EVENT_REQUEST_KEY);
	model = model.wrapperLog(model, request, operatorService.getOperator(request));
	return model;
}
 
Example 4
Source File: AbstractApplicationEventMulticaster.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Return a Collection of ApplicationListeners matching the given
 * event type. Non-matching listeners get excluded early.
 * @param event the event to be propagated. Allows for excluding
 * non-matching listeners early, based on cached matching information.
 * @param eventType the event type
 * @return a Collection of ApplicationListeners
 * @see org.springframework.context.ApplicationListener
 */
protected Collection<ApplicationListener<?>> getApplicationListeners(
		ApplicationEvent event, ResolvableType eventType) {

	Object source = event.getSource();
	Class<?> sourceType = (source != null ? source.getClass() : null);
	ListenerCacheKey cacheKey = new ListenerCacheKey(eventType, sourceType);

	// Quick check for existing entry on ConcurrentHashMap...
	ListenerRetriever retriever = this.retrieverCache.get(cacheKey);
	if (retriever != null) {
		return retriever.getApplicationListeners();
	}

	if (this.beanClassLoader == null ||
			(ClassUtils.isCacheSafe(event.getClass(), this.beanClassLoader) &&
					(sourceType == null || ClassUtils.isCacheSafe(sourceType, this.beanClassLoader)))) {
		// Fully synchronized building and caching of a ListenerRetriever
		synchronized (this.retrievalMutex) {
			retriever = this.retrieverCache.get(cacheKey);
			if (retriever != null) {
				return retriever.getApplicationListeners();
			}
			retriever = new ListenerRetriever(true);
			Collection<ApplicationListener<?>> listeners =
					retrieveApplicationListeners(eventType, sourceType, retriever);
			this.retrieverCache.put(cacheKey, retriever);
			return listeners;
		}
	}
	else {
		// No ListenerRetriever caching -> no synchronization necessary
		return retrieveApplicationListeners(eventType, sourceType, null);
	}
}
 
Example 5
Source File: FileContentStore.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void onApplicationEvent(ApplicationEvent event)
{
    // Once the context has been refreshed, we tell other interested beans about the existence of this content store
    // (e.g. for monitoring purposes)
    if (event instanceof ContextRefreshedEvent && event.getSource() == this.applicationContext)
    {
        publishEvent(((ContextRefreshedEvent) event).getApplicationContext(), Collections.<String, Serializable> emptyMap());
    }
}
 
Example 6
Source File: SafeApplicationEventMulticaster.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void multicastEvent(ApplicationEvent event)
{
    if (event instanceof ContextRefreshedEvent && event.getSource() == this.appContext)
    {
        this.isApplicationStarted = true;
        for (ApplicationEvent queuedEvent : this.queuedEvents)
        {
            multicastEventInternal(queuedEvent);
        }
        this.queuedEvents.clear();
        multicastEventInternal(event);
    }
    else if (event instanceof ContextClosedEvent && event.getSource() == this.appContext)
    {
        this.isApplicationStarted = false;
        multicastEventInternal(event);
    }
    else if (this.isApplicationStarted)
    {
        multicastEventInternal(event);
    }
    else
    {
        this.queuedEvents.add(event);
    }
}
 
Example 7
Source File: LoginFailureListener.java    From airsonic with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof AbstractAuthenticationFailureEvent) {
        if (event.getSource() instanceof AbstractAuthenticationToken) {
            AbstractAuthenticationToken token = (AbstractAuthenticationToken) event.getSource();
            Object details = token.getDetails();
            if (details instanceof WebAuthenticationDetails) {
                LOG.info("Login failed from [" + ((WebAuthenticationDetails) details).getRemoteAddress() + "]");
            }
        }
    }

}
 
Example 8
Source File: AbstractApplicationEventMulticaster.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return a Collection of ApplicationListeners matching the given
 * event type. Non-matching listeners get excluded early.
 * @param event the event to be propagated. Allows for excluding
 * non-matching listeners early, based on cached matching information.
 * @param eventType the event type
 * @return a Collection of ApplicationListeners
 * @see org.springframework.context.ApplicationListener
 */
protected Collection<ApplicationListener<?>> getApplicationListeners(
		ApplicationEvent event, ResolvableType eventType) {

	Object source = event.getSource();
	Class<?> sourceType = (source != null ? source.getClass() : null);
	ListenerCacheKey cacheKey = new ListenerCacheKey(eventType, sourceType);

	// Quick check for existing entry on ConcurrentHashMap...
	ListenerRetriever retriever = this.retrieverCache.get(cacheKey);
	if (retriever != null) {
		return retriever.getApplicationListeners();
	}

	if (this.beanClassLoader == null ||
			(ClassUtils.isCacheSafe(event.getClass(), this.beanClassLoader) &&
					(sourceType == null || ClassUtils.isCacheSafe(sourceType, this.beanClassLoader)))) {
		// Fully synchronized building and caching of a ListenerRetriever
		synchronized (this.retrievalMutex) {
			retriever = this.retrieverCache.get(cacheKey);
			if (retriever != null) {
				return retriever.getApplicationListeners();
			}
			retriever = new ListenerRetriever(true);
			Collection<ApplicationListener<?>> listeners =
					retrieveApplicationListeners(eventType, sourceType, retriever);
			this.retrieverCache.put(cacheKey, retriever);
			return listeners;
		}
	}
	else {
		// No ListenerRetriever caching -> no synchronization necessary
		return retrieveApplicationListeners(eventType, sourceType, null);
	}
}
 
Example 9
Source File: AbstractApplicationEventMulticaster.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Return a Collection of ApplicationListeners matching the given
 * event type. Non-matching listeners get excluded early.
 * @param event the event to be propagated. Allows for excluding
 * non-matching listeners early, based on cached matching information.
 * @param eventType the event type
 * @return a Collection of ApplicationListeners
 * @see org.springframework.context.ApplicationListener
 */
protected Collection<ApplicationListener<?>> getApplicationListeners(
		ApplicationEvent event, ResolvableType eventType) {

	Object source = event.getSource();
	Class<?> sourceType = (source != null ? source.getClass() : null);
	ListenerCacheKey cacheKey = new ListenerCacheKey(eventType, sourceType);

	// Quick check for existing entry on ConcurrentHashMap...
	ListenerRetriever retriever = this.retrieverCache.get(cacheKey);
	if (retriever != null) {
		return retriever.getApplicationListeners();
	}

	if (this.beanClassLoader == null ||
			(ClassUtils.isCacheSafe(event.getClass(), this.beanClassLoader) &&
					(sourceType == null || ClassUtils.isCacheSafe(sourceType, this.beanClassLoader)))) {
		// Fully synchronized building and caching of a ListenerRetriever
		synchronized (this.retrievalMutex) {
			retriever = this.retrieverCache.get(cacheKey);
			if (retriever != null) {
				return retriever.getApplicationListeners();
			}
			retriever = new ListenerRetriever(true);
			Collection<ApplicationListener<?>> listeners =
					retrieveApplicationListeners(eventType, sourceType, retriever);
			this.retrieverCache.put(cacheKey, retriever);
			return listeners;
		}
	}
	else {
		// No ListenerRetriever caching -> no synchronization necessary
		return retrieveApplicationListeners(eventType, sourceType, null);
	}
}
 
Example 10
Source File: SubsonicApplicationEventListener.java    From subsonic with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof AbstractAuthenticationFailureEvent) {
        if (event.getSource() instanceof AbstractAuthenticationToken) {
            AbstractAuthenticationToken token = (AbstractAuthenticationToken) event.getSource();
            Object details = token.getDetails();
            if (details instanceof WebAuthenticationDetails) {
                loginFailureLogger.log(((WebAuthenticationDetails) details).getRemoteAddress(), String.valueOf(token.getPrincipal()));
            }
        }
    }

}
 
Example 11
Source File: UiEventsMulticasterImpl.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Override
public void multicastEvent(ApplicationEvent event) {
    Object source = event.getSource();
    Class<?> sourceType = (source != null ? source.getClass() : null);

    ResolvableType type = resolveDefaultEventType(event);
    for (ApplicationListener<?> listener : retrieveApplicationListeners(type, sourceType)) {
        invokeListener(listener, event);
    }
}
 
Example 12
Source File: SourceFilteringListener.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationEvent event) {
	if (event.getSource() == this.source) {
		onApplicationEventInternal(event);
	}
}
 
Example 13
Source File: SourceFilteringListener.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationEvent event) {
	if (event.getSource() == this.source) {
		onApplicationEventInternal(event);
	}
}
 
Example 14
Source File: AbstractPropertyBackedBean.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void onApplicationEvent(ApplicationEvent event)
{
    if (this.autoStart && event instanceof ContextRefreshedEvent && event.getSource() == this.parent)
    {
        this.lock.writeLock().lock();
        try
        {
            start(false, false);
        }
        catch (Exception e)
        {
            // Let's log and swallow auto-start exceptions so that they are non-fatal. This means that the system
            // can hopefully be brought up to a level where its configuration can be edited and corrected
            logger.error("Error auto-starting subsystem", e);
        }
        finally
        {
            this.lock.writeLock().unlock();
        }
    }
    else if (event instanceof PropertyBackedBeanStartedEvent)
    {
        this.lock.writeLock().lock();
        try
        {
            // If we aren't started, reinitialize so that we pick up state changes from the database
            switch (this.runtimeState)
            {
            case PENDING_BROADCAST_START:
            case STOPPED:
                destroy(false);
                // fall through
            case UNINITIALIZED:
                start(false, false);                
            }
        }
        finally
        {
            this.lock.writeLock().unlock();                
        }
    }
    else if (event instanceof PropertyBackedBeanStoppedEvent)
    {
        this.lock.writeLock().lock();
        try
        {
            // Completely destroy the state so that it will have to be reinitialized should the bean be put back in
            // to use by this node
            destroy(false);
        }
        finally
        {
            this.lock.writeLock().unlock();
        }
    }
}
 
Example 15
Source File: SourceFilteringListener.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationEvent event) {
	if (event.getSource() == this.source) {
		onApplicationEventInternal(event);
	}
}
 
Example 16
Source File: SourceFilteringListener.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationEvent event) {
	if (event.getSource() == this.source) {
		onApplicationEventInternal(event);
	}
}