001/* 002 * (C) Copyright 2006-2019 Nuxeo (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Florent Guillaume 018 */ 019package org.nuxeo.ecm.core.blob; 020 021import static org.apache.commons.lang3.StringUtils.isBlank; 022import static org.apache.commons.lang3.StringUtils.isNotBlank; 023 024import java.util.Map; 025 026import org.apache.logging.log4j.LogManager; 027import org.apache.logging.log4j.Logger; 028import org.nuxeo.runtime.api.Framework; 029 030/** 031 * Basic configuration based on properties. 032 * 033 * @since 11.1 034 */ 035public class PropertyBasedConfiguration { 036 037 private static final Logger log = LogManager.getLogger(PropertyBasedConfiguration.class); 038 039 public final String systemPropertyPrefix; 040 041 public final Map<String, String> properties; 042 043 public PropertyBasedConfiguration(String systemPropertyPrefix, Map<String, String> properties) { 044 this.systemPropertyPrefix = systemPropertyPrefix; 045 this.properties = properties; 046 } 047 048 /** Gets a string property. */ 049 public String getProperty(String propertyName) { 050 return getProperty(propertyName, null); 051 } 052 053 /** Gets a string property, or the given default if undefined or blank. */ 054 public String getProperty(String propertyName, String defaultValue) { 055 String propValue = properties.get(propertyName); 056 if (isNotBlank(propValue)) { 057 return propValue; 058 } 059 if (systemPropertyPrefix != null) { 060 propValue = Framework.getProperty(systemPropertyPrefix + "." + propertyName); 061 if (isNotBlank(propValue)) { 062 return propValue; 063 } 064 } 065 return defaultValue; 066 } 067 068 /** Gets a long property, or -1 if undefined or blank. */ 069 public long getLongProperty(String key) { 070 String s = getProperty(key); 071 long value = -1; 072 if (!isBlank(s)) { 073 try { 074 value = Long.parseLong(s.trim()); 075 } catch (NumberFormatException e) { 076 log.error("Cannot parse long " + key + ": " + s); 077 } 078 } 079 return value; 080 } 081 082 /** Gets an integer property, or -1 if undefined or blank. */ 083 public int getIntProperty(String key) { 084 String s = getProperty(key); 085 int value = -1; 086 if (!isBlank(s)) { 087 try { 088 value = Integer.parseInt(s.trim()); 089 } catch (NumberFormatException e) { 090 log.error("Cannot parse integer " + key + ": " + s); 091 } 092 } 093 return value; 094 } 095 096 /** Gets a boolean property. */ 097 public boolean getBooleanProperty(String key) { 098 return Boolean.parseBoolean(getProperty(key)); 099 } 100 101}