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 /** 20 * The SystemProperties implementation of an {@link org.settings4j.Connector}. 21 * <p> 22 * 23 * see also {@link System#getProperty(String, String)}. 24 * 25 * @author Harald.Brabenetz 26 * 27 */ 28 public class SystemPropertyConnector extends AbstractPropertyConnector { 29 30 /** General Logger for this Class. */ 31 private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(SystemPropertyConnector.class); 32 33 /** 34 * Very similar to <code>System.getProperty</code> except that the {@link SecurityException} is hidden. 35 * 36 * @param key The key to search for. 37 * @param def The default value to return. 38 * @return the string value of the system property, or the default value if there is no property with that key. 39 */ 40 @Override 41 protected String getProperty(final String key, final String def) { 42 try { 43 return System.getProperty(key, def); 44 } catch (final SecurityException e) { 45 LOG.info("Was not allowed to read system property value for key '{}'.", key); 46 return def; 47 } catch (final Throwable e) { // MS-Java throws com.ms.security.SecurityExceptionEx 48 LOG.warn("Exception reading system property value for key '{}': {}", key, e.getMessage()); 49 return def; 50 } 51 } 52 }