| 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.hiveutils.service.impl; |
| 16 | |
| 17 | import java.util.HashMap; |
| 18 | import java.util.Map; |
| 19 | |
| 20 | import org.apache.commons.logging.Log; |
| 21 | import org.apache.hivemind.events.RegistryShutdownListener; |
| 22 | |
| 23 | import net.sourceforge.hiveutils.service.PreferencesManager; |
| 24 | import net.sourceforge.hiveutils.util.PreferencesUtils; |
| 25 | import net.sourceforge.hiveutils.util.PrefsMapper; |
| 26 | |
| 27 | /** |
| 28 | * @author Jean-Francois Poilpret |
| 29 | */ |
| 30 | public class PreferencesManagerImpl |
| 31 | implements PreferencesManager, RegistryShutdownListener |
| 32 | { |
| 33 | public PreferencesManagerImpl( Log logger, |
| 34 | Map<String, PreferenceContribution> prefsDef, |
| 35 | Map<Class, PrefsMapper> mappers, |
| 36 | boolean autoWrite) |
| 37 | { |
| 38 | _logger = logger; |
| 39 | _autoWrite = autoWrite; |
| 40 | _prefsDef = prefsDef; |
| 41 | _mappers = mappers; |
| 42 | } |
| 43 | |
| 44 | public void registryDidShutdown() |
| 45 | { |
| 46 | if (!_autoWrite) |
| 47 | { |
| 48 | return; |
| 49 | } |
| 50 | // Save all loaded preference beans back to the preferences store |
| 51 | for (String pref: _prefs.keySet()) |
| 52 | { |
| 53 | write(pref); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | public Object read(String name) |
| 58 | { |
| 59 | // First check if preference is already read and cached |
| 60 | Object pref; |
| 61 | synchronized (this) |
| 62 | { |
| 63 | pref = _prefs.get(name); |
| 64 | } |
| 65 | if (pref != null) |
| 66 | { |
| 67 | return pref; |
| 68 | } |
| 69 | // Not read yet, get configuration for this preference |
| 70 | PreferenceContribution def = _prefsDef.get(name); |
| 71 | if (def == null) |
| 72 | { |
| 73 | _logger.warn("read(): '" + name + "' does not exist."); |
| 74 | return null; |
| 75 | } |
| 76 | // Instantiate this preference from the actual prefs repository (JDK1.4) |
| 77 | pref = load(def); |
| 78 | synchronized (this) |
| 79 | { |
| 80 | if (_prefs.get(name) == null) |
| 81 | { |
| 82 | _prefs.put(name, pref); |
| 83 | } |
| 84 | } |
| 85 | return pref; |
| 86 | } |
| 87 | |
| 88 | protected Object load(PreferenceContribution def) |
| 89 | { |
| 90 | return PreferencesUtils.load( def.getName(), |
| 91 | def.getBeanClass(), |
| 92 | def.getType(), |
| 93 | _mappers); |
| 94 | } |
| 95 | |
| 96 | @SuppressWarnings("unchecked") |
| 97 | public void write(String name) |
| 98 | { |
| 99 | // First check that preference is already read and cached |
| 100 | Object pref; |
| 101 | synchronized (this) |
| 102 | { |
| 103 | pref = _prefs.get(name); |
| 104 | } |
| 105 | if (pref == null) |
| 106 | { |
| 107 | _logger.warn("write(): '" + name + "' is not in cache."); |
| 108 | return; |
| 109 | } |
| 110 | PreferenceContribution def = _prefsDef.get(name); |
| 111 | store(def, pref); |
| 112 | if (def.getEventChannel() != null) |
| 113 | { |
| 114 | def.getEventChannel().push(pref); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | protected void store(PreferenceContribution def, Object pref) |
| 119 | { |
| 120 | PreferencesUtils.store( def.getName(), |
| 121 | def.getType(), |
| 122 | pref, |
| 123 | _mappers); |
| 124 | } |
| 125 | |
| 126 | private final Log _logger; |
| 127 | private final Map<String, PreferenceContribution> _prefsDef; |
| 128 | private final Map<Class, PrefsMapper> _mappers; |
| 129 | private final Map<String, Object> _prefs = new HashMap<String, Object>(); |
| 130 | private final boolean _autoWrite; |
| 131 | } |