I've been developing software for over 15 years working in Delphi and now Java. This site is a home for my open source projects, writings, articles and tutorials mainly focusing on Java and Java EE. Andy is a DZone MVB and is not an employee of DZone and has posted 34 posts at DZone. You can read more from them at their website. View Full User Profile

Shrinkwrap DSLs Makes Building Config Files Easy

02.09.2011
| 4188 views |
  • submit to reddit

If you’ve used Shrinkwrap you might have noticed that creating configuration files can be a bit of a burden requiring you to manually build XML configuration files yourself as strings. This article shows how the DSLs being added to Shrinkwrap will make configuring your deployments far easier.

I’m not sure on the current status of the DSL project, but you can find it on GitHub on the shrinkwrap / descriptors page. These are still in development, but as they are fairly stand-alone, they work well enough to use. To get started, download the maven project from the GitHub page, build and install it.

The descriptors project contains classes implementing DSLs for creating configuration files that can be exported to text. These files can then be added to the Shrinkwrap archive. Here’s a basic example that adds a beans.xml for CDI support.

// first create the descriptor
BeansDescriptor bd = Descriptors.create(BeansDescriptor.class);

// add alternatives classes
bd.alternativeClasses(EmailEventHandler.class, LogEventHandler.class)
.decorator(BillingProcessorDecorator.class);// add decorator classes

System.out.println(bd.exportAsString());

Produces a result of

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans1_0.xsd">
<decorators>
<class>org.demo.beans.BillingProcessorDecorator</class>
</decorators>
<alternatives>
<class>org.demo.EventHandlers.EmailEventHandler</class>
<class>org.demo.EventHandlers.LogEventHandler</class>
</alternatives>
</beans>

First you must get an instance of the descriptor which is the basis for the dsl, and then you can define the properties for the descriptor using the methods defined on it, chaining the calls as needed.

Another example uses the PersistenceDescriptor and can be used to create persistence.xml files :

PersistenceDescriptor pd = Descriptors.create(PersistenceDescriptor.class);

pd.persistenceUnit("myUnit")
.classes(Person.class, User.class)
.description("Default Persistence Unit")
.formatSql()
.schemaGenerationMode(SchemaGenerationModeType.CREATE_DROP)
.jtaDataSource("java:/DefaultDS")
.property("hibernate.cache.provider_class","org.hibernate.cache.HashtableCacheProvider")
.version("2.0");

System.out.println(pd.exportAsString());

produces

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="myUnit">
<description>Default Persistence Unit</description>
<jta-data-source>java:/DefaultDS</jta-data-source>
<class>org.demo.model.Person</class>
<class>org.demo.model.User</class>
<properties>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
</properties>
</persistence-unit>
</persistence>

The project also supports importing configuration files to the associated description type since the descriptors are modeled using JAXB XML bindings. At the moment the following descriptor types are supported :

  • web.xml – Web deployment descriptor file
  • persistence.xml – JPA Persistence config file
  • faces-config.xml – Configuration file for Java Server Faces
  • beans.xml – Triggers the use of CDI and can contain configuration info

While the goal is to use these to help generate Shrinkwrap archives, particularly for Arquillian testing, there’s nothing wrong with using them to automatically generate configuration files, especially for new projects.

 

From http://www.andygibson.net/blog/article/shrinkwrap-dsls-makes-building-config-files-easy/

Published at DZone with permission of Andy Gibson, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)