Example usage for java.util Properties isEmpty

List of usage examples for java.util Properties isEmpty

Introduction

In this page you can find the example usage for java.util Properties isEmpty.

Prototype

@Override
    public boolean isEmpty() 

Source Link

Usage

From source file:org.official.json.Property.java

/**
 * Converts a property file object into a JSONObject. The property file object is a table of name value pairs.
 * @param properties java.util.Properties
 * @return JSONObject//from www.ja  v  a 2s  . c  o m
 * @throws JSONException
 */
public static JSONObject toJSONObject(java.util.Properties properties) throws JSONException {
    JSONObject jo = new JSONObject();
    if (properties != null && !properties.isEmpty()) {
        Enumeration enumProperties = properties.propertyNames();
        while (enumProperties.hasMoreElements()) {
            String name = (String) enumProperties.nextElement();
            jo.put(name, properties.getProperty(name));
        }
    }
    return jo;
}

From source file:org.pepstock.jem.util.VariableSubstituter.java

/**
 * Substitutes in text all possible variables which are keys of properties
 * //from w  w  w .  ja  va2  s  .  c om
 * @param text text string to update
 * @param variables list of variables
 * @return text modified and feeded by values of variables
 */
public static String substitute(String text, Properties variables) {
    // checks if the string to change is null
    // or there are any properties
    if (text == null || variables.isEmpty()) {
        return text;
    }
    // sets return value to initial text
    String returnValue = text;
    // gets variables
    Set<Object> vars = variables.keySet();
    for (Object variable : vars) {
        String key = variable.toString();
        String value = variables.getProperty(key);
        // unix pattern 
        String pattern = "${" + key + "}";
        // to avoid never end loop, checks if value of variable is equal
        // to name of variable
        if (!key.equalsIgnoreCase(pattern)) {
            // replaces variable
            returnValue = StringUtils.replace(returnValue, pattern, value);
        }
    }
    return returnValue;
}

From source file:de.pawlidi.openaletheia.utils.PropertiesUtils.java

/**
 * <p>/*  w  ww.j  a v  a2s .c om*/
 * Checks if given properties object is empty or null.
 * </p>
 * 
 * @param properties
 *            the Properties to check, may be null
 * @return <code>true</code> if the Properties is empty or null
 */
public static boolean isEmpty(Properties properties) {
    return properties == null || properties.isEmpty();
}

From source file:JNDIUtil.java

/**
 * Get the JNDI Context.//from  w  w  w .j a  v  a2s.  c o m
 * <p/>
 * Don't forget to close it when done!
 *
 * @param jndiProperties JNDI properties.
 * @return The context.
 * @throws javax.naming.NamingException Error getting context.
 */
public static Context getNamingContext(final Properties jndiProperties) throws NamingException {
    Context context;
    try {
        context = jndiProperties.isEmpty() ? new InitialContext() : new InitialContext(jndiProperties);
    } catch (NamingException e) {
        System.out.println(
                "NamingException while try to create initialContext. jndiProperties are " + jndiProperties + e);
        throw ((NamingException) new NamingException("Failed to load InitialContext: " + jndiProperties)
                .initCause(e));
    }
    if (context == null) {
        throw new NamingException("Failed to create JNDI context.  Check that '" + Context.PROVIDER_URL + "', '"
                + Context.INITIAL_CONTEXT_FACTORY + "', '" + Context.URL_PKG_PREFIXES
                + "' are correctly configured in the supplied JNDI properties.");
    }

    return context;
}

From source file:org.dhatim.util.JNDIUtil.java

/**
 * Get the JNDI Context./*from   w  w w.j  a v  a 2 s  .  c o m*/
 * <p/>
 * Don't forget to close it when done!
 *
 * @param jndiProperties JNDI properties.
 * @return The context.
 * @throws javax.naming.NamingException Error getting context.
 */
public static Context getNamingContext(final Properties jndiProperties) throws NamingException {
    Context context;
    try {
        context = jndiProperties.isEmpty() ? new InitialContext() : new InitialContext(jndiProperties);
    } catch (NamingException e) {
        logger.error("NamingException while try to create initialContext. jndiProperties are " + jndiProperties,
                e);
        throw ((NamingException) new NamingException("Failed to load InitialContext: " + jndiProperties)
                .initCause(e));
    }
    if (context == null) {
        throw new NamingException("Failed to create JNDI context.  Check that '" + Context.PROVIDER_URL + "', '"
                + Context.INITIAL_CONTEXT_FACTORY + "', '" + Context.URL_PKG_PREFIXES
                + "' are correctly configured in the supplied JNDI properties.");
    }

    return context;
}

From source file:org.apache.kylin.common.persistence.JDBCSqlQueryFormatProvider.java

public static JDBCSqlQueryFormat createJDBCSqlQueriesFormat(String dialect) {
    String key = String.format(Locale.ROOT, "/metadata-jdbc-%s.properties", dialect.toLowerCase(Locale.ROOT));
    if (cache.containsKey(key)) {
        return new JDBCSqlQueryFormat(cache.get(key));
    } else {/*from  ww w. java 2s.  c om*/
        Properties props = new Properties();
        InputStream input = null;
        try {
            input = props.getClass().getResourceAsStream(key);
            props.load(input);
            if (!props.isEmpty()) {
                cache.put(key, props);
            }
            return new JDBCSqlQueryFormat(props);
        } catch (Exception e) {
            throw new RuntimeException(
                    String.format(Locale.ROOT, "Can't find properties named %s for metastore", key), e);
        } finally {
            IOUtils.closeQuietly(input);
        }
    }

}

From source file:com.glaf.core.jdbc.connection.ConnectionProviderFactory.java

public static ConnectionProvider createProvider(Properties properties) {
    if (properties == null || properties.isEmpty()) {
        return null;
    }//from w w w.ja  va 2 s.c  o  m
    String jdbcUrl = properties.getProperty(DBConfiguration.JDBC_URL);
    String cacheKey = DigestUtils.md5Hex(jdbcUrl);
    if (providerCache.get(cacheKey) != null) {
        return providerCache.get(cacheKey);
    }
    ConnectionProvider provider = createProvider(properties, null);
    providerCache.put(cacheKey, provider);
    return provider;
}

From source file:org.apache.solr.core.SolrXMLCoresLocator.java

/**
 * Serialize a coredescriptor as a String containing an XML &lt;core> tag.
 * @param cd the CoreDescriptor//from w  w w  . j av  a2 s .  c o  m
 * @return an XML representation of the CoreDescriptor
 */
protected static String buildCoreTag(CoreDescriptor cd) {

    StringBuilder builder = new StringBuilder(NEWLINE).append(INDENT).append("<core");
    for (Map.Entry<Object, Object> entry : cd.getPersistableStandardProperties().entrySet()) {
        builder.append(" ").append(entry.getKey()).append("=\"").append(entry.getValue()).append("\"");
    }

    Properties userProperties = cd.getPersistableUserProperties();
    if (userProperties.isEmpty()) {
        return builder.append("/>").append(NEWLINE).toString();
    }

    builder.append(">").append(NEWLINE);
    for (Map.Entry<Object, Object> entry : userProperties.entrySet()) {
        builder.append(INDENT).append(INDENT).append("<property name=\"").append(entry.getKey())
                .append("\" value=\"").append(entry.getValue()).append("\"/>").append(NEWLINE);
    }

    return builder.append("</core>").append(NEWLINE).toString();

}

From source file:org.nuxeo.theme.Utils.java

public static void loadProperties(final Properties properties, final String resourceName) {
    if (properties.isEmpty()) {
        InputStream in = null;//from w  ww. j  a  va 2 s.co  m
        try {
            in = Utils.class.getResourceAsStream(resourceName);
            if (in != null) {
                properties.load(in);
            }
        } catch (IOException e) {
            log.error("Could not load properties", e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    log.error("Failed to close stream", e);
                }
            }
        }
    }
}

From source file:com.glaf.core.jdbc.connection.ConnectionProviderFactory.java

@SuppressWarnings("rawtypes")
private static ConnectionProvider createProvider(Properties properties, Map connectionProviderInjectionData) {
    if (properties == null || properties.isEmpty()) {
        return null;
    }//  www .  jav  a  2s  .com
    log.debug("---------------------------ConnectionProvider create----------------");
    ConnectionProvider provider = null;
    String providerClass = properties.getProperty(DBConfiguration.JDBC_PROVIDER);
    if (providerClass != null) {
        provider = initializeConnectionProviderFromConfig(providerClass);
    } else if (c3p0ConfigDefined(properties) && c3p0ProviderPresent()) {
        provider = initializeConnectionProviderFromConfig(
                "com.glaf.core.jdbc.connection.C3P0ConnectionProvider");
    } else if (druidConfigDefined(properties) && druidProviderPresent()) {
        provider = initializeConnectionProviderFromConfig(
                "com.glaf.core.jdbc.connection.DruidConnectionProvider");
    }

    if (provider == null) {
        provider = initializeConnectionProviderFromConfig(
                "com.glaf.core.jdbc.connection.DruidConnectionProvider");
        if (StringUtils.equals(properties.getProperty(DBConfiguration.JDBC_DRIVER), "org.sqlite.JDBC")) {
            provider = initializeConnectionProviderFromConfig(
                    "com.glaf.core.jdbc.connection.C3P0ConnectionProvider");
        }
    }

    if (connectionProviderInjectionData != null && connectionProviderInjectionData.size() != 0) {
        try {
            BeanInfo info = Introspector.getBeanInfo(provider.getClass());
            PropertyDescriptor[] descritors = info.getPropertyDescriptors();
            int size = descritors.length;
            for (int index = 0; index < size; index++) {
                String propertyName = descritors[index].getName();
                if (connectionProviderInjectionData.containsKey(propertyName)) {
                    Method method = descritors[index].getWriteMethod();
                    method.invoke(provider, new Object[] { connectionProviderInjectionData.get(propertyName) });
                }
            }
        } catch (IntrospectionException e) {
            throw new RuntimeException("Unable to inject objects into the connection provider", e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException("Unable to inject objects into the connection provider", e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException("Unable to inject objects into the connection provider", e);
        }
    }
    provider.configure(properties);
    log.debug("---------------------------ConnectionProvider end----------------");
    return provider;
}