1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.mail2.javax.resolver;
18
19 import java.io.IOException;
20
21 import javax.activation.DataSource;
22
23 import org.apache.commons.mail2.javax.DataSourceResolver;
24
25 /**
26 * 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.
27 *
28 * @since 1.3
29 */
30 public class DataSourceCompositeResolver extends DataSourceBaseResolver {
31
32 /** The list of resolvers */
33 private final DataSourceResolver[] dataSourceResolvers;
34
35 /**
36 * Constructs a new instance.
37 *
38 * @param dataSourceResolvers a list of resolvers being used
39 */
40 public DataSourceCompositeResolver(final DataSourceResolver[] dataSourceResolvers) {
41 this.dataSourceResolvers = dataSourceResolvers.clone();
42 }
43
44 /**
45 * Constructs a new instance.
46 *
47 * @param dataSourceResolvers a list of resolvers being used
48 * @param isLenient shall we ignore resources not found or throw an exception?
49 */
50 public DataSourceCompositeResolver(final DataSourceResolver[] dataSourceResolvers, final boolean isLenient) {
51 super(isLenient);
52 this.dataSourceResolvers = dataSourceResolvers.clone();
53 }
54
55 /**
56 * Gets the underlying data source resolvers.
57 *
58 * @return underlying data source resolvers
59 */
60 public DataSourceResolver[] getDataSourceResolvers() {
61 // clone the internal array to prevent external modification (see EMAIL-116)
62 return dataSourceResolvers.clone();
63 }
64
65 /** {@inheritDoc} */
66 @Override
67 public DataSource resolve(final String resourceLocation) throws IOException {
68 final DataSource result = resolve(resourceLocation, true);
69 if (isLenient() || result != null) {
70 return result;
71 }
72 throw new IOException("The following resource was not found : " + resourceLocation);
73
74 }
75
76 /** {@inheritDoc} */
77 @Override
78 public DataSource resolve(final String resourceLocation, final boolean isLenient) throws IOException {
79 for (final DataSourceResolver dataSourceResolver : dataSourceResolvers) {
80 final DataSource dataSource = dataSourceResolver.resolve(resourceLocation, isLenient);
81 if (dataSource != null) {
82 return dataSource;
83 }
84 }
85 if (isLenient) {
86 return null;
87 }
88 throw new IOException("The following resource was not found : " + resourceLocation);
89 }
90 }