/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you 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.elasticsearch.spark.cfg;

import java.io.InputStream;
import java.util.Properties;

import org.apache.spark.SparkConf;
import org.elasticsearch.hadoop.cfg.Settings;
import org.elasticsearch.hadoop.util.Assert;
import org.elasticsearch.hadoop.util.IOUtils;

import scala.Option;
import scala.Tuple2;

public class SparkSettings extends Settings {

    private final SparkConf cfg;

    public SparkSettings(SparkConf cfg) {
        Assert.notNull(cfg, "non-null spark configuration expected");
        this.cfg = cfg;
    }

    @Override
    public InputStream loadResource(String location) {
        // delegate to good ol' TCCL
        return IOUtils.open(location);
    }

    @Override
    public Settings copy() {
        return new SparkSettings(cfg.clone());
    }

    @Override
    public String getProperty(String name) {
        Option<String> op = cfg.getOption(name);
        if (!op.isDefined()) {
            op = cfg.getOption("spark." + name);
        }
        return (op.isDefined() ? op.get() : null);
    }

    @Override
    public void setProperty(String name, String value) {
        cfg.set(name, value);
    }

    @Override
    public Properties asProperties() {
        Properties props = new Properties();

        if (cfg != null) {
            for (Tuple2<String, String> tuple : cfg.getAll()) {
                props.setProperty(tuple._1, tuple._2);
            }
        }

        return props;
    }
}

@@ -2054,24 +2054,63 @@
 != null) %7B%0A 
+       %09String sparkPrefix = %22spark.%22;%0A
            f
@@ -2108,16 +2108,17 @@
         
+ 
 for (Tup
@@ -2157,24 +2157,232 @@
 getAll()) %7B%0A
+                // spark. are special so save them without the prefix as well%0A            %09// since its unlikely the other implementations will be aware of this convention%0A            %09String key = tuple._1;%0A
             
@@ -2407,28 +2407,387 @@
 rty(
+key, 
 tuple._
-1, tuple._2);
+2);%0A                if (key.startsWith(sparkPrefix)) %7B%0A                %09String simpleKey = key.substring(sparkPrefix.length());%0A                %09// double check to not override a property defined directly in the config%0A                %09if (!props.containsKey(simpleKey)) %7B%0A                %09%09props.setProperty(simpleKey, tuple._2);%0A                %09%7D%0A                %7D
 %0A   

Handle cmd-line passed properties when saving