Skip to content

Commit

Permalink
SYMMETRICDS-338 - Don't write out a temporary properties file if Stan…
Browse files Browse the repository at this point in the history
…daloneSymmetricEngine(Properties) api is used
  • Loading branch information
chenson42 committed Sep 8, 2010
1 parent ac37219 commit 4bc88f0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 35 deletions.
@@ -1,8 +1,5 @@
package org.jumpmind.symmetric;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -13,13 +10,13 @@
import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.symmetric.common.Constants;
import org.jumpmind.symmetric.common.ParameterConstants;
import org.jumpmind.symmetric.common.TableConstants;
import org.jumpmind.symmetric.common.logging.ILog;
import org.jumpmind.symmetric.common.logging.LogFactory;
import org.jumpmind.symmetric.config.PropertiesFactoryBean;
import org.jumpmind.symmetric.db.IDbDialect;
import org.jumpmind.symmetric.ddl.model.Table;
import org.jumpmind.symmetric.ext.IExtensionPointManager;
Expand Down Expand Up @@ -187,33 +184,28 @@ protected void init(ApplicationContext ctx, boolean isParentContext, Properties
// this setup. Synchronizing on the class so creating multiple engines
// is thread safe.
synchronized (this.getClass()) {
if (overrideProperties != null) {
FileOutputStream fos = null;
try {
File file = File.createTempFile("symmetric.", ".properties");
fos = new FileOutputStream(file);
overrideProperties.store(fos, "");
System.setProperty(Constants.OVERRIDE_PROPERTIES_FILE_TEMP, "file:" + file.getAbsolutePath());
} catch (IOException ex) {
log.error(ex);
throw new RuntimeException(ex);
} finally {
IOUtils.closeQuietly(fos);
try {
if (overrideProperties != null) {
PropertiesFactoryBean.setLocalProperties(overrideProperties);
}
}

if (!StringUtils.isBlank(overridePropertiesResource1)) {
System.setProperty(Constants.OVERRIDE_PROPERTIES_FILE_1, overridePropertiesResource1);
}

if (!StringUtils.isBlank(overridePropertiesResource2)) {
System.setProperty(Constants.OVERRIDE_PROPERTIES_FILE_2, overridePropertiesResource2);
}

if (isParentContext || ctx == null) {
init(createContext(ctx));
} else {
init(ctx);

if (!StringUtils.isBlank(overridePropertiesResource1)) {
System.setProperty(Constants.OVERRIDE_PROPERTIES_FILE_1,
overridePropertiesResource1);
}

if (!StringUtils.isBlank(overridePropertiesResource2)) {
System.setProperty(Constants.OVERRIDE_PROPERTIES_FILE_2,
overridePropertiesResource2);
}

if (isParentContext || ctx == null) {
init(createContext(ctx));
} else {
init(ctx);
}
} finally {
PropertiesFactoryBean.clearLocalProperties();
}
}
}
Expand Down
Expand Up @@ -3,15 +3,33 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;

public class PropertiesFactoryBean extends
org.springframework.beans.factory.config.PropertiesFactoryBean {

DynamicPropertiesFiles dynamicPropertiesFiles;
protected DynamicPropertiesFiles dynamicPropertiesFiles;

private static Properties localProperties;

public PropertiesFactoryBean() {
this.setLocalOverride(true);
if (localProperties != null) {
this.setProperties(localProperties);
}
}

public static void setLocalProperties(Properties localProperties) {
PropertiesFactoryBean.localProperties = localProperties;
}

public static void clearLocalProperties() {
PropertiesFactoryBean.localProperties = null;
}

@Override
public void setLocations(Resource[] locations) {
List<Resource> resources = new ArrayList<Resource>();
Expand All @@ -24,7 +42,8 @@ public void setLocations(Resource[] locations) {
}

super.setLocations(resources.toArray(new Resource[resources.size()]));
}
}


public void setDynamicPropertiesFiles(
DynamicPropertiesFiles dynamicPropertiesFiles) {
Expand Down
Expand Up @@ -10,7 +10,8 @@
<!-- The list of properties file names need to be singletons because we rely on System properties which can be swapped out at runtime. -->
<bean id="dynamicPropertiesFiles" class="org.jumpmind.symmetric.config.DynamicPropertiesFiles"/>

<bean id="symmetricProperties" class="org.jumpmind.symmetric.config.PropertiesFactoryBean" scope="prototype">
<bean id="symmetricProperties" class="org.jumpmind.symmetric.config.PropertiesFactoryBean">
<property name="singleton" value="false"/>
<property name="ignoreResourceNotFound" value="true" />
<property name="dynamicPropertiesFiles" ref="dynamicPropertiesFiles"/>
<!-- Allow for the (optional) override of these properties -->
Expand Down
Expand Up @@ -22,6 +22,7 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
Expand All @@ -40,6 +41,7 @@
import javax.sql.DataSource;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -152,8 +154,12 @@ public static void setup(String testPrefix, String sqlScriptSuffix, String clien
}

if (clientDb != null) {
clientEngine = new StandaloneSymmetricEngine("file:"
+ writeTempPropertiesFileFor(testPrefix, clientDb, DatabaseRole.CLIENT).getAbsolutePath(), null);
String file = writeTempPropertiesFileFor(testPrefix, clientDb, DatabaseRole.CLIENT).getAbsolutePath();
Properties properties = new Properties();
FileReader reader = new FileReader(file);
properties.load(reader);
IOUtils.closeQuietly(reader);
clientEngine = new StandaloneSymmetricEngine(properties);
dropAndCreateDatabaseTables(clientDb, clientEngine);
}
}
Expand Down

0 comments on commit 4bc88f0

Please sign in to comment.