Coverage report

  %line %branch
net.sf.infrared.tools.server.JbossIntegrator
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  
 import java.util.ArrayList;
 29  
 import java.util.Iterator;
 30  
 import java.util.List;
 31  
 
 32  
 import net.sf.infrared.tools.util.JarHelper;
 33  
 
 34  
 import org.apache.commons.io.FileUtils;
 35  
 import org.apache.xerces.parsers.DOMParser;
 36  
 import org.w3c.dom.Document;
 37  
 import org.w3c.dom.Element;
 38  
 import org.w3c.dom.Node;
 39  
 
 40  
 /**
 41  
  * Integrates InfraRed with JBoss container.
 42  
  *
 43  
  * @author binil.thomas
 44  
  */
 45  0
 public class JbossIntegrator extends AbstractServerIntegrator {
 46  0
     private final String TEMPLATE_SAR_RES_NAME = "template-infrared-mbean.sar";
 47  
     
 48  0
     private final String TEMPLATE_SAR_NAME = "infrared-mbean";
 49  
     
 50  0
     private final String SAR_NAME = "infrared-mbean.sar";
 51  
     
 52  0
     private ServletIntegrator si = new ServletIntegrator();
 53  
     
 54  
     private String applicationName;
 55  
     
 56  
     private String portNumber;
 57  
     
 58  
     private File cfg;
 59  
     
 60  
     public void setApplicationName(String name) {
 61  0
         this.applicationName = name;
 62  0
     }
 63  
     
 64  
     public void setApplicationPort(String port) {
 65  0
         int p = -1;
 66  
         try {
 67  0
             p = Integer.parseInt(port);
 68  0
         } catch (NumberFormatException numex) {
 69  0
             throw new RuntimeException("Port number set is not valid " + port, numex);
 70  0
         }
 71  0
         if (p < 0) {
 72  0
             throw new RuntimeException("Cannot set negative port number " + port);
 73  
         }
 74  0
         portNumber = port;
 75  0
     }
 76  
     
 77  
     public void setCfg(File cfg){
 78  0
     	this.cfg = cfg;
 79  0
     }
 80  
     
 81  
     public void integrateEar(File rootDir) {
 82  
         // Find this resource; there will be a template-infrared-mbean.sar.jar 
 83  
         // bundled with the ant task
 84  0
         URL sarUrl =
 85  
                 getClass().getClassLoader().getResource(TEMPLATE_SAR_RES_NAME);
 86  
         try {
 87  0
             File archivedSar = new File(rootDir, TEMPLATE_SAR_RES_NAME);
 88  
             // copy the resource template-infrared-mbean.sar.jar into the rootDir
 89  0
             FileUtils.copyURLToFile(sarUrl, archivedSar);
 90  
             // explode it
 91  0
             new JarHelper().unjarDir(archivedSar, rootDir);
 92  
             // delete the the archive; what we now have is the
 93  
             // exploded sar (template-infrared-mbean.sar)
 94  0
             FileUtils.forceDelete(archivedSar);
 95  0
         } catch (IOException ex) {
 96  0
             throw new RuntimeException("Failed to add sar into ear at " +
 97  
                     rootDir.getAbsolutePath(), ex);
 98  0
         }
 99  
         
 100  
         // modify the jboss-service.xml file of the exploded sar
 101  0
         File explodedSar = new File(rootDir, TEMPLATE_SAR_NAME);
 102  0
         File metaDir = new File(explodedSar, "META-INF");
 103  
         
 104  
         try{
 105  0
             if (cfg.isDirectory()) {
 106  0
                 FileUtils.copyDirectory(cfg, explodedSar);
 107  
             }else {
 108  0
                 FileUtils.copyFileToDirectory(cfg, explodedSar);
 109  
             }        	
 110  
         }
 111  0
         catch(IOException e){
 112  0
             throw new RuntimeException("Failed to copy file " + cfg.getAbsolutePath() + 
 113  
                     " into the jboss sar" , e);
 114  
 
 115  0
         }
 116  
 
 117  0
         File jbossServiceDotXmlFile = new File(metaDir, "jboss-service.xml");
 118  0
         addAppNameAndPortToJbossServiceDotXml(jbossServiceDotXmlFile);
 119  
         
 120  
         try {
 121  
             // archive the sar and delete the exploded form
 122  0
             new JarHelper().jarDir(explodedSar, class="keyword">new File(rootDir, SAR_NAME));
 123  0
             FileUtils.forceDelete(explodedSar);
 124  0
         } catch (IOException e) {
 125  0
             throw new RuntimeException("Failed to create sar archive", e);
 126  0
         }
 127  
         
 128  
         // modify the jboss-app.xml to point to the newly added sar
 129  0
         File metaInfDir = new File(rootDir, "META-INF");
 130  0
         File jbossAppDotXmlFile = new File(metaInfDir, "jboss-app.xml");
 131  0
         addSarToJbossAppDotXml(jbossAppDotXmlFile);
 132  0
     }
 133  
     
 134  
     public void integrateWar(File rootDir) {
 135  0
         si.integrateWar(rootDir);
 136  0
     }
 137  
     
 138  
     public String copyIntoWar(File[] toCopy, File warRootDir) {
 139  0
         return si.copyIntoWar(toCopy, warRootDir);
 140  
     }
 141  
     
 142  
     public String copyIntoEar(File[] toCopy, File earRootDir) {
 143  0
         File appInfLib = ensureAppInfLibExists(earRootDir);
 144  0
         File appInfClasses = ensureAppInfClassesExists(earRootDir);
 145  
         
 146  0
         List jarsAdded = new ArrayList();
 147  
 //        List toJar = new ArrayList();
 148  0
         for (int i = 0; i < toCopy.length; i++) {
 149  0
             File f = toCopy[i];
 150  0
             if ( (f.isFile()) && (f.getName().endsWith(".jar")) ) {
 151  
                 try {
 152  0
                     FileUtils.copyFileToDirectory(f, appInfLib);
 153  0
                     jarsAdded.add(f.getName());
 154  0
                 } catch (IOException ex) {
 155  0
                     throw new RuntimeException("Failed to copy file " + f.getAbsolutePath() + 
 156  
                             " into " + appInfLib.getAbsolutePath(), ex);
 157  0
                 }
 158  
             } else {
 159  
             	try{
 160  0
                     if (f.isDirectory()) {
 161  0
                         FileUtils.copyDirectory(f, appInfClasses);
 162  
                     } else {
 163  0
                         FileUtils.copyFileToDirectory(f, appInfClasses);
 164  
                     }            		
 165  
             	}
 166  0
                 catch (IOException ex) {
 167  0
                     throw new RuntimeException("Failed to copy file " + f.getAbsolutePath() + 
 168  
                             " to directory " + appInfClasses.getAbsolutePath(), ex);
 169  0
                 }
 170  
 //                toJar.add(f);
 171  
             } 
 172  
         }
 173  
 //        if(toJar.size() > 0){
 174  
 //        	File jarFile = archiveFilesIntoAppInfLib(toJar, appInfLib);
 175  
 //        	jarsAdded.add(jarFile.getName());
 176  
 //        }
 177  
         
 178  0
         return getClassPathEntries(jarsAdded);
 179  
     }
 180  
     
 181  
     String getClassPathEntries(List cpEntries) {
 182  0
         String cp = "";
 183  0
         for (Iterator it = cpEntries.iterator(); it.hasNext();) {
 184  0
             String cpEntry = (String) it.next();
 185  0
             StringBuffer buffer = new StringBuffer();
 186  0
             buffer.append(".").append(File.separator).append("APP-INF").append(File.separator)
 187  
             	  .append("lib").append(File.separator).append(cpEntry).append(" ");
 188  
             
 189  
 //            cpEntry = "." + File.separator + "APP-INF" + File.separator + "lib" 
 190  
 //            						+ File.separator + cpEntry + " ";
 191  
 //            cpEntry = "APP-INF/lib/" + cpEntry + " ";
 192  0
             cp += buffer.toString();
 193  
         }
 194  0
         return cp + "." + File.separator + "APP-INF" + File.separator + "classes" + File.separator;
 195  
     }
 196  
     
 197  
     File archiveFilesIntoAppInfLib(List toJar, File appInfLib) {
 198  0
         File jarFile = null;
 199  0
         for (int i = 0; true; i++) {
 200  0
             jarFile = new File(appInfLib, "added-by-infrared-" + i + ".jar");
 201  0
             if (! jarFile.exists()) {
 202  0
                 break;
 203  
             }
 204  
         }
 205  
         
 206  
         try {
 207  0
             new JarHelper().jarDirs( (File[]) toJar.toArray( class="keyword">new File[toJar.size()] ), jarFile);
 208  0
         } catch (IOException ex) {
 209  0
             throw new RuntimeException("Failed to archive " + toJar + 
 210  
                     " into " + jarFile.getAbsolutePath());
 211  0
         }
 212  0
         return jarFile;
 213  
     }
 214  
     
 215  
     void addAppNameAndPortToJbossServiceDotXml(File jbossServiceDotXmlFile) {
 216  0
         DOMParser parser = createParser();
 217  
         try {
 218  0
             parser.parse(jbossServiceDotXmlFile.getAbsolutePath());
 219  0
         } catch (Exception ex) {
 220  0
             if (ex instanceof RuntimeException) {
 221  0
                 throw (RuntimeException) ex;
 222  
             }
 223  0
             throw new RuntimeException("Failed to parse jboss-service.xml file at " +
 224  
                     jbossServiceDotXmlFile.getAbsolutePath(), ex);
 225  0
         }
 226  0
         Document doc = parser.getDocument();
 227  
         
 228  0
         Node mBeanNode = doc.getElementsByTagName("mbean").item(0);
 229  
         
 230  0
         Element attributeNode1 = doc.createElement("attribute");
 231  0
         attributeNode1.setAttribute("name", "ApplicatonName");
 232  0
         attributeNode1.appendChild(doc.createTextNode(applicationName));
 233  
         
 234  0
         Element attributeNode2 = doc.createElement("attribute");
 235  0
         attributeNode2.setAttribute("name", "PortNo");
 236  0
         attributeNode2.appendChild(doc.createTextNode(portNumber));
 237  
         
 238  0
         mBeanNode.insertBefore(attributeNode1, null);
 239  0
         mBeanNode.insertBefore(attributeNode2, null);
 240  
         
 241  0
         writeFile(jbossServiceDotXmlFile, doc);
 242  0
     }
 243  
     
 244  
     void addSarToJbossAppDotXml(File jbossAppDotXmlFile) {
 245  0
         if (! jbossAppDotXmlFile.exists()) {
 246  0
             createTemplateJbossAppDotXmlFile(jbossAppDotXmlFile);
 247  
         }
 248  
         
 249  0
         DOMParser parser = createParser();
 250  
         try {
 251  0
             parser.parse(jbossAppDotXmlFile.getAbsolutePath());
 252  0
         } catch (Exception ex) {
 253  0
             if (ex instanceof RuntimeException) {
 254  0
                 throw (RuntimeException) ex;
 255  
             }
 256  0
             throw new RuntimeException("Failed to parse jboss-app.xml file at " +
 257  
                     jbossAppDotXmlFile.getAbsolutePath(), ex);
 258  0
         }
 259  0
         Document doc = parser.getDocument();
 260  0
         Node moduleNode = doc.getElementsByTagName("module").item(0);
 261  0
         Node serviceNode = doc.createElement("service");
 262  0
         serviceNode.appendChild(doc.createTextNode(SAR_NAME));
 263  
         
 264  0
         moduleNode.insertBefore(serviceNode, null);
 265  
         
 266  0
         writeFile(jbossAppDotXmlFile, doc);
 267  0
     }
 268  
     
 269  
     void createTemplateJbossAppDotXmlFile(File jbossAppDotXmlFile) {
 270  
         try {
 271  0
             FileUtils.forceMkdir(jbossAppDotXmlFile.getParentFile());
 272  0
         } catch (IOException ex) {
 273  0
             throw new RuntimeException("Failed to create directory " +
 274  
                     jbossAppDotXmlFile.getParentFile(), ex);
 275  0
         }
 276  0
         URL template =
 277  
                 getClass().getClassLoader().getResource("template-jboss-app.xml");
 278  0
         if (template != null) {
 279  
             try {
 280  0
                 FileUtils.copyURLToFile(template, jbossAppDotXmlFile);
 281  0
             } catch (IOException ex) {
 282  0
                 throw new RuntimeException("Failed to write " +
 283  
                         jbossAppDotXmlFile.getAbsolutePath(), ex);
 284  0
             }
 285  
         }
 286  0
     }
 287  
 }

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