View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * This class is meant to be a temporary implementation of serialization operations
35   * for {@link ServiceConfiguration} classes.  Coming into Rice 2.0, there is not a
36   * "pluggable" spi for dealing with custom service configurations, definitions,
37   * connectors, and exporters.  So for now everything is hardcoded and this
38   * class creates a hardcoded JAXBContext to perform marshalling/unmarshalling
39   * of the standard {@link ServiceConfiguration} classes.
40   * 
41   * <p>NOTE: JAXBContext is thread-safe, but marshaller/unmarshaller are *NOT* thread-safe.
42   * 
43   * @author Kuali Rice Team (rice.collab@kuali.org)
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  	 * Implements the lazy initialization holder class idiom as per Effective Java item 71.
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 }