/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF 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.apache.beam.sdk.io.hadoop;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Map;
import javax.annotation.Nullable;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.mapreduce.Job;

/**
 * A wrapper to allow Hadoop {@link Configuration}s to be serialized using Java's standard
 * serialization mechanisms.
 */
public class SerializableConfiguration implements Externalizable {
  private static final long serialVersionUID = 0L;

  private transient Configuration conf;

  public SerializableConfiguration() {
  }

  public SerializableConfiguration(Configuration conf) {
    if (conf == null) {
      throw new NullPointerException("Configuration must not be null.");
    }
    this.conf = conf;
  }

  public Configuration get() {
    return conf;
  }

  @Override
  public void writeExternal(ObjectOutput out) throws IOException {
    out.writeInt(conf.size());
    for (Map.Entry<String, String> entry : conf) {
      out.writeUTF(entry.getKey());
      out.writeUTF(entry.getValue());
    }
  }

  @Override
  public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    conf = new Configuration(false);
    int size = in.readInt();
    for (int i = 0; i < size; i++) {
      conf.set(in.readUTF(), in.readUTF());
    }
  }

  /**
   * Returns new configured {@link Job} object.
   */
  public static Job newJob(@Nullable SerializableConfiguration conf) throws IOException {
    if (conf == null) {
      return Job.getInstance();
    } else {
      // Don't reading configuration from slave thread, but only from master thread.
      Job job = Job.getInstance(new Configuration(false));
      for (Map.Entry<String, String> entry : conf.get()) {
        job.getConfiguration().set(entry.getKey(), entry.getValue());
      }
      return job;
    }
  }

  /**
   * Returns new populated {@link Configuration} object.
   */
  public static Configuration newConfiguration(@Nullable SerializableConfiguration conf) {
    if (conf == null) {
      return new Configuration();
    } else {
      return conf.get();
    }
  }

}

@@ -1665,24 +1665,25 @@
  conf;%0A  %7D%0A%0A
+%0A
   @Override%0A
@@ -1766,108 +1766,44 @@
 rite
-Int
+UTF
 (conf.
-size());%0A    for (Map.Entry%3CString, String%3E entry : conf) %7B%0A      out.writeUTF(entry.getKey
+getClass().getCanonicalName
 ());
@@ -1811,47 +1811,24 @@
     
-  out
+conf
 .write
-UTF(entry.getValue());%0A    %7D
+(out);
 %0A  %7D
@@ -1937,148 +1937,279 @@
     
-conf = new Configuration(false);%0A    int size = in.readInt();%0A    for (int i = 0; i %3C size; i++) %7B%0A      conf.set(in.readUTF(), in.readUTF()
+String className = in.readUTF();%0A    try %7B%0A      conf = (Configuration) Class.forName(className).newInstance();%0A      conf.readFields(in);%0A    %7D catch (InstantiationException %7C IllegalAccessException e) %7B%0A      throw new IOException(%22Unable to create configuration: %22 + e
 );%0A 

Make SerializableConfiguration more robust by using Hadoop based serialization