Skip to content

Commit

Permalink
Merge pull request #3 from ochaloup/tofile
Browse files Browse the repository at this point in the history
Going to 1.0.0.Final
  • Loading branch information
ochaloup committed Mar 4, 2014
2 parents 30c5f23 + b351e1b commit f23999c
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 20 deletions.
17 changes: 16 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
apply plugin: 'groovy'
apply plugin: 'idea'
apply plugin: 'application'
apply plugin: 'maven-publish'

group = 'org.jboss.qe'
version = '1.0.0-SNAPSHOT'
version = '1.0.0.Final'

mainClassName = 'org.jboss.qe.dscreator.Main'

Expand Down Expand Up @@ -41,3 +42,17 @@ dependencies {
}



task sourceJar(type: Jar) {
from sourceSets.main.allSource
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
artifact sourceJar {
classifier "source"
}
}
}
}
25 changes: 6 additions & 19 deletions src/main/groovy/org/jboss/qe/dscreator/Main.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import groovy.xml.StreamingMarkupBuilder;

import org.jboss.qe.dscreator.common.XMLFormattable;
import org.jboss.qe.dscreator.common.XMLFormatter;
import org.jboss.qe.dscreator.common.XMLDatasourcePrinter;
import org.jboss.qe.dscreator.datasource.Datasource
import org.jboss.qe.dscreator.datasource.DatasourceFactory
import org.jboss.qe.dscreator.xadatasource.XADatasource
Expand Down Expand Up @@ -70,35 +71,21 @@ class Main {
}
}

XMLFormatter formatter = new XMLFormatter();

XMLDatasourcePrinter printer = new XMLDatasourcePrinter();
if(datasourceOutput == null) {
// conditions for creating datasource were not fullfiled
printUsage();
} else if (opt.out) {
// printing to file
FileWriter fileWriter = new FileWriter(opt.out)
def xmlOut = XmlUtil.serialize("\n<datasources>" + datasourceOutput.toXml() + "</datasources>");
fileWriter.write(xmlOut)
fileWriter.flush()
printer.printToFile(datasourceOutput, opt.out);
println "Result XML was saved to file " << opt.out
} else if (opt.config){
def standaloneXmlNode = new XmlParser(false, true).parseText(new File(opt.config).text)
def datasourceNode = new XmlParser(false, true).parseText(datasourceOutput.toXml())

// we want to have new datasource if it's not added yet
String datasourceType = opt.xa ? 'xa-datasource' : 'datasource'
if(standaloneXmlNode.profile.subsystem.datasources."${datasourceType}".find{it.'@name' == opt.datasourceName} == null) {
// warn: not sure why but appendNode does strange things here
standaloneXmlNode.profile.subsystem.datasources[0]?.append(datasourceNode)
}

// writing results back to config file (standalone.xml)
new XmlNodePrinter(new PrintWriter(opt.config)).print(standaloneXmlNode)

// add to standalone xml
printer.printToStandaloneXml(datasourceOutput, opt.config, opt.datasourceName, opt.xa);
println "XML was added to config file " << opt.config
} else {
// print the newnly created datasource to std out
XMLFormatter formatter = new XMLFormatter();
println formatter.prettyXML(datasourceOutput.toXml())
}
}
Expand Down
25 changes: 25 additions & 0 deletions src/main/groovy/org/jboss/qe/dscreator/common/Utils.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.jboss.qe.dscreator.common

import java.util.Properties;

class Utils {

/**
* Loading java.util.Properties from file and putting them to Properties instance.
*
* @param dbAllocPropsFile path to properties file
* @return properties instance
*/
public static Properties loadDbAllocatorProperties(String dbAllocPropsFile) {
Properties props = new Properties()
File propertiesFile = new File((String) dbAllocPropsFile)
if (!propertiesFile.exists()) {
System.err.println("File " + propertiesFile.absoluteFile + " doesn't exist")
System.exit(500)
}
propertiesFile.withInputStream {
stream -> props.load(stream)
}
return props
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.jboss.qe.dscreator.common

import groovy.xml.XmlUtil;

/**
* Class with utility methods to print xml string to file or something.
*/
class XMLDatasourcePrinter {
/**
* Printing xml datasource to -ds.xml file.
*
* @param xmlFormat datasource created by datasource factory which implements methods of XMLFormattable and returns xml as string
* @param outFile where to save the ds.xml file
*/
def printToFile(XMLFormattable xmlFormat, String outFile) {
FileWriter fileWriter = new FileWriter(outFile)
def xmlOut = XmlUtil.serialize("\n<datasources>" + xmlFormat.toXml() + "</datasources>");
fileWriter.write(xmlOut)
fileWriter.flush()
}

/**
* Taking the datasource and putting it inside of the standalone.xml configuration.
*
* @param xmlFormat datasource created by datasource factory which implements methods of XMLFormattable and returns xml as string
* @param standaloneXmlFile path to standalone.xml file
* @param datasourceName name of datasource which will be added to config xml file
* @param isXA is the datasource xa or not
*/
def printToStandaloneXml(XMLFormattable xmlFormat, String standaloneXmlFile, String datasourceName, boolean isXA) {
def standaloneXmlNode = new XmlParser(false, true).parseText(new File(standaloneXmlFile).text)
def datasourceNode = new XmlParser(false, true).parseText(xmlFormat.toXml())

// we want to have new datasource if it's not added yet
String datasourceType = isXA ? 'xa-datasource' : 'datasource'
if(standaloneXmlNode.profile.subsystem.datasources."${datasourceType}".find{it.'@name' == datasourceName} == null) {
// warn: not sure why but appendNode does strange things here
standaloneXmlNode.profile.subsystem.datasources[0]?.append(datasourceNode)
}

// writing results back to config file (standalone.xml)
new XmlNodePrinter(new PrintWriter(standaloneXmlFile)).print(standaloneXmlNode)
}
}
17 changes: 17 additions & 0 deletions src/main/groovy/org/jboss/qe/dscreator/common/XMLFormatter.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,34 @@ package org.jboss.qe.dscreator.common
import groovy.xml.StreamingMarkupBuilder

class XMLFormatter {

/**
* Taking XML Object used by StreamingMarkupBuilder and transforming it to XML string.
* @param xmlObject xml object from StreamingMarkupBuilderStreamingMarkupBuilder
* @return pretiffied xml string
*/
def String getSimpleXML(xmlObject) {
return prettyXML(transformToXML(xmlObject))
}

/**
* From XML object via StreamingMarkupBuilder to XML String.
* @param xmlObject xml type object
* @return xml string
*/
def String transformToXML(xmlObject) {
def markupBuilder = new StreamingMarkupBuilder()
markupBuilder.encoding = "UTF-8"
markupBuilder.useDoubleQuotes = true
return markupBuilder.bind(xmlObject)
}

/**
* Get xml string and format it to be nicer.
*
* @param xmlString string containing xml document
* @return prettier xml document in string
*/
def String prettyXML(String xmlString) {
if(xmlString) {
def stringWriter = new StringWriter()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package org.jboss.qe.dscreator.datasource

import org.jboss.qe.dscreator.common.Utils;

/**
* @author Martin Simka
*/
class DatasourceFactory {

public static Datasource createDatasource(String dbAllocatorPropertiesPath, String name, String jndiName, String driver) {
Properties loadedProps = Utils.loadDbAllocatorProperties(dbAllocatorPropertiesPath);
return createDatasource(loadedProps, name, jndiName, driver);
}

public static Datasource createDatasource(Properties dbAllocatorProperties, String name, String jndiName, String driver) {
if(dbAllocatorProperties["db.primary_label"] != null) {
//add specific implementation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package org.jboss.qe.dscreator.xadatasource

import org.jboss.qe.dscreator.common.Utils;


/**
* @author Martin Simka
*/
class XADatasourceFactory {
public static XADatasource createXADatasource(String dbAllocatorPropertiesPath, String name, String jndiName, String driver) {
Properties loadedProps = Utils.loadDbAllocatorProperties(dbAllocatorPropertiesPath);
return createXADatasource(loadedProps, name, jndiName, driver);
}

public static XADatasource createXADatasource(Properties dbAllocatorProperties, String name, String jndiName, String driver) {
if (dbAllocatorProperties["db.primary_label"] != null) {
String label = dbAllocatorProperties["db.primary_label"]
Expand Down

0 comments on commit f23999c

Please sign in to comment.