TAMAYA-297: Removed OSGIConfigMapper to reduce complexity. Added restore functionality for backups.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/commit/dab1da7a Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/tree/dab1da7a Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/diff/dab1da7a Branch: refs/heads/master Commit: dab1da7a7f959634c6b6ba7e6037e14b1f4f8575 Parents: fc0c72a Author: anatole <[email protected]> Authored: Sun Sep 24 21:41:31 2017 +0200 Committer: anatole <[email protected]> Committed: Sun Sep 24 21:41:31 2017 +0200 ---------------------------------------------------------------------- .../java/org/apache/tamaya/osgi/Backups.java | 110 +++++++++++++++++++ .../org/apache/tamaya/osgi/InitialState.java | 110 ------------------- .../apache/tamaya/osgi/OSGIConfigMapper.java | 38 ------- .../tamaya/osgi/commands/BackupCommands.java | 44 ++++++-- 4 files changed, 147 insertions(+), 155 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/dab1da7a/osgi/common/src/main/java/org/apache/tamaya/osgi/Backups.java ---------------------------------------------------------------------- diff --git a/osgi/common/src/main/java/org/apache/tamaya/osgi/Backups.java b/osgi/common/src/main/java/org/apache/tamaya/osgi/Backups.java new file mode 100644 index 0000000..cacf830 --- /dev/null +++ b/osgi/common/src/main/java/org/apache/tamaya/osgi/Backups.java @@ -0,0 +1,110 @@ +/* + * 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.tamaya.osgi; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Created by atsticks on 19.09.17. + */ +public final class Backups { + + private static final Logger LOG = Logger.getLogger(Backups.class.getName()); + private static Map<String, Hashtable<String,?>> initialConfigState = new ConcurrentHashMap<>(); + + private Backups(){} + + public static void set(String pid, Dictionary<String,?> config){ + initialConfigState.put(pid, toHashtable(config)); + } + + private static Hashtable<String, ?> toHashtable(Dictionary<String, ?> dictionary) { + if (dictionary == null) { + return null; + } + if(dictionary instanceof Hashtable){ + return (Hashtable) dictionary; + } + Hashtable<String, Object> map = new Hashtable<>(dictionary.size()); + Enumeration<String> keys = dictionary.keys(); + while (keys.hasMoreElements()) { + String key = keys.nextElement(); + map.put(key, dictionary.get(key)); + } + return map; + } + + public static Dictionary<String,?> remove(String pid){ + return initialConfigState.remove(pid); + } + + public static void removeAll(){ + initialConfigState.clear(); + } + + public static Dictionary<String,?> get(String pid){ + return initialConfigState.get(pid); + } + + public static Map<String,Dictionary<String,?>> get(){ + return new HashMap<>(initialConfigState); + } + + public static Set<String> getPids(){ + return initialConfigState.keySet(); + } + + public static boolean contains(String pid){ + return initialConfigState.containsKey(pid); + } + + public static void save(TamayaConfigPlugin plugin){ + try{ + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(bos); + oos.writeObject(initialConfigState); + oos.flush(); + Base64.getEncoder().encode(bos.toByteArray()); + plugin.setConfigValue("backup", Base64.getEncoder().encode(bos.toByteArray())); + }catch(Exception e){ + LOG.log(Level.SEVERE, "Failed to restore OSGI Backups.", e); + } + } + + public static void restore(TamayaConfigPlugin plugin){ + try{ + String serialized = (String)plugin.getConfigValue("backup"); + if(serialized!=null) { + ByteArrayInputStream bis = new ByteArrayInputStream(Base64.getDecoder().decode(serialized)); + ObjectInputStream ois = new ObjectInputStream(bis); + initialConfigState = (Map<String, Hashtable<String,?>>) ois.readObject(); + ois.close(); + } + } catch (Exception e) { + LOG.log(Level.WARNING, "Failed to store getConfig change history.", e); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/dab1da7a/osgi/common/src/main/java/org/apache/tamaya/osgi/InitialState.java ---------------------------------------------------------------------- diff --git a/osgi/common/src/main/java/org/apache/tamaya/osgi/InitialState.java b/osgi/common/src/main/java/org/apache/tamaya/osgi/InitialState.java deleted file mode 100644 index 9cc0a4a..0000000 --- a/osgi/common/src/main/java/org/apache/tamaya/osgi/InitialState.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * 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.tamaya.osgi; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.util.*; -import java.util.concurrent.ConcurrentHashMap; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Created by atsticks on 19.09.17. - */ -public final class InitialState { - - private static final Logger LOG = Logger.getLogger(InitialState.class.getName()); - private static Map<String, Hashtable<String,?>> initialConfigState = new ConcurrentHashMap<>(); - - private InitialState(){} - - public static void set(String pid, Dictionary<String,?> config){ - initialConfigState.put(pid, toHashtable(config)); - } - - private static Hashtable<String, ?> toHashtable(Dictionary<String, ?> dictionary) { - if (dictionary == null) { - return null; - } - if(dictionary instanceof Hashtable){ - return (Hashtable) dictionary; - } - Hashtable<String, Object> map = new Hashtable<>(dictionary.size()); - Enumeration<String> keys = dictionary.keys(); - while (keys.hasMoreElements()) { - String key = keys.nextElement(); - map.put(key, dictionary.get(key)); - } - return map; - } - - public static Dictionary<String,?> remove(String pid){ - return initialConfigState.remove(pid); - } - - public static void removeAll(){ - initialConfigState.clear(); - } - - public static Dictionary<String,?> get(String pid){ - return initialConfigState.get(pid); - } - - public static Map<String,Dictionary<String,?>> get(){ - return new HashMap<>(initialConfigState); - } - - public static Set<String> getPids(){ - return initialConfigState.keySet(); - } - - public static boolean contains(String pid){ - return initialConfigState.containsKey(pid); - } - - public static void save(TamayaConfigPlugin plugin){ - try{ - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream(bos); - oos.writeObject(initialConfigState); - oos.flush(); - Base64.getEncoder().encode(bos.toByteArray()); - plugin.setConfigValue("backup", Base64.getEncoder().encode(bos.toByteArray())); - }catch(Exception e){ - LOG.log(Level.SEVERE, "Failed to restore OSGI Backups.", e); - } - } - - public static void restore(TamayaConfigPlugin plugin){ - try{ - String serialized = (String)plugin.getConfigValue("backup"); - if(serialized!=null) { - ByteArrayInputStream bis = new ByteArrayInputStream(Base64.getDecoder().decode(serialized)); - ObjectInputStream ois = new ObjectInputStream(bis); - initialConfigState = (Map<String, Hashtable<String,?>>) ois.readObject(); - ois.close(); - } - } catch (Exception e) { - LOG.log(Level.WARNING, "Failed to store getConfig change history.", e); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/dab1da7a/osgi/common/src/main/java/org/apache/tamaya/osgi/OSGIConfigMapper.java ---------------------------------------------------------------------- diff --git a/osgi/common/src/main/java/org/apache/tamaya/osgi/OSGIConfigMapper.java b/osgi/common/src/main/java/org/apache/tamaya/osgi/OSGIConfigMapper.java deleted file mode 100644 index 2e90436..0000000 --- a/osgi/common/src/main/java/org/apache/tamaya/osgi/OSGIConfigMapper.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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.tamaya.osgi; - -import org.apache.tamaya.Configuration; -import org.osgi.framework.ServiceReference; - -/** - * Mapping function for mapping Tamaya configuration sections to OSGI pids. - */ -public interface OSGIConfigMapper { - - /** - * Map the given OSGI pid to a corresponding configuration section in Tamaya. Es an example (and this is also the - * default implemented) a configuration mapping for {@code pid/factoryPid==myBundle} could be {@code [bundle:myBundle]}. - * This mapping is used as a prefix when collecting the corresponding entries for the OSGI configuration. - * @param pid the OSGI pid, or null - * @return return the corresponding getConfig root section. For ommitting any root section simply return an empty - * String. - */ - Configuration getConfiguration(String pid); -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/dab1da7a/osgi/common/src/main/java/org/apache/tamaya/osgi/commands/BackupCommands.java ---------------------------------------------------------------------- diff --git a/osgi/common/src/main/java/org/apache/tamaya/osgi/commands/BackupCommands.java b/osgi/common/src/main/java/org/apache/tamaya/osgi/commands/BackupCommands.java index ee1b299..d49f1cd 100644 --- a/osgi/common/src/main/java/org/apache/tamaya/osgi/commands/BackupCommands.java +++ b/osgi/common/src/main/java/org/apache/tamaya/osgi/commands/BackupCommands.java @@ -18,7 +18,8 @@ */ package org.apache.tamaya.osgi.commands; -import org.apache.tamaya.osgi.InitialState; +import org.apache.tamaya.osgi.Backups; +import org.apache.tamaya.osgi.TamayaConfigPlugin; import org.osgi.service.cm.Configuration; import org.osgi.service.cm.ConfigurationAdmin; @@ -43,8 +44,8 @@ public final class BackupCommands { if(cfg!=null){ Dictionary<String,?> props = cfg.getProperties(); if(props!=null){ - if(force || !InitialState.contains(pid)){ - InitialState.set(pid, props); + if(force || !Backups.contains(pid)){ + Backups.set(pid, props); return "Backup created, PID = " + pid + '\n' + printProps(props); } @@ -55,17 +56,46 @@ public final class BackupCommands { public static String deleteBackup(String pid) throws IOException { if("*".equals(pid)){ - InitialState.removeAll(); + Backups.removeAll(); return "All Backups deleted."; }else { - InitialState.remove(pid); + Backups.remove(pid); return "Backup deleted: " + pid; } } + public static String restoreBackup(TamayaConfigPlugin plugin, String pid) throws IOException { + StringBuilder b = new StringBuilder("Restored Configurations:\n") + .append("------------------------\n"); + if("*".equals(pid)){ + for(String current: Backups.getPids()){ + try{ + if(plugin.restoreBackup(current)){ + b.append(current).append(" -> restored.\n"); + }else{ + b.append(current).append(" -> no backup found.\n"); + } + }catch(Exception e){ + b.append(current).append(" -> failed: " + e).append('\n'); + } + } + return b.toString(); + }else { + try{ + if(plugin.restoreBackup(pid)){ + return pid + " -> restored.\n"; + }else{ + return pid + " -> no backup found.\n"; + } + }catch(Exception e){ + return pid + " -> failed: " + e + '\n'; + } + } + } + public static String listBackup(String pid) throws IOException { if(pid!=null){ - Dictionary<String, ?> props = InitialState.get(pid); + Dictionary<String, ?> props = Backups.get(pid); if(props==null){ return "No backup found: " + pid; }else{ @@ -75,7 +105,7 @@ public final class BackupCommands { }else { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); - for(Map.Entry<String, Dictionary<String,?>> en: InitialState.get().entrySet()){ + for(Map.Entry<String, Dictionary<String,?>> en: Backups.get().entrySet()){ pw.println("PID: " + en.getKey()); pw.println(printProps(en.getValue())); }
