DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Shrinkwrap DSLs Makes Building Config Files Easy

Shrinkwrap DSLs Makes Building Config Files Easy

Andy Gibson user avatar by
Andy Gibson
·
Feb. 09, 11 · Java Zone · Interview
Like (0)
Save
Tweet
4.82K Views

Join the DZone community and get the full member experience.

Join For Free

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/

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Data Analysis Using Google Cloud Data Studio
  • Transactions vs. Analytics in Apache Kafka
  • Top Soft Skills to Identify a Great Software Engineer
  • Kafka Fail-Over Using Quarkus Reactive Messaging

Comments

Java Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo