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.connector;
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.UnsupportedEncodingException;
25  import java.nio.charset.Charset;
26  
27  import org.apache.commons.io.Charsets;
28  import org.settings4j.ContentResolver;
29  import org.settings4j.contentresolver.FSContentResolver;
30  import org.settings4j.contentresolver.UnionContentResolver;
31  
32  /**
33   * The FileSystem implementation of an {@link org.settings4j.Connector}.
34   *
35   * @author Harald.Brabenetz
36   */
37  public class FSConnector extends AbstractConnector {
38  
39      private final FSContentResolver fsContentResolver;
40      private UnionContentResolver unionContentResolver;
41      private Charset charset = Charsets.UTF_8;
42  
43      /** Default Constructor (e.g. use in settings4j.xml). */
44      public FSConnector() {
45          this(new FSContentResolver());
46      }
47  
48      /**
49       * protected Constructor for extensions and Unit-Tests.
50       *
51       * @param fsContentResolver
52       *        {@link FSContentResolver}
53       */
54      protected FSConnector(final FSContentResolver fsContentResolver) {
55          super();
56          this.fsContentResolver = fsContentResolver;
57          this.unionContentResolver = new UnionContentResolver(this.fsContentResolver);
58      }
59  
60      @Override
61      public byte[] getContent(final String key) {
62          return this.fsContentResolver.getContent(key);
63      }
64  
65      @Override
66      public Object getObject(final String key) {
67          if (getObjectResolver() != null) {
68              return getObjectResolver().getObject(key, this.unionContentResolver);
69          }
70          // else
71          return null;
72      }
73  
74      @Override
75      public String getString(final String key) {
76          final byte[] content = getContent(key);
77          if (content != null) {
78              return new String(this.fsContentResolver.getContent(key), this.charset);
79          }
80          // else
81          return null;
82      }
83  
84      /**
85       * @param key The Settings4j Key.
86       * @param value The value to Store.
87       * @throws IOException if an error occured.
88       */
89      public void setContent(final String key, final byte[] value) throws IOException {
90          this.fsContentResolver.setContent(key, value);
91      }
92  
93      /**
94       * @param key The Settings4j Key.
95       * @param value The value to Store.
96       * @throws IOException if an error occured.
97       */
98      public void setString(final String key, final String value) throws IOException {
99          try {
100             setContent(key, value.getBytes(this.charset));
101         } catch (final UnsupportedEncodingException e) {
102             throw new UnsupportedOperationException(String.format("Charset not found: %s", this.charset), e);
103         }
104     }
105 
106     public String getCharset() {
107         return this.charset.name();
108     }
109 
110     /**
111      * @param charset
112      *        a valid {@link Charset}
113      * @see Charset#isSupported(String)
114      */
115     public void setCharset(final String charset) {
116         this.charset = Charset.forName(charset);
117     }
118 
119     /**
120      * Delegate the rootFolderPath to the {@link FSContentResolver#setRootFolderPath(String)}.
121      *
122      * @param rootFolderPath The root Folder Path where the settings could be stored.
123      */
124     public void setRootFolderPath(final String rootFolderPath) {
125         this.fsContentResolver.setRootFolderPath(rootFolderPath);
126     }
127 
128     @Override
129     public void setContentResolver(final ContentResolver contentResolver) {
130         this.unionContentResolver = new UnionContentResolver(this.fsContentResolver);
131         this.unionContentResolver.addContentResolver(contentResolver);
132     }
133 
134     @Override
135     protected ContentResolver getContentResolver() {
136         if (hasCustomContentResolver()) {
137             return this.unionContentResolver.getContentResolvers()[1];
138         }
139         return null;
140     }
141 
142     private boolean hasCustomContentResolver() {
143         return this.unionContentResolver.getContentResolvers().length > 1;
144     }
145     /**
146      * return the root of this FileSystem ContenResolver.
147      * <p>
148      * if no one is set, the "." will be returned.
149      * </p>
150      *
151      * @return the root of this FileSystem ContenResolver.
152      */
153     public File getRootFolder() {
154         return this.fsContentResolver.getRootFolder();
155     }
156 }