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;
017
018import java.util.Properties;
019
020import org.kuali.common.util.Assert;
021import org.kuali.common.util.LocationUtils;
022import org.kuali.common.util.ModeUtils;
023import org.kuali.common.util.PropertyUtils;
024
025public final class LocationLoader implements PropertiesLoader {
026
027        private final String value;
028        private final Location location;
029
030        public LocationLoader(Location location) {
031                this(location, location.getValue());
032        }
033
034        public LocationLoader(Location location, String value) {
035                Assert.noNulls(location);
036                Assert.noBlanks(value);
037                this.value = value;
038                this.location = location;
039        }
040
041        @Override
042        public Properties load() {
043                if (!LocationUtils.exists(value)) {
044                        // Take appropriate action for missing locations (ignore, inform, warn, or error out)
045                        ModeUtils.validate(location.getMissingMode(), "Non-existent location [" + value + "]");
046                        return PropertyUtils.EMPTY;
047                } else {
048                        return PropertyUtils.load(value, location.getEncoding(), location.getFormat());
049                }
050        }
051
052        public String getValue() {
053                return value;
054        }
055
056        public Location getLocation() {
057                return location;
058        }
059
060}