| 1 | /** |
| 2 | * Copyright (C) 2008 NetMind Consulting Bt. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 3 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | |
| 19 | package hu.netmind.beankeeper.config.impl; |
| 20 | |
| 21 | import hu.netmind.beankeeper.config.ConfigurationTracker; |
| 22 | import hu.netmind.beankeeper.config.ExtendedConfigurationListener; |
| 23 | import hu.netmind.beankeeper.common.StoreException; |
| 24 | import org.apache.commons.configuration.AbstractConfiguration; |
| 25 | import org.apache.commons.configuration.Configuration; |
| 26 | import org.apache.commons.configuration.event.ConfigurationListener; |
| 27 | import org.apache.commons.configuration.PropertiesConfiguration; |
| 28 | import java.util.*; |
| 29 | |
| 30 | /** |
| 31 | * Configuration can be queried and altered through this tracker. |
| 32 | * @author Brautigam Robert |
| 33 | * @version Revision: $Revision$ |
| 34 | */ |
| 35 | public class ConfigurationTrackerImpl implements ConfigurationTracker |
| 36 | { |
| 37 | private AbstractConfiguration configuration = null; |
| 38 | private List listeners = new ArrayList(); |
| 39 | |
| 40 | public void init(Map parameters) |
| 41 | { |
| 42 | try |
| 43 | { |
| 44 | // Initialize with property reader configuration |
| 45 | setConfiguration(new PropertiesConfiguration("beankeeper.properties")); |
| 46 | } catch ( Exception e ) { |
| 47 | throw new StoreException("fatal error, could not load default configuration",e); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | public void release() |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | public Configuration getConfiguration() |
| 56 | { |
| 57 | return configuration; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Set a new configuration for beankeeper. If you set a new configuration |
| 62 | * object, all values will be re-read on the fly. |
| 63 | */ |
| 64 | public void setConfiguration(AbstractConfiguration configuration) |
| 65 | { |
| 66 | if ( configuration == null ) |
| 67 | return; |
| 68 | // First clear all listeners from previous configuration if |
| 69 | // there was one |
| 70 | if ( this.configuration != null ) |
| 71 | { |
| 72 | Iterator listenersIterator = listeners.iterator(); |
| 73 | while ( listenersIterator.hasNext() ) |
| 74 | this.configuration.removeConfigurationListener((ConfigurationListener) listenersIterator.next()); |
| 75 | } |
| 76 | // Set new configuration |
| 77 | this.configuration=configuration; |
| 78 | // Set all listeners, and invoke reload on all |
| 79 | Iterator listenersIterator = listeners.iterator(); |
| 80 | while ( listenersIterator.hasNext() ) |
| 81 | { |
| 82 | ExtendedConfigurationListener listener = (ExtendedConfigurationListener) listenersIterator.next(); |
| 83 | this.configuration.addConfigurationListener(listener); |
| 84 | listener.configurationReload(); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Add a listener to the configuration tracker. |
| 90 | */ |
| 91 | public void addListener(ExtendedConfigurationListener listener) |
| 92 | { |
| 93 | // Add to our internal listener list |
| 94 | listeners.add(listener); |
| 95 | // Add to current configuration |
| 96 | configuration.addConfigurationListener(listener); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Remove a listener from the configuration tracker. |
| 101 | */ |
| 102 | public void removeListener(ExtendedConfigurationListener listener) |
| 103 | { |
| 104 | // Remove from our internal listener list |
| 105 | listeners.remove(listener); |
| 106 | // Remove from current configuration |
| 107 | configuration.removeConfigurationListener(listener); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | |