List of usage examples for org.apache.commons.lang StringUtils defaultIfEmpty
public static String defaultIfEmpty(String str, String defaultStr)
Returns either the passed in String, or if the String is empty or null
, the value of defaultStr
.
From source file:net.ymate.platform.commons.i18n.I18N.java
/** * @param resourceName ???//from w ww . ja va 2s.com * @param key * @param defaultValue * @return ????key */ public static String load(String resourceName, String key, String defaultValue) { Map<String, Properties> _cache = __RESOURCES_CAHCES.get(current()); Properties _prop = _cache != null ? _cache.get(resourceName) : null; if (_prop == null) { if (__EVENT_HANDLER != null) { try { List<String> _localeResourceNames = resourceNames(current(), resourceName); InputStream _inputStream = null; for (String _localeResourceName : _localeResourceNames) { _inputStream = __EVENT_HANDLER.onLoadProperties(_localeResourceName); if (_inputStream != null) { break; } } if (_inputStream == null) { for (String _localeResourceName : _localeResourceNames) { _inputStream = I18N.class.getClassLoader().getResourceAsStream(_localeResourceName); if (_inputStream != null) { break; } } } if (_inputStream != null) { _prop = new Properties(); _prop.load(_inputStream); } if (_prop != null && !_prop.isEmpty()) { if (_cache == null) { __RESOURCES_CAHCES.put(current(), new ConcurrentHashMap<String, Properties>()); } __RESOURCES_CAHCES.get(current()).put(resourceName, _prop); } } catch (IOException e) { _LOG.warn("", RuntimeUtils.unwrapThrow(e)); } } } String _returnValue = null; if (_prop != null) { _returnValue = _prop.getProperty(key, defaultValue); } return StringUtils.defaultIfEmpty(_returnValue, defaultValue); }
From source file:net.ymate.platform.configuration.impl.DefaultConfigurationProvider.java
public String getString(String key, String defaultValue) { return StringUtils.defaultIfEmpty(getString(key), defaultValue); }
From source file:net.ymate.platform.configuration.impl.DefaultConfigurationProvider.java
public String getString(String category, String key, String defaultValue) { XMLConfigFileHandler.XMLCategory _category = __config.getCategory(category); if (_category == null) { return null; }//from ww w. j av a2 s . co m XMLConfigFileHandler.XMLProperty _prop = _category.getProperty(key); return StringUtils.defaultIfEmpty(_prop == null ? null : _prop.getContent(), defaultValue); }
From source file:net.ymate.platform.configuration.impl.DefaultModuleCfg.java
@SuppressWarnings("unchecked") public DefaultModuleCfg(YMP owner) { Map<String, String> _moduleCfgs = owner.getConfig().getModuleConfigs(IConfig.MODULE_NAME); ////from www . ja v a 2s .c om this.configHome = RuntimeUtils .replaceEnvVariable(StringUtils.defaultIfEmpty(_moduleCfgs.get("config_home"), "${root}")); // this.projectName = _moduleCfgs.get("project_name"); this.moduleName = _moduleCfgs.get("module_name"); // try { if (StringUtils.isNotBlank(_moduleCfgs.get("provider_class"))) { this.providerClass = (Class<? extends IConfigurationProvider>) ClassUtils .loadClass(_moduleCfgs.get("provider_class"), this.getClass()); } else { this.providerClass = DefaultConfigurationProvider.class; } } catch (Exception e) { this.providerClass = DefaultConfigurationProvider.class; } }
From source file:net.ymate.platform.core.i18n.I18N.java
/** * @param resourceName ???/*from w w w .ja v a 2 s. c o m*/ * @param key * @param defaultValue * @return ????key */ public static String load(String resourceName, String key, String defaultValue) { Locale _local = current(); Map<String, Properties> _cache = __RESOURCES_CAHCES.get(_local); Properties _prop = _cache != null ? _cache.get(resourceName) : null; if (_prop == null) { if (__EVENT_HANDLER != null) { try { List<String> _localeResourceNames = __doGetResourceNames(_local, resourceName); InputStream _inputStream = null; for (String _localeResourceName : _localeResourceNames) { _inputStream = __EVENT_HANDLER.onLoad(_localeResourceName); if (_inputStream != null) { _prop = new Properties(); _prop.load(_inputStream); break; } } if (_prop != null && !_prop.isEmpty()) { if (_cache == null) { __RESOURCES_CAHCES.put(_local, new ConcurrentHashMap<String, Properties>()); } __RESOURCES_CAHCES.get(_local).put(resourceName, _prop); } } catch (IOException e) { _LOG.warn("", RuntimeUtils.unwrapThrow(e)); } } } String _returnValue = null; if (_prop != null) { _returnValue = _prop.getProperty(key, defaultValue); } else { try { _returnValue = ResourceBundle.getBundle(resourceName, _local).getString(key); } catch (Exception ignored) { } } return StringUtils.defaultIfEmpty(_returnValue, defaultValue); }
From source file:net.ymate.platform.extra.scaffold.TomcatServScaffold.java
/** * ???//from w ww .j a v a 2 s . c om */ public TomcatServScaffold() { // ??? __FREEMARKER_CFG = new Configuration(); __FREEMARKER_CFG.setClassForTemplateLoading(this.getClass(), "/"); __FREEMARKER_CFG.setObjectWrapper(new DefaultObjectWrapper()); __FREEMARKER_CFG.setDefaultEncoding("UTF-8"); // __catalinaHome = System.getProperty(__PARAM_CATALINA_HOME); if (StringUtils.isBlank(__catalinaHome)) { throw new NullArgumentException(__PARAM_CATALINA_HOME); } __properties.put("catalina_home", __catalinaHome); // __catalinaBase = System.getProperty(__PARAM_CATALINA_BASE); if (StringUtils.isBlank(__catalinaBase)) { throw new NullArgumentException(__PARAM_CATALINA_BASE); } __properties.put("catalina_base", __catalinaBase); // __tomcatVersion = System.getProperty(__PARAM_TOMCAT_VERSION); if (StringUtils.isBlank(__tomcatVersion) || !(__tomcatVersion.equals("6") || __tomcatVersion.equals("7"))) { throw new IllegalArgumentException(__PARAM_TOMCAT_VERSION); } __properties.put("tomcat_version", __tomcatVersion); // String _hostName = System.getProperty(__PARAM_HOST_NAME); if (StringUtils.isBlank(_hostName)) { throw new NullArgumentException(__PARAM_HOST_NAME); } __properties.put("host_name", _hostName); __properties.put("host_alias", StringUtils.defaultIfEmpty(System.getProperty(__PARAM_HOST_ALIAS), "")); __properties.put("website_root_path", new File(__catalinaBase, "webapps/ROOT").getPath()); // String _serviceName = System.getProperty(__PARAM_SERVICE_NAME); if (StringUtils.isBlank(_serviceName)) { throw new NullArgumentException(__PARAM_SERVICE_NAME); } __properties.put("service_name", _serviceName); // String _serverPort = System.getProperty(__PARAM_SERVER_PORT); if (StringUtils.isBlank(_serverPort)) { throw new NullArgumentException(__PARAM_SERVER_PORT); } if (!StringUtils.isNumeric(_serverPort)) { throw new IllegalArgumentException(__PARAM_SERVER_PORT); } __properties.put("server_port", _serverPort); // String _connectorPort = System.getProperty(__PARAM_CONNECTOR_PORT); if (StringUtils.isBlank(_connectorPort)) { throw new NullArgumentException(__PARAM_CONNECTOR_PORT); } if (!StringUtils.isNumeric(_connectorPort)) { throw new IllegalArgumentException(__PARAM_CONNECTOR_PORT); } __properties.put("connector_port", _connectorPort); // String _redirectPort = System.getProperty(__PARAM_REDIRECT_PORT); if (StringUtils.isBlank(_redirectPort)) { throw new NullArgumentException(__PARAM_REDIRECT_PORT); } if (!StringUtils.isNumeric(_redirectPort)) { throw new IllegalArgumentException(__PARAM_REDIRECT_PORT); } __properties.put("redirect_port", _redirectPort); // __properties.put("ajp_host", StringUtils.defaultIfEmpty(System.getProperty(__PARAM_AJP_HOST), "localhost")); // String _ajpPort = System.getProperty(__PARAM_AJP_PORT); if (StringUtils.isBlank(_ajpPort)) { throw new NullArgumentException(__PARAM_AJP_PORT); } if (!StringUtils.isNumeric(_ajpPort)) { throw new IllegalArgumentException(__PARAM_AJP_PORT); } __properties.put("ajp_port", _ajpPort); }
From source file:net.ymate.platform.extra.scaffold.TomcatServScaffold.java
private void __doWriterTargetFile(String targetFileName, String tmplFile, Map<String, Object> properties) { Writer _outWriter = null;/*from ww w. jav a2s .c o m*/ try { File _outputFile = new File(this.__catalinaBase, targetFileName); _outWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(_outputFile), StringUtils .defaultIfEmpty(__FREEMARKER_CFG.getOutputEncoding(), __FREEMARKER_CFG.getDefaultEncoding()))); Template _template = __FREEMARKER_CFG.getTemplate(__TEMPLATE_ROOT_PATH + tmplFile); _template.process(properties, _outWriter); System.out.println("Output file: " + _outputFile); } catch (Exception e) { e.printStackTrace(System.err); } finally { if (_outWriter != null) { try { _outWriter.flush(); _outWriter.close(); } catch (IOException e) { } } } }
From source file:net.ymate.platform.module.cache.CachesModule.java
@Override public void initialize(final Map<String, String> moduleCfgs) throws Exception { if (!Caches.getInstance().isInited()) { Caches.getInstance().initialize(new ICacheConfig() { public ICacheProvider getInternalProviderClass() { String _providerImpl = StringUtils.defaultIfEmpty(moduleCfgs.get("internal_provider_impl"), "ehcache"); return ClassUtils.impl(StringUtils .defaultIfEmpty(Caches.__INTERNAL_PROVIDER_CLASS.get(_providerImpl), _providerImpl), ICacheProvider.class, Caches.class); }/* w w w . j a va 2 s . com*/ public ICacheProvider getExternalProviderClass() { return ClassUtils.impl(moduleCfgs.get("external_provider_impl"), ICacheProvider.class, Caches.class); } public IObjectSerializer getObjectSerializerClass() { return ClassUtils.impl(StringUtils.defaultIfEmpty(moduleCfgs.get("serializer_impl"), Caches.__SERIALIZER_CLASS.get("java")), IObjectSerializer.class, Caches.class); } public ICacheEventListener getCacheEventListenerClass() { return ClassUtils.impl(moduleCfgs.get("event_listener_impl"), ICacheEventListener.class, Caches.class); } }); } }
From source file:net.ymate.platform.module.ConfigModule.java
public void initialize(final Map<String, String> moduleCfgs) throws Exception { Cfgs.initialize(new ICfgConfig() { public String getConfigHome() { String _configHomeF = StringUtils.defaultIfEmpty(moduleCfgs.get("config_home"), "${root}"); if (_configHomeF.equalsIgnoreCase("${root}")) { _configHomeF = RuntimeUtils.getRootPath(); }/*from w w w . j a va 2 s . c o m*/ return _configHomeF; } public String getProjectName() { return moduleCfgs.get("project_name"); } public String getModuleName() { return moduleCfgs.get("module_name"); } public IConfigurationProvider getConfigurationProviderClassImpl() { IConfigurationProvider __provider = ClassUtils.impl(StringUtils .defaultIfEmpty(moduleCfgs.get("provider_impl_class"), JConfigProvider.class.getName()), IConfigurationProvider.class, ConfigModule.class); return __provider; } }); }
From source file:net.ymate.platform.module.JdbcModule.java
public void initialize(final Map<String, String> moduleCfgs) throws Exception { final boolean _showSql = new BlurObject(moduleCfgs.get("base.show_sql")).toBooleanValue(); final Set<JdbcDataSourceCfgMeta> _metas = new HashSet<JdbcDataSourceCfgMeta>(); for (String _name : StringUtils.split(StringUtils .defaultIfEmpty(StringUtils.trimToEmpty(moduleCfgs.get("base.datasource_list")), "default"), "|")) { String _adaptorClass = moduleCfgs.get("datasource." + _name + ".adapter_class"); String _driverClass = moduleCfgs.get("datasource." + _name + ".driver_class"); String _connectionUrl = moduleCfgs.get("datasource." + _name + ".connection_url"); String _userName = moduleCfgs.get("datasource." + _name + ".username"); String _password = moduleCfgs.get("datasource." + _name + ".password"); ////www . j ava2s . co m Map<String, String> _params = new HashMap<String, String>(); String _paramKey = "datasource." + _name + ".params."; for (String _cfgKey : moduleCfgs.keySet()) { if (_cfgKey.startsWith(_paramKey)) { _params.put(StringUtils.substringAfter(_cfgKey, _paramKey), moduleCfgs.get(_cfgKey)); } } _metas.add(new JdbcDataSourceCfgMeta(_name, _adaptorClass, _driverClass, _connectionUrl, _userName, _password, _params)); } JDBC.initialize(new IJdbcConfig() { public boolean isShowSql() { return _showSql; } public String getTablePrefix() { return moduleCfgs.get("base.table_prefix"); } public String getDefaultDataSourceName() { return moduleCfgs.get("base.datasource_default"); } public String[] getRepositoryPackages() { return StringUtils.split(moduleCfgs.get("base.repository_packages"), "|"); } public Set<JdbcDataSourceCfgMeta> getDataSourceCfgMetas() { return _metas; } }); }