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.util;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import org.apache.commons.collections4.Transformer;
24  import org.apache.commons.collections4.map.LazyMap;
25  import org.settings4j.Connector;
26  
27  /**
28   * This Wrapper makes the Getter-Methods of Connectors simply accessible by Expressionlanguages like JSP 2.0, Velocity
29   * or Freemarker.<BR />
30   * <BR />
31   * <B>Example:</B><BR />
32   * <code>${connectors.string['xyz']}</code> returns the first founded Value in all Connectors:
33   * <code>connector.getString("xyz");</code>
34   * 
35   * @author Harald.Brabenetz
36   */
37  public class ELConnectorWrapper {
38  
39      private final Connector[] connectors;
40  
41      /**
42       * The list of all connectors, where Values can be searched.
43       * 
44       * @param connectors a array of Connectors which will be processed in Sequence.
45       */
46      public ELConnectorWrapper(final Connector[] connectors) {
47          super();
48          this.connectors = connectors;
49      }
50  
51      /**
52       * Usage: <code>${connectors.string['xyz']}</code> returns the first founded Value in all Connectors:
53       * <code>connector.getString("xyz");</code>.
54       * 
55       * @return the first founded Value in all connectors
56       */
57      public Map<String, String> getString() {
58          final Transformer<String, String> transformer = new Transformer<String, String>() {
59  
60              public String transform(final String input) {
61                  if (input != null) {
62                      final String key = input.toString();
63                      for (Connector connector : ELConnectorWrapper.this.connectors) {
64                          final String result = connector.getString(key);
65                          if (result != null) {
66                              return result;
67                          }
68                      }
69                  }
70                  return null;
71              }
72          };
73          return LazyMap.lazyMap(new HashMap<String, String>(), transformer);
74      }
75  
76      /**
77       * Usage: <code>${connectors.content['xyz']}</code> returns the first founded Value in all Connectors:
78       * <code>connector.getContent("xyz");</code>.
79       * 
80       * @return the first founded Value in all connectors
81       */
82      public Map<String, byte[]> getContent() {
83          final Transformer<String, byte[]> transformer = new Transformer<String, byte[]>() {
84  
85              public byte[] transform(final String input) {
86                  if (input != null) {
87                      final String key = input.toString();
88                      for (Connector connector : ELConnectorWrapper.this.connectors) {
89                          final byte[] result = connector.getContent(key);
90                          if (result != null) {
91                              return result;
92                          }
93                      }
94                  }
95                  return null;
96              }
97          };
98          return LazyMap.lazyMap(new HashMap<String, byte[]>(), transformer);
99      }
100 
101     /**
102      * Usage: <code>${connectors.object['xyz']}</code> returns the first founded Value in all Connectors:
103      * <code>connector.getObject("xyz");</code>.
104      * 
105      * @return the first founded Value in all connectors
106      */
107     public Map<String, Object> getObject() {
108         final Transformer<String, Object> transformer = new Transformer<String, Object>() {
109 
110             public Object transform(final String input) {
111                 if (input != null) {
112                     final String key = input.toString();
113                     for (Connector connector : ELConnectorWrapper.this.connectors) {
114                         final Object result = connector.getObject(key);
115                         if (result != null) {
116                             return result;
117                         }
118                     }
119                 }
120                 return null;
121             }
122         };
123         return LazyMap.lazyMap(new HashMap<String, Object>(), transformer);
124     }
125 }