/*
   Copyright 2016, 2018 - 2019 Nationale-Nederlanden

   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 nl.nn.adapterframework.configuration.classloaders;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import nl.nn.adapterframework.configuration.ConfigurationException;
import nl.nn.adapterframework.configuration.IbisContext;
import nl.nn.adapterframework.util.AppConstants;

public class DirectoryClassLoader extends ClassLoaderBase {
	private List<File> directories;
	private String directory = null;

	public DirectoryClassLoader(ClassLoader parent) throws ConfigurationException {
		super(parent);
	}

	@Override
	public void configure(IbisContext ibisContext, String configurationName) throws ConfigurationException {
		super.configure(ibisContext, configurationName);

		if (directory == null) {
			AppConstants appConstants = AppConstants.getInstance();
			String configurationsDirectory = appConstants.getResolvedProperty("configurations.directory");
			if (configurationsDirectory == null) {
				throw new ConfigurationException("Could not find property configurations.directory");
			}
			retrieveDirectories(configurationsDirectory);
		} else {
			retrieveDirectories(directory);
		}
	}

	public void setDirectory(String directory) {
		this.directory = directory;
	}

	private void retrieveDirectories(String directoriesString) throws ConfigurationException {
		directories = new ArrayList<File>();
		List<String> directoriesStringAsList = Arrays.asList(directoriesString.split(","));
		boolean existingDir = false;
		for (String directoryString : directoriesStringAsList) {
			File directory = new File(directoryString);
			if (directory.isDirectory()) {
				existingDir = true;
			}
			directories.add(directory);
		}
		if (!existingDir) {
			throw new ConfigurationException("Could not find directory to load configuration from: " + directoriesString);
		}
	}
	
	@Override
	public URL getResource(String name) {
		for (File directory: directories) {
			File file = new File(directory, name);
			if (file.exists()) {
				try {
					return file.toURI().toURL();
				} catch (MalformedURLException e) {
					log.error("Could not create url for '" + name + "'", e);
				}
			}
		}
		return super.getResource(name);
	}
}
@@ -748,84 +748,8 @@
 URL;
-%0Aimport java.util.ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.List;
 %0A%0Aim
@@ -994,47 +994,12 @@
 ate 
-List%3CFile%3E directories;%0A%09private String
+File
  dir
@@ -1606,38 +1606,45 @@
 );%0A%09%09%09%7D%0A
+%0A
 %09%09%09
-retrieveDirectories
+this.directory = new File
 (configu
@@ -1669,48 +1669,43 @@
 %0A%09%09%7D
- else %7B%0A%09%09%09retrieveD
+%0A%0A%09%09if (!this.d
 irector
-ies(d
+y.isD
 irectory
 );%0A%09
@@ -1704,51 +1704,65 @@
 tory
-);%0A%09%09%7D%0A%09%7D%0A%0A%09public void setDirectory(String
+()) %7B%0A%09%09%09throw new ConfigurationException(%22Could not find
  dir
@@ -1771,31 +1771,46 @@
 tory
-) %7B%0A%09%09this.directory = 
+ to load configuration from: %22 + this.
 dire
@@ -1818,268 +1818,195 @@
 tory
+)
 ;%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09
-private void retrieveDirectories(String directoriesString) throws ConfigurationException %7B%0A%09%09directories = new ArrayList%3CFile%3E();%0A%09%09List%3CString%3E directoriesStringAsList = Arrays.asList(directoriesString.split(%22,%22));%0A%09%09boolean existingDir = false;%0A%09%09for 
+/**%0A%09 * Set the directory from which the configuration files should be loaded%0A%09 * @throws ConfigurationException if the directory can't be found%0A%09 */%0A%09public void setDirectory
 (Str
@@ -2022,55 +2022,52 @@
 tory
-String : directoriesStringAsList)
+) throws ConfigurationException
  %7B%0A%09%09
-%09
 File dir
 ecto
@@ -2058,30 +2058,24 @@
 %7B%0A%09%09File dir
-ectory
  = new File(
@@ -2087,33 +2087,20 @@
 tory
-String
 );%0A
-%09
 %09%09if
- 
 (
+!
 dir
-ectory
 .isD
@@ -2114,219 +2114,113 @@
 y())
- %7B%0A%09%09%09%09existingDir = true;%0A%09%09%09%7D%0A%09%09%09directories.add(directory);%0A%09%09%7D%0A%09%09if (!existingDir) %7B%0A%09%09%09throw new ConfigurationException(%22Could not find directory to load configuration from: %22 + directoriesString)
+%0A%09%09%09throw new ConfigurationException(%22directory %5B%22+directory+%22%5D not found%22);%0A%0A%09%09this.directory = dir
 ;%0A
-%09
 %09%7D%0A
-%09%7D%0A%09
 %0A%09@O
@@ -2270,47 +2270,8 @@
 ) %7B%0A
-%09%09for (File directory: directories) %7B%0A%09
 %09%09Fi
@@ -2305,25 +2305,24 @@
 y, name);%0A%09%09
-%09
 if (file.exi
@@ -2337,16 +2337,14 @@
 %0A%09%09%09
-%09
 try %7B%0A
-%09
 %09%09%09%09
@@ -2375,17 +2375,16 @@
 L();%0A%09%09%09
-%09
 %7D catch 
@@ -2411,17 +2411,16 @@
 on e) %7B%0A
-%09
 %09%09%09%09log.
@@ -2476,14 +2476,8 @@
 e);%0A
-%09%09%09%09%7D%0A
 %09%09%09%7D
@@ -2481,16 +2481,17 @@
 %09%09%7D%0A%09%09%7D%0A
+%0A
 %09%09return

Remove ability to specify multiple directory's for a single classloader