View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.jdiagnose.config;
17  
18  import java.util.ArrayList;
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.Map;
22  
23  /***
24   * @author jmccrindle
25   */
26  public class DefaultConfig implements Config {
27      
28      private ConfigDataService dataService = null;
29      private Map defaultValues = null;
30      private ArrayList listeners = new ArrayList();
31      
32      public DefaultConfig(ConfigDataService dataService, Map defaultValues) {
33          this.dataService = dataService;
34          this.defaultValues = defaultValues;
35      }
36      
37      /* (non-Javadoc)
38       * @see org.jdiagnose.config.Config#get(java.lang.String)
39       */
40      public Object get(String key) {
41          Object result = dataService.get(key);
42          if(result == null) {
43              result = defaultValues.get(key);
44          }
45          System.out.println("!!!!!!!!!!!!!!!!!!!!!! key " + key + " result " + result);
46          return result;
47      }
48  
49      /* (non-Javadoc)
50       * @see org.jdiagnose.config.Config#put(java.lang.String, java.lang.Object)
51       */
52      public void put(String key, Object newValue) throws ConfigException {
53          Object oldValue = dataService.get(key);
54          dataService.put(key, newValue);
55          List changeExceptions = null;
56          for (Iterator listenerIterator = listeners.iterator(); listenerIterator.hasNext();) {
57              ConfigChangeListener listener = (ConfigChangeListener) listenerIterator.next();
58              try {
59                  listener.onConfigChange(new ConfigChangeEvent(this, this, key,
60                          oldValue, newValue));
61              } catch (ConfigException e) {
62                  if(changeExceptions == null) {
63                      changeExceptions = new ArrayList();
64                  }
65                  changeExceptions.add(e);
66              }
67          }
68          if(changeExceptions != null) {
69              throw new ConfigChangeException(changeExceptions);
70          }
71      }
72  
73      /* (non-Javadoc)
74       * @see org.jdiagnose.config.Config#getDefaultValue(java.lang.String)
75       */
76      public Object getDefaultValue(String key) {
77          return defaultValues.get(key);
78      }
79  
80      public void setDefaultValues(Map defaultValues) {
81          this.defaultValues = defaultValues;
82      }
83  
84      public void setDataService(ConfigDataService dataService) {
85          this.dataService = dataService;
86      }
87      
88      public void setListeners(List listeners) {
89          this.listeners = new ArrayList(listeners);
90      }
91  
92  }