|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectuk.org.retep.util.concurrent.ReadWriteConcurrencySupport
uk.org.retep.util.concurrent.ConcurrencySupport
uk.org.retep.util.xml.JAXBUtil
@ThreadSafe public class JAXBUtil
A suite of utility methods that make using JAXB a lot easier.
After creating an instance it allows you to add either the packages or a single class from each package that has been generated by xjc. This allows you to select what you want JAXB to be able to marshall/unmarshall.
Once you have selected everything you want, you can then use the getMashaller() and getUnmarshaller() methods to obtain the Marshaller/Unmarshaller's that operate over those packages.
In addition, due to the creation of the Marshaller/Unmarshaller's are an expensive operation, the class provides a concurrent pool of each, so you can return them back into the pool once you have finished them - really useful in a high load environment.
The MarshallerCallBack and UnmarshallerCallBack interfaces allow you to do additional configuration on Marshaller/Unmarshaller's when they are created.
Finally if you are using just the marshall/unmarshall methods, the class also provides those methods which will use the pool, removing the need to handle the get/release process from your code.
| Nested Class Summary | |
|---|---|
static interface |
JAXBUtil.MarshallerCallback
A callback called when a Marshaller is first created allowing the application to perform additional configuration on the Marshaller before it is first used |
static interface |
JAXBUtil.UnmarshallerCallback
A callback called when a Unmarshaller is first created allowing the application to perform additional configuration on the Unmarshaller before it is first used |
| Constructor Summary | |
|---|---|
JAXBUtil()
Creates a new instance of JAXBUtil with no initial packages |
|
JAXBUtil(ArrayList<String> packages)
Creates a new instance of JAXBUtil with packages added |
|
JAXBUtil(String... packages)
Creates a new instance of JAXBUtil with packages added |
|
| Method Summary | ||
|---|---|---|
void |
addPackage(Class<?> clazz)
Add the package containing the given class to this instance |
|
void |
addPackage(Package... packages)
Add the Package to this instance |
|
void |
addPackage(String... pkg)
Add one or more packages to this instance |
|
void |
addPackage(String pkg)
Add one or more ':' delimited package names to this instance |
|
void |
addPackages(Class<?> clazz)
Add all packages below this one to this instance |
|
void |
addPackages(Package pkg)
Add all packages below this one to this instance |
|
void |
addSubPackages(String name)
Add all subpackages to this instance |
|
void |
clear()
Releases the JAXBContext and clears both the Marshaller and Unmarshaller pools. |
|
void |
ensureMarshallerPoolSize(int size)
Ensure that the Marshaller pool contains this number of entries. |
|
void |
ensureUnmarshallerPoolSize(int size)
Ensure that the Unmarshaller pool contains this number of entries. |
|
JAXBContext |
getJAXBContext()
The current JAXBContext in use. |
|
static
|
getJAXBElement(Object o)
Utiity to cast an object to JAXBElement<T> |
|
Marshaller |
getMarshaller()
Get a Marshaller from the pool, creating one as necessary. |
|
JAXBUtil.MarshallerCallback |
getMarshallerCallback()
The callback currently used by Marshaller's, null if none |
|
Unmarshaller |
getUnmarshaller()
Get an Unmarshaller from the pool, creating one as necessary. |
|
JAXBUtil.UnmarshallerCallback |
getUnmarshallerCallback()
The callback currently used by Unmarshaller's, null if none |
|
String |
marshall(Object object)
Marshall an object into a String |
|
void |
marshall(Object object,
ContentHandler out)
Marshal an object |
|
void |
marshall(Object object,
File out)
Marshal an object |
|
void |
marshall(Object object,
Node out)
Marshal an object |
|
void |
marshall(Object object,
OutputStream out)
Marshal an object |
|
void |
marshall(Object object,
Result out)
Marshal an object |
|
void |
marshall(Object object,
Writer out)
Marshal an object |
|
void |
marshall(Object object,
XMLEventWriter out)
Marshal an object |
|
void |
marshall(Object object,
XMLStreamWriter out)
Marshal an object |
|
boolean |
release(Marshaller marshaller)
Releases a Marshaller back to the pool. |
|
boolean |
release(Unmarshaller unmarshaller)
Releases a Unmarshaller back to the pool. |
|
void |
removePackage(Class<?> clazz)
Remove the package containing this class |
|
void |
removePackage(Package pkg)
Remove package |
|
void |
removePackage(String... pkg)
Remove packages |
|
void |
removePackage(String pkg)
Remove package |
|
void |
removePackages(Class<?> clazz)
Remove the package containing this class and all sub-packages |
|
void |
removePackages(String name)
Remove the package and all packages within it |
|
void |
removeSubPackages(Package pkg)
Remove the package and all packages within it |
|
void |
setClassLoader(ClassLoader classLoader)
Set the ClassLoader that JAXB will use. |
|
void |
setMarshallerCallback(JAXBUtil.MarshallerCallback marshallerCallback)
Set the MarshallerCallback being used to configure Marshaller's, null if none |
|
void |
setPoolSize(int poolSize)
Sets the size of the marshaller and unmarshaller pools. |
|
void |
setUnmarshallerCallback(JAXBUtil.UnmarshallerCallback unmarshallerCallback)
Set the UnmarshallerCallback being used to configure Unmarshaller's, null if none |
|
|
unmarshall(File in)
Unmarshal from a source |
|
|
unmarshall(InputSource in)
Unmarshal from a source |
|
|
unmarshall(InputStream in)
Unmarshal from a source |
|
|
unmarshall(Node in)
Unmarshal from a source |
|
|
unmarshall(Reader in)
Unmarshal from a source |
|
|
unmarshall(Source in)
Unmarshal from a source |
|
|
unmarshall(String in)
Unmarshal from a source |
|
|
unmarshall(URL in)
Unmarshal from a source |
|
|
unmarshall(XMLEventReader in)
Unmarshal from a source |
|
|
unmarshall(XMLStreamReader in)
Unmarshal from a source |
|
| Methods inherited from class uk.org.retep.util.concurrent.ConcurrencySupport |
|---|
getLog |
| Methods inherited from class uk.org.retep.util.concurrent.ReadWriteConcurrencySupport |
|---|
readLock, writeLock |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public JAXBUtil()
public JAXBUtil(String... packages)
packages - packages to usepublic JAXBUtil(ArrayList<String> packages)
packages - packages to use| Method Detail |
|---|
public void setClassLoader(ClassLoader classLoader)
classLoader - ClassLoader to use instead of the caller's ClassLoader@WriteLock public void clear()
The various add and remove package methods call this method internally. It is public to enable the the pool's to be reset at any moment
@WriteLock public void addPackage(String... pkg)
pkg - One or more package names@WriteLock public void addPackage(String pkg)
pkg - list of packages delimited by ':'public void addPackage(Package... packages)
packages - Package(s) to addpublic void addPackage(Class<?> clazz)
clazz - Class who's package is to be addedpublic void addPackages(Class<?> clazz)
clazz - Class who's package and subpackages are to be added@WriteLock public void addPackages(Package pkg)
pkg - Package and it's subpackages are to be addedpublic void addSubPackages(String name)
name - Package and subpackages are to be added@WriteLock public void removePackage(String... pkg)
pkg - @WriteLock public void removePackage(String pkg)
pkg - public void removePackage(Package pkg)
pkg - Package to removepublic void removePackage(Class<?> clazz)
clazz - Class who's package to removepublic void removePackages(Class<?> clazz)
clazz - Class who's package and subpackages are to be removed@WriteLock public void removeSubPackages(Package pkg)
pkg - Package to removepublic void removePackages(String name)
name - Package to remove and all subpackages
@WriteLock
@Unsafe
public JAXBContext getJAXBContext()
throws JAXBException
Unless specifically necessary it is advised you do not use this method as the returned value can change during the lifetime of the JAXBUtil instance.
JAXBException
public void ensureMarshallerPoolSize(int size)
throws JAXBException
size - The number of entries required
JAXBException
IllegalArgumentException - if size < 1
public void ensureUnmarshallerPoolSize(int size)
throws JAXBException
size - The number of entries required
JAXBException
IllegalArgumentException - if size < 1@WriteLock public void setPoolSize(int poolSize)
By default, this is set to 0 (unlimited), but if this is set with a value > 0, then the pool's are limited to that number of free entries.
When release() is called and the pool is full, then that Marshaller or Unmarshaller is made available to the garbage collector.
When called, this will lock the pools for the duration of the call, as it resizes the existing pools. If the value is less than the current size but not 0, then the excess entries are made available to the garbage collector.
poolSize - The size of each pool, 0 for unlimited.
public Marshaller getMarshaller()
throws JAXBException
Marshaller marshaller = jaxbUtils.getMarshaller();
try {
// use the Marshaller here
} finally {
jaxbUtils.release( marshaller );
}
JAXBException
public Unmarshaller getUnmarshaller()
throws JAXBException
Unmarshaller unmarshaller = jaxbUtils.getUnmarshaller();
try {
// use the Marshaller here
} finally {
jaxbUtils.release( unmarshaller );
}
JAXBException@WriteLock public boolean release(Marshaller marshaller)
Once this method has been called the caller must not use that instance again as it may be reused by another thread.
marshaller - Marshaller to return to the pool
@WriteLock public boolean release(Unmarshaller unmarshaller)
Once this method has been called the caller must not use that instance again as it may be reused by another thread.
unmarshaller - Unmarshaller to return to the pool
public static <T> JAXBElement<T> getJAXBElement(Object o)
T - Type of the JAXBElemento - Object to cast
@ReadLock public JAXBUtil.MarshallerCallback getMarshallerCallback()
@WriteLock public void setMarshallerCallback(JAXBUtil.MarshallerCallback marshallerCallback)
marshallerCallback - @ReadLock public JAXBUtil.UnmarshallerCallback getUnmarshallerCallback()
@WriteLock public void setUnmarshallerCallback(JAXBUtil.UnmarshallerCallback unmarshallerCallback)
unmarshallerCallback -
public void marshall(Object object,
ContentHandler out)
throws JAXBException
object - Object to marshallout - destination of object
JAXBException
public void marshall(Object object,
File out)
throws JAXBException
object - Object to marshallout - destination of object
JAXBException
public void marshall(Object object,
Node out)
throws JAXBException
object - Object to marshallout - destination of object
JAXBException
public void marshall(Object object,
OutputStream out)
throws JAXBException
object - Object to marshallout - destination of object
JAXBException
public void marshall(Object object,
Result out)
throws JAXBException
object - Object to marshallout - destination of object
JAXBException
public void marshall(Object object,
Writer out)
throws JAXBException
object - Object to marshallout - destination of object
JAXBException
public void marshall(Object object,
XMLEventWriter out)
throws JAXBException
object - Object to marshallout - destination of object
JAXBException
public void marshall(Object object,
XMLStreamWriter out)
throws JAXBException
object - Object to marshallout - destination of object
JAXBException
public String marshall(Object object)
throws JAXBException
object - Object to marshall
JAXBException
public <T> T unmarshall(File in)
throws JAXBException
T - Type of required Objectin - source
JAXBException
public <T> T unmarshall(InputSource in)
throws JAXBException
T - Type of required Objectin - source
JAXBException
public <T> T unmarshall(InputStream in)
throws JAXBException
T - Type of required Objectin - source
JAXBException
public <T> T unmarshall(Node in)
throws JAXBException
T - Type of required Objectin - source
JAXBException
public <T> T unmarshall(Reader in)
throws JAXBException
T - Type of required Objectin - source
JAXBException
public <T> T unmarshall(Source in)
throws JAXBException
T - Type of required Objectin - source
JAXBException
public <T> T unmarshall(URL in)
throws JAXBException
T - Type of required Objectin - source
JAXBException
public <T> T unmarshall(XMLEventReader in)
throws JAXBException
T - Type of required Objectin - source
JAXBException
public <T> T unmarshall(XMLStreamReader in)
throws JAXBException
T - Type of required Objectin - source
JAXBException
public <T> T unmarshall(String in)
throws JAXBException
T - Type of required Objectin - source
JAXBException
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||