Created
March 24, 2015 00:21
-
-
Save nuzayats/109f6bca193f8a99d5ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package some; | |
import org.w3c.dom.Document; | |
import org.w3c.dom.Element; | |
import org.w3c.dom.Node; | |
import org.xml.sax.InputSource; | |
import org.xml.sax.SAXException; | |
import javax.xml.parsers.DocumentBuilder; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import javax.xml.parsers.ParserConfigurationException; | |
import javax.xml.transform.OutputKeys; | |
import javax.xml.transform.Transformer; | |
import javax.xml.transform.TransformerException; | |
import javax.xml.transform.TransformerFactory; | |
import javax.xml.transform.dom.DOMSource; | |
import javax.xml.transform.stream.StreamResult; | |
import javax.xml.xpath.*; | |
import java.io.IOException; | |
import java.io.StringWriter; | |
public class PersistenceDescriptorModifier { | |
private final Document doc; | |
private final XPath xPath; | |
private final XPathExpression persistenceUnitExpression; | |
private final XPathExpression propertiesExpression; | |
public PersistenceDescriptorModifier(InputSource inputSource) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException { | |
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
final DocumentBuilder builder = factory.newDocumentBuilder(); | |
this.doc = builder.parse(inputSource); | |
final XPathFactory xPathFactory = XPathFactory.newInstance(); | |
this.xPath = xPathFactory.newXPath(); | |
this.persistenceUnitExpression = xPath.compile("/persistence/persistence-unit"); | |
this.propertiesExpression = xPath.compile("properties"); | |
} | |
public void setProperty(String name, String value) throws XPathExpressionException { | |
final Node persistenceUnit = (Node) persistenceUnitExpression.evaluate(doc, XPathConstants.NODE); | |
if (persistenceUnit == null) { | |
// no persistence unit | |
throw new IllegalStateException("no persistence unit"); | |
} | |
final Node properties = (Node) propertiesExpression.evaluate(persistenceUnit, XPathConstants.NODE); | |
if (properties == null) { | |
// construct a properties element which contains a property element | |
persistenceUnit.appendChild(createPropertiesElement(doc, name, value)); | |
return; | |
} | |
final Node property = (Node) xPath.evaluate("property[@name='" + name + "']", persistenceUnit, XPathConstants.NODE); | |
if (property != null) { | |
// delete the existing property element | |
properties.removeChild(property); | |
} | |
// add a property element | |
properties.appendChild(createPropertyElement(doc, name, value)); | |
} | |
@Override | |
public String toString() { | |
try { | |
return toString(doc); | |
} catch (TransformerException | IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
private static Element createPropertiesElement(Document doc, String name, String value) { | |
final Element properties = doc.createElement("properties"); | |
properties.appendChild(createPropertyElement(doc, name, value)); | |
return properties; | |
} | |
private static Element createPropertyElement(Document doc, String name, String value) { | |
final Element property = doc.createElement("property"); | |
property.setAttribute("name", name); | |
property.setAttribute("value", value); | |
return property; | |
} | |
private static String toString(Document doc) throws TransformerException, IOException { | |
final TransformerFactory factory = TransformerFactory.newInstance(); | |
factory.setAttribute("indent-number", 4); | |
final Transformer transformer = factory.newTransformer(); | |
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); | |
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); | |
try (final StringWriter sw = new StringWriter()) { | |
transformer.transform(new DOMSource(doc), new StreamResult(sw)); | |
return sw.toString(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package some; | |
import org.junit.Assert; | |
import org.junit.Test; | |
import org.xml.sax.InputSource; | |
import java.io.StringReader; | |
public class PersistenceDescriptorModifierTest { | |
private static final String expected = "<property name=\"javax.persistence.schema-generation.database.action\" value=\"drop-and-create\"/>"; | |
@Test | |
public void modify() throws Exception { | |
String s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + | |
"<persistence xmlns=\"http://xmlns.jcp.org/xml/ns/persistence\" version=\"2.1\">\n" + | |
" <persistence-unit name=\"Model2PU\">\n" + | |
" <jta-data-source>java:jboss/datasources/Model2DS</jta-data-source>\n" + | |
" <properties>\n" + | |
" <property name=\"javax.persistence.schema-generation.database.action\" value=\"none\"/>\n" + | |
" <property name=\"someProp\" value=\"someValue\"/>\n" + | |
" </properties>\n" + | |
" </persistence-unit>\n" + | |
"</persistence>"; | |
try (StringReader r = new StringReader(s)) { | |
PersistenceDescriptorModifier m = new PersistenceDescriptorModifier(new InputSource(r)); | |
m.setProperty("javax.persistence.schema-generation.database.action", "drop-and-create"); | |
Assert.assertTrue(m.toString().contains(expected)); | |
} | |
} | |
@Test | |
public void add1() throws Exception { | |
String s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + | |
"<persistence xmlns=\"http://xmlns.jcp.org/xml/ns/persistence\" version=\"2.1\">\n" + | |
" <persistence-unit name=\"Model2PU\">\n" + | |
" <jta-data-source>java:jboss/datasources/Model2DS</jta-data-source>\n" + | |
" <properties>\n" + | |
" <property name=\"someProp\" value=\"someValue\"/>\n" + | |
" </properties>\n" + | |
" </persistence-unit>\n" + | |
"</persistence>"; | |
try (StringReader r = new StringReader(s)) { | |
PersistenceDescriptorModifier m = new PersistenceDescriptorModifier(new InputSource(r)); | |
m.setProperty("javax.persistence.schema-generation.database.action", "drop-and-create"); | |
Assert.assertTrue(m.toString().contains(expected)); | |
} | |
} | |
@Test | |
public void add2() throws Exception { | |
String s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + | |
"<persistence xmlns=\"http://xmlns.jcp.org/xml/ns/persistence\" version=\"2.1\">\n" + | |
" <persistence-unit name=\"Model2PU\">\n" + | |
" <jta-data-source>java:jboss/datasources/Model2DS</jta-data-source>\n" + | |
" </persistence-unit>\n" + | |
"</persistence>"; | |
try (StringReader r = new StringReader(s)) { | |
PersistenceDescriptorModifier m = new PersistenceDescriptorModifier(new InputSource(r)); | |
m.setProperty("javax.persistence.schema-generation.database.action", "drop-and-create"); | |
Assert.assertTrue(m.toString().contains(expected)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello Sir,
How can i invoke the class "PersistenceDescriptorModifier.java" ? Is there any Listener or Xml file or dependency injection to instantiate the class?
Do you have any example ?
Thanks,
Breno