001/**
002 * Copyright 2010-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.common.util.properties.rice;
017
018import static org.kuali.common.util.base.Exceptions.illegalState;
019import static org.kuali.common.util.base.Precondition.checkNotNull;
020
021import java.io.File;
022import java.io.IOException;
023import java.io.InputStream;
024import java.util.Properties;
025
026import javax.xml.bind.JAXBContext;
027import javax.xml.bind.JAXBException;
028import javax.xml.bind.Unmarshaller;
029import javax.xml.bind.UnmarshallerHandler;
030import javax.xml.parsers.ParserConfigurationException;
031import javax.xml.parsers.SAXParser;
032import javax.xml.parsers.SAXParserFactory;
033
034import org.apache.commons.io.IOUtils;
035import org.kuali.common.util.LocationUtils;
036import org.xml.sax.InputSource;
037import org.xml.sax.SAXException;
038import org.xml.sax.XMLReader;
039
040public class RiceLoader {
041
042        public static Properties load(File file) {
043                checkNotNull(file, "file");
044                return load(file.getAbsolutePath());
045        }
046
047        public static Properties load(String location) {
048                checkNotNull(location, "location");
049                Config config = getConfig(location);
050                return convert(config);
051        }
052
053        public static Properties load(InputStream in) throws IOException {
054                return convert(getConfig(in));
055        }
056
057        protected static Properties convert(Config config) {
058                checkNotNull(config, "config");
059                checkNotNull(config.getParams(), "config.params");
060                Properties properties = new Properties();
061                for (Param param : config.getParams()) {
062                        String key = param.getName();
063                        String val = param.getValue();
064                        properties.setProperty(key, val);
065                }
066                return properties;
067        }
068
069        protected static Config getConfig(String location) {
070                InputStream in = null;
071                try {
072                        in = LocationUtils.getInputStream(location);
073                        return getConfig(in);
074                } catch (IOException e) {
075                        throw illegalState(e, "unexpected io error -> [%s]", location);
076                } finally {
077                        IOUtils.closeQuietly(in);
078                }
079        }
080
081        protected static Config getConfig(InputStream in) throws IOException {
082                return unmarshal(Config.class, in);
083        }
084
085        @SuppressWarnings("unchecked")
086        protected static <T> T unmarshal(Class<T> type, InputStream in) throws IOException {
087                try {
088                        JAXBContext context = JAXBContext.newInstance(type);
089                        Unmarshaller unmarshaller = context.createUnmarshaller();
090                        UnmarshallerHandler unmarshallerHandler = unmarshaller.getUnmarshallerHandler();
091                        SAXParserFactory spf = SAXParserFactory.newInstance();
092                        SAXParser sp = spf.newSAXParser();
093                        XMLReader xr = sp.getXMLReader();
094                        xr.setContentHandler(unmarshallerHandler);
095                        InputSource xmlSource = new InputSource(in);
096                        xr.parse(xmlSource);
097                        return (T) unmarshallerHandler.getResult();
098                } catch (SAXException e) {
099                        throw new IllegalStateException("Unexpected SAX error", e);
100                } catch (ParserConfigurationException e) {
101                        throw new IllegalStateException("Unexpected parser configuration error", e);
102                } catch (JAXBException e) {
103                        throw new IllegalStateException("Unexpected JAXB error", e);
104                }
105        }
106
107}