Polishing

This commit is contained in:
Juergen Hoeller 2014-08-18 19:27:08 +02:00
parent 0d0d7139ee
commit 2ef3d66c89
6 changed files with 94 additions and 97 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -20,7 +20,6 @@ import java.net.URI;
import java.util.Properties;
import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.spi.CachingProvider;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.DisposableBean;
@ -75,8 +74,7 @@ public class JCacheManagerFactoryBean
@Override
public void afterPropertiesSet() {
CachingProvider provider = Caching.getCachingProvider();
this.cacheManager = provider.getCacheManager(
this.cacheManager = Caching.getCachingProvider().getCacheManager(
this.cacheManagerUri, this.beanClassLoader, this.cacheManagerProperties);
}

View File

@ -43,24 +43,63 @@ public abstract class AbstractCacheManager implements CacheManager, Initializing
private Set<String> cacheNames = new LinkedHashSet<String>(16);
// Early cache initialization on startup
@Override
public void afterPropertiesSet() {
Collection<? extends Cache> caches = loadCaches();
// preserve the initial order of the cache names
// Preserve the initial order of the cache names
this.cacheMap.clear();
this.cacheNames.clear();
for (Cache cache : caches) {
this.cacheMap.put(cache.getName(), decorateCache(cache));
this.cacheNames.add(cache.getName());
addCache(cache);
}
}
/**
* Load the initial caches for this cache manager.
* <p>Called by {@link #afterPropertiesSet()} on startup.
* The returned collection may be empty but must not be {@code null}.
*/
protected abstract Collection<? extends Cache> loadCaches();
// Lazy cache initialization on access
@Override
public Cache getCache(String name) {
Cache cache = lookupCache(name);
if (cache != null) {
return cache;
}
else {
Cache missingCache = getMissingCache(name);
if (missingCache != null) {
addCache(missingCache);
return lookupCache(name); // may be decorated
}
return null;
}
}
@Override
public Collection<String> getCacheNames() {
return Collections.unmodifiableSet(this.cacheNames);
}
// Common cache initialization delegates/callbacks
protected final void addCache(Cache cache) {
this.cacheMap.put(cache.getName(), decorateCache(cache));
this.cacheNames.add(cache.getName());
}
protected final Cache lookupCache(String name) {
return this.cacheMap.get(name);
}
/**
* Decorate the given Cache object if necessary.
* @param cache the Cache object to be added to this CacheManager
@ -87,35 +126,4 @@ public abstract class AbstractCacheManager implements CacheManager, Initializing
return null;
}
@Override
public Cache getCache(String name) {
Cache cache = lookupCache(name);
if (cache != null) {
return cache;
}
else {
Cache missingCache = getMissingCache(name);
if (missingCache != null) {
addCache(missingCache);
return lookupCache(name); // May be decorated
}
return null;
}
}
@Override
public Collection<String> getCacheNames() {
return Collections.unmodifiableSet(this.cacheNames);
}
private Cache lookupCache(String name) {
return this.cacheMap.get(name);
}
/**
* Load the caches for this cache manager. Occurs at startup.
* The returned collection must not be null.
*/
protected abstract Collection<? extends Cache> loadCaches();
}

View File

@ -53,8 +53,8 @@ import org.springframework.util.ClassUtils;
* @author Sebastien Deleuze
* @since 4.1
*/
public abstract class AbstractJackson2HttpMessageConverter extends
AbstractHttpMessageConverter<Object> implements GenericHttpMessageConverter<Object> {
public abstract class AbstractJackson2HttpMessageConverter extends AbstractHttpMessageConverter<Object>
implements GenericHttpMessageConverter<Object> {
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
@ -82,6 +82,7 @@ public abstract class AbstractJackson2HttpMessageConverter extends
this.objectMapper = objectMapper;
}
/**
* Set the {@code ObjectMapper} for this view.
* If not set, a default {@link ObjectMapper#ObjectMapper() ObjectMapper} is used.
@ -126,6 +127,7 @@ public abstract class AbstractJackson2HttpMessageConverter extends
}
}
@Override
public boolean canRead(Class<?> clazz, MediaType mediaType) {
return canRead(clazz, null, mediaType);

View File

@ -54,6 +54,7 @@ public class MappingJackson2HttpMessageConverter extends AbstractJackson2HttpMes
new MediaType("application", "*+json", DEFAULT_CHARSET));
}
/**
* Specify a custom prefix to use for this view's JSON output.
* Default is none.
@ -75,15 +76,14 @@ public class MappingJackson2HttpMessageConverter extends AbstractJackson2HttpMes
this.jsonPrefix = (prefixJson ? "{} && " : null);
}
@Override
protected void writePrefix(JsonGenerator generator, Object object) throws IOException {
if (this.jsonPrefix != null) {
generator.writeRaw(this.jsonPrefix);
}
String jsonpFunction = null;
if (object instanceof MappingJacksonValue) {
jsonpFunction = ((MappingJacksonValue)object).getJsonpFunction();
}
String jsonpFunction =
(object instanceof MappingJacksonValue ? ((MappingJacksonValue) object).getJsonpFunction() : null);
if (jsonpFunction != null) {
generator.writeRaw(jsonpFunction + "(");
}
@ -91,10 +91,8 @@ public class MappingJackson2HttpMessageConverter extends AbstractJackson2HttpMes
@Override
protected void writeSuffix(JsonGenerator generator, Object object) throws IOException {
String jsonpFunction = null;
if (object instanceof MappingJacksonValue) {
jsonpFunction = ((MappingJacksonValue)object).getJsonpFunction();
}
String jsonpFunction =
(object instanceof MappingJacksonValue ? ((MappingJacksonValue) object).getJsonpFunction() : null);
if (jsonpFunction != null) {
generator.writeRaw(");");
}

View File

@ -20,12 +20,11 @@ import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
/**
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverter} that
* can read and write XML using <a href="https://github.com/FasterXML/jackson-dataformat-xml">Jackson 2.x extension component for
* reading and writing XML encoded data</a>.
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverter}
* that can read and write XML using <a href="https://github.com/FasterXML/jackson-dataformat-xml">
* Jackson 2.x extension component for reading and writing XML encoded data</a>.
*
* @author Sebastien Deleuze
* @since 4.1
@ -33,8 +32,7 @@ import org.springframework.http.converter.json.MappingJackson2HttpMessageConvert
public class MappingJackson2XmlHttpMessageConverter extends AbstractJackson2HttpMessageConverter {
public MappingJackson2XmlHttpMessageConverter() {
super(new XmlMapper(),
new MediaType("application", "xml", MappingJackson2HttpMessageConverter.DEFAULT_CHARSET));
super(new XmlMapper(), new MediaType("application", "xml", DEFAULT_CHARSET));
}
}

View File

@ -16,6 +16,11 @@
package org.springframework.web.socket.sockjs.client;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.http.HttpHeaders;
@ -38,11 +43,6 @@ import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.sockjs.frame.SockJsFrame;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
/**
* An {@code XhrTransport} implementation that uses a
* {@link org.springframework.web.client.RestTemplate RestTemplate}.
@ -76,12 +76,9 @@ public class RestTemplateXhrTransport extends AbstractXhrTransport implements Xh
/**
* Configure the {@code TaskExecutor} to use to execute XHR receive requests.
*
* <p>By default {@link org.springframework.core.task.SimpleAsyncTaskExecutor
* SimpleAsyncTaskExecutor} is configured which creates a new thread every
* time the transports connects.
*
* @param taskExecutor the task executor, cannot be {@code null}
*/
public void setTaskExecutor(TaskExecutor taskExecutor) {
Assert.notNull(this.taskExecutor);
@ -147,43 +144,11 @@ public class RestTemplateXhrTransport extends AbstractXhrTransport implements Xh
}
/**
* A RequestCallback to add the headers and (optionally) String content.
*/
private static class XhrRequestCallback implements RequestCallback {
private final HttpHeaders headers;
private final String body;
public XhrRequestCallback(HttpHeaders headers) {
this(headers, null);
}
public XhrRequestCallback(HttpHeaders headers, String body) {
this.headers = headers;
this.body = body;
}
@Override
public void doWithRequest(ClientHttpRequest request) throws IOException {
if (this.headers != null) {
request.getHeaders().putAll(this.headers);
}
if (this.body != null) {
StreamUtils.copy(this.body, SockJsFrame.CHARSET, request.getBody());
}
}
}
/**
* A simple ResponseExtractor that reads the body into a String.
*/
private final static ResponseExtractor<ResponseEntity<String>> textExtractor =
new ResponseExtractor<ResponseEntity<String>>() {
@Override
public ResponseEntity<String> extractData(ClientHttpResponse response) throws IOException {
if (response.getBody() == null) {
@ -196,6 +161,37 @@ public class RestTemplateXhrTransport extends AbstractXhrTransport implements Xh
}
};
/**
* A RequestCallback to add the headers and (optionally) String content.
*/
private static class XhrRequestCallback implements RequestCallback {
private final HttpHeaders headers;
private final String body;
public XhrRequestCallback(HttpHeaders headers) {
this(headers, null);
}
public XhrRequestCallback(HttpHeaders headers, String body) {
this.headers = headers;
this.body = body;
}
@Override
public void doWithRequest(ClientHttpRequest request) throws IOException {
if (this.headers != null) {
request.getHeaders().putAll(this.headers);
}
if (this.body != null) {
StreamUtils.copy(this.body, SockJsFrame.CHARSET, request.getBody());
}
}
}
/**
* Splits the body of an HTTP response into SockJS frames and delegates those
* to an {@link XhrClientSockJsSession}.
@ -204,12 +200,10 @@ public class RestTemplateXhrTransport extends AbstractXhrTransport implements Xh
private final XhrClientSockJsSession sockJsSession;
public XhrReceiveExtractor(XhrClientSockJsSession sockJsSession) {
this.sockJsSession = sockJsSession;
}
@Override
public Object extractData(ClientHttpResponse response) throws IOException {
if (!HttpStatus.OK.equals(response.getStatusCode())) {
@ -262,4 +256,3 @@ public class RestTemplateXhrTransport extends AbstractXhrTransport implements Xh
}
}