1 package org.kuali.common.util.properties.rice; 2 3 import static org.kuali.common.util.base.Exceptions.illegalState; 4 import static org.kuali.common.util.base.Precondition.checkNotNull; 5 6 import java.io.File; 7 import java.io.IOException; 8 import java.io.InputStream; 9 import java.util.Properties; 10 11 import javax.xml.bind.JAXBContext; 12 import javax.xml.bind.JAXBException; 13 import javax.xml.bind.Unmarshaller; 14 import javax.xml.bind.UnmarshallerHandler; 15 import javax.xml.parsers.ParserConfigurationException; 16 import javax.xml.parsers.SAXParser; 17 import javax.xml.parsers.SAXParserFactory; 18 19 import org.apache.commons.io.IOUtils; 20 import org.kuali.common.util.LocationUtils; 21 import org.xml.sax.InputSource; 22 import org.xml.sax.SAXException; 23 import org.xml.sax.XMLReader; 24 25 public class RiceLoader { 26 27 public static Properties load(File file) { 28 checkNotNull(file, "file"); 29 return load(file.getAbsolutePath()); 30 } 31 32 public static Properties load(String location) { 33 checkNotNull(location, "location"); 34 Config config = getConfig(location); 35 return convert(config); 36 } 37 38 public static Properties load(InputStream in) throws IOException { 39 return convert(getConfig(in)); 40 } 41 42 protected static Properties convert(Config config) { 43 checkNotNull(config, "config"); 44 checkNotNull(config.getParams(), "config.params"); 45 Properties properties = new Properties(); 46 for (Param param : config.getParams()) { 47 String key = param.getName(); 48 String val = param.getValue(); 49 properties.setProperty(key, val); 50 } 51 return properties; 52 } 53 54 protected static Config getConfig(String location) { 55 InputStream in = null; 56 try { 57 in = LocationUtils.getInputStream(location); 58 return getConfig(in); 59 } catch (IOException e) { 60 throw illegalState(e, "unexpected io error -> [%s]", location); 61 } finally { 62 IOUtils.closeQuietly(in); 63 } 64 } 65 66 protected static Config getConfig(InputStream in) throws IOException { 67 return unmarshal(Config.class, in); 68 } 69 70 @SuppressWarnings("unchecked") 71 protected static <T> T unmarshal(Class<T> type, InputStream in) throws IOException { 72 try { 73 JAXBContext context = JAXBContext.newInstance(type); 74 Unmarshaller unmarshaller = context.createUnmarshaller(); 75 UnmarshallerHandler unmarshallerHandler = unmarshaller.getUnmarshallerHandler(); 76 SAXParserFactory spf = SAXParserFactory.newInstance(); 77 SAXParser sp = spf.newSAXParser(); 78 XMLReader xr = sp.getXMLReader(); 79 xr.setContentHandler(unmarshallerHandler); 80 InputSource xmlSource = new InputSource(in); 81 xr.parse(xmlSource); 82 return (T) unmarshallerHandler.getResult(); 83 } catch (SAXException e) { 84 throw new IllegalStateException("Unexpected SAX error", e); 85 } catch (ParserConfigurationException e) { 86 throw new IllegalStateException("Unexpected parser configuration error", e); 87 } catch (JAXBException e) { 88 throw new IllegalStateException("Unexpected JAXB error", e); 89 } 90 } 91 92 }