1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.ksb.impl.bus; |
17 | |
|
18 | |
import java.io.StringReader; |
19 | |
import java.io.StringWriter; |
20 | |
import java.util.HashSet; |
21 | |
import java.util.Set; |
22 | |
|
23 | |
import javax.xml.bind.JAXBContext; |
24 | |
import javax.xml.bind.JAXBException; |
25 | |
|
26 | |
import org.apache.commons.lang.StringUtils; |
27 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
28 | |
import org.kuali.rice.ksb.api.bus.ServiceConfiguration; |
29 | |
import org.kuali.rice.ksb.api.bus.support.JavaServiceConfiguration; |
30 | |
import org.kuali.rice.ksb.api.bus.support.RestServiceConfiguration; |
31 | |
import org.kuali.rice.ksb.api.bus.support.SoapServiceConfiguration; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 0 | public class ServiceConfigurationSerializationHandler { |
47 | |
|
48 | 0 | private static final Set<Class<?>> CONFIG_CLASSES_SET = new HashSet<Class<?>>(); |
49 | |
static { |
50 | 0 | CONFIG_CLASSES_SET.add(JavaServiceConfiguration.class); |
51 | 0 | CONFIG_CLASSES_SET.add(SoapServiceConfiguration.class); |
52 | 0 | CONFIG_CLASSES_SET.add(RestServiceConfiguration.class); |
53 | 0 | } |
54 | |
|
55 | |
public static String marshallToXml(ServiceConfiguration serviceConfiguration) { |
56 | 0 | if (serviceConfiguration == null) { |
57 | 0 | throw new IllegalArgumentException("serviceConfiguration was null"); |
58 | |
} |
59 | 0 | if (!CONFIG_CLASSES_SET.contains(serviceConfiguration.getClass())) { |
60 | 0 | throw new IllegalArgumentException("Illegal ServiceConfiguration class: " + serviceConfiguration.getClass()); |
61 | |
} |
62 | 0 | StringWriter writer = new StringWriter(); |
63 | |
try { |
64 | 0 | getContext().createMarshaller().marshal(serviceConfiguration, writer); |
65 | 0 | } catch (JAXBException e) { |
66 | 0 | throw new RiceRuntimeException("Failed to marshall ServiceConfiguration to XML: " + serviceConfiguration, e); |
67 | 0 | } |
68 | 0 | return writer.toString(); |
69 | |
} |
70 | |
|
71 | |
public static ServiceConfiguration unmarshallFromXml(String xml) { |
72 | 0 | if (StringUtils.isBlank(xml)) { |
73 | 0 | throw new IllegalArgumentException("xml was null or blank"); |
74 | |
} |
75 | |
try { |
76 | 0 | Object unmarshalled = getContext().createUnmarshaller().unmarshal(new StringReader(xml)); |
77 | 0 | if (!(unmarshalled instanceof ServiceConfiguration)) { |
78 | 0 | throw new RiceRuntimeException("Unmarshalled value was not a valid ServiceConfiguration: " + unmarshalled.getClass()); |
79 | |
} |
80 | 0 | return (ServiceConfiguration)unmarshalled; |
81 | 0 | } catch (JAXBException e) { |
82 | 0 | throw new RiceRuntimeException("Failed to unmarhsal ServiceConfiguration from XML: " + xml, e); |
83 | |
} |
84 | |
} |
85 | |
|
86 | |
private static JAXBContext getContext() { |
87 | 0 | return ContextHolder.context; |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | 0 | private static class ContextHolder { |
94 | 0 | private static final Class<?>[] CONFIG_CLASSES_ARRAY = CONFIG_CLASSES_SET.toArray(new Class<?>[0]); |
95 | |
static final JAXBContext context; |
96 | |
static { |
97 | |
try { |
98 | 0 | context = JAXBContext.newInstance(CONFIG_CLASSES_ARRAY); |
99 | 0 | } catch (JAXBException e) { |
100 | 0 | throw new RiceRuntimeException(e); |
101 | 0 | } |
102 | 0 | } |
103 | |
} |
104 | |
|
105 | |
} |