Skip to content

Commit

Permalink
Completed the detection and processing of configuration file changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Dec 5, 2016
1 parent f3ac899 commit a8750da
Show file tree
Hide file tree
Showing 16 changed files with 484 additions and 32 deletions.
29 changes: 29 additions & 0 deletions rapidoid-commons/src/main/java/org/rapidoid/collection/Coll.java
Expand Up @@ -3,7 +3,9 @@
import org.rapidoid.RapidoidThing;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.cls.Cls;
import org.rapidoid.commons.Err;
import org.rapidoid.data.JSON;
import org.rapidoid.datamodel.DataItems;
import org.rapidoid.lambda.Mapper;
import org.rapidoid.u.U;
Expand Down Expand Up @@ -356,4 +358,31 @@ public static <K, V> ChangeTrackingMap<K, V> trackChanges(Map<K, V> map, AtomicB
return new ChangeTrackingMap<K, V>(map, dirtyFlag);
}

public static <T> Map<String, T> toBeanMap(Map<String, Object> data, Class<T> type) {
Map<String, T> map = U.map();

for (Map.Entry<String, Object> e : data.entrySet()) {
T bean;
Object value = e.getValue();

if (value instanceof Map) {
bean = JSON.MAPPER.convertValue(value, type);

} else if (value instanceof String) {
bean = Cls.newInstance(type, value);

} else {
throw U.rte("Unsupported configuration type: %s", Cls.of(value));
}

map.put(e.getKey(), bean);
}

return Collections.unmodifiableMap(map);
}

public static Map<String, Object> deepCopyOf(Map<String, Object> map) {
return JSON.parseMap(JSON.stringify(map)); // FIXME proper implementation
}

}
Expand Up @@ -22,6 +22,7 @@

import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.lambda.Operation;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -95,4 +96,10 @@ public interface Config extends BasicConfig {

boolean isInitialized();

ConfigChanges getChangesSince(Config previousConfig);

void addChangeListener(Operation<ConfigChanges> configChangeListener);

void removeChangeListener(Operation<ConfigChanges> configChangeListener);

}
Expand Up @@ -27,6 +27,7 @@
import org.rapidoid.u.U;

import java.util.Map;
import java.util.Set;

@Authors("Nikolche Mihajlovski")
@Since("5.2.0")
Expand All @@ -40,6 +41,8 @@ public class ConfigBase extends RapidoidInitializer {

final Map<String, Object> initial = Coll.synchronizedMap();

final Set<ConfigChangeListener> configChangesListeners = Coll.synchronizedSet();

volatile boolean initializing;

volatile boolean initialized;
Expand Down
@@ -0,0 +1,59 @@
package org.rapidoid.config;

import org.rapidoid.RapidoidThing;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.lambda.Operation;

import java.util.List;

/*
* #%L
* rapidoid-commons
* %%
* Copyright (C) 2014 - 2016 Nikolche Mihajlovski and 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.
* #L%
*/

@Authors("Nikolche Mihajlovski")
@Since("5.3.0")
class ConfigChangeListener extends RapidoidThing {

final List<String> keys;
final Operation<ConfigChanges> operation;

ConfigChangeListener(List<String> keys, Operation<ConfigChanges> operation) {
this.keys = keys;
this.operation = operation;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

ConfigChangeListener that = (ConfigChangeListener) o;

if (!keys.equals(that.keys)) return false;
return operation.equals(that.operation);
}

@Override
public int hashCode() {
int result = keys.hashCode();
result = 31 * result + operation.hashCode();
return result;
}
}
108 changes: 108 additions & 0 deletions rapidoid-commons/src/main/java/org/rapidoid/config/ConfigChanges.java
@@ -0,0 +1,108 @@
package org.rapidoid.config;

import org.rapidoid.RapidoidThing;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.collection.Coll;
import org.rapidoid.u.U;

import java.util.List;
import java.util.Map;
import java.util.Set;

/*
* #%L
* rapidoid-commons
* %%
* Copyright (C) 2014 - 2016 Nikolche Mihajlovski and 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.
* #L%
*/

@Authors("Nikolche Mihajlovski")
@Since("5.3.0")
public class ConfigChanges extends RapidoidThing {

public final boolean initial;

public final Map<String, Object> added = Coll.synchronizedMap();

public final Map<String, Object> changed = Coll.synchronizedMap();

public final Set<String> removed = Coll.synchronizedSet();

public final List<String> keys = Coll.synchronizedList();

private ConfigChanges(boolean initial) {
this.initial = initial;
}

public static ConfigChanges from(List<String> keys, Map<String, Object> old, Map<String, Object> fresh, boolean initial) {

ConfigChanges changes = new ConfigChanges(initial);

Coll.assign(changes.keys, keys);

for (Map.Entry<String, Object> e : old.entrySet()) {

String key = e.getKey();
Object oldValue = e.getValue();
Object newValue = fresh.get(key);

if (newValue != null) {
if (U.neq(oldValue, newValue)) {
changes.changed.put(key, newValue);
}
} else {
changes.removed.add(key);
}
}

for (Map.Entry<String, Object> e : fresh.entrySet()) {

String key = e.getKey();
Object newValue = fresh.get(key);

if (!old.containsKey(key)) {
changes.added.put(key, newValue);
}
}

return changes;
}

public int count() {
return added.size() + changed.size() + removed.size();
}

public <T> Map<String, T> added(Class<T> type) {
return Coll.toBeanMap(added, type);
}

public <T> Map<String, T> changed(Class<T> type) {
return Coll.toBeanMap(changed, type);
}

@Override
public String toString() {
return "ConfigChanges{" +
"initial=" + initial +
", added=" + added +
", changed=" + changed +
", removed=" + removed +
", keys=" + keys +
'}';
}

}

0 comments on commit a8750da

Please sign in to comment.