ResourceLoader.java :  » Database-ORM » dozer-5.2.1 » org » dozer » util » Java Open Source

Java Open Source » Database ORM » dozer 5.2.1 
dozer 5.2.1 » org » dozer » util » ResourceLoader.java
/*
 * Copyright 2005-2010 the original author or authors.
 *
 * Licensed 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.dozer.util;

import org.apache.commons.lang.StringUtils;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * Internal class that loads resources from the classpath. Also supports loading
 * resources outside of the classpath if it is prepended with "file:". Only
 * intended for internal use.
 * 
 * 
 * @author tierney.matt
 * @author garsombke.franz
 */
public class ResourceLoader {

  public URL getResource(String resource) {
    resource = resource.trim();
    
    URL result = Thread.currentThread().getContextClassLoader().getResource(resource);

    // Could not find resource. Try with the classloader that loaded this class.
    if (result == null) {
      ClassLoader classLoader = ResourceLoader.class.getClassLoader();
      if (classLoader != null) {
        result = classLoader.getResource(resource);
      }
    }

    // Last ditch attempt searching classpath
    if (result == null) {
      result = ClassLoader.getSystemResource(resource);
    }

    // one more time
    if (result == null && StringUtils.contains(resource, ":")) {
      try {
        result = new URL(resource);
      } catch (MalformedURLException e) {
        MappingUtils.throwMappingException(e);
      }
    }

    return result;
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.