Home > Java > Glassfish-Embedded and WebFragments

Glassfish-Embedded and WebFragments

I lost a couple of working days struggling for deploying JEE6 webfragment to glassfish-embedded. I had no luck looking around forums and docs about this (quite) new way of JEE6 packaging in glassfish embedded so… I found how to work-around it by myself: packaging ScatteredArchive and merging it with my web fragment jar.

Of course this is a temporary solution (till someone will solve the problem) and it’s only for unit test porpose but this allows me to go on working.. :-)

Anyway, the solution. Suppose you have a war project called test-web and a jar project (a web fragment project) called test-web-fragment, the following code assembles the war with fragment merge and deployes it.

ScatteredArchive archiveWar = new ScatteredArchive(“test-web”,ScatteredArchive.Type.WAR, new File(“../test-web/WebContent”));
archiveWar.addClassPath( new File(“../test-web/build/classes”));
archiveWar.addClassPath( new File(“../test-web-fragment/build/classes”));
File mergeXml = JEEArchiveXml.getMergedWebXml(new File(“../test-web/WebContent/WEB-INF/web.xml”), new File(“../test-web-fragment/src/META-INF/web-fragment.xml”));

Here is the getMergedWebXml() utility method (a bit long but with no extra JDK dependencies ;-) :

public static File getMergedWebXml(File webXmlFile,File webFragmentXmlFile  ) throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document webXml = factory.newDocumentBuilder().parse(webXmlFile);
Document webFragmentXml = factory.newDocumentBuilder().parse(webFragmentXmlFile);
NodeList servletsWebXml = webXml.getElementsByTagName(“servlet”);
NodeList servletsWebFragmentXml = webFragmentXml.getElementsByTagName(“servlet”);
NodeList servletMappsWebXml = webXml.getElementsByTagName(“servlet-mapping”);
NodeList servletMappsWebFragmentXml = webFragmentXml.getElementsByTagName(“servlet-mapping”);

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
if (servletsWebXml.getLength()==0){
fail(webXmlFile.toString()+” does not contain servlets??”);
if (servletsWebFragmentXml.getLength()==0){
log.warning(webFragmentXmlFile.toString()+” does not contain servlets??”);
Node lastServlet = servletsWebXml.item(servletsWebXml.getLength()-1);
for (int i = 0; i < servletsWebFragmentXml.getLength(); i++) {
Node imported = webXml.importNode(servletsWebFragmentXml.item(i), true);
lastServlet.getParentNode().insertBefore(imported, lastServlet.getNextSibling());
imported = lastServlet;
if (servletMappsWebFragmentXml.getLength()==0){
log.warning(webFragmentXmlFile.toString()+” does not contain servletMapping??”);
if (servletMappsWebXml.getLength()==0){
log.warning(webXmlFile.toString()+” does not contain servletMapping??”);
Node lastServletMapping = servletMappsWebXml.item(servletMappsWebXml.getLength()-1);

for (int i = 0; i < servletMappsWebFragmentXml.getLength(); i++) {
Node imported = webXml.importNode(servletMappsWebFragmentXml.item(i), true);
lastServletMapping.getParentNode().insertBefore(imported, lastServletMapping.getNextSibling());
imported = lastServletMapping;
DOMSource source = new DOMSource(webXml);
transformer.setOutputProperty(“{http://xml.apache.org/xslt}indent-amount”, “2”);
transformer.setOutputProperty(OutputKeys.INDENT, “yes”);
File tempDir = new File(System.getProperty(“java.io.tmpdir”)+”/mergedWebXml”+System.currentTimeMillis()+”/”);
File tempFileXml = new File(tempDir,”/web.xml”);
if (tempFileXml.exists()){
StreamResult result = new StreamResult(tempFileXml);
transformer.transform(source, result);
return tempFileXml;

Hope this helps.

  1. Non c'è ancora nessun commento.
  1. aprile 14, 2015 alle 08:17
  2. dicembre 30, 2016 alle 09:30


Effettua il login con uno di questi metodi per inviare il tuo commento:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )


Connessione a %s...

%d blogger hanno fatto clic su Mi Piace per questo: