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.connector;
19  
20  /**
21   * Basic Implementation of {@link org.settings4j.Connector}s whiche are Property-related.
22   * <p>
23   * Only #getProperty(String, String) must be implemented.
24   * Example implementations are {@link PropertyFileConnector} or {@link SystemPropertyConnector}.
25   * 
26   * @author Harald.Brabenetz
27   *
28   */
29  public abstract class AbstractPropertyConnector extends AbstractConnector {
30  
31      /** {@inheritDoc} */
32      public byte[] getContent(final String key) {
33          final String path = getString(key);
34          if (path != null && getContentResolver() != null) {
35              return getContentResolver().getContent(path);
36          }
37          // else
38          return null;
39  
40      }
41  
42      /** {@inheritDoc} */
43      public Object getObject(final String key) {
44          final String path = getString(key);
45          if (path != null && getObjectResolver() != null) {
46              return getObjectResolver().getObject(path, getContentResolver());
47          }
48          // else
49          return null;
50          
51      }
52  
53      /** {@inheritDoc} */
54      public String getString(final String key) {
55          return getProperty(key, null);
56      }
57  
58      /**
59       * Very similar to <code>System.getProperty</code> except that the {@link SecurityException} is hidden.
60       * 
61       * @param key The key to search for.
62       * @param defaultValue The default value to return.
63       * @return the string value of the system property, or the default value if there is no property with that key.
64       */
65      protected abstract String getProperty(String key, String defaultValue);
66  }