| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hivegui.application; |
| 16 | |
| 17 | import java.beans.PersistenceDelegate; |
| 18 | import java.beans.XMLEncoder; |
| 19 | import java.io.IOException; |
| 20 | import java.util.HashMap; |
| 21 | import java.util.Map; |
| 22 | |
| 23 | import org.apache.commons.logging.Log; |
| 24 | import org.apache.hivemind.events.RegistryShutdownListener; |
| 25 | import org.jdesktop.application.LocalStorage; |
| 26 | |
| 27 | import net.sourceforge.hiveutils.service.PreferencesManager; |
| 28 | import net.sourceforge.hiveutils.service.impl.PreferenceContribution; |
| 29 | |
| 30 | /** |
| 31 | * @author Jean-Francois Poilpret |
| 32 | */ |
| 33 | public class PreferencesManagerImpl |
| 34 | implements PreferencesManager, RegistryShutdownListener |
| 35 | { |
| 36 | public PreferencesManagerImpl( Log logger, |
| 37 | Map<String, PreferenceContribution> prefsDef, |
| 38 | Map<Class, PersistenceDelegate> mappers, |
| 39 | boolean autoWrite) |
| 40 | { |
| 41 | _logger = logger; |
| 42 | _autoWrite = autoWrite; |
| 43 | _prefsDef = prefsDef; |
| 44 | // Initialize XMLEncoder PersistenceDelegates |
| 45 | XMLEncoder encoder = null; |
| 46 | for (Map.Entry<Class, PersistenceDelegate> entry: mappers.entrySet()) |
| 47 | { |
| 48 | if (encoder == null) |
| 49 | { |
| 50 | encoder = new XMLEncoder(System.out); |
| 51 | } |
| 52 | encoder.setPersistenceDelegate(entry.getKey(), entry.getValue()); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | public void setContextHolder(ApplicationContextHolder holder) |
| 57 | { |
| 58 | _storage = holder.getContext().getLocalStorage(); |
| 59 | } |
| 60 | |
| 61 | public void registryDidShutdown() |
| 62 | { |
| 63 | if (!_autoWrite) |
| 64 | { |
| 65 | return; |
| 66 | } |
| 67 | // Save all loaded preference beans back to the preferences store |
| 68 | for (String pref: _prefs.keySet()) |
| 69 | { |
| 70 | write(pref); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | public Object read(String name) |
| 75 | { |
| 76 | // First check if preference is already read and cached |
| 77 | Object pref; |
| 78 | synchronized (this) |
| 79 | { |
| 80 | pref = _prefs.get(name); |
| 81 | } |
| 82 | if (pref != null) |
| 83 | { |
| 84 | return pref; |
| 85 | } |
| 86 | // Not read yet, get configuration for this preference |
| 87 | PreferenceContribution def = _prefsDef.get(name); |
| 88 | if (def == null) |
| 89 | { |
| 90 | _logger.warn("read(): '" + name + "' does not exist."); |
| 91 | return null; |
| 92 | } |
| 93 | // Instantiate this preference from the actual prefs repository (AppFW) |
| 94 | pref = load(def); |
| 95 | synchronized (this) |
| 96 | { |
| 97 | if (_prefs.get(name) == null) |
| 98 | { |
| 99 | _prefs.put(name, pref); |
| 100 | } |
| 101 | } |
| 102 | return pref; |
| 103 | } |
| 104 | |
| 105 | protected Object load(PreferenceContribution def) |
| 106 | { |
| 107 | Object prefs = null; |
| 108 | try |
| 109 | { |
| 110 | prefs = _storage.load(def.getName() + ".xml"); |
| 111 | } |
| 112 | catch (IOException e) |
| 113 | { |
| 114 | _logger.warn("load(): '" + def.getName() + "' could not be loaded", e); |
| 115 | } |
| 116 | if (prefs != null) |
| 117 | { |
| 118 | return prefs; |
| 119 | } |
| 120 | // CSOFF: IllegalCatchCheck |
| 121 | try |
| 122 | { |
| 123 | // Instantiate a new default value for this class |
| 124 | return def.getBeanClass().newInstance(); |
| 125 | } |
| 126 | catch (Exception e) |
| 127 | { |
| 128 | _logger.error("load(): '" + def.getName() + "' has failed.", e); |
| 129 | return null; |
| 130 | } |
| 131 | // CSON: IllegalCatchCheck |
| 132 | } |
| 133 | |
| 134 | @SuppressWarnings("unchecked") |
| 135 | public void write(String name) |
| 136 | { |
| 137 | // First check that preference is already read and cached |
| 138 | Object pref; |
| 139 | synchronized (this) |
| 140 | { |
| 141 | pref = _prefs.get(name); |
| 142 | } |
| 143 | if (pref == null) |
| 144 | { |
| 145 | _logger.warn("write(): '" + name + "' is not in cache."); |
| 146 | return; |
| 147 | } |
| 148 | PreferenceContribution def = _prefsDef.get(name); |
| 149 | store(def, pref); |
| 150 | if (def.getEventChannel() != null) |
| 151 | { |
| 152 | def.getEventChannel().push(pref); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | protected void store(PreferenceContribution def, Object pref) |
| 157 | { |
| 158 | try |
| 159 | { |
| 160 | _storage.save(pref, def.getName() + ".xml"); |
| 161 | } |
| 162 | catch (IOException e) |
| 163 | { |
| 164 | _logger.error("store():" + def.getName(), e); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | private final Log _logger; |
| 169 | private final Map<String, PreferenceContribution> _prefsDef; |
| 170 | private final Map<String, Object> _prefs = new HashMap<String, Object>(); |
| 171 | private final boolean _autoWrite; |
| 172 | private LocalStorage _storage; |
| 173 | } |