Java Code Examples for org.springframework.web.context.WebApplicationContext#getBeanNamesForType()

The following examples show how to use org.springframework.web.context.WebApplicationContext#getBeanNamesForType() . 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: SpringConfigurator.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private String getBeanNameByType(WebApplicationContext wac, Class<?> endpointClass) {
	String wacId = wac.getId();

	Map<Class<?>, String> beanNamesByType = cache.get(wacId);
	if (beanNamesByType == null) {
		beanNamesByType = new ConcurrentHashMap<Class<?>, String>();
		cache.put(wacId, beanNamesByType);
	}

	if (!beanNamesByType.containsKey(endpointClass)) {
		String[] names = wac.getBeanNamesForType(endpointClass);
		if (names.length == 1) {
			beanNamesByType.put(endpointClass, names[0]);
		}
		else {
			beanNamesByType.put(endpointClass, NO_VALUE);
			if (names.length > 1) {
				throw new IllegalStateException("Found multiple @ServerEndpoint's of type [" +
						endpointClass.getName() + "]: bean names " + Arrays.asList(names));
			}
		}
	}

	String beanName = beanNamesByType.get(endpointClass);
	return (NO_VALUE.equals(beanName) ? null : beanName);
}
 
Example 2
Source File: AbstractWidgetExecutorService.java    From entando-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected List<IFrameDecoratorContainer> extractDecorators(RequestContext reqCtx) throws ApsSystemException {
	HttpServletRequest request = reqCtx.getRequest();
	WebApplicationContext wac = ApsWebApplicationUtils.getWebApplicationContext(request);
	List<IFrameDecoratorContainer> containters = new ArrayList<IFrameDecoratorContainer>();
	try {
		String[] beanNames = wac.getBeanNamesForType(IFrameDecoratorContainer.class);
		for (int i = 0; i < beanNames.length; i++) {
			IFrameDecoratorContainer container = (IFrameDecoratorContainer) wac.getBean(beanNames[i]);
			containters.add(container);
		}
		BeanComparator comparator = new BeanComparator("order");
		Collections.sort(containters, comparator);
	} catch (Throwable t) {
		_logger.error("Error extracting widget decorators", t);
		throw new ApsSystemException("Error extracting widget decorators", t);
	}
	return containters;
}
 
Example 3
Source File: ProtectedResourceWardenServlet.java    From entando-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException {
	_logger.debug("Request: {}", request.getRequestURI());
	try {
		ServletContext svCtx = request.getSession().getServletContext();
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(svCtx);
		String[] resourceProviderNames = wac.getBeanNamesForType(IProtectedResourceProvider.class);
		for (int i = 0; i < resourceProviderNames.length; i++) {
			String resourceProviderName = resourceProviderNames[i];
			IProtectedResourceProvider provider = (IProtectedResourceProvider) wac.getBean(resourceProviderName);
			boolean responseCommitted = provider.provideProtectedResource(request, response);
			if (responseCommitted) {
				return;
			}
		}
	} catch (Throwable t) {
		_logger.error("Error providing protected resource", t);
		throw new ServletException("Error providing protected resource", t);
	}
}
 
Example 4
Source File: ApsWebApplicationUtils.java    From entando-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static void executeSystemRefresh(WebApplicationContext wac) throws Throwable {
	RefreshableBean configManager = (RefreshableBean) wac.getBean(SystemConstants.BASE_CONFIG_MANAGER);
	configManager.refresh();
	String[] defNames = wac.getBeanNamesForType(RefreshableBean.class);
	for (int i=0; i<defNames.length; i++) {
		Object bean = null;
		try {
			bean = wac.getBean(defNames[i]);
		} catch (Throwable t) {
               logger.error("error in executeSystemRefresh", t);
			bean = null;
		}
		if (bean != null) {
			((RefreshableBean) bean).refresh();
		}
	}
}
 
Example 5
Source File: PluginsSubMenuTag.java    From entando-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public int doStartTag() throws JspException {
	HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
	WebApplicationContext wac = ApsWebApplicationUtils.getWebApplicationContext(request);
	List<PluginSubMenuContainer> containters = new ArrayList<PluginSubMenuContainer>();
	ValueStack stack = this.getStack();
	try {
		String[] beanNames =  wac.getBeanNamesForType(PluginSubMenuContainer.class);
		for (int i=0; i<beanNames.length; i++) {
			PluginSubMenuContainer container = (PluginSubMenuContainer) wac.getBean(beanNames[i]);
			containters.add(container);
		}
		if (containters.size()>0) {
			stack.getContext().put(this.getObjectName(), containters);
			stack.setValue("#attr['" + this.getObjectName() + "']", containters, false);
			return EVAL_BODY_INCLUDE;
		}
	} catch (Throwable t) {
		throw new JspException("Error creating the plugins menu list", t);
	}
	return super.doStartTag();
}
 
Example 6
Source File: SpringConfigurator.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Nullable
private String getBeanNameByType(WebApplicationContext wac, Class<?> endpointClass) {
	String wacId = wac.getId();

	Map<Class<?>, String> beanNamesByType = cache.get(wacId);
	if (beanNamesByType == null) {
		beanNamesByType = new ConcurrentHashMap<>();
		cache.put(wacId, beanNamesByType);
	}

	if (!beanNamesByType.containsKey(endpointClass)) {
		String[] names = wac.getBeanNamesForType(endpointClass);
		if (names.length == 1) {
			beanNamesByType.put(endpointClass, names[0]);
		}
		else {
			beanNamesByType.put(endpointClass, NO_VALUE);
			if (names.length > 1) {
				throw new IllegalStateException("Found multiple @ServerEndpoint's of type [" +
						endpointClass.getName() + "]: bean names " + Arrays.asList(names));
			}
		}
	}

	String beanName = beanNamesByType.get(endpointClass);
	return (NO_VALUE.equals(beanName) ? null : beanName);
}
 
Example 7
Source File: SpringConfigurator.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Nullable
private String getBeanNameByType(WebApplicationContext wac, Class<?> endpointClass) {
	String wacId = wac.getId();

	Map<Class<?>, String> beanNamesByType = cache.get(wacId);
	if (beanNamesByType == null) {
		beanNamesByType = new ConcurrentHashMap<>();
		cache.put(wacId, beanNamesByType);
	}

	if (!beanNamesByType.containsKey(endpointClass)) {
		String[] names = wac.getBeanNamesForType(endpointClass);
		if (names.length == 1) {
			beanNamesByType.put(endpointClass, names[0]);
		}
		else {
			beanNamesByType.put(endpointClass, NO_VALUE);
			if (names.length > 1) {
				throw new IllegalStateException("Found multiple @ServerEndpoint's of type [" +
						endpointClass.getName() + "]: bean names " + Arrays.asList(names));
			}
		}
	}

	String beanName = beanNamesByType.get(endpointClass);
	return (NO_VALUE.equals(beanName) ? null : beanName);
}
 
Example 8
Source File: HookPointTag.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
private List<HookPointElementContainer> extractElements(HttpServletRequest request) {
	WebApplicationContext wac = ApsWebApplicationUtils.getWebApplicationContext(request);
	String[] beanNames =  wac.getBeanNamesForType(HookPointElementContainer.class);
	List<HookPointElementContainer> containers = new ArrayList<HookPointElementContainer>();
	for (int i=0; i<beanNames.length; i++) {
		HookPointElementContainer container = (HookPointElementContainer) wac.getBean(beanNames[i]);
		if (null != container && null != container.getHookPointKey() && container.getHookPointKey().equals(this.getKey())) {
			containers.add(container);
		}
	}
	BeanComparator comparator = new BeanComparator("priority");
	Collections.sort(containers, comparator);
	return containers;
}