org.jumpmind.symmetric.service.impl.ParameterService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of symmetric-ds Show documentation
Show all versions of symmetric-ds Show documentation
SymmetricDS is an open source database synchronization solution. It is platform-independent,
web-enabled, and database-agnostic. SymmetricDS was first built to replicate changes between 'retail store'
databases and ad centralized 'corporate' database.
The newest version!
/*
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU Lesser General Public License (the
* "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see
* .
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jumpmind.symmetric.service.impl;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.symmetric.common.Constants;
import org.jumpmind.symmetric.common.ParameterConstants;
import org.jumpmind.symmetric.config.IParameterFilter;
import org.jumpmind.symmetric.service.IParameterService;
import org.jumpmind.symmetric.util.AppUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.jdbc.core.RowMapper;
/**
*
*/
public class ParameterService extends AbstractService implements IParameterService,
BeanFactoryAware {
private Map parameters;
private BeanFactory beanFactory;
private long cacheTimeoutInMs = 0;
private Date lastTimeParameterWereCached;
private IParameterFilter parameterFilter;
private Properties systemProperties;
private boolean initialized = false;
public ParameterService() {
systemProperties = (Properties) System.getProperties().clone();
}
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
public BigDecimal getDecimal(String key, BigDecimal defaultVal) {
String val = getString(key);
if (val != null) {
return new BigDecimal(val);
}
return defaultVal;
}
public BigDecimal getDecimal(String key) {
return getDecimal(key, BigDecimal.ZERO);
}
public boolean is(String key) {
return is(key, false);
}
public boolean is(String key, boolean defaultVal) {
String val = getString(key);
if (val != null) {
val = val.trim();
if (val.equals("1")) {
return true;
} else {
return Boolean.parseBoolean(val);
}
} else {
return false;
}
}
public int getInt(String key) {
return getInt(key, 0);
}
public int getInt(String key, int defaultVal) {
String val = getString(key);
if (val != null) {
return Integer.parseInt(val);
}
return defaultVal;
}
public long getLong(String key) {
return getLong(key, 0);
}
public long getLong(String key, long defaultVal) {
String val = getString(key);
if (val != null) {
return Long.parseLong(val);
}
return defaultVal;
}
public String getString(String key) {
return getString(key, null);
}
public String getString(String key, String defaultVal) {
String value = null;
if (StringUtils.isBlank(value)) {
value = getParameters().get(key);
}
if (this.parameterFilter != null) {
value = this.parameterFilter.filterParameter(key, value);
}
return StringUtils.isBlank(value) ? defaultVal : value;
}
/**
* Save a parameter that applies to {@link IParameterService#ALL} external
* ids and all node groups.
*/
public void saveParameter(String key, Object paramValue) {
this.saveParameter(IParameterService.ALL, IParameterService.ALL, key, paramValue);
}
public void saveParameter(String externalId, String nodeGroupId, String key, Object paramValue) {
paramValue = paramValue != null ? paramValue.toString() : null;
int count = jdbcTemplate.update(getSql("updateParameterSql"), new Object[] { paramValue,
externalId, nodeGroupId, key });
if (count == 0) {
jdbcTemplate.update(getSql("insertParameterSql"), new Object[] { externalId,
nodeGroupId, key, paramValue });
}
rereadParameters();
}
public void saveParameters(String externalId, String nodeGroupId, Map parameters) {
Set keys = parameters.keySet();
for (String key : keys) {
saveParameter(externalId, nodeGroupId, key, parameters.get(key));
}
}
public synchronized void rereadParameters() {
this.parameters = null;
getParameters();
}
/**
* Every time we pull the properties out of the bean factory they should get
* reread from the file system.
*/
private Properties rereadFileParameters() {
return (Properties) beanFactory.getBean(Constants.PROPERTIES);
}
private Map rereadDatabaseParameters(Properties p) {
try {
Map map = rereadDatabaseParameters(ALL, ALL);
map.putAll(rereadDatabaseParameters(ALL, p
.getProperty(ParameterConstants.NODE_GROUP_ID)));
map.putAll(rereadDatabaseParameters(p.getProperty(ParameterConstants.EXTERNAL_ID), p
.getProperty(ParameterConstants.NODE_GROUP_ID)));
return map;
} catch (Exception ex) {
if (initialized) {
log.warn("DatabaseParametersReadingFailed");
}
return new HashMap();
}
}
private Map rereadDatabaseParameters(String externalId, String nodeGroupId) {
final Map map = new HashMap();
jdbcTemplate.query(getSql("selectParametersSql"), new Object[] { externalId, nodeGroupId },
new RowMapper