001/**
002 * Copyright 2005-2016 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;
020
021import org.apache.log4j.Logger;
022import org.kuali.rice.kew.engine.RouteContext;
023import org.kuali.rice.kew.engine.node.PropertiesUtil;
024import org.kuali.rice.kew.engine.node.var.Property;
025import org.kuali.rice.kew.engine.node.var.PropertyScheme;
026
027
028/**
029 * A property scheme that loads resources from the class loader.
030 *  
031 * @author Kuali Rice Team (rice.collab@kuali.org)
032 */
033public class ResourceScheme implements PropertyScheme {
034    private static final Logger LOG = Logger.getLogger(ResourceScheme.class);
035
036    public String getName() {
037        return "resource";
038    }
039    public String getShortName() {
040        return "res";
041    }
042
043    public Object load(Property property, RouteContext context) {
044        String resource;
045        boolean relative = false;
046//        if (property.locator.startsWith("/")) {
047            resource = property.locator;
048//        } else {
049//            relative = true;
050//            String prefix;
051//            /* if a resource prefix is set, use it */
052//            if (state.getResourcePrefix() != null) {
053//                prefix = state.getResourcePrefix();
054//            } else {
055//                /* otherwise use the location of the Script class */
056//                prefix = Script.class.getPackage().getName().replace('.', '/');
057//            }
058//            if (!prefix.endsWith("/")) {
059//                prefix += "/"; 
060//            }
061//            resource = prefix + property.locator;
062//        }
063        String resStr = property.locator + (relative ? "(" + resource + ")" : "");
064        LOG.info("Reading resource " + resStr + "...");
065        
066        InputStream is = getClass().getResourceAsStream(resource);
067        if (is == null) {
068            throw new RuntimeException("Resource not found: " + resStr);
069        }
070        try {
071            return PropertiesUtil.readResource(is);
072        } catch (IOException ioe) {
073            throw new RuntimeException("Error loading resource: " + resStr, ioe);
074        }
075    }
076
077    public String toString() {
078        return "[ResourceScheme]";
079    }
080}