List of usage examples for org.springframework.util Assert state
public static void state(boolean expression, Supplier<String> messageSupplier)
From source file:org.springframework.boot.actuate.endpoint.annotation.AnnotationEndpointDiscoverer.java
private void addExtension(Map<Class<?>, DiscoveredEndpoint> endpoints, Map<Class<?>, DiscoveredExtension> extensions, String beanName) { Class<?> extensionType = this.applicationContext.getType(beanName); Class<?> endpointType = getEndpointType(extensionType); DiscoveredEndpoint endpoint = getExtendingEndpoint(endpoints, extensionType, endpointType); if (isExtensionExposed(endpointType, extensionType, endpoint.getInfo())) { Assert.state(endpoint.isExposed() || isEndpointFiltered(endpoint.getInfo()), () -> "Invalid extension " + extensionType.getName() + "': endpoint '" + endpointType.getName() + "' does not support such extension"); Object target = this.applicationContext.getBean(beanName); Map<Method, T> operations = this.operationsFactory.createOperations(endpoint.getInfo().getId(), target, extensionType);/* w w w . ja v a2s . com*/ DiscoveredExtension extension = new DiscoveredExtension(extensionType, operations.values()); DiscoveredExtension previous = extensions.putIfAbsent(endpointType, extension); Assert.state(previous == null, () -> "Found two extensions for the same endpoint '" + endpointType.getName() + "': " + extension.getExtensionType().getName() + " and " + previous.getExtensionType().getName()); } }
From source file:org.springframework.boot.actuate.endpoint.annotation.AnnotationEndpointDiscoverer.java
private Class<?> getEndpointType(Class<?> extensionType) { AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(extensionType, EndpointExtension.class); Class<?> endpointType = attributes.getClass("endpoint"); Assert.state(!endpointType.equals(Void.class), () -> "Extension " + endpointType.getName() + " does not specify an endpoint"); return endpointType; }
From source file:org.springframework.boot.actuate.endpoint.annotation.AnnotationEndpointDiscoverer.java
private DiscoveredEndpoint getExtendingEndpoint(Map<Class<?>, DiscoveredEndpoint> endpoints, Class<?> extensionType, Class<?> endpointType) { DiscoveredEndpoint endpoint = endpoints.get(endpointType); Assert.state(endpoint != null, () -> "Invalid extension '" + extensionType.getName() + "': no endpoint found with type '" + endpointType.getName() + "'"); return endpoint; }
From source file:org.springframework.boot.admin.SpringApplicationAdminMXBeanRegistrar.java
@Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { Assert.state(applicationContext instanceof ConfigurableApplicationContext, "ApplicationContext does not implement ConfigurableApplicationContext"); this.applicationContext = (ConfigurableApplicationContext) applicationContext; }
From source file:org.springframework.boot.bind.PropertiesConfigurationFactory.java
public void bindPropertiesToTarget() throws BindException { Assert.state(this.properties != null || this.propertySources != null, "Properties or propertySources should not be null"); try {/* ww w.j a va 2 s. c om*/ if (this.logger.isTraceEnabled()) { if (this.properties != null) { this.logger.trace("Properties:\n" + this.properties); } else { this.logger.trace("Property Sources: " + this.propertySources); } } this.hasBeenBound = true; doBindPropertiesToTarget(); } catch (BindException ex) { if (this.exceptionIfInvalid) { throw ex; } this.logger.error("Failed to load Properties validation bean. " + "Your Properties may be invalid.", ex); } }
From source file:org.springframework.boot.bind.YamlConfigurationFactory.java
@Override @SuppressWarnings("unchecked") public void afterPropertiesSet() throws Exception { if (this.yaml == null) { Assert.state(this.resource != null, "Resource should not be null"); this.yaml = StreamUtils.copyToString(this.resource.getInputStream(), Charset.defaultCharset()); }/* www. j a v a2 s. c o m*/ Assert.state(this.yaml != null, "Yaml document should not be null: " + "either set it directly or set the resource to load it from"); try { if (this.logger.isTraceEnabled()) { this.logger.trace("Yaml document is\n" + this.yaml); } Constructor constructor = new YamlJavaBeanPropertyConstructor(this.type, this.propertyAliases); this.configuration = (T) (new Yaml(constructor)).load(this.yaml); if (this.validator != null) { validate(); } } catch (YAMLException ex) { if (this.exceptionIfInvalid) { throw ex; } this.logger.error("Failed to load YAML validation bean. " + "Your YAML file may be invalid.", ex); } }
From source file:org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactory.java
protected final File getValidSessionStoreDir(boolean mkdirs) { File dir = getSessionStoreDir(); if (dir == null) { return new ApplicationTemp().getDir("servlet-sessions"); }// ww w. j a v a2s . co m if (!dir.isAbsolute()) { dir = new File(new ApplicationHome().getDir(), dir.getPath()); } if (!dir.exists() && mkdirs) { dir.mkdirs(); } Assert.state(!mkdirs || dir.exists(), "Session dir " + dir + " does not exist"); Assert.state(!dir.isFile(), "Session dir " + dir + " points to a file"); return dir; }
From source file:org.springframework.boot.context.web.SpringBootServletInitializer.java
protected WebApplicationContext createRootApplicationContext(ServletContext servletContext) { SpringApplicationBuilder builder = createSpringApplicationBuilder(); builder.main(getClass());//from w ww . ja v a2 s .c o m ApplicationContext parent = getExistingRootWebApplicationContext(servletContext); if (parent != null) { this.logger.info("Root context already created (using as parent)."); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, null); builder.initializers(new ParentContextApplicationContextInitializer(parent)); } builder.initializers(new ServletContextApplicationContextInitializer(servletContext)); builder.contextClass(AnnotationConfigEmbeddedWebApplicationContext.class); builder = configure(builder); SpringApplication application = builder.build(); if (application.getSources().isEmpty() && AnnotationUtils.findAnnotation(getClass(), Configuration.class) != null) { application.getSources().add(getClass()); } Assert.state(application.getSources().size() > 0, "No SpringApplication sources have been defined. Either override the " + "configure method or add an @Configuration annotation"); // Ensure error pages are registered if (this.registerErrorPageFilter) { application.getSources().add(ErrorPageFilter.class); } return run(application); }
From source file:org.springframework.boot.devtools.livereload.LiveReloadServer.java
/** * Start the livereload server and accept incoming connections. * @throws IOException in case of I/O errors *///from w w w.j a va 2 s . com public synchronized void start() throws IOException { Assert.state(!isStarted(), "Server already started"); logger.debug("Starting live reload server on port " + this.port); this.serverSocket = new ServerSocket(this.port); this.listenThread = this.threadFactory.newThread(new Runnable() { @Override public void run() { acceptConnections(); } }); this.listenThread.setDaemon(true); this.listenThread.setName("Live Reload Server"); this.listenThread.start(); }
From source file:org.springframework.boot.devtools.remote.client.ClassPathChangeUploader.java
@Override public void onApplicationEvent(ClassPathChangedEvent event) { try {//from w w w. j av a 2 s. c om ClassLoaderFiles classLoaderFiles = getClassLoaderFiles(event); ClientHttpRequest request = this.requestFactory.createRequest(this.uri, HttpMethod.POST); byte[] bytes = serialize(classLoaderFiles); HttpHeaders headers = request.getHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentLength(bytes.length); FileCopyUtils.copy(bytes, request.getBody()); logUpload(classLoaderFiles); ClientHttpResponse response = request.execute(); Assert.state(response.getStatusCode() == HttpStatus.OK, "Unexpected " + response.getStatusCode() + " response uploading class files"); } catch (IOException ex) { throw new IllegalStateException(ex); } }