Skip to content

Instantly share code, notes, and snippets.

@aaronwalker
Last active December 6, 2016 01:27
Show Gist options
  • Select an option

  • Save aaronwalker/9125708 to your computer and use it in GitHub Desktop.

Select an option

Save aaronwalker/9125708 to your computer and use it in GitHub Desktop.
sample blueprint xml
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
xmlns:camel="http://camel.apache.org/schema/blueprint"
xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0"
xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0 http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0/blueprint-ext.xsd
http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd
">
<!-- blueprint property placeholders -->
<ext:property-placeholder placeholder-prefix="$[" placeholder-suffix="]" />
<cm:property-placeholder persistent-id="">
<cm:default-properties>
<cm:property name="" value=""/>
</cm:default-properties>
</cm:property-placeholder>
<reference id="activemq" interface="org.apache.camel.Component"/>
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
<contextScan/>
</camelContext>
</blueprint>
find . -name 'beans.xml' | grep '/src/main/resources/META-INF/spring/' | xargs -I beans spring2blueprint.groovy beans
#!/usr/bin/env groovy
import javax.xml.transform.*
import javax.xml.transform.stream.*
import groovy.xml.*
def xslt = '''<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:s="http://www.springframework.org/schema/beans"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
xmlns:sec="http://cxf.apache.org/configuration/security"
xmlns:cxf="http://camel.apache.org/schema/cxf"
xmlns:camel="http://camel.apache.org/schema/blueprint"
xmlns:drools="http://drools.org/schema/drools-spring"
exclude-result-prefixes="s,drools,osgi,osgix,ctx"
version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:preserve-space elements="blueprint" />
<xsl:template match="s:beans">
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
xmlns:camel="http://camel.apache.org/schema/blueprint"
xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0 http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0/blueprint-ext.xsd http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">
<xsl:comment>
blueprint property placeholders
</xsl:comment>
<ext:property-placeholder placeholder-prefix="$[" placeholder-suffix="]" />
<cm:property-placeholder>
<xsl:attribute name="persistent-id">
<xsl:value-of select="//@*[local-name()='persistent-id']"/>
</xsl:attribute>
<cm:default-properties>
<xsl:for-each select="//*[local-name()='prop']">
<xsl:element name="cm:property">
<xsl:attribute name="name">
<xsl:value-of select="@key"/>
</xsl:attribute>
<xsl:attribute name="value">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:element>
</xsl:for-each>
<xsl:element name="cm:property">
<xsl:attribute name="name">karaf.base</xsl:attribute>
<xsl:attribute name="value">$[karaf.base]</xsl:attribute>
</xsl:element>
<xsl:element name="cm:property">
<xsl:attribute name="name">karaf.home</xsl:attribute>
<xsl:attribute name="value">$[karaf.home]</xsl:attribute>
</xsl:element>
</cm:default-properties>
</cm:property-placeholder>
<xsl:comment>
Inject activemq camel component
</xsl:comment>
<reference id="activemq" interface="org.apache.camel.Component"/>
<xsl:comment>
Setup camel context
</xsl:comment>
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
<contextScan/>
</camelContext>
<xsl:if test="http-conf:*">
<xsl:comment>
Http configuration
</xsl:comment>
</xsl:if>
<xsl:apply-templates select="http-conf:*"/>
<xsl:if test="cxf:*">
<xsl:comment>
CXF configuration
</xsl:comment>
</xsl:if>
<xsl:apply-templates select="cxf:*"/>
<xsl:if test="drools:*">
<xsl:comment>
Drools configuration
</xsl:comment>
</xsl:if>
<xsl:apply-templates select="drools:*"/>
<xsl:comment>
Configure blueprint beans
</xsl:comment>
<xsl:apply-templates select="s:bean"/>
</blueprint>
</xsl:template>
<xsl:template match="http-conf:*">
<xsl:element name="{name()}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="sec:*">
<xsl:element name="{name()}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="cxf:*">
<xsl:element name="camelcxf:{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
<xsl:template match="cxf:cxfEndpoint">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="drools:grid-node">
<xsl:comment>
Spring replacement of &lt;drools:grid-node id="node1"/&gt;
</xsl:comment>
<xsl:element name="bean">
<xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
<xsl:attribute name="class">com.base2.ss.mdh.common.util.DroolsBlueprintBean</xsl:attribute>
<xsl:attribute name="factory-method">createGridNode</xsl:attribute>
<xsl:element name="argument">
<xsl:attribute name="value"><xsl:value-of select="@id"/></xsl:attribute>
</xsl:element>
</xsl:element>
</xsl:template>
<xsl:template match="drools:kbase">
<xsl:comment>
Spring replacement of :
&lt;drools:kbase id="kbase1" node="node1"&gt;
&lt;drools:resources&gt;
&lt;drools:resource type="DRL" source="classpath:testSpring.drl"/&gt;
&lt;/drools:resources&gt;
&lt;/drools:kbase>
</xsl:comment>
<xsl:element name="bean">
<xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
<xsl:attribute name="class">com.base2.ss.mdh.common.util.DroolsBlueprintBean</xsl:attribute>
<xsl:attribute name="factory-method">createKnowledgeBase</xsl:attribute>
<xsl:element name="argument">
<xsl:attribute name="ref"><xsl:value-of select="//drools:grid-node/@id"/></xsl:attribute>
</xsl:element>
<xsl:element name="argument">
<xsl:attribute name="value"><xsl:value-of select="drools:resources/drools:resource/@source"/></xsl:attribute>
</xsl:element>
<xsl:element name="argument">
<xsl:attribute name="value"><xsl:value-of select="drools:resources/drools:resource/@type"/></xsl:attribute>
</xsl:element>
</xsl:element>
</xsl:template>
<xsl:template match="s:bean">
<xsl:if test="@id != \'activemq\'">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:if>
</xsl:template>
<xsl:template match="@class">
<xsl:attribute name="{local-name()}">
<xsl:choose>
<xsl:when test=". = \'org.apache.camel.component.file.AntPathMatcherGenericFileFilter\'">com.base2.ss.mdh.common.camel.file.SimpleFileFilter</xsl:when>
<xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
<xsl:template match="@factory-bean">
<xsl:attribute name="factory-ref">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="s:constructor-arg">
<xsl:element name="argument">
<xsl:choose>
<xsl:when test="s:ref">
<xsl:attribute name="ref">
<xsl:value-of select="./s:ref/@bean"/>
</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="@* | node()" />
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:template>
<xsl:template match="s:entry">
<xsl:element name="entry">
<xsl:attribute name="key"><xsl:value-of select="@key"/></xsl:attribute>
<xsl:choose>
<xsl:when test="s:bean">
<xsl:copy-of select="s:bean"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="@* | node()" />
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
'''
def copyAndReplaceText(source, dest, Closure replaceText){
dest.write(replaceText(source.text))
}
def spring2blueprint(springSourceFile,xslt) {
def factory = TransformerFactory.newInstance()
def transformer = factory.newTransformer(new StreamSource(new StringReader(xslt)))
def Writer out = new StringWriter();
transformer.transform(new StreamSource(new FileReader(springSourceFile)), new StreamResult(out))
return XmlUtil.serialize(out.toString())
}
def writeBlueprintXml(blueprintXml,springSourceFile) {
def springFile = new File(springSourceFile)
def resourcesDir = springFile.getParentFile().getParentFile().getParentFile()
def blueprintDir = new File(resourcesDir,"OSGI-INF/blueprint")
blueprintDir.mkdirs()
def blueprintFile = new File(blueprintDir,"blueprint.xml")
FileWriter writer = new FileWriter(blueprintFile)
writer.write(blueprintXml)
writer.close()
//fix the property replace mapping replace ${...} with $[...]
copyAndReplaceText(blueprintFile,blueprintFile) {
it.replaceAll(/<cm:property name="(.*)" value="(.*)\$\{(.*?)\}(.*)"/,'<cm:property name="$1" value="$2\\$\\[$3\\]$4"')
}
copyAndReplaceText(blueprintFile,blueprintFile) {
it.replaceAll(/file\:\$\[(.*)\](.*)/,'\\$\\[$1\\]$2')
}
//remove any empty namespaces
copyAndReplaceText(blueprintFile,blueprintFile) {
it.replaceAll("xmlns=\"\"","")
}
//remove spring namespace
copyAndReplaceText(blueprintFile,blueprintFile) {
it.replaceAll("xmlns:s=\"http://www.springframework.org/schema/beans\"","")
}
copyAndReplaceText(blueprintFile,blueprintFile) {
it.replaceAll("xmlns=\"http://www.springframework.org/schema/beans\"","")
}
copyAndReplaceText(blueprintFile,blueprintFile) {
it.replaceAll("xmlns:osgi=\"http://www.springframework.org/schema/osgi\"","")
}
copyAndReplaceText(blueprintFile,blueprintFile) {
it.replaceAll("xmlns:osgix=\"http://www.springframework.org/schema/osgi-compendium\"","")
}
copyAndReplaceText(blueprintFile,blueprintFile) {
it.replaceAll("xmlns:ctx=\"http://www.springframework.org/schema/context\"","")
}
copyAndReplaceText(blueprintFile,blueprintFile) {
it.replaceAll("xmlns:camel=\"http://camel.apache.org/schema/spring\"","")
}
//remove drools namespace
copyAndReplaceText(blueprintFile,blueprintFile) {
it.replaceAll("xmlns:drools=\"http://drools.org/schema/drools-spring\"","")
}
//replace spring cxf namespace with blueprint
copyAndReplaceText(blueprintFile,blueprintFile) {
it.replaceAll("xmlns:cxf=\"http://camel.apache.org/schema/cxf\"","xmlns:cxf=\"http://camel.apache.org/schema/blueprint/cxf\"")
}
println("writing blueprint xml " + blueprintFile)
}
def removeSpring(springSourceFile) {
def sout = new StringBuffer(), serr = new StringBuffer()
def springFile = new File(springSourceFile)
def gitCmd = "git rm -rf ${springFile.getParentFile()}"
def proc = gitCmd.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitFor()
println "removed spring file " + springSourceFile
}
def fixPom(springSourceFile) {
def springFile = new File(springSourceFile)
def projectDir = springFile.getParentFile().getParentFile().getParentFile().getParentFile().getParentFile().getParentFile()
def pomFile = new File(projectDir,"pom.xml")
copyAndReplaceText(pomFile, pomFile){
it.replaceAll('<artifactId>camel-spring</artifactId>','<artifactId>camel-blueprint</artifactId>')
}
copyAndReplaceText(pomFile, pomFile){
it.replaceAll("org.apache.camel.spring.util,","")
}
copyAndReplaceText(pomFile, pomFile){
it.replaceAll("org.apache.camel.spring,\n","")
}
println "replaced camel-spring dependency with camel-blueprint"
}
writeBlueprintXml(spring2blueprint(args[0],xslt),args[0])
removeSpring(args[0])
fixPom(args[0])
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:s="http://www.springframework.org/schema/beans"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
xmlns:sec="http://cxf.apache.org/configuration/security"
xmlns:cxf="http://camel.apache.org/schema/cxf"
xmlns:camel="http://camel.apache.org/schema/blueprint"
xmlns:drools="http://drools.org/schema/drools-spring"
exclude-result-prefixes="s,drools,osgi,osgix,ctx"
version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:preserve-space elements="blueprint" />
<xsl:template match="s:beans">
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
xmlns:camel="http://camel.apache.org/schema/blueprint"
xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0 http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0/blueprint-ext.xsd http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">
<xsl:comment>
blueprint property placeholders
</xsl:comment>
<ext:property-placeholder placeholder-prefix="$[" placeholder-suffix="]" />
<cm:property-placeholder>
<xsl:attribute name="persistent-id">
<xsl:value-of select="//@*[local-name()='persistent-id']"/>
</xsl:attribute>
<cm:default-properties>
<xsl:for-each select="//*[local-name()='prop']">
<xsl:element name="cm:property">
<xsl:attribute name="name">
<xsl:value-of select="@key"/>
</xsl:attribute>
<xsl:attribute name="value">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:element>
</xsl:for-each>
<xsl:element name="cm:property">
<xsl:attribute name="name">karaf.base</xsl:attribute>
<xsl:attribute name="value">$[karaf.base]</xsl:attribute>
</xsl:element>
<xsl:element name="cm:property">
<xsl:attribute name="name">karaf.home</xsl:attribute>
<xsl:attribute name="value">$[karaf.home]</xsl:attribute>
</xsl:element>
</cm:default-properties>
</cm:property-placeholder>
<xsl:comment>
Inject activemq camel component
</xsl:comment>
<reference id="activemq" interface="org.apache.camel.Component"/>
<xsl:comment>
Setup camel context
</xsl:comment>
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
<contextScan/>
</camelContext>
<xsl:if test="http-conf:*">
<xsl:comment>
Http configuration
</xsl:comment>
</xsl:if>
<xsl:apply-templates select="http-conf:*"/>
<xsl:if test="cxf:*">
<xsl:comment>
CXF configuration
</xsl:comment>
</xsl:if>
<xsl:apply-templates select="cxf:*"/>
<xsl:if test="drools:*">
<xsl:comment>
Drools configuration
</xsl:comment>
</xsl:if>
<xsl:apply-templates select="drools:*"/>
<xsl:comment>
Configure blueprint beans
</xsl:comment>
<xsl:apply-templates select="s:bean"/>
</blueprint>
</xsl:template>
<xsl:template match="http-conf:*">
<xsl:element name="{name()}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="sec:*">
<xsl:element name="{name()}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="cxf:*">
<xsl:element name="camelcxf:{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
<xsl:template match="cxf:cxfEndpoint">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="drools:grid-node">
<xsl:comment>
Spring replacement of &lt;drools:grid-node id="node1"/&gt;
</xsl:comment>
<xsl:element name="bean">
<xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
<xsl:attribute name="class">com.base2.ss.mdh.common.util.DroolsBlueprintBean</xsl:attribute>
<xsl:attribute name="factory-method">createGridNode</xsl:attribute>
<xsl:element name="argument">
<xsl:attribute name="value"><xsl:value-of select="@id"/></xsl:attribute>
</xsl:element>
</xsl:element>
</xsl:template>
<xsl:template match="drools:kbase">
<xsl:comment>
Spring replacement of :
&lt;drools:kbase id="kbase1" node="node1"&gt;
&lt;drools:resources&gt;
&lt;drools:resource type="DRL" source="classpath:testSpring.drl"/&gt;
&lt;/drools:resources&gt;
&lt;/drools:kbase>
</xsl:comment>
<xsl:element name="bean">
<xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
<xsl:attribute name="class">com.base2.ss.mdh.common.util.DroolsBlueprintBean</xsl:attribute>
<xsl:attribute name="factory-method">createKnowledgeBase</xsl:attribute>
<xsl:element name="argument">
<xsl:attribute name="ref"><xsl:value-of select="//drools:grid-node/@id"/></xsl:attribute>
</xsl:element>
<xsl:element name="argument">
<xsl:attribute name="value"><xsl:value-of select="drools:resources/drools:resource/@source"/></xsl:attribute>
</xsl:element>
<xsl:element name="argument">
<xsl:attribute name="value"><xsl:value-of select="drools:resources/drools:resource/@type"/></xsl:attribute>
</xsl:element>
</xsl:element>
</xsl:template>
<xsl:template match="s:bean">
<xsl:if test="@id != \'activemq\'">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:if>
</xsl:template>
<xsl:template match="@class">
<xsl:attribute name="{local-name()}">
<xsl:choose>
<xsl:when test=". = \'org.apache.camel.component.file.AntPathMatcherGenericFileFilter\'">com.base2.ss.mdh.common.camel.file.SimpleFileFilter</xsl:when>
<xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
<xsl:template match="@factory-bean">
<xsl:attribute name="factory-ref">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="s:constructor-arg">
<xsl:element name="argument">
<xsl:choose>
<xsl:when test="s:ref">
<xsl:attribute name="ref">
<xsl:value-of select="./s:ref/@bean"/>
</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="@* | node()" />
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment