001    /*
002     * Copyright 2005-2008 The Kuali Foundation
003     * 
004     * 
005     * Licensed under the Educational Community License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     * 
009     * http://www.opensource.org/licenses/ecl2.php
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.kuali.rice.kew.engine.node.var.schemes;
019    
020    import java.io.IOException;
021    import java.io.InputStream;
022    
023    import org.apache.log4j.Logger;
024    import org.kuali.rice.kew.engine.RouteContext;
025    import org.kuali.rice.kew.engine.node.PropertiesUtil;
026    import org.kuali.rice.kew.engine.node.var.Property;
027    import org.kuali.rice.kew.engine.node.var.PropertyScheme;
028    
029    
030    /**
031     * A property scheme that loads resources from the class loader.
032     *  
033     * @author Kuali Rice Team (rice.collab@kuali.org)
034     */
035    public class ResourceScheme implements PropertyScheme {
036        private static final Logger LOG = Logger.getLogger(ResourceScheme.class);
037    
038        public String getName() {
039            return "resource";
040        }
041        public String getShortName() {
042            return "res";
043        }
044    
045        public Object load(Property property, RouteContext context) {
046            String resource;
047            boolean relative = false;
048    //        if (property.locator.startsWith("/")) {
049                resource = property.locator;
050    //        } else {
051    //            relative = true;
052    //            String prefix;
053    //            /* if a resource prefix is set, use it */
054    //            if (state.getResourcePrefix() != null) {
055    //                prefix = state.getResourcePrefix();
056    //            } else {
057    //                /* otherwise use the location of the Script class */
058    //                prefix = Script.class.getPackage().getName().replace('.', '/');
059    //            }
060    //            if (!prefix.endsWith("/")) {
061    //                prefix += "/"; 
062    //            }
063    //            resource = prefix + property.locator;
064    //        }
065            String resStr = property.locator + (relative ? "(" + resource + ")" : "");
066            LOG.info("Reading resource " + resStr + "...");
067            
068            InputStream is = getClass().getResourceAsStream(resource);
069            if (is == null) {
070                throw new RuntimeException("Resource not found: " + resStr);
071            }
072            try {
073                return PropertiesUtil.readResource(is);
074            } catch (IOException ioe) {
075                throw new RuntimeException("Error loading resource: " + resStr, ioe);
076            }
077        }
078    
079        public String toString() {
080            return "[ResourceScheme]";
081        }
082    }