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  package org.settings4j.connector;
18  
19  import java.io.UnsupportedEncodingException;
20  
21  import org.settings4j.ContentResolver;
22  import org.settings4j.contentresolver.ClasspathContentResolver;
23  import org.settings4j.contentresolver.UnionContentResolver;
24  
25  /**
26   * Connector implementation to read Settings from the Classpath.
27   * <p>
28   * Default Charset "UTF-8" is used to read the content from Classpath.
29   * 
30   * 
31   * @author Harald.Brabenetz
32   *
33   */
34  public class ClasspathConnector extends AbstractConnector {
35  
36      /** General Logger for this Class. */
37      private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(ClasspathConnector.class);
38  
39      private final ClasspathContentResolver classpathContentResolver = new ClasspathContentResolver();
40      private ContentResolver unionContentResolver = new UnionContentResolver(this.classpathContentResolver);
41      private String charset = "UTF-8";
42  
43      /** {@inheritDoc} */
44      public byte[] getContent(final String key) {
45          return this.classpathContentResolver.getContent(key);
46      }
47  
48      /** {@inheritDoc} */
49      public Object getObject(final String key) {
50          if (getObjectResolver() != null) {
51              return getObjectResolver().getObject(key, this.unionContentResolver);
52          }
53          // else
54          return null;
55          
56      }
57  
58      /** {@inheritDoc} */
59      public String getString(final String key) {
60          try {
61              final byte[] content = getContent(key);
62              if (content != null) {
63                  return new String(this.classpathContentResolver.getContent(key), this.charset);
64              }
65              // else
66              return null;
67              
68          } catch (final UnsupportedEncodingException e) {
69              // should never occure with "UTF-8"
70              LOG.error("Charset not found: " + this.charset, e);
71              return null;
72          }
73      }
74  
75      public String getCharset() {
76          return this.charset;
77      }
78  
79      public void setCharset(final String charset) {
80          this.charset = charset;
81      }
82  
83      /** {@inheritDoc} */
84      @Override
85      public void setContentResolver(final ContentResolver contentResolver) {
86          this.unionContentResolver = new UnionContentResolver(this.classpathContentResolver);
87          this.unionContentResolver.addContentResolver(contentResolver);
88      }
89  }