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.File;
20  import java.io.IOException;
21  import java.io.UnsupportedEncodingException;
22  
23  import org.settings4j.ContentResolver;
24  import org.settings4j.contentresolver.FSContentResolver;
25  import org.settings4j.contentresolver.UnionContentResolver;
26  
27  /**
28   * The FileSystem implementation of an {@link org.settings4j.Connector}.
29   * <p>
30   * 
31   * @author Harald.Brabenetz
32   */
33  public class FSConnector extends AbstractConnector {
34  
35      /** General Logger for this Class. */
36      private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(FSConnector.class);
37  
38      private final FSContentResolver fsContentResolver = new FSContentResolver();
39      private ContentResolver unionContentResolver = new UnionContentResolver(fsContentResolver);
40      private String charset = "UTF-8";
41  
42      /** {@inheritDoc} */
43      public byte[] getContent(final String key) {
44          return fsContentResolver.getContent(key);
45      }
46  
47      /** {@inheritDoc} */
48      public Object getObject(final String key) {
49          if (getObjectResolver() != null) {
50              return getObjectResolver().getObject(key, unionContentResolver);
51          }
52          // else
53          return null;
54      }
55  
56      /** {@inheritDoc} */
57      public String getString(final String key) {
58          try {
59              final byte[] content = getContent(key);
60              if (content != null) {
61                  return new String(fsContentResolver.getContent(key), charset);
62              }
63              // else
64              return null;
65  
66          } catch (final UnsupportedEncodingException e) {
67              // should never occure with "UTF-8"
68              LOG.error("Charset not found: " + charset, e);
69              return null;
70          }
71      }
72  
73      /**
74       * @param key The Settings4j Key.
75       * @param value The value to Store.
76       * @throws IOException if an error occured.
77       */
78      public void setContent(final String key, final byte[] value) throws IOException {
79          fsContentResolver.setContent(key, value);
80      }
81  
82      /**
83       * @param key The Settings4j Key.
84       * @param value The value to Store.
85       * @throws IOException if an error occured.
86       */
87      public void setString(final String key, final String value) throws IOException {
88          try {
89              setContent(key, value.getBytes(charset));
90          } catch (final UnsupportedEncodingException e) {
91              // should never occure with "UTF-8"
92              LOG.error("Charset not found: " + charset, e);
93          }
94      }
95  
96      public String getCharset() {
97          return charset;
98      }
99  
100     public void setCharset(final String charset) {
101         this.charset = charset;
102     }
103 
104     /**
105      * Delegate the rootFolderPath to the {@link FSContentResolver#setRootFolderPath(String)}.
106      * 
107      * @param rootFolderPath The root Folder Path where the settings could be stored.
108      */
109     public void setRootFolderPath(final String rootFolderPath) {
110         fsContentResolver.setRootFolderPath(rootFolderPath);
111     }
112 
113     /** {@inheritDoc} */
114     @Override
115     public void setContentResolver(final ContentResolver contentResolver) {
116         unionContentResolver = new UnionContentResolver(fsContentResolver);
117         unionContentResolver.addContentResolver(contentResolver);
118     }
119 
120     /**
121      * return the root of this FileSystem ContenResolver.
122      * <p>
123      * if no one is set, the "." will be returned.
124      * 
125      * @return the root of this FileSystem ContenResolver.
126      */
127     public File getRootFolder() {
128         return fsContentResolver.getRootFolder();
129     }
130 }