cc8f94d1822448d1ce5a1aacb1ec8c1c3002e778
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / PropertyUtils.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.yangtools.util;
10
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14 import com.google.common.base.Strings;
15
16 /**
17  * Provides utilities for system properties.
18  *
19  * @author Thomas Pantelis
20  */
21 public final class PropertyUtils {
22
23     private static final Logger LOG = LoggerFactory.getLogger(PropertyUtils.class);
24
25     private PropertyUtils() {
26     }
27
28     /**
29      * Obtains the given property from the System properties and returns as an int. If the property
30      * is not found the specified default value is returned. If the property value can't be parsed
31      * to an int, a warning is logged and the default value is returned.
32      *
33      * @param propName the name of the property to get
34      * @param defaultValue the default value
35      * @return the System property as an int or the <code>defaultValue</code> if not found.
36      */
37     public static int getIntSystemProperty( String propName, int defaultValue ) {
38         int propValue = defaultValue;
39         String strValue = System.getProperty(propName);
40         if (!Strings.isNullOrEmpty(strValue) && !strValue.trim().isEmpty() ) {
41             try {
42                 propValue = Integer.parseInt(strValue);
43             } catch (NumberFormatException e) {
44                 LOG.warn("Cannot parse value {} for system property {}, using default {}",
45                          strValue, propName, defaultValue);
46             }
47         }
48
49         return propValue;
50     }
51 }