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 public class ServiceConfigurationSerializationHandler {
47
48 private static final Set<Class<?>> CONFIG_CLASSES_SET = new HashSet<Class<?>>();
49 static {
50 CONFIG_CLASSES_SET.add(JavaServiceConfiguration.class);
51 CONFIG_CLASSES_SET.add(SoapServiceConfiguration.class);
52 CONFIG_CLASSES_SET.add(RestServiceConfiguration.class);
53 }
54
55 public static String marshallToXml(ServiceConfiguration serviceConfiguration) {
56 if (serviceConfiguration == null) {
57 throw new IllegalArgumentException("serviceConfiguration was null");
58 }
59 if (!CONFIG_CLASSES_SET.contains(serviceConfiguration.getClass())) {
60 throw new IllegalArgumentException("Illegal ServiceConfiguration class: " + serviceConfiguration.getClass());
61 }
62 StringWriter writer = new StringWriter();
63 try {
64 getContext().createMarshaller().marshal(serviceConfiguration, writer);
65 } catch (JAXBException e) {
66 throw new RiceRuntimeException("Failed to marshall ServiceConfiguration to XML: " + serviceConfiguration, e);
67 }
68 return writer.toString();
69 }
70
71 public static ServiceConfiguration unmarshallFromXml(String xml) {
72 if (StringUtils.isBlank(xml)) {
73 throw new IllegalArgumentException("xml was null or blank");
74 }
75 try {
76 Object unmarshalled = getContext().createUnmarshaller().unmarshal(new StringReader(xml));
77 if (!(unmarshalled instanceof ServiceConfiguration)) {
78 throw new RiceRuntimeException("Unmarshalled value was not a valid ServiceConfiguration: " + unmarshalled.getClass());
79 }
80 return (ServiceConfiguration)unmarshalled;
81 } catch (JAXBException e) {
82 throw new RiceRuntimeException("Failed to unmarhsal ServiceConfiguration from XML: " + xml, e);
83 }
84 }
85
86 private static JAXBContext getContext() {
87 return ContextHolder.context;
88 }
89
90
91
92
93 private static class ContextHolder {
94 private static final Class<?>[] CONFIG_CLASSES_ARRAY = CONFIG_CLASSES_SET.toArray(new Class<?>[0]);
95 static final JAXBContext context;
96 static {
97 try {
98 context = JAXBContext.newInstance(CONFIG_CLASSES_ARRAY);
99 } catch (JAXBException e) {
100 throw new RiceRuntimeException(e);
101 }
102 }
103 }
104
105 }