-
Notifications
You must be signed in to change notification settings - Fork 218
/
ThreadSafePropertyEditor.java
86 lines (76 loc) · 2.96 KB
/
ThreadSafePropertyEditor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Copyright (c) 2007, 2008, 2014, 2016, 2022 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
* Created on 20. September 2007 by Joerg Schaible
*/
package com.thoughtworks.xstream.core.util;
import java.beans.PropertyEditor;
import com.thoughtworks.xstream.converters.ConversionException;
import com.thoughtworks.xstream.converters.ErrorWritingException;
import com.thoughtworks.xstream.converters.reflection.ObjectAccessException;
/**
* Wrapper around {@link PropertyEditor} that can be called by multiple threads concurrently.
* <p>
* A PropertyEditor is not thread safe. To make best use of resources, the PropertyEditor provides a dynamically sizing
* pool of instances, each of which will only be called by a single thread at a time.
* </p>
* <p>
* The pool has a maximum capacity, to limit overhead. If all instances in the pool are in use and another is required,
* it shall block until one becomes available.
* </p>
*
* @author Jörg Schaible
* @since 1.3
*/
public class ThreadSafePropertyEditor {
private final Class<? extends PropertyEditor> editorType;
private final Pool<PropertyEditor> pool;
public ThreadSafePropertyEditor(
final Class<? extends PropertyEditor> type, final int initialPoolSize, final int maxPoolSize) {
if (!PropertyEditor.class.isAssignableFrom(type)) {
throw new IllegalArgumentException(type.getName() + " is not a " + PropertyEditor.class.getName());
}
editorType = type;
pool = new Pool<>(initialPoolSize, maxPoolSize, new Pool.Factory<PropertyEditor>() {
@Override
public PropertyEditor newInstance() {
ErrorWritingException ex = null;
try {
return editorType.newInstance();
} catch (final InstantiationException e) {
ex = new ConversionException("Faild to call default constructor", e);
} catch (final IllegalAccessException e) {
ex = new ObjectAccessException("Cannot call default constructor", e);
}
ex.add("construction-type", editorType.getName());
throw ex;
}
});
}
public String getAsText(final Object object) {
final PropertyEditor editor = fetchFromPool();
try {
editor.setValue(object);
return editor.getAsText();
} finally {
pool.putInPool(editor);
}
}
public Object setAsText(final String str) {
final PropertyEditor editor = fetchFromPool();
try {
editor.setAsText(str);
return editor.getValue();
} finally {
pool.putInPool(editor);
}
}
private PropertyEditor fetchFromPool() {
return pool.fetchFromPool();
}
}