View Javadoc

1   /*
2    * $Id: CombinedGlueConfig.java 3054 2011-12-19 17:51:55Z andrewinkler $
3    * ============================================================================
4    * Project awtools-config
5    * Copyright (c) 2004-2010 by Andre Winkler. All rights reserved.
6    * ============================================================================
7    *          GNU LESSER GENERAL PUBLIC LICENSE
8    *  TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
9    *
10   *  This library is free software; you can redistribute it and/or
11   *  modify it under the terms of the GNU Lesser General Public
12   *  License as published by the Free Software Foundation; either
13   *  version 2.1 of the License, or (at your option) any later version.
14   *
15   *  This library is distributed in the hope that it will be useful,
16   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   *  Lesser General Public License for more details.
19   *
20   *  You should have received a copy of the GNU Lesser General Public
21   *  License along with this library; if not, write to the Free Software
22   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23   *
24   */
25  
26  package de.awtools.config;
27  
28  import java.io.IOException;
29  import java.util.Iterator;
30  import java.util.LinkedList;
31  import java.util.List;
32  
33  import org.apache.commons.collections.IteratorUtils;
34  import org.apache.commons.lang.StringUtils;
35  
36  /**
37   * Verwaltet mehrere {@link GlueConfig}s. Eine Konfiguration überschreibt
38   * ggf. die Eigenschaften der nachfolgenden Konfiguration.
39   * 
40   * @author  $Author: andrewinkler $
41   * @version $Revision: 3054 $ $Date: 2011-12-19 18:51:55 +0100 (Mo, 19 Dez 2011) $
42   */
43  public final class CombinedGlueConfig extends AbstractGlueConfig {
44  
45      /**
46       * Die Liste der verwalteten Konfigurationen. Die Reihenfolge definiert
47       * eine Ordnung über die Konfigurationen. Auf der Suche nach einer
48       * Eigenschaft wird mit der Konfiguration an Index 0 begonnen. Ist diese
49       * Eigenschaft nicht definiert, wird die Suche mit der Konfiguration an
50       * Stelle 1 fortgeführt usw.<br>
51       * Die Konfiguration an Position 0 wird für das Ändern und die Neuanlage
52       * von Eigenschaften verwendet.
53       */
54      private final List<GlueConfig> configs = new LinkedList<GlueConfig>();
55  
56      /**
57       * Eine weitere Konfiguration hinzufügen.
58       *
59       * @param config Eine Konfiguration.
60       *
61       * @see #configs
62       */
63      public void addConfig(final GlueConfig config) {
64          configs.add(config);
65      }
66  
67      public void load() throws IOException {
68          for (GlueConfig cc : configs) {
69              cc.load();
70          }
71      }
72  
73      /**
74       * Gespeichert wird nur die erste Konfiguration!
75       */
76      public void save() throws IOException {
77          if (configs.size() == 0) {
78              throw new IllegalStateException("There is no configuration added!");
79          }
80          GlueConfig cc = configs.get(0);
81          cc.save();
82      }
83  
84      /**
85       * Modifikationen oder Neuanlagen von Eigenschaften werden in die erste
86       * Konfiguration geschrieben.
87       *
88       * @param key Der Schlüssel.
89       * @param value Wert der Eigenschaft.
90       */
91      public void setProperty(final String key, final String value) {
92          if (configs.size() == 0) {
93              throw new IllegalStateException("There is no configuration added!");
94          }
95          GlueConfig cc = configs.get(0);
96          cc.setProperty(key, value);
97      }
98  
99      /**
100      * Verwaltet den internen Zugang zu der Eigenschaft
101      * {@link CombinedGlueConfig#configs}.
102      * 
103      * @param key Der Schlüssel.
104      * @return Die gefundene Eigenschaft 
105      */
106     @Override
107     protected Object doGetProperty(final String key) {
108         Object result = null;
109         for (GlueConfig gc : configs) {
110             String value = gc.getString(key);
111             if (StringUtils.isNotBlank(value)) {
112                 result = value;
113                 break;
114             }   
115         }
116         return result;
117     }
118 
119     @SuppressWarnings("unchecked")
120 	public Iterator<String> getKeyIterator() {
121         @SuppressWarnings("rawtypes")
122 		Iterator[] iterators = new Iterator[configs.size()];
123         int index = 0;
124         for (GlueConfig gc : configs) {
125             iterators[index] = gc.getKeyIterator();
126             index++;
127         }
128         return (IteratorUtils.chainedIterator(iterators));
129     }
130 
131 }