Coverage report

  %line %branch
net.sf.infrared.tools.server.ServletIntegrator
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.DOMException;
 32  
 import org.w3c.dom.Document;
 33  
 import org.w3c.dom.Node;
 34  
 import org.xml.sax.SAXException;
 35  
 
 36  
 /**
 37  
  * Integrates InfraRed with a servlet container.
 38  
  *
 39  
  * @author binil.thomas
 40  
  */
 41  0
 public class ServletIntegrator extends AbstractServerIntegrator {
 42  
     public void integrateEar(File rootDir) {
 43  0
         throw new UnsupportedOperationException("Servlet containers cannot handle EAR files");
 44  
     }
 45  
     
 46  
     public void integrateWar(File rootDir) {
 47  0
         File webInfDir = new File(rootDir, "WEB-INF");
 48  0
         File webDotXmlFile = new File(webInfDir, "web.xml");
 49  
         
 50  0
         addListenerAndFilterToWebDotXml(webDotXmlFile);        
 51  0
     }
 52  
     
 53  
     public String copyIntoWar(File[] toCopy, File warRootDir) {
 54  0
         File web_inf_lib = ensureWebInfLibExists(warRootDir);
 55  0
         File web_inf_classes = ensureWebInfClassesExists(warRootDir);
 56  0
         for (int i = 0; i < toCopy.length; i++) {
 57  0
             File f = toCopy[i];
 58  0
             if ( (f.isFile()) && (f.getName().endsWith(".jar")) ) {
 59  
                 try {
 60  0
                     FileUtils.copyFileToDirectory(f, web_inf_lib);
 61  0
                 } catch (IOException ex) {
 62  0
                     throw new RuntimeException("Failed to copy file " + f.getAbsolutePath() + 
 63  
                             " to directory " + web_inf_lib.getAbsolutePath(), ex);
 64  0
                 }
 65  
             } else {
 66  
                 try {
 67  0
                     if (f.isDirectory()) {
 68  0
                         FileUtils.copyDirectory(f, web_inf_classes);
 69  
                     } else {
 70  0
                         FileUtils.copyFileToDirectory(f, web_inf_classes);
 71  
                     }                	
 72  0
                 } catch (IOException ex) {
 73  0
                     throw new RuntimeException("Failed to copy file " + f.getAbsolutePath() + 
 74  
                             " to directory " + web_inf_classes.getAbsolutePath(), ex);
 75  0
                 }
 76  
             }            
 77  
         }
 78  0
         return "";
 79  
     }
 80  
     
 81  
     public String copyIntoEar(File[] toCopy, File earRootDir) {
 82  0
         throw new UnsupportedOperationException("Servlet containers cannot handle EAR files");
 83  
     }
 84  
     
 85  
     void addListenerAndFilterToWebDotXml(File webDotXmlFile) {
 86  0
         if (! webDotXmlFile.exists()) {
 87  0
             createTemplateWebDotXmlFile(webDotXmlFile);
 88  0
             return;
 89  
         }
 90  
         
 91  0
         DOMParser parser = createParser();
 92  
         try {
 93  0
             parser.parse(webDotXmlFile.getAbsolutePath());
 94  0
         } catch (Exception ex) {
 95  0
             if (ex instanceof RuntimeException) {
 96  0
                 throw (RuntimeException) ex;
 97  
             }
 98  0
             throw new RuntimeException("Failed to parse web.xml file at " + 
 99  
                     webDotXmlFile.getAbsolutePath(), ex);
 100  0
         }
 101  0
         Document doc = parser.getDocument();
 102  0
         Node webAppNode = doc.getElementsByTagName("web-app").item(0);
 103  0
         if (webAppNode == null) {
 104  0
             throw new IllegalArgumentException("WEB-INF/web.xml does not contain <web-app> element");
 105  
         }
 106  0
         addListenerNode(doc, webAppNode);
 107  0
         addFilterNode(doc, webAppNode);
 108  0
         addFilterMappingNode(doc, webAppNode);
 109  
         
 110  0
         writeFile(webDotXmlFile, doc);
 111  0
     }
 112  
 
 113  
     void addFilterMappingNode(final Document doc, class="keyword">final Node webAppNode) throws DOMException {        
 114  
         // adding <filter-mapping>. This is added as the first mapping, right after <filter>
 115  0
         Node filterMappingNode = createFilterMappingNode(doc);
 116  0
         Node refNode = findNodeAfterFilter(doc);
 117  
         // find the first node after <filter> and insert before it
 118  
         // if no node after <filter>, insert at the end of the document
 119  0
         if (refNode != null) {
 120  0
             webAppNode.insertBefore(filterMappingNode, refNode);
 121  
         } else {
 122  0
             webAppNode.appendChild(filterMappingNode);
 123  
         }
 124  0
     }
 125  
 
 126  
     void addFilterNode(final Document doc, class="keyword">final Node webAppNode) throws DOMException {        
 127  
         // adding <filter>. This is added as the first filter, right after <context-param>
 128  0
         Node filterNode = createFilterNode(doc);
 129  0
         Node refNode = findNodeAfterContextParam(doc);
 130  
         // find the first node after <context-param> and insert before it
 131  
         // if no node after <context-param>, insert at the end of the document
 132  0
         if (refNode != null) {
 133  0
             webAppNode.insertBefore(filterNode, refNode);
 134  
         } else {
 135  0
             webAppNode.appendChild(filterNode);
 136  
         }
 137  0
     }
 138  
 
 139  
     void addListenerNode(final Document doc, class="keyword">final Node webAppNode) throws DOMException {        
 140  
         // adding <listener>.
 141  0
         Node listenerNode = createListenerNode(doc);
 142  0
         Node refNode = findNodeAfterListener(doc);
 143  
         // find the first node after <listener> and insert before it
 144  
         // if no node after <listener>, insert at the end of the document
 145  0
         if (refNode != null) {
 146  0
             webAppNode.insertBefore(listenerNode, refNode);
 147  
         } else {
 148  0
             webAppNode.appendChild(listenerNode);
 149  
         } 
 150  0
     }
 151  
     
 152  
     void createTemplateWebDotXmlFile(File webDotXmlFile) {
 153  
         try {
 154  0
             FileUtils.forceMkdir(webDotXmlFile.getParentFile());
 155  0
         } catch (IOException ex) {
 156  
             
 157  0
         }
 158  0
         URL template = Thread.currentThread().getContextClassLoader().getResource("template-web.xml");
 159  0
         if (template != null) {
 160  
             try {
 161  0
                 FileUtils.copyURLToFile(template, webDotXmlFile);
 162  0
             } catch (IOException ex) {
 163  0
                 throw new RuntimeException("Failed to create web.xml file " + 
 164  
                         webDotXmlFile.getAbsolutePath());
 165  0
             }
 166  
         }
 167  0
     }
 168  
     
 169  
     Node findNodeAfterListener(Document doc) {
 170  
         // possible nodes after <listener>
 171  0
         String[] possibleChildren = new String[] {
 172  
                 "servlet", "servlet-mapping", "session-config", "mime-mapping", 
 173  
                 "welcome-file-list", "error-page", "taglib", "resource-env-ref", 
 174  
                 "resource-ref", "security-constraint", "login-config", "security-role", 
 175  
                 "env-entry", "ejb-ref",  "ejb-local-ref"
 176  
                 };
 177  0
         return findFirstMatchingElement(doc, possibleChildren);
 178  
     }
 179  
     
 180  
     Node findNodeAfterContextParam(Document doc) {
 181  
         // possible nodes after <context-param>
 182  0
         String[] possibleChildren = new String[] {
 183  
                 "filter", "filter-mapping", "listener",
 184  
                 "servlet", "servlet-mapping", "session-config", "mime-mapping", 
 185  
                 "welcome-file-list", "error-page", "taglib", "resource-env-ref", 
 186  
                 "resource-ref", "security-constraint", "login-config", "security-role", 
 187  
                 "env-entry", "ejb-ref",  "ejb-local-ref"
 188  
                 };
 189  0
         return findFirstMatchingElement(doc, possibleChildren);
 190  
     }
 191  
     
 192  
     Node findNodeAfterFilter(Document doc) {
 193  
         // possible nodes after <filter>
 194  0
         String[] possibleChildren = new String[] {
 195  
                 "filter-mapping", "listener",
 196  
                 "servlet", "servlet-mapping", "session-config", "mime-mapping", 
 197  
                 "welcome-file-list", "error-page", "taglib", "resource-env-ref", 
 198  
                 "resource-ref", "security-constraint", "login-config", "security-role", 
 199  
                 "env-entry", "ejb-ref",  "ejb-local-ref"
 200  
                 };
 201  0
         return findFirstMatchingElement(doc, possibleChildren);
 202  
     }
 203  
     
 204  
 //    <web-app>
 205  
 //      ..    
 206  
 //        <listener>
 207  
 //            <listener-class>net.sf.infrared.agent.setup.InfraREDServletContextListener</listener-class>
 208  
 //        </listener>
 209  
 //        ..
 210  
 //    </web-app>    
 211  
     Node createListenerNode(Document doc) {
 212  0
         Node listenerNode = doc.createElement("listener");
 213  0
         Node listenerClassNode = doc.createElement("listener-class");
 214  0
         listenerClassNode.appendChild(
 215  
                 doc.createTextNode("net.sf.infrared.agent.setup.InfraREDServletContextListener"));
 216  0
         listenerNode.appendChild(listenerClassNode);
 217  0
         return listenerNode;
 218  
     }
 219  
 
 220  
     
 221  
 //    <web-app>
 222  
 //      ..    
 223  
 //        <filter>
 224  
 //            <filter-name>infrared</filter-name>
 225  
 //            <filter-class>net.sf.infrared.aspects.servlet.InfraREDServletFilter</filter-class>
 226  
 //        </filter>
 227  
 //        ..
 228  
 //    </web-app>        
 229  
     Node createFilterNode(Document doc) {
 230  0
         Node filterNode = doc.createElement("filter");
 231  0
         Node filterNameNode = doc.createElement("filter-name");
 232  0
         filterNameNode.appendChild( doc.createTextNode("infrared") );
 233  0
         Node filterClassNode = doc.createElement("filter-class");
 234  0
         filterClassNode.appendChild(
 235  
                 doc.createTextNode("net.sf.infrared.aspects.servlet.InfraREDServletFilter"));
 236  0
         filterNode.appendChild(filterNameNode);
 237  0
         filterNode.appendChild(filterClassNode);
 238  0
         return filterNode;
 239  
     }
 240  
 
 241  
 //    <web-app>
 242  
 //     ..    
 243  
 //       <filter-mapping>
 244  
 //          <filter-name>infrared</filter-name>
 245  
 //          <url-pattern>/*</url-pattern>
 246  
 //       </filter-mapping>
 247  
 //     ..
 248  
 //    </web-app>    
 249  
     Node createFilterMappingNode(Document doc) {
 250  0
         Node filterMappingNode = doc.createElement("filter-mapping");
 251  0
         Node filterNameNode = doc.createElement("filter-name");
 252  0
         filterNameNode.appendChild( doc.createTextNode("infrared") );
 253  0
         Node urlPatternNode = doc.createElement("url-pattern");
 254  0
         urlPatternNode.appendChild( doc.createTextNode("/*") );
 255  0
         filterMappingNode.appendChild(filterNameNode);
 256  0
         filterMappingNode.appendChild(urlPatternNode);
 257  0
         return filterMappingNode;
 258  
     }
 259  
 }

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