Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Completed the detection and processing of configuration file changes.
- Loading branch information
1 parent
f3ac899
commit a8750da
Showing
16 changed files
with
484 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
rapidoid-commons/src/main/java/org/rapidoid/config/ConfigChangeListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
108
rapidoid-commons/src/main/java/org/rapidoid/config/ConfigChanges.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 + | ||
'}'; | ||
} | ||
|
||
} |
Oops, something went wrong.