001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019
020package org.apache.isis.core.commons.resource;
021
022import java.io.InputStream;
023
024import org.apache.isis.core.commons.lang.StringExtensions;
025
026/**
027 * Loads the properties from the ContextClassLoader.
028 * 
029 * <p>
030 * If this class is on the system class path, then the class loader obtained
031 * from this.getClassLoader() won't be able to load resources from the
032 * application class path.
033 */
034public class ResourceStreamSourceContextLoaderClassPath extends ResourceStreamSourceAbstract {
035
036    public static ResourceStreamSourceContextLoaderClassPath create() {
037        return create("");
038    }
039
040    public static ResourceStreamSourceContextLoaderClassPath create(final String prefix) {
041        return new ResourceStreamSourceContextLoaderClassPath(prefix);
042    }
043
044    private final String prefix;
045
046    private ResourceStreamSourceContextLoaderClassPath(final String prefix) {
047        this.prefix = prefix;
048    }
049
050    @Override
051    protected InputStream doReadResource(final String resourcePath) {
052        final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
053        final String path = StringExtensions.combinePath(prefix, resourcePath);
054        return classLoader.getResourceAsStream(path);
055    }
056
057    @Override
058    public String getName() {
059        return "context loader classpath";
060    }
061
062}