001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
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 */
017package org.apache.activemq.spring;
018
019import java.io.File;
020import java.io.FileNotFoundException;
021import java.net.MalformedURLException;
022import org.springframework.core.io.ClassPathResource;
023import org.springframework.core.io.FileSystemResource;
024import org.springframework.core.io.Resource;
025import org.springframework.core.io.UrlResource;
026import org.springframework.util.ResourceUtils;
027
028public class Utils {
029
030    public static Resource resourceFromString(String uri) throws MalformedURLException {
031        Resource resource;
032        File file = new File(uri);
033        if (file.exists()) {
034            resource = new FileSystemResource(uri);
035        } else if (ResourceUtils.isUrl(uri)) {
036            try {
037                resource = new UrlResource(ResourceUtils.getURL(uri));
038            } catch (FileNotFoundException e) {
039                MalformedURLException malformedURLException = new MalformedURLException(uri);
040                malformedURLException.initCause(e);
041                throw  malformedURLException;
042            }
043        } else {
044            resource = new ClassPathResource(uri);
045        }
046        return resource;
047    }
048}