/**
 * Copyright 2015-2016 Red Hat, Inc, and individual contributors.
 *
 * 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 org.wildfly.swarm.fractions;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author Toby Crawley
 */
public class PropertiesUtil {

    private PropertiesUtil() {
    }

    public static Properties loadProperties(final InputStream in) throws IOException {
        final Properties props = new Properties();
        try {
            props.load(in);
        } finally {
            in.close();
        }

        return props;
    }

    public static Properties loadProperties(final String file) throws IOException {
        return loadProperties(new File(file));
    }

    public static Properties loadProperties(final File file) throws IOException {
        return loadProperties(new FileInputStream(file));
    }

    public static String versionFromPomProperties() {
        try {
            return loadProperties(PropertiesUtil.class
                                          .getClassLoader()
                                          .getResourceAsStream("META-INF/maven/org.wildfly.swarm/tools/pom.properties"))
                    .getProperty("version");
        } catch (Exception e) {
            throw new RuntimeException("Failed to load version from pom.properties", e);
        }
    }

    /**
     * Copies any jboss.*, swarm.*, or wildfly.* (and optionally maven.*) sysprops from System,
     * along with anything that shadows a specified property.
     *
     * @return only the filtered properties, existing is unchanged
     */
    public static Properties filteredSystemProperties(final Properties existing, final boolean withMaven) {
        final Properties properties = new Properties();

        System.getProperties().stringPropertyNames().forEach(key -> {
            if (key.startsWith("jboss.") ||
                    key.startsWith("swarm.") ||
                    key.startsWith("wildfly.") ||
                    (withMaven && key.startsWith("maven.")) ||
                    existing.containsKey(key)) {
                properties.put(key, System.getProperty(key));
            }
        });

        return properties;
    }
}

@@ -760,32 +760,92 @@
 io.InputStream;%0A
+import java.net.MalformedURLException;%0Aimport java.net.URL;%0A
 import java.util
@@ -857,16 +857,16 @@
 erties;%0A
-
 %0A/**%0A * 
@@ -1215,32 +1215,344 @@
 n props;%0A    %7D%0A%0A
+    /**%0A     * Loads properties from the file identified by the given %7B@code fileString%7D,%0A     * which can be a regular file (path), a classpath resource or a URL.%0A     *%0A     * @param fileString identifies the file%0A     * @return the properties%0A     * @throws IOException on errors reading the file/URL%0A     */%0A
     public stati
@@ -1592,24 +1592,30 @@
  String file
+String
 ) throws IOE
@@ -1637,44 +1637,793 @@
     
-return loadProperties(new File(file)
+final File file = new File(fileString);%0A        // first try: regular file%0A        if (file.exists()) %7B%0A            return loadProperties(file);%0A        %7D%0A        // second try: classpath resource%0A        final InputStream resourceStream = Thread.currentThread().getContextClassLoader()%0A                .getResourceAsStream(fileString);%0A        if (resourceStream != null) %7B%0A            return loadProperties(resourceStream);%0A        %7D%0A        // third/last try: URL%0A        try %7B%0A            final URL url = new URL(fileString);%0A            return loadProperties(url.openStream());%0A        %7D catch (final MalformedURLException e) %7B%0A            // there is no guarantee that fileString is a URL at all%0A        %7D%0A        throw new IllegalArgumentException(%22Unable to find %22 + fileString
 );%0A 

Support propertiesFile loading from classpath/URL. (#431)