Coverage report

  %line %branch
net.sf.infrared.tools.server.WeblogicIntegrator
0% 
0% 

 1  
 /*
 2  
  *
 3  
  * Copyright 2005 Tavant Technologies and Contributors
 4  
  * 
 5  
  * Licensed under the Apache License, Version 2.0 (the "License")
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  *     http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  *
 17  
  *
 18  
  *
 19  
  * Original Author:  binil.thomas (Tavant Technologies)
 20  
  * Contributor(s):   -;
 21  
  *
 22  
  */
 23  
 package net.sf.infrared.tools.server;
 24  
 
 25  
 import java.io.File;
 26  
 import java.io.IOException;
 27  
 import java.net.URL;
 28  
 
 29  
 import org.apache.commons.io.FileUtils;
 30  
 import org.apache.xerces.parsers.DOMParser;
 31  
 import org.w3c.dom.Document;
 32  
 import org.w3c.dom.Node;
 33  
 import org.xml.sax.SAXException;
 34  
 
 35  
 /**
 36  
  * 
 37  
  * @author binil.thomas
 38  
  */
 39  0
 public class WeblogicIntegrator extends AbstractServerIntegrator {
 40  0
     private ServletIntegrator si = new ServletIntegrator();
 41  
     
 42  
     public void integrateWar(File rootDir) {
 43  0
         si.integrateWar(rootDir);
 44  0
     }
 45  
     
 46  
     public void integrateEar(File rootDir) {
 47  0
         File metaInfDir = new File(rootDir, "META-INF");
 48  0
         File weblogicApplicationDotXmlFile = new File(metaInfDir, "weblogic-application.xml");
 49  
         
 50  0
         addListenerToWeblogicApplicationDotXml(weblogicApplicationDotXmlFile);
 51  0
     }
 52  
     
 53  
     public String copyIntoWar(File[] toCopy, File warRootDir) {
 54  0
         return si.copyIntoWar(toCopy, warRootDir);
 55  
     }
 56  
     
 57  
     public String copyIntoEar(File[] toCopy, File earRootDir) {
 58  0
         File appInfLib = ensureAppInfLibExists(earRootDir);
 59  0
         File appInfClasses = ensureAppInfClassesExists(earRootDir);
 60  
         
 61  0
         for (int i = 0; i < toCopy.length; i++) {
 62  0
             File f = toCopy[i];
 63  0
             if ( (f.isFile()) && (f.getName().endsWith(".jar")) ) {
 64  
                 try {
 65  0
                     FileUtils.copyFileToDirectory(f, appInfLib);
 66  0
                 } catch (IOException ex) {
 67  0
                     throw new RuntimeException("Failed to copy file " + f.getAbsolutePath() + 
 68  
                             " into " + appInfLib.getAbsolutePath(), ex);
 69  0
                 }
 70  
             } else {
 71  
                 try {
 72  0
                     if (f.isDirectory()) {
 73  0
                         FileUtils.copyDirectory(f, appInfClasses);
 74  
                     } else {
 75  0
                         FileUtils.copyFileToDirectory(f, appInfClasses);
 76  
                     }
 77  0
                 } catch (IOException ex) {
 78  0
                     throw new RuntimeException("Failed to copy file " + f.getAbsolutePath() + 
 79  
                             " into " + appInfClasses.getAbsolutePath(), ex);
 80  0
                 }
 81  
             } 
 82  
         }
 83  0
         return "";
 84  
     }
 85  
     
 86  
     void addListenerToWeblogicApplicationDotXml(File weblogicApplicationDotXmlFile) {
 87  0
         if (! weblogicApplicationDotXmlFile.exists()) {
 88  0
             createTemplateWeblogicApplicationDotXmlFile(weblogicApplicationDotXmlFile);
 89  0
             return;
 90  
         }
 91  
         
 92  0
         DOMParser parser = createParser();
 93  
         try {
 94  0
             parser.parse(weblogicApplicationDotXmlFile.getAbsolutePath());
 95  0
         } catch (Exception ex) {
 96  0
             if (ex instanceof RuntimeException) {
 97  0
                 throw (RuntimeException) ex;
 98  
             }
 99  0
             throw new RuntimeException("Failed to parse weblogic-application.xml file at " + 
 100  
                     weblogicApplicationDotXmlFile.getAbsolutePath(), ex);
 101  0
         } 
 102  0
         Document doc = parser.getDocument();
 103  0
         Node weblogicApplicationNode = doc.getElementsByTagName("weblogic-application").item(0);
 104  0
         if (weblogicApplicationNode == null) {
 105  0
             throw new IllegalArgumentException("META-INF/weblogic-application.xml does not contain " +
 106  
                     "<weblogic-application> element");
 107  
         }
 108  
         
 109  0
         Node listenerNode = createListenerNode(doc);
 110  
         
 111  0
         Node refNode = findNodeAfterListener(doc);
 112  0
         if (refNode != null) {
 113  0
             weblogicApplicationNode.insertBefore(listenerNode, refNode);
 114  
         } else {
 115  0
             weblogicApplicationNode.appendChild(listenerNode);
 116  
         } 
 117  0
         writeFile(weblogicApplicationDotXmlFile, doc);
 118  0
     }
 119  
 
 120  
     void createTemplateWeblogicApplicationDotXmlFile(File weblogicApplicationDotXmlFile) {
 121  
         try {
 122  0
             FileUtils.forceMkdir(weblogicApplicationDotXmlFile.getParentFile());
 123  0
         } catch (IOException ex) {
 124  0
             throw new RuntimeException("Failed to create directory " + 
 125  
                     weblogicApplicationDotXmlFile.getParentFile(), ex);
 126  0
         }
 127  0
         URL template = 
 128  
             getClass().getClassLoader().getResource("template-weblogic-application.xml");
 129  0
         if (template != null) {
 130  
             try {
 131  0
                 FileUtils.copyURLToFile(template, weblogicApplicationDotXmlFile);            
 132  0
             } catch (IOException ex) {
 133  0
                 throw new RuntimeException("Failed to create new weblogic-application.xml at " + 
 134  
                         weblogicApplicationDotXmlFile.getAbsolutePath(), ex);
 135  0
             }            
 136  
         } 
 137  0
     }
 138  
     
 139  
     Node findNodeAfterListener(Document doc) {
 140  0
         String[] possibleChildNames = new String[] {"startup", "shutdown"};
 141  0
         return findFirstMatchingElement(doc, possibleChildNames);
 142  
     }
 143  
     
 144  
 //  <weblogic-application>
 145  
 //      ...  
 146  
 //      <listener>
 147  
 //          <listener-class>net.sf.infrared.weblogic.setup.InfraREDApplicationLifeCycleListener</listener-class>
 148  
 //      </listener>
 149  
 //      ...
 150  
 //  </weblogic-application>
 151  
     Node createListenerNode(Document doc) {
 152  0
         Node listenerNode = doc.createElement("listener");
 153  0
         Node listenerClassNode = doc.createElement("listener-class");
 154  0
         listenerClassNode.appendChild(
 155  
                 doc.createTextNode("net.sf.infrared.weblogic.setup.InfraREDApplicationLifeCycleListener"));
 156  0
         listenerNode.appendChild(listenerClassNode);
 157  0
         return listenerNode;
 158  
     }        
 159  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.