View Javadoc
1   /* ***************************************************************************
2    * Copyright (c) 2008 Brabenetz Harald, Austria.
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   *****************************************************************************/
17  
18  package org.settings4j.connector;
19  
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.settings4j.Connector;
25  import org.settings4j.ContentResolver;
26  import org.settings4j.ObjectResolver;
27  
28  /**
29   * Wrap a Connector and caches all Values.
30   * <p>
31   * This wrapper will be used if you add in your settings4j.xml the cached="true" Attribute to the Connector TAG.
32   * 
33   * @author Harald.Brabenetz
34   *
35   */
36  public class CachedConnectorWrapper implements Connector {
37  
38      private final Connector targetConnector;
39  
40      private final Map<String, String> cachedStrings = Collections.synchronizedMap(new HashMap<String, String>());
41      private final Map<String, byte[]> cachedContents = Collections.synchronizedMap(new HashMap<String, byte[]>());
42      private final Map<String, Object> cachedObjects = Collections.synchronizedMap(new HashMap<String, Object>());
43  
44  
45      /**
46       * @param targetConnector The connector to wrap.
47       */
48      public CachedConnectorWrapper(final Connector targetConnector) {
49          super();
50          this.targetConnector = targetConnector;
51      }
52  
53      /** {@inheritDoc} */
54      public byte[] getContent(final String key) {
55          byte[] result = this.cachedContents.get(key);
56          if (result != null) {
57              return result;
58          }
59  
60          result = this.targetConnector.getContent(key);
61  
62          if (result != null) {
63              this.cachedContents.put(key, result);
64          }
65          return result;
66      }
67  
68      /** {@inheritDoc} */
69      public Object getObject(final String key) {
70          Object result = this.cachedObjects.get(key);
71          if (result != null) {
72              return result;
73          }
74  
75          result = this.targetConnector.getObject(key);
76  
77          if (result != null) {
78              this.cachedObjects.put(key, result);
79          }
80          return result;
81      }
82  
83      /** {@inheritDoc} */
84      public String getString(final String key) {
85          String result = this.cachedStrings.get(key);
86          if (result != null) {
87              return result;
88          }
89  
90          result = this.targetConnector.getString(key);
91  
92          if (result != null) {
93              this.cachedStrings.put(key, result);
94          }
95          return result;
96      }
97  
98      /**
99       * @param key the key to clear from all caches.
100      */
101     public void clearCachedValue(final String key) {
102         this.cachedStrings.remove(key);
103         this.cachedContents.remove(key);
104         this.cachedObjects.remove(key);
105     }
106 
107     /*
108      * Delegating Methodes: 
109      */
110 
111     /** {@inheritDoc} */
112     public void addConnector(final Connector connector) {
113         this.targetConnector.addConnector(connector);
114     }
115 
116     /** {@inheritDoc} */
117     public void setContentResolver(final ContentResolver contentResolver) {
118         this.targetConnector.setContentResolver(contentResolver);
119     }
120 
121     /** {@inheritDoc} */
122     public void setObjectResolver(final ObjectResolver objectResolver) {
123         this.targetConnector.setObjectResolver(objectResolver);
124     }
125 
126     /** {@inheritDoc} */
127     public void init() {
128         this.targetConnector.init();
129     }
130 
131     /** {@inheritDoc} */
132     public String getName() {
133         return this.targetConnector.getName();
134     }
135 
136     /** {@inheritDoc} */
137     public void setName(final String name) {
138         this.targetConnector.setName(name);
139     }
140 
141 }