001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.conf;
020
021import java.util.Map;
022import java.util.Collection;
023import java.util.HashMap;
024
025public class ReconfigurationUtil {
026
027  public static class PropertyChange {
028    public String prop;
029    public String oldVal;
030    public String newVal;
031
032    public PropertyChange(String prop, String newVal, String oldVal) {
033      this.prop = prop;
034      this.newVal = newVal;
035      this.oldVal = oldVal;
036    }
037  }
038
039  public static Collection<PropertyChange> 
040    getChangedProperties(Configuration newConf, Configuration oldConf) {
041    Map<String, PropertyChange> changes = new HashMap<String, PropertyChange>();
042
043    // iterate over old configuration
044    for (Map.Entry<String, String> oldEntry: oldConf) {
045      String prop = oldEntry.getKey();
046      String oldVal = oldEntry.getValue();
047      String newVal = newConf.getRaw(prop);
048      
049      if (newVal == null || !newVal.equals(oldVal)) {
050        changes.put(prop, new PropertyChange(prop, newVal, oldVal));
051      }
052    }
053    
054    // now iterate over new configuration
055    // (to look for properties not present in old conf)
056    for (Map.Entry<String, String> newEntry: newConf) {
057      String prop = newEntry.getKey();
058      String newVal = newEntry.getValue();
059      if (oldConf.get(prop) == null) {
060        changes.put(prop, new PropertyChange(prop, newVal, null));
061      }
062    } 
063
064    return changes.values();
065  }
066
067  public Collection<PropertyChange> parseChangedProperties(
068      Configuration newConf, Configuration oldConf) {
069    return getChangedProperties(newConf, oldConf);
070  }
071}