Coverage Report - org.kuali.rice.ksb.impl.bus.ServiceConfigurationSerializationHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
ServiceConfigurationSerializationHandler
0%
0/26
0%
0/8
5.333
ServiceConfigurationSerializationHandler$ContextHolder
0%
0/7
N/A
5.333
 
 1  
 /**
 2  
  * Copyright 2005-2011 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  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  
          * Implements the lazy initialization holder class idiom as per Effective Java item 71.
 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  
 }