1e4f0983ce7449fcd011af90f606d33883781c74
[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 com.google.common.base.Strings;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 /**
16  * Provides utilities for system properties.
17  *
18  * @author Thomas Pantelis
19  */
20 public final class PropertyUtils {
21
22     private static final Logger LOG = LoggerFactory.getLogger(PropertyUtils.class);
23
24     private PropertyUtils() {
25     }
26
27     /**
28      * Obtains the given property from the System properties and returns as an int. If the property
29      * is not found the specified default value is returned. If the property value can't be parsed
30      * to an int, a warning is logged and the default value is returned.
31      *
32      * @param propName the name of the property to get
33      * @param defaultValue the default value
34      * @return the System property as an int or the <code>defaultValue</code> if not found.
35      */
36     public static int getIntSystemProperty( String propName, int defaultValue ) {
37         int propValue = defaultValue;
38         String strValue = System.getProperty(propName);
39         if (!Strings.isNullOrEmpty(strValue) && !strValue.trim().isEmpty() ) {
40             try {
41                 propValue = Integer.parseInt(strValue);
42             } catch (NumberFormatException e) {
43                 LOG.warn("Cannot parse value {} for system property {}, using default {}",
44                          strValue, propName, defaultValue);
45             }
46         }
47
48         return propValue;
49     }
50 }