protected void registerEndpoint(String beanName, Endpoint<?> endpoint) { @SuppressWarnings("rawtypes") Class<? extends Endpoint> type = endpoint.getClass(); if (AnnotationUtils.findAnnotation(type, ManagedResource.class) != null) { // Already managed return; } if (type.isMemberClass() && AnnotationUtils.findAnnotation(type.getEnclosingClass(), ManagedResource.class) != null) { // Nested class with @ManagedResource in parent return; } try { registerBeanNameOrInstance(getEndpointMBean(beanName, endpoint), beanName); } catch (MBeanExportException ex) { logger.error("Could not register MBean for endpoint [" + beanName + "]", ex); } }
@Override public void afterPropertiesSet() throws Exception { Collection<MvcEndpoint> existing = BeanFactoryUtils .beansOfTypeIncludingAncestors(this.applicationContext, MvcEndpoint.class) .values(); this.endpoints.addAll(existing); this.customTypes = findEndpointClasses(existing); @SuppressWarnings("rawtypes") Collection<Endpoint> delegates = BeanFactoryUtils .beansOfTypeIncludingAncestors(this.applicationContext, Endpoint.class) .values(); for (Endpoint<?> endpoint : delegates) { if (isGenericEndpoint(endpoint.getClass()) && endpoint.isEnabled()) { EndpointMvcAdapter adapter = new EndpointMvcAdapter(endpoint); String path = determinePath(endpoint, this.applicationContext.getEnvironment()); if (path != null) { adapter.setPath(path); } this.endpoints.add(adapter); } } }
@Bean public static Endpoint<String> pingEndpoint() { return new Endpoint<String>() { @Override public String getId() { return "ping"; } @Override public boolean isEnabled() { return true; } @Override public boolean isSensitive() { return false; } @Override public String invoke() { return "OK"; } }; }
@Override public void afterPropertiesSet() throws Exception { Collection<MvcEndpoint> existing = BeanFactoryUtils .beansOfTypeIncludingAncestors(this.applicationContext, MvcEndpoint.class) .values(); this.endpoints.addAll(existing); this.customTypes = findEndpointClasses(existing); @SuppressWarnings("rawtypes") Collection<Endpoint> delegates = BeanFactoryUtils .beansOfTypeIncludingAncestors(this.applicationContext, Endpoint.class) .values(); for (Endpoint<?> endpoint : delegates) { if (isGenericEndpoint(endpoint.getClass()) && endpoint.isEnabled()) { EndpointMvcAdapter adapter = new EndpointMvcAdapter(endpoint); String path = this.applicationContext.getEnvironment() .getProperty("endpoints." + endpoint.getId() + ".path"); if (path != null) { adapter.setPath(path); } this.endpoints.add(adapter); } } }
@SuppressWarnings({ "rawtypes" }) protected void locateAndRegisterEndpoints() { Map<String, Endpoint> endpoints = this.beanFactory.getBeansOfType(Endpoint.class); for (Map.Entry<String, Endpoint> endpointEntry : endpoints.entrySet()) { if (!this.registeredEndpoints.contains(endpointEntry.getValue()) && endpointEntry.getValue().isEnabled()) { registerEndpoint(endpointEntry.getKey(), endpointEntry.getValue()); this.registeredEndpoints.add(endpointEntry.getValue()); } } }
private boolean parentContextContainsSameBean(ApplicationContext applicationContext, String beanKey) { if (applicationContext.getParent() != null) { try { this.applicationContext.getParent().getBean(beanKey, Endpoint.class); return true; } catch (BeansException ex) { return parentContextContainsSameBean(applicationContext.getParent(), beanKey); } } return false; }
/** * Create a new {@link EndpointMBean} instance. * @param beanName the bean name * @param endpoint the endpoint to wrap * @param objectMapper the {@link ObjectMapper} used to convert the payload */ public EndpointMBean(String beanName, Endpoint<?> endpoint, ObjectMapper objectMapper) { Assert.notNull(beanName, "BeanName must not be null"); Assert.notNull(endpoint, "Endpoint must not be null"); Assert.notNull(objectMapper, "ObjectMapper must not be null"); this.endpoint = endpoint; this.mapper = objectMapper; this.listObject = objectMapper.getTypeFactory() .constructParametricType(List.class, Object.class); this.mapStringObject = objectMapper.getTypeFactory() .constructParametricType(Map.class, String.class, Object.class); }
private String determinePath(Endpoint<?> endpoint, Environment environment) { ConfigurationProperties configurationProperties = AnnotationUtils .findAnnotation(endpoint.getClass(), ConfigurationProperties.class); if (configurationProperties != null) { return environment.getProperty(configurationProperties.prefix() + ".path"); } return null; }
private String determinePath(Endpoint<?> endpoint, Environment environment) { ConfigurationProperties configurationProperties = AnnotationUtils .findAnnotation(endpoint.getClass(), ConfigurationProperties.class); if (configurationProperties != null) { String prefix = StringUtils.hasText(configurationProperties.prefix()) ? configurationProperties.prefix() : configurationProperties.value(); return environment.getProperty(prefix + ".path"); } return null; }
/** * Create a new {@link EndpointMBean} instance. * @param beanName the bean name * @param endpoint the endpoint to wrap * @param objectMapper the {@link ObjectMapper} used to convert the payload */ public EndpointMBean(String beanName, Endpoint<?> endpoint, ObjectMapper objectMapper) { Assert.notNull(beanName, "BeanName must not be null"); Assert.notNull(endpoint, "Endpoint must not be null"); Assert.notNull(objectMapper, "ObjectMapper must not be null"); this.endpoint = endpoint; this.mapper = objectMapper; }
@Override public Class<? extends Endpoint> getEndpointType() { return null; }
protected EndpointMBean getEndpointMBean(String beanName, Endpoint<?> endpoint) { if (endpoint instanceof ShutdownEndpoint) { return new ShutdownEndpointMBean(beanName, endpoint, this.objectMapper); } return new DataEndpointMBean(beanName, endpoint, this.objectMapper); }
public Endpoint<?> getEndpoint() { return this.endpoint; }
@Override @SuppressWarnings("rawtypes") public Class<? extends Endpoint> getEndpointType() { return null; }
@Override @SuppressWarnings("rawtypes") public Class<? extends Endpoint> getEndpointType() { return this.delegate.getClass(); }
@Override @SuppressWarnings("rawtypes") public Class<? extends Endpoint> getEndpointType() { return Endpoint.class; }
@Bean public Endpoint<Boolean> nested() { return new Nested(); }
@Override public Class<? extends Endpoint<?>> getEndpointType() { return null; }