View Javadoc
1   /*
2    * #%L
3    * settings4j
4    * ===============================================================
5    * Copyright (C) 2008 - 2015 Brabenetz Harald, Austria
6    * ===============================================================
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package org.settings4j.util;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.commons.collections4.Transformer;
26  import org.apache.commons.collections4.map.LazyMap;
27  import org.settings4j.Connector;
28  
29  /**
30   * This Wrapper makes the Getter-Methods of Connectors simply accessible by Expressionlanguages like JSP 2.0, Velocity
31   * or Freemarker.<br>
32   * <br>
33   * <B>Example:</B><br>
34   * <code>${connectors.string['xyz']}</code> returns the first founded Value in all Connectors:
35   * <code>connector.getString("xyz");</code>
36   *
37   * @author Harald.Brabenetz
38   */
39  public class ELConnectorWrapper {
40  
41      private final Connector[] connectors;
42  
43      /**
44       * The list of all connectors, where Values can be searched.
45       *
46       * @param connectors a array of Connectors which will be processed in Sequence.
47       */
48      public ELConnectorWrapper(final Connector... connectors) {
49          super();
50          this.connectors = connectors;
51      }
52  
53      /**
54       * Usage: <code>${connectors.string['xyz']}</code> returns the first founded Value in all Connectors:
55       * <code>connector.getString("xyz");</code>.
56       *
57       * @return the first founded Value in all connectors
58       */
59      public Map<String, String> getString() {
60          final Transformer<String, String> transformer = new Transformer<String, String>() {
61  
62              @Override
63              public String transform(final String key) {
64                  if (key != null) {
65                      for (Connector connector : ELConnectorWrapper.this.connectors) {
66                          final String result = connector.getString(key);
67                          if (result != null) {
68                              return result;
69                          }
70                      }
71                  }
72                  return null;
73              }
74          };
75          return LazyMap.lazyMap(new HashMap<String, String>(), transformer);
76      }
77  
78      /**
79       * Usage: <code>${connectors.content['xyz']}</code> returns the first founded Value in all Connectors:
80       * <code>connector.getContent("xyz");</code>.
81       *
82       * @return the first founded Value in all connectors
83       */
84      public Map<String, byte[]> getContent() {
85          final Transformer<String, byte[]> transformer = new Transformer<String, byte[]>() {
86  
87              // SuppressWarnings PMD.ReturnEmptyArrayRatherThanNull: returning null for this byte-Arrays is OK.
88              @Override
89              @SuppressWarnings("PMD.ReturnEmptyArrayRatherThanNull")
90              public byte[] transform(final String key) {
91                  if (key != null) {
92                      for (Connector connector : ELConnectorWrapper.this.connectors) {
93                          final byte[] result = connector.getContent(key);
94                          if (result != null) {
95                              return result;
96                          }
97                      }
98                  }
99                  return null;
100             }
101         };
102         return LazyMap.lazyMap(new HashMap<String, byte[]>(), transformer);
103     }
104 
105     /**
106      * Usage: <code>${connectors.object['xyz']}</code> returns the first founded Value in all Connectors:
107      * <code>connector.getObject("xyz");</code>.
108      *
109      * @return the first founded Value in all connectors
110      */
111     public Map<String, Object> getObject() {
112         final Transformer<String, Object> transformer = new Transformer<String, Object>() {
113 
114             @Override
115             public Object transform(final String key) {
116                 if (key != null) {
117                     for (Connector connector : ELConnectorWrapper.this.connectors) {
118                         final Object result = connector.getObject(key);
119                         if (result != null) {
120                             return result;
121                         }
122                     }
123                 }
124                 return null;
125             }
126         };
127         return LazyMap.lazyMap(new HashMap<String, Object>(), transformer);
128     }
129 }