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.commons.mail2.javax.resolver;
018
019import java.io.IOException;
020
021import javax.activation.DataSource;
022
023import org.apache.commons.mail2.javax.DataSourceResolver;
024
025/**
026 * A composite data source resolver. It allows to resolve data sources coming from multiple locations such as the classpath, the file system or an URL.
027 *
028 * @since 1.3
029 */
030public class DataSourceCompositeResolver extends DataSourceBaseResolver {
031
032    /** The list of resolvers */
033    private final DataSourceResolver[] dataSourceResolvers;
034
035    /**
036     * Constructs a new instance.
037     *
038     * @param dataSourceResolvers a list of resolvers being used
039     */
040    public DataSourceCompositeResolver(final DataSourceResolver[] dataSourceResolvers) {
041        this.dataSourceResolvers = dataSourceResolvers.clone();
042    }
043
044    /**
045     * Constructs a new instance.
046     *
047     * @param dataSourceResolvers a list of resolvers being used
048     * @param isLenient           shall we ignore resources not found or throw an exception?
049     */
050    public DataSourceCompositeResolver(final DataSourceResolver[] dataSourceResolvers, final boolean isLenient) {
051        super(isLenient);
052        this.dataSourceResolvers = dataSourceResolvers.clone();
053    }
054
055    /**
056     * Gets the underlying data source resolvers.
057     *
058     * @return underlying data source resolvers
059     */
060    public DataSourceResolver[] getDataSourceResolvers() {
061        // clone the internal array to prevent external modification (see EMAIL-116)
062        return dataSourceResolvers.clone();
063    }
064
065    /** {@inheritDoc} */
066    @Override
067    public DataSource resolve(final String resourceLocation) throws IOException {
068        final DataSource result = resolve(resourceLocation, true);
069        if (isLenient() || result != null) {
070            return result;
071        }
072        throw new IOException("The following resource was not found : " + resourceLocation);
073
074    }
075
076    /** {@inheritDoc} */
077    @Override
078    public DataSource resolve(final String resourceLocation, final boolean isLenient) throws IOException {
079        for (final DataSourceResolver dataSourceResolver : dataSourceResolvers) {
080            final DataSource dataSource = dataSourceResolver.resolve(resourceLocation, isLenient);
081            if (dataSource != null) {
082                return dataSource;
083            }
084        }
085        if (isLenient) {
086            return null;
087        }
088        throw new IOException("The following resource was not found : " + resourceLocation);
089    }
090}