Commit 8879b785 authored by Jonathan CLARKE's avatar Jonathan CLARKE
Browse files

Fixes #40. Generation now places files in the etc/ directory.

parent e3f3b9be
......@@ -105,7 +105,7 @@ public abstract class AbstractGenerator {
* name.
*/
public AbstractGenerator() {
this.separator = System.getProperty("file.separator");
this.separator = Configuration.getSeparator();
}
/**
......
......@@ -165,8 +165,6 @@ public final class Generator {
* @throws NamingException DOCUMENT ME!
*/
public static void main(final String[] args) throws NamingException {
PropertyConfigurator.configure(LOG4J_CONFIGURATION_FILE);
Generator instance = new Generator();
if (instance.parseArgs(args) == 0) {
......
......@@ -46,6 +46,7 @@
package org.lsc.persistence;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import java.util.Properties;
......@@ -121,12 +122,23 @@ public final class DaoConfig
reader = Resources.getResourceAsReader(IBATIS_SQLMAP_CONFIGURATION_FILE);
}
Properties props = new Properties();
// read the database configuration file to pass to sql-map-config XML file
Properties props = Configuration.getPropertiesFromFileInConfigDir(Configuration.DATABASE_PROPERTIES_FILENAME);
// this is maintained for backwards compatibility, although the database.properties file no longer exists
try
{
props.putAll(Configuration.getPropertiesFromFileInConfigDir(Configuration.DATABASE_PROPERTIES_FILENAME));
}
catch (FileNotFoundException e) {}
// ignore this, it probably just means that we're not using database.properties file anymore
// add the database configuration properties from lsc.properties
props.putAll(Configuration.getAsProperties("src.database"));
// add the configuration directory to properties so that sql-map-config can use relative paths
props.put("lsc.config.sqlmapdir", Configuration.getConfigurationDirectory() + IBATIS_SQLMAP_FILES_DIRNAME);
props.put("lsc.config", Configuration.getConfigurationDirectory());
sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader, props);
// clean up
......
......@@ -48,6 +48,7 @@ package org.lsc.persistence;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.net.MalformedURLException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
......@@ -61,6 +62,7 @@ import javax.xml.transform.stream.StreamResult;
import org.apache.log4j.Logger;
import org.apache.xerces.parsers.DOMParser;
import org.lsc.AbstractGenerator;
import org.lsc.Configuration;
import org.w3c.dom.Attr;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
......@@ -107,15 +109,39 @@ public class SqlMapXmlFileGenerator extends AbstractGenerator {
* @return the generation status
*/
protected final boolean generate(final String unused) {
if (getDestination() != null && getDestination().length() > 0) {
sqlMapConfigFullFilename = getDestination() + getSeparator()
+ SQL_MAP_CONFIG_FILENAME;
// Then remove the destination from the filename
if(xmlFilename.indexOf(getDestination()) == 0) {
xmlFilename = xmlFilename.substring(getDestination().length()+1);
// Test if we have a IBATIS_SQLMAP_CONFIGURATION_FILENAME file in the global config dir.
// This test is for backwards compatibility since the IBATIS_SQLMAP_CONFIGURATION_FILENAME
// file always used to be in a JAR file. It should be removed in the future.
try
{
File configFile = new File(Configuration.getConfigurationDirectory() + DaoConfig.IBATIS_SQLMAP_CONFIGURATION_FILENAME);
if (configFile.exists())
{
// set sql-map-config.xml file path
sqlMapConfigFullFilename = configFile.toURI().toURL().getPath();
// adapt the generated .xml file name to be relative to the config dir
xmlFilename = xmlFilename.substring(Configuration.getConfigurationDirectory().length());
} else {
// revert back to old behavior - this should be removed soon!
LOGGER.warn("Falling back to old-style configuration files");
if (getDestination() != null && getDestination().length() > 0) {
sqlMapConfigFullFilename = getDestination() + getSeparator()
+ SQL_MAP_CONFIG_FILENAME;
// Then remove the destination from the filename
if(xmlFilename.indexOf(getDestination()) == 0) {
xmlFilename = xmlFilename.substring(getDestination().length()+1);
}
} else {
sqlMapConfigFullFilename = SQL_MAP_CONFIG_FILENAME;
}
}
} else {
sqlMapConfigFullFilename = SQL_MAP_CONFIG_FILENAME;
}
catch (MalformedURLException e)
{
throw new ExceptionInInitializerError("Error reading the IBatis SQLMap configuration file");
}
String content = generateContent();
......@@ -168,11 +194,11 @@ public class SqlMapXmlFileGenerator extends AbstractGenerator {
NamedNodeMap newSqlMapAttributes = newSqlMapEntry.getAttributes();
// Create a new resource attribute ...
Attr resource = doc.createAttribute("resource");
Attr url = doc.createAttribute("url");
// ... with the xml ibatis description filename as value
resource.setValue(xmlFilename);
url.setValue("file://${lsc.config}/" + xmlFilename);
// Set the attribute in the new object attributes
newSqlMapAttributes.setNamedItem(resource);
newSqlMapAttributes.setNamedItem(url);
// Append the new node as the new last node
try {
last.appendChild(newSqlMapEntry);
......
......@@ -46,8 +46,10 @@
package org.lsc.service;
import org.lsc.AbstractGenerator;
import org.lsc.Configuration;
import org.lsc.jndi.parser.LdapAttributeType;
import org.lsc.jndi.parser.LdapObjectClass;
import org.lsc.persistence.DaoConfig;
import java.io.File;
import java.io.FileNotFoundException;
......@@ -55,6 +57,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Iterator;
......@@ -205,23 +208,36 @@ public class JdbcSrcServiceObjectGenerator extends AbstractGenerator {
* @return the xml persistence filename
*/
public final String getMyXMLFileName() {
String myXMLPackage = this.getClass().getPackage().getName();
myXMLPackage = myXMLPackage.substring(0, myXMLPackage.lastIndexOf("."))
+ ".persistence.xml";
String mainLocation = null;
if (getDestination() != null) {
mainLocation = getDestination();
} else {
mainLocation = System.getProperty("user.dir") + getSeparator()
+ "src" + getSeparator() + "main" + getSeparator()
+ "java";
}
return mainLocation + getSeparator()
+ myXMLPackage.replaceAll("\\.", getSeparator())
+ getSeparator() + this.objectName + ".xml";
// Test if we have a IBATIS_SQLMAP_CONFIGURATION_FILENAME file in the global config dir.
// This test is for backwards compatibility since the IBATIS_SQLMAP_CONFIGURATION_FILENAME
// file always used to be in a JAR file. It should be removed in the future.
File configFile = new File(Configuration.getConfigurationDirectory() + DaoConfig.IBATIS_SQLMAP_CONFIGURATION_FILENAME);
if (configFile.exists())
{
String xmlFileName = Configuration.getConfigurationDirectory() + DaoConfig.IBATIS_SQLMAP_FILES_DIRNAME + Configuration.getSeparator() + this.objectName + ".xml";
return xmlFileName;
} else {
// revert back to old behavior - this should be removed soon!
LOGGER.warn("Falling back to old-style configuration files");
String myXMLPackage = this.getClass().getPackage().getName();
myXMLPackage = myXMLPackage.substring(0, myXMLPackage.lastIndexOf(".")) + ".persistence.xml";
String mainLocation = null;
if (getDestination() != null) {
mainLocation = getDestination();
} else {
mainLocation = System.getProperty("user.dir") + getSeparator()
+ "src" + getSeparator() + "main" + getSeparator()
+ "java";
}
return mainLocation + getSeparator()
+ myXMLPackage.replaceAll("\\.", getSeparator())
+ getSeparator() + this.objectName + ".xml";
}
}
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment