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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 java.util.Properties;
import javax.cache.CacheManager; import javax.cache.CacheManager;
import javax.cache.Caching; import javax.cache.Caching;
import javax.cache.spi.CachingProvider;
import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.DisposableBean;
@ -75,8 +74,7 @@ public class JCacheManagerFactoryBean
@Override @Override
public void afterPropertiesSet() { public void afterPropertiesSet() {
CachingProvider provider = Caching.getCachingProvider(); this.cacheManager = Caching.getCachingProvider().getCacheManager(
this.cacheManager = provider.getCacheManager(
this.cacheManagerUri, this.beanClassLoader, this.cacheManagerProperties); 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); private Set<String> cacheNames = new LinkedHashSet<String>(16);
// Early cache initialization on startup
@Override @Override
public void afterPropertiesSet() { public void afterPropertiesSet() {
Collection<? extends Cache> caches = loadCaches(); 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.cacheMap.clear();
this.cacheNames.clear(); this.cacheNames.clear();
for (Cache cache : caches) { for (Cache cache : caches) {
this.cacheMap.put(cache.getName(), decorateCache(cache)); addCache(cache);
this.cacheNames.add(cache.getName());
} }
} }
/**
* 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) { protected final void addCache(Cache cache) {
this.cacheMap.put(cache.getName(), decorateCache(cache)); this.cacheMap.put(cache.getName(), decorateCache(cache));
this.cacheNames.add(cache.getName()); this.cacheNames.add(cache.getName());
} }
protected final Cache lookupCache(String name) {
return this.cacheMap.get(name);
}
/** /**
* Decorate the given Cache object if necessary. * Decorate the given Cache object if necessary.
* @param cache the Cache object to be added to this CacheManager * @param cache the Cache object to be added to this CacheManager
@ -87,35 +126,4 @@ public abstract class AbstractCacheManager implements CacheManager, Initializing
return null; 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 * @author Sebastien Deleuze
* @since 4.1 * @since 4.1
*/ */
public abstract class AbstractJackson2HttpMessageConverter extends public abstract class AbstractJackson2HttpMessageConverter extends AbstractHttpMessageConverter<Object>
AbstractHttpMessageConverter<Object> implements GenericHttpMessageConverter<Object> { implements GenericHttpMessageConverter<Object> {
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
@ -82,6 +82,7 @@ public abstract class AbstractJackson2HttpMessageConverter extends
this.objectMapper = objectMapper; this.objectMapper = objectMapper;
} }
/** /**
* Set the {@code ObjectMapper} for this view. * Set the {@code ObjectMapper} for this view.
* If not set, a default {@link ObjectMapper#ObjectMapper() ObjectMapper} is used. * If not set, a default {@link ObjectMapper#ObjectMapper() ObjectMapper} is used.
@ -126,6 +127,7 @@ public abstract class AbstractJackson2HttpMessageConverter extends
} }
} }
@Override @Override
public boolean canRead(Class<?> clazz, MediaType mediaType) { public boolean canRead(Class<?> clazz, MediaType mediaType) {
return canRead(clazz, null, mediaType); return canRead(clazz, null, mediaType);

View File

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

View File

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

View File

@ -16,6 +16,11 @@
package org.springframework.web.socket.sockjs.client; 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.SimpleAsyncTaskExecutor;
import org.springframework.core.task.TaskExecutor; import org.springframework.core.task.TaskExecutor;
import org.springframework.http.HttpHeaders; 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.WebSocketSession;
import org.springframework.web.socket.sockjs.frame.SockJsFrame; 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 * An {@code XhrTransport} implementation that uses a
* {@link org.springframework.web.client.RestTemplate RestTemplate}. * {@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. * Configure the {@code TaskExecutor} to use to execute XHR receive requests.
*
* <p>By default {@link org.springframework.core.task.SimpleAsyncTaskExecutor * <p>By default {@link org.springframework.core.task.SimpleAsyncTaskExecutor
* SimpleAsyncTaskExecutor} is configured which creates a new thread every * SimpleAsyncTaskExecutor} is configured which creates a new thread every
* time the transports connects. * time the transports connects.
*
* @param taskExecutor the task executor, cannot be {@code null}
*/ */
public void setTaskExecutor(TaskExecutor taskExecutor) { public void setTaskExecutor(TaskExecutor taskExecutor) {
Assert.notNull(this.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. * A simple ResponseExtractor that reads the body into a String.
*/ */
private final static ResponseExtractor<ResponseEntity<String>> textExtractor = private final static ResponseExtractor<ResponseEntity<String>> textExtractor =
new ResponseExtractor<ResponseEntity<String>>() { new ResponseExtractor<ResponseEntity<String>>() {
@Override @Override
public ResponseEntity<String> extractData(ClientHttpResponse response) throws IOException { public ResponseEntity<String> extractData(ClientHttpResponse response) throws IOException {
if (response.getBody() == null) { 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 * Splits the body of an HTTP response into SockJS frames and delegates those
* to an {@link XhrClientSockJsSession}. * to an {@link XhrClientSockJsSession}.
@ -204,12 +200,10 @@ public class RestTemplateXhrTransport extends AbstractXhrTransport implements Xh
private final XhrClientSockJsSession sockJsSession; private final XhrClientSockJsSession sockJsSession;
public XhrReceiveExtractor(XhrClientSockJsSession sockJsSession) { public XhrReceiveExtractor(XhrClientSockJsSession sockJsSession) {
this.sockJsSession = sockJsSession; this.sockJsSession = sockJsSession;
} }
@Override @Override
public Object extractData(ClientHttpResponse response) throws IOException { public Object extractData(ClientHttpResponse response) throws IOException {
if (!HttpStatus.OK.equals(response.getStatusCode())) { if (!HttpStatus.OK.equals(response.getStatusCode())) {
@ -262,4 +256,3 @@ public class RestTemplateXhrTransport extends AbstractXhrTransport implements Xh
} }
} }