All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.cxf.resource.DefaultResourceManager Maven / Gradle / Ivy

There is a newer version: 2.7.18
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.cxf.resource;

import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.cxf.common.logging.LogUtils;

public class DefaultResourceManager implements ResourceManager {
    
    private static final Logger LOG = LogUtils.getL7dLogger(DefaultResourceManager.class);

    protected final List registeredResolvers 
        = new CopyOnWriteArrayList();
    protected boolean firstCalled;

    public DefaultResourceManager() { 
        initializeDefaultResolvers(); 
    } 
    
    public DefaultResourceManager(ResourceResolver resolver) {
        addResourceResolver(resolver);
    }
    
    public DefaultResourceManager(List resolvers) {
        addResourceResolvers(resolvers);
    }
    
    protected void onFirstResolve() {
        //nothing
        firstCalled = true;
    }
 
    public final  T resolveResource(String name, Class type) {
        return findResource(name, type, false, registeredResolvers);
    } 

    public final  T resolveResource(String name, Class type, List resolvers) { 
        return findResource(name, type, false, resolvers);
    } 

    
    public final InputStream getResourceAsStream(String name) { 
        return findResource(name, InputStream.class, true, registeredResolvers);
    } 

    public final void addResourceResolver(ResourceResolver resolver) { 
        if (!registeredResolvers.contains(resolver)) { 
            registeredResolvers.add(0, resolver);
        }
    } 
    public final void addResourceResolvers(Collection resolvers) { 
        for (ResourceResolver r : resolvers) {
            addResourceResolver(r);
        }
    } 

    public final void removeResourceResolver(ResourceResolver resolver) { 
        if (registeredResolvers.contains(resolver)) { 
            registeredResolvers.remove(resolver);
        }
    } 


    public final List getResourceResolvers() {
        return Collections.unmodifiableList(registeredResolvers); 
    }

    
    private  T findResource(String name, Class type, boolean asStream, 
                               List resolvers) {
        if (!firstCalled) {
            onFirstResolve();
        }
        if (resolvers == null) {
            resolvers = registeredResolvers;
        }
        
        if (LOG.isLoggable(Level.FINE)) { 
            LOG.fine("resolving resource <" + name + ">" + (asStream ? " as stream "  
                                                            : " type <" + type + ">"));
        }

        T ret = null; 
        
        for (ResourceResolver rr : resolvers) { 
            if (asStream) { 
                ret = type.cast(rr.getAsStream(name));
            } else { 
                ret = rr.resolve(name, type);
            }
            if (ret != null) { 
                break;
            }
        } 
        return ret;
    } 

    private void initializeDefaultResolvers() { 
        addResourceResolver(new ClasspathResolver());
        addResourceResolver(new ClassLoaderResolver(getClass().getClassLoader()));
    } 

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy