001/**
002 * Copyright 2005-2017 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.rice.kew.engine.node.var.schemes;
017
018import java.io.IOException;
019import java.io.InputStream;
020import java.net.URL;
021
022import org.apache.log4j.Logger;
023import org.kuali.rice.kew.engine.RouteContext;
024import org.kuali.rice.kew.engine.node.PropertiesUtil;
025import org.kuali.rice.kew.engine.node.var.Property;
026import org.kuali.rice.kew.engine.node.var.PropertyScheme;
027
028
029/**
030 * A property scheme that interprets the locator as a URL.
031 * 
032 * @author Kuali Rice Team (rice.collab@kuali.org)
033 */
034public class URLScheme implements PropertyScheme {
035    private static final Logger LOG = Logger.getLogger(URLScheme.class);
036
037    public String getName() {
038        return "url";
039    }
040    public String getShortName() {
041        return "url";
042    }
043
044    public Object load(Property property, RouteContext context) {
045        LOG.info("Reading url '" + property.locator + "'...");
046        InputStream is;
047        try {
048            is = new URL(property.locator).openStream();
049            if (is == null) {
050                throw new RuntimeException("Unable to access URL: " + property.locator);
051            }
052            return PropertiesUtil.readResource(is);
053        } catch (IOException ioe) {
054            throw new RuntimeException("Error loading resource: " + property.locator, ioe);
055        }
056    }
057
058    public String toString() {
059        return "[URLScheme]";
060    }
061}