Skip to content

Commit 7ed5934

Browse files
author
Robert Sauer
committed
#371 - moving JAXBContext initialization into a static, lazily initialized field
Applying an initialization-on-demand holder pattern for lazy thread-safe JAXB context initialization.
1 parent 25aac91 commit 7ed5934

File tree

5 files changed

+95
-11
lines changed

5 files changed

+95
-11
lines changed

src/main/java/com/marklogic/client/io/QueryOptionsHandle.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ public final class QueryOptionsHandle
115115
pfactory.setNamespaceAware(true);
116116
}
117117

118-
private JAXBContext jc;
119118
private Marshaller marshaller;
120119
private QueryOptions optionsHolder;
121120
private Unmarshaller unmarshaller;
@@ -135,7 +134,7 @@ public QueryOptionsHandle() {
135134
QueryOptionsTransformExtractNS transform = new QueryOptionsTransformExtractNS();
136135
transform.setParent(reader);
137136

138-
jc = JAXBContext.newInstance(QueryOptions.class);
137+
JAXBContext jc = JaxbContextLoader.CACHED_CONTEXT;
139138
marshaller = jc.createMarshaller();
140139
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
141140
unmarshaller = jc.createUnmarshaller();
@@ -145,6 +144,8 @@ public QueryOptionsHandle() {
145144
throw new MarkLogicBindingException(e);
146145
} catch (ParserConfigurationException e) {
147146
throw new MarkLogicBindingException(e);
147+
} catch (NoClassDefFoundError ncdfe) {
148+
throw new MarkLogicBindingException(new JAXBException("JAXB context initialization failed"));
148149
}
149150
}
150151

@@ -1133,7 +1134,20 @@ public QueryOptionsHandle withConfiguration(QueryOptionsConfiguration configurat
11331134
return this;
11341135
}
11351136

1136-
1137+
/**
1138+
* Initialization-on-demand holder for {@link QueryOptionsHandle}'s JAXB context.
1139+
*/
1140+
private static class JaxbContextLoader {
1141+
1142+
private static final JAXBContext CACHED_CONTEXT;
11371143

1144+
static {
1145+
try {
1146+
CACHED_CONTEXT = JAXBContext.newInstance(QueryOptions.class);
1147+
} catch (JAXBException e) {
1148+
throw new MarkLogicBindingException(e);
1149+
}
1150+
}
1151+
}
11381152

11391153
}

src/main/java/com/marklogic/client/io/QueryOptionsListHandle.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public class QueryOptionsListHandle
4545
static final private Logger logger = LoggerFactory.getLogger(QueryOptionsListHandle.class);
4646

4747
private QueryOptionsListBuilder.OptionsList optionsHolder;
48-
private JAXBContext jc;
4948
private Marshaller marshaller;
5049
private Unmarshaller unmarshaller;
5150

@@ -56,12 +55,14 @@ public QueryOptionsListHandle() {
5655
super();
5756
super.setFormat(Format.XML);
5857
try {
59-
jc = JAXBContext.newInstance(QueryOptionsListBuilder.OptionsList.class);
58+
JAXBContext jc = JaxbContextLoader.CACHED_CONTEXT;
6059
marshaller = jc.createMarshaller();
6160
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
6261
unmarshaller = jc.createUnmarshaller();
6362
} catch (JAXBException e) {
6463
throw new MarkLogicBindingException(e);
64+
} catch (NoClassDefFoundError ncdfe) {
65+
throw new MarkLogicBindingException(new JAXBException("JAXB context initialization failed"));
6566
}
6667
}
6768

@@ -123,4 +124,20 @@ public HashMap<String, String> getValuesMap() {
123124
if (optionsHolder == null ) return null;
124125
else return optionsHolder.getOptionsMap();
125126
}
127+
128+
/**
129+
* Initialization-on-demand holder for {@link QueryOptionsListHandle}'s JAXB context.
130+
*/
131+
private static class JaxbContextLoader {
132+
133+
private static final JAXBContext CACHED_CONTEXT;
134+
135+
static {
136+
try {
137+
CACHED_CONTEXT = JAXBContext.newInstance(QueryOptionsListBuilder.OptionsList.class);
138+
} catch (JAXBException e) {
139+
throw new MarkLogicBindingException(e);
140+
}
141+
}
142+
}
126143
}

src/main/java/com/marklogic/client/io/TuplesHandle.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public class TuplesHandle
4949
static final private Logger logger = LoggerFactory.getLogger(DOMHandle.class);
5050

5151
private TuplesBuilder.Tuples tuplesHolder;
52-
private JAXBContext jc;
5352
private Unmarshaller unmarshaller;
5453

5554
private ValuesDefinition valdef = null;
@@ -60,10 +59,12 @@ public TuplesHandle() {
6059
super.setFormat(Format.XML);
6160

6261
try {
63-
jc = JAXBContext.newInstance(TuplesBuilder.Tuples.class);
62+
JAXBContext jc = JaxbContextLoader.CACHED_CONTEXT;
6463
unmarshaller = jc.createUnmarshaller();
6564
} catch (JAXBException e) {
6665
throw new MarkLogicBindingException(e);
66+
} catch (NoClassDefFoundError ncdfe) {
67+
throw new MarkLogicBindingException(new JAXBException("JAXB context initialization failed"));
6768
}
6869
}
6970

@@ -174,4 +175,21 @@ public AggregateResult getAggregate(String name) {
174175
public ValuesMetrics getMetrics() {
175176
return tuplesHolder.getMetrics();
176177
}
178+
179+
/**
180+
* Initialization-on-demand holder for {@link TuplesHandle}'s JAXB context.
181+
*/
182+
private static class JaxbContextLoader {
183+
184+
private static final JAXBContext CACHED_CONTEXT;
185+
186+
static {
187+
try {
188+
CACHED_CONTEXT = JAXBContext.newInstance(TuplesBuilder.Tuples.class);
189+
} catch (JAXBException e) {
190+
throw new MarkLogicBindingException(e);
191+
}
192+
}
193+
}
194+
177195
}

src/main/java/com/marklogic/client/io/ValuesHandle.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public class ValuesHandle
5151
static final private Logger logger = LoggerFactory.getLogger(DOMHandle.class);
5252

5353
private ValuesBuilder.Values valuesHolder;
54-
private JAXBContext jc;
5554
private Unmarshaller unmarshaller;
5655
private HashMap<String, AggregateResult> hashedAggregates = null;
5756

@@ -65,10 +64,12 @@ public ValuesHandle() {
6564
super.setFormat(Format.XML);
6665

6766
try {
68-
jc = JAXBContext.newInstance(ValuesBuilder.Values.class);
67+
JAXBContext jc = JaxbContextLoader.CACHED_CONTEXT;
6968
unmarshaller = jc.createUnmarshaller();
7069
} catch (JAXBException e) {
7170
throw new MarkLogicBindingException(e);
71+
} catch (NoClassDefFoundError ncdfe) {
72+
throw new MarkLogicBindingException(new JAXBException("JAXB context initialization failed"));
7273
}
7374
}
7475

@@ -180,4 +181,21 @@ public AggregateResult getAggregate(String name) {
180181
public ValuesMetrics getMetrics() {
181182
return valuesHolder.getMetrics();
182183
}
184+
185+
/**
186+
* Initialization-on-demand holder for {@link ValuesHandle}'s JAXB context.
187+
*/
188+
private static class JaxbContextLoader {
189+
190+
private static final JAXBContext CACHED_CONTEXT;
191+
192+
static {
193+
try {
194+
CACHED_CONTEXT = JAXBContext.newInstance(ValuesBuilder.Values.class);
195+
} catch (JAXBException e) {
196+
throw new MarkLogicBindingException(e);
197+
}
198+
}
199+
}
200+
183201
}

src/main/java/com/marklogic/client/io/ValuesListHandle.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public class ValuesListHandle
4646
static final private Logger logger = LoggerFactory.getLogger(ValuesListHandle.class);
4747

4848
private ValuesListBuilder.ValuesList valuesHolder;
49-
private JAXBContext jc;
5049
private Unmarshaller unmarshaller;
5150

5251
String optionsName = null;
@@ -59,10 +58,12 @@ public ValuesListHandle() {
5958
super.setFormat(Format.XML);
6059

6160
try {
62-
jc = JAXBContext.newInstance(ValuesListBuilder.ValuesList.class);
61+
JAXBContext jc = JaxbContextLoader.CACHED_CONTEXT;
6362
unmarshaller = jc.createUnmarshaller();
6463
} catch (JAXBException e) {
6564
throw new MarkLogicBindingException(e);
65+
} catch (NoClassDefFoundError ncdfe) {
66+
throw new MarkLogicBindingException(new JAXBException("JAXB context initialization failed"));
6667
}
6768
}
6869

@@ -149,4 +150,20 @@ protected void receiveContent(InputStream content) {
149150
public HashMap<String, String> getValuesMap() {
150151
return valuesHolder.getValuesMap();
151152
}
153+
154+
/**
155+
* Initialization-on-demand holder for {@link ValuesListHandle}'s JAXB context.
156+
*/
157+
private static class JaxbContextLoader {
158+
159+
private static final JAXBContext CACHED_CONTEXT;
160+
161+
static {
162+
try {
163+
CACHED_CONTEXT = JAXBContext.newInstance(ValuesListBuilder.ValuesList.class);
164+
} catch (JAXBException e) {
165+
throw new MarkLogicBindingException(e);
166+
}
167+
}
168+
}
152169
}

0 commit comments

Comments
 (0)