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: */package org.apache.cxf.resource;
019:
020: import java.io.InputStream;
021: import java.util.Collections;
022: import java.util.List;
023: import java.util.concurrent.CopyOnWriteArrayList;
024: import java.util.logging.Level;
025: import java.util.logging.Logger;
026:
027: import org.apache.cxf.common.logging.LogUtils;
028:
029: public class DefaultResourceManager implements ResourceManager {
030:
031: private static final Logger LOG = LogUtils
032: .getL7dLogger(DefaultResourceManager.class);
033:
034: protected final List<ResourceResolver> registeredResolvers = new CopyOnWriteArrayList<ResourceResolver>();
035:
036: public DefaultResourceManager() {
037: initializeDefaultResolvers();
038: }
039:
040: public DefaultResourceManager(ResourceResolver resolver) {
041: addResourceResolver(resolver);
042: }
043:
044: public DefaultResourceManager(List<ResourceResolver> resolvers) {
045: registeredResolvers.addAll(resolvers);
046: }
047:
048: public final <T> T resolveResource(String name, Class<T> type) {
049: return findResource(name, type, false, registeredResolvers);
050: }
051:
052: public final <T> T resolveResource(String name, Class<T> type,
053: List<ResourceResolver> resolvers) {
054: return findResource(name, type, false, resolvers);
055: }
056:
057: public final InputStream getResourceAsStream(String name) {
058: return findResource(name, InputStream.class, true,
059: registeredResolvers);
060: }
061:
062: public final void addResourceResolver(ResourceResolver resolver) {
063: if (!registeredResolvers.contains(resolver)) {
064: registeredResolvers.add(0, resolver);
065: }
066: }
067:
068: public final void removeResourceResolver(ResourceResolver resolver) {
069: if (registeredResolvers.contains(resolver)) {
070: registeredResolvers.remove(resolver);
071: }
072: }
073:
074: public final List<ResourceResolver> getResourceResolvers() {
075: return Collections.unmodifiableList(registeredResolvers);
076: }
077:
078: private <T> T findResource(String name, Class<T> type,
079: boolean asStream, List<ResourceResolver> resolvers) {
080:
081: if (LOG.isLoggable(Level.FINE)) {
082: LOG.fine("resolving resource <"
083: + name
084: + ">"
085: + (asStream ? " as stream " : " type <" + type
086: + ">"));
087: }
088:
089: T ret = null;
090:
091: for (ResourceResolver rr : resolvers) {
092: if (asStream) {
093: ret = type.cast(rr.getAsStream(name));
094: } else {
095: ret = rr.resolve(name, type);
096: }
097: if (ret != null) {
098: break;
099: }
100: }
101: return ret;
102: }
103:
104: private void initializeDefaultResolvers() {
105: addResourceResolver(new ClasspathResolver());
106: addResourceResolver(new ClassLoaderResolver(getClass()
107: .getClassLoader()));
108: }
109:
110: }
|