Skip to content

Commit 2ef3d66

Browse files
committed
Polishing
1 parent 0d0d713 commit 2ef3d66

File tree

6 files changed

+81
-84
lines changed

6 files changed

+81
-84
lines changed

spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheManagerFactoryBean.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,6 @@
2020
import java.util.Properties;
2121
import javax.cache.CacheManager;
2222
import javax.cache.Caching;
23-
import javax.cache.spi.CachingProvider;
2423

2524
import org.springframework.beans.factory.BeanClassLoaderAware;
2625
import org.springframework.beans.factory.DisposableBean;
@@ -75,8 +74,7 @@ public void setBeanClassLoader(ClassLoader classLoader) {
7574

7675
@Override
7776
public void afterPropertiesSet() {
78-
CachingProvider provider = Caching.getCachingProvider();
79-
this.cacheManager = provider.getCacheManager(
77+
this.cacheManager = Caching.getCachingProvider().getCacheManager(
8078
this.cacheManagerUri, this.beanClassLoader, this.cacheManagerProperties);
8179
}
8280

spring-context/src/main/java/org/springframework/cache/support/AbstractCacheManager.java

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,63 @@ public abstract class AbstractCacheManager implements CacheManager, Initializing
4343
private Set<String> cacheNames = new LinkedHashSet<String>(16);
4444

4545

46+
// Early cache initialization on startup
47+
4648
@Override
4749
public void afterPropertiesSet() {
4850
Collection<? extends Cache> caches = loadCaches();
4951

50-
// preserve the initial order of the cache names
52+
// Preserve the initial order of the cache names
5153
this.cacheMap.clear();
5254
this.cacheNames.clear();
5355
for (Cache cache : caches) {
54-
this.cacheMap.put(cache.getName(), decorateCache(cache));
55-
this.cacheNames.add(cache.getName());
56+
addCache(cache);
57+
}
58+
}
59+
60+
/**
61+
* Load the initial caches for this cache manager.
62+
* <p>Called by {@link #afterPropertiesSet()} on startup.
63+
* The returned collection may be empty but must not be {@code null}.
64+
*/
65+
protected abstract Collection<? extends Cache> loadCaches();
66+
67+
68+
// Lazy cache initialization on access
69+
70+
@Override
71+
public Cache getCache(String name) {
72+
Cache cache = lookupCache(name);
73+
if (cache != null) {
74+
return cache;
75+
}
76+
else {
77+
Cache missingCache = getMissingCache(name);
78+
if (missingCache != null) {
79+
addCache(missingCache);
80+
return lookupCache(name); // may be decorated
81+
}
82+
return null;
5683
}
5784
}
5885

86+
@Override
87+
public Collection<String> getCacheNames() {
88+
return Collections.unmodifiableSet(this.cacheNames);
89+
}
90+
91+
92+
// Common cache initialization delegates/callbacks
93+
5994
protected final void addCache(Cache cache) {
6095
this.cacheMap.put(cache.getName(), decorateCache(cache));
6196
this.cacheNames.add(cache.getName());
6297
}
6398

99+
protected final Cache lookupCache(String name) {
100+
return this.cacheMap.get(name);
101+
}
102+
64103
/**
65104
* Decorate the given Cache object if necessary.
66105
* @param cache the Cache object to be added to this CacheManager
@@ -87,35 +126,4 @@ protected Cache getMissingCache(String name) {
87126
return null;
88127
}
89128

90-
@Override
91-
public Cache getCache(String name) {
92-
Cache cache = lookupCache(name);
93-
if (cache != null) {
94-
return cache;
95-
}
96-
else {
97-
Cache missingCache = getMissingCache(name);
98-
if (missingCache != null) {
99-
addCache(missingCache);
100-
return lookupCache(name); // May be decorated
101-
}
102-
return null;
103-
}
104-
}
105-
106-
@Override
107-
public Collection<String> getCacheNames() {
108-
return Collections.unmodifiableSet(this.cacheNames);
109-
}
110-
111-
private Cache lookupCache(String name) {
112-
return this.cacheMap.get(name);
113-
}
114-
115-
/**
116-
* Load the caches for this cache manager. Occurs at startup.
117-
* The returned collection must not be null.
118-
*/
119-
protected abstract Collection<? extends Cache> loadCaches();
120-
121129
}

spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@
5353
* @author Sebastien Deleuze
5454
* @since 4.1
5555
*/
56-
public abstract class AbstractJackson2HttpMessageConverter extends
57-
AbstractHttpMessageConverter<Object> implements GenericHttpMessageConverter<Object> {
56+
public abstract class AbstractJackson2HttpMessageConverter extends AbstractHttpMessageConverter<Object>
57+
implements GenericHttpMessageConverter<Object> {
5858

5959
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
6060

@@ -82,6 +82,7 @@ protected AbstractJackson2HttpMessageConverter(ObjectMapper objectMapper, MediaT
8282
this.objectMapper = objectMapper;
8383
}
8484

85+
8586
/**
8687
* Set the {@code ObjectMapper} for this view.
8788
* If not set, a default {@link ObjectMapper#ObjectMapper() ObjectMapper} is used.
@@ -126,6 +127,7 @@ private void configurePrettyPrint() {
126127
}
127128
}
128129

130+
129131
@Override
130132
public boolean canRead(Class<?> clazz, MediaType mediaType) {
131133
return canRead(clazz, null, mediaType);

spring-web/src/main/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public MappingJackson2HttpMessageConverter() {
5454
new MediaType("application", "*+json", DEFAULT_CHARSET));
5555
}
5656

57+
5758
/**
5859
* Specify a custom prefix to use for this view's JSON output.
5960
* Default is none.
@@ -75,26 +76,23 @@ public void setPrefixJson(boolean prefixJson) {
7576
this.jsonPrefix = (prefixJson ? "{} && " : null);
7677
}
7778

79+
7880
@Override
7981
protected void writePrefix(JsonGenerator generator, Object object) throws IOException {
8082
if (this.jsonPrefix != null) {
8183
generator.writeRaw(this.jsonPrefix);
8284
}
83-
String jsonpFunction = null;
84-
if (object instanceof MappingJacksonValue) {
85-
jsonpFunction = ((MappingJacksonValue)object).getJsonpFunction();
86-
}
85+
String jsonpFunction =
86+
(object instanceof MappingJacksonValue ? ((MappingJacksonValue) object).getJsonpFunction() : null);
8787
if (jsonpFunction != null) {
8888
generator.writeRaw(jsonpFunction + "(");
8989
}
9090
}
9191

9292
@Override
9393
protected void writeSuffix(JsonGenerator generator, Object object) throws IOException {
94-
String jsonpFunction = null;
95-
if (object instanceof MappingJacksonValue) {
96-
jsonpFunction = ((MappingJacksonValue)object).getJsonpFunction();
97-
}
94+
String jsonpFunction =
95+
(object instanceof MappingJacksonValue ? ((MappingJacksonValue) object).getJsonpFunction() : null);
9896
if (jsonpFunction != null) {
9997
generator.writeRaw(");");
10098
}

spring-web/src/main/java/org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverter.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,19 @@
2020

2121
import org.springframework.http.MediaType;
2222
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
23-
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
2423

2524
/**
26-
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverter} that
27-
* can read and write XML using <a href="https://github.com/FasterXML/jackson-dataformat-xml">Jackson 2.x extension component for
28-
* reading and writing XML encoded data</a>.
25+
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverter}
26+
* that can read and write XML using <a href="https://github.com/FasterXML/jackson-dataformat-xml">
27+
* Jackson 2.x extension component for reading and writing XML encoded data</a>.
2928
*
3029
* @author Sebastien Deleuze
3130
* @since 4.1
3231
*/
3332
public class MappingJackson2XmlHttpMessageConverter extends AbstractJackson2HttpMessageConverter {
3433

3534
public MappingJackson2XmlHttpMessageConverter() {
36-
super(new XmlMapper(),
37-
new MediaType("application", "xml", MappingJackson2HttpMessageConverter.DEFAULT_CHARSET));
35+
super(new XmlMapper(), new MediaType("application", "xml", DEFAULT_CHARSET));
3836
}
3937

4038
}

spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/RestTemplateXhrTransport.java

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
package org.springframework.web.socket.sockjs.client;
1818

19+
import java.io.ByteArrayOutputStream;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.net.URI;
23+
1924
import org.springframework.core.task.SimpleAsyncTaskExecutor;
2025
import org.springframework.core.task.TaskExecutor;
2126
import org.springframework.http.HttpHeaders;
@@ -38,11 +43,6 @@
3843
import org.springframework.web.socket.WebSocketSession;
3944
import org.springframework.web.socket.sockjs.frame.SockJsFrame;
4045

41-
import java.io.ByteArrayOutputStream;
42-
import java.io.IOException;
43-
import java.io.InputStream;
44-
import java.net.URI;
45-
4646
/**
4747
* An {@code XhrTransport} implementation that uses a
4848
* {@link org.springframework.web.client.RestTemplate RestTemplate}.
@@ -76,12 +76,9 @@ public RestOperations getRestTemplate() {
7676

7777
/**
7878
* Configure the {@code TaskExecutor} to use to execute XHR receive requests.
79-
*
8079
* <p>By default {@link org.springframework.core.task.SimpleAsyncTaskExecutor
8180
* SimpleAsyncTaskExecutor} is configured which creates a new thread every
8281
* time the transports connects.
83-
*
84-
* @param taskExecutor the task executor, cannot be {@code null}
8582
*/
8683
public void setTaskExecutor(TaskExecutor taskExecutor) {
8784
Assert.notNull(this.taskExecutor);
@@ -147,6 +144,24 @@ public void run() {
147144
}
148145

149146

147+
/**
148+
* A simple ResponseExtractor that reads the body into a String.
149+
*/
150+
private final static ResponseExtractor<ResponseEntity<String>> textExtractor =
151+
new ResponseExtractor<ResponseEntity<String>>() {
152+
@Override
153+
public ResponseEntity<String> extractData(ClientHttpResponse response) throws IOException {
154+
if (response.getBody() == null) {
155+
return new ResponseEntity<String>(response.getHeaders(), response.getStatusCode());
156+
}
157+
else {
158+
String body = StreamUtils.copyToString(response.getBody(), SockJsFrame.CHARSET);
159+
return new ResponseEntity<String>(body, response.getHeaders(), response.getStatusCode());
160+
}
161+
}
162+
};
163+
164+
150165
/**
151166
* A RequestCallback to add the headers and (optionally) String content.
152167
*/
@@ -156,7 +171,6 @@ private static class XhrRequestCallback implements RequestCallback {
156171

157172
private final String body;
158173

159-
160174
public XhrRequestCallback(HttpHeaders headers) {
161175
this(headers, null);
162176
}
@@ -166,7 +180,6 @@ public XhrRequestCallback(HttpHeaders headers, String body) {
166180
this.body = body;
167181
}
168182

169-
170183
@Override
171184
public void doWithRequest(ClientHttpRequest request) throws IOException {
172185
if (this.headers != null) {
@@ -178,23 +191,6 @@ public void doWithRequest(ClientHttpRequest request) throws IOException {
178191
}
179192
}
180193

181-
/**
182-
* A simple ResponseExtractor that reads the body into a String.
183-
*/
184-
private final static ResponseExtractor<ResponseEntity<String>> textExtractor =
185-
new ResponseExtractor<ResponseEntity<String>>() {
186-
187-
@Override
188-
public ResponseEntity<String> extractData(ClientHttpResponse response) throws IOException {
189-
if (response.getBody() == null) {
190-
return new ResponseEntity<String>(response.getHeaders(), response.getStatusCode());
191-
}
192-
else {
193-
String body = StreamUtils.copyToString(response.getBody(), SockJsFrame.CHARSET);
194-
return new ResponseEntity<String>(body, response.getHeaders(), response.getStatusCode());
195-
}
196-
}
197-
};
198194

199195
/**
200196
* Splits the body of an HTTP response into SockJS frames and delegates those
@@ -204,12 +200,10 @@ private class XhrReceiveExtractor implements ResponseExtractor<Object> {
204200

205201
private final XhrClientSockJsSession sockJsSession;
206202

207-
208203
public XhrReceiveExtractor(XhrClientSockJsSession sockJsSession) {
209204
this.sockJsSession = sockJsSession;
210205
}
211206

212-
213207
@Override
214208
public Object extractData(ClientHttpResponse response) throws IOException {
215209
if (!HttpStatus.OK.equals(response.getStatusCode())) {
@@ -262,4 +256,3 @@ private void handleFrame(ByteArrayOutputStream os) {
262256
}
263257

264258
}
265-

0 commit comments

Comments
 (0)