/**
 * (C) Copyright IBM Corporation 2019.
 *
 * 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 net.wasdev.wlp.ant.types;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class EmbeddedServerInfo {

    private final String serverName;
    private final File userDir;
    private final File outputDir;

    public EmbeddedServerInfo(String serverName, File userDir, File outputDir) {
        this.serverName = serverName;
        this.userDir = userDir;
        this.outputDir = outputDir;
    }

    public String getServerName() {
        return serverName;
    }

    public File getUserDir() {
        return userDir;
    }

    public File getOutputDir() {
        return outputDir;
    }

    public boolean equals(EmbeddedServerInfo info) {
        return 
            this.serverName.equals(info.serverName)
            && this.userDir.getAbsolutePath().equals(info.userDir.getAbsolutePath())
            && this.outputDir.getAbsolutePath().equals(info.outputDir.getAbsolutePath());
    }

    public static class EmbeddedServers {

        private static final Map<EmbeddedServerInfo, Object> EMBEDDED_SERVERS = new HashMap<EmbeddedServerInfo, Object>();

        public static Object get(EmbeddedServerInfo info) {
            // Look for the server based on the server info
            for (Entry<EmbeddedServerInfo, Object> entry : EMBEDDED_SERVERS.entrySet()) {
                EmbeddedServerInfo serverInfo = entry.getKey();
                if (serverInfo.equals(info)) {
                    return entry.getValue();
                }
            }
            // Return null if we didn't find it
            return null;
        }

        public static void put(EmbeddedServerInfo info, Object server) {
            EMBEDDED_SERVERS.put(info, server);
        }

    }

    public static class EmbeddedServerClassLoaders {

        private static final Map<URL, URLClassLoader> CLASSLOADERS = new HashMap<URL, URLClassLoader>();

        public static URLClassLoader get(URL url) {
            // Look for the classloader based on the server info
            for (Entry<URL, URLClassLoader> entry : CLASSLOADERS.entrySet()) {
                URL entryUrl = entry.getKey();
                if (url.equals(entryUrl)) {
                    return entry.getValue();
                }
            }
            // Return null if we didn't find it
            return null;
        }

        public static void put(URL url, URLClassLoader classloader) {
            CLASSLOADERS.put(url, classloader);
        }

    }

}

@@ -1432,16 +1432,19 @@
 verName)
+ &&
 %0A       
@@ -1452,110 +1452,248 @@
     
-&& this.userDir.getAbsolutePath().equals(info.userDir.getAbsolutePath())%0A            && this.outputDir
+areFilesEqual(this.userDir, info.userDir) &&%0A            areFilesEqual(this.outputDir, info.outputDir);%0A    %7D%0A%0A    private boolean areFilesEqual(File file1, File file2) %7B%0A        if (file1 != null && file2 != null) %7B%0A           return file1
 .get
@@ -1714,30 +1714,21 @@
 .equals(
-info.outputDir
+file2
 .getAbso
@@ -1740,16 +1740,75 @@
 ath());%0A
+        %7D%0A        return (file1 == null && file2 == null);%0A
     %7D%0A%0A 

Fix embedded server to handle null outputDir and userDir