Coverage Report - com.aurea.maven.plugins.sonic.utils.DependencyAnalyzer
 
Classes in this File Line Coverage Branch Coverage Complexity
DependencyAnalyzer
0%
0/65
0%
0/38
3.333
 
 1  
 package com.aurea.maven.plugins.sonic.utils;
 2  
 
 3  
 import java.io.ByteArrayInputStream;
 4  
 import java.io.File;
 5  
 import java.util.ArrayList;
 6  
 import java.util.List;
 7  
 
 8  
 import com.aurea.maven.plugins.sonic.sdm.container.IServiceType;
 9  
 import com.aurea.maven.plugins.sonic.sdm.container.impl.DefaultServiceType;
 10  
 import com.progress.sonic.utilities.esb.admin.XarAnalyzer;
 11  
 import com.sonicsw.deploy.IArtifact;
 12  
 import com.sonicsw.deploy.artifact.ESBArtifact;
 13  
 import com.sonicsw.deploy.artifact.SonicFSArtifact;
 14  
 
 15  
 /**
 16  
  * This class povides some convenience methods to analyze the dependencies that have been copied the the dependency
 17  
  * directory.
 18  
  * 
 19  
  * @author Andreas Gies
 20  
  */
 21  
 public class DependencyAnalyzer {
 22  
 
 23  
 
 24  0
   private String       baseDir           = null;
 25  0
   private List<String> serviceJarFiles   = null;
 26  
 
 27  
   /**
 28  
    * Jar files on the ServiceType 'container' classpath
 29  
    */
 30  0
   private List<String> serviceCtJarFiles = null;
 31  0
   private List<String> packagedJarFiles  = null;
 32  
 
 33  
   /**
 34  
    * Create the analyzer with the location of the dependency directory. The dependency directory contains two sub
 35  
    * directories <b>jar</b> and <b>xar</b> that contain the jar respective the xar dependencies.
 36  
    * 
 37  
    * @param _baseDir
 38  
    *          _baseDir
 39  
    */
 40  0
   public DependencyAnalyzer(final String _baseDir) {
 41  
 
 42  0
     baseDir = _baseDir;
 43  0
   }
 44  
 
 45  
   /**
 46  
    * Calculate the file names of the dependency jar files.
 47  
    * 
 48  
    * @return An array of Strings that contains the names of the jar file locations relative to the jar-subdirectory of
 49  
    *         the dependency directory.
 50  
    */
 51  
   public final String[] getDependencyJars() {
 52  
 
 53  0
     return FileUtilities.getFileList(getJarDependencyDir(), "**/*.jar", null);
 54  
   }
 55  
 
 56  
   /**
 57  
    * Calculate the list of jar files are referenced in the classpath settings of the servicetypes that occurr in the
 58  
    * dependent xar archives. The result is a list of jar files as they are stored in the directory service (i.e.
 59  
    * <b>SonicFS/ESBResources/log4j/log4j-1.2.13.jar</b>)
 60  
    * 
 61  
    * @return The list of jar files referenced by service types in the dependcy tree.
 62  
    */
 63  
   public final List<String> getServiceJarFiles() {
 64  
 
 65  
     // Only calculate the list once
 66  0
     if (serviceJarFiles == null) {
 67  0
       serviceJarFiles = new ArrayList<String>();
 68  0
       serviceCtJarFiles = new ArrayList<String>();
 69  
 
 70  
       // Iterate over the xar archives
 71  0
       for (String xarFile : FileUtilities.getFileList(getXarDependencyDir(), "**/*.xar", null)) {
 72  
         try {
 73  0
           XarAnalyzer analyzer = new XarAnalyzer(new File(getXarDependencyDir() + "/" + xarFile));
 74  
           // now get the servicetypes in the xar file and record all refrenced jar files from the claspath
 75  0
           for (IArtifact sTypeArtifact : analyzer.getArtifacts(ESBArtifact.SERVICE_TYPE)) {
 76  0
             IServiceType sType = new DefaultServiceType();
 77  0
             byte[] bytes = analyzer.getContent(sTypeArtifact);
 78  
             // in Sonic 7.6 the self first flag is not set corrctly: <xq:selfFirst/> --> bug 351
 79  0
             String temp = new String(bytes);
 80  0
             temp = temp.replace("<xq:selfFirst/>", "<xq:selfFirst>false</xq:selfFirst>");
 81  0
             sType.load(new ByteArrayInputStream(temp.getBytes()));
 82  
 
 83  0
             serviceJarFiles.addAll(sType.getInstanceClasspath());
 84  0
             serviceJarFiles.addAll(sType.getTypeClasspath());
 85  0
             serviceJarFiles.addAll(sType.getContainerClasspath());
 86  0
             serviceCtJarFiles.addAll(sType.getContainerClasspath());
 87  0
           }
 88  0
         } catch (Exception e) {
 89  0
           e.printStackTrace();
 90  0
         }
 91  
       }
 92  
     }
 93  0
     return serviceJarFiles;
 94  
   }
 95  
 
 96  
   /**
 97  
    * Get the list of jar files that are on dependent ServiceType 'container' classpath.
 98  
    * 
 99  
    * @return list of service container jar files
 100  
    */
 101  
   public final List<String> getServiceCtJarFiles() {
 102  
 
 103  0
     if (serviceCtJarFiles == null) {
 104  0
       getServiceJarFiles();
 105  
     }
 106  
 
 107  0
     return serviceCtJarFiles;
 108  
   }
 109  
 
 110  
   /**
 111  
    * Calculate the list of unpackaged jar files. These are the jar files that occurr in the dependency directory, but
 112  
    * not in the list of packaged jar files.
 113  
    * 
 114  
    * @return The list of jar files that are not yet packaged.
 115  
    */
 116  
   public final List<String> getUnpackagedJarFiles() {
 117  
 
 118  0
     List<String> result = new ArrayList<String>();
 119  
 
 120  
     // SF changed from SonicFS:/... to /SonicFS/ESBResources/... as that is what is returned by the SonicFSArtifact
 121  0
     for (String jarFile : getDependencyJars()) {
 122  0
       String toCompare = "/SonicFS/ESBResources/" + jarFile.replaceAll("\\\\", "/");
 123  
 
 124  0
       if (!getPackagedJarFiles().contains(toCompare)) {
 125  0
         result.add(jarFile);
 126  
       }
 127  
     }
 128  
 
 129  0
     return result;
 130  
   }
 131  
 
 132  
   /**
 133  
    * Calculate a list of jar files that are already packaged in the dependent xar files. This will help us to avoid
 134  
    * duplpicate packaging of jars and keep the deliverables small. For now we assume that the jar files reside in the
 135  
    * <b>ESBResources</b> directory of the domain storage.
 136  
    * 
 137  
    * @return the list of packaged jar files.
 138  
    */
 139  
   public final List<String> getPackagedJarFiles() {
 140  
 
 141  0
     if (packagedJarFiles == null) {
 142  0
       packagedJarFiles = new ArrayList<String>();
 143  0
       for (String xarFile : FileUtilities.getFileList(getXarDependencyDir(), "**/*.xar", null)) {
 144  0
         XarAnalyzer analyzer = new XarAnalyzer(new File(getXarDependencyDir() + "/" + xarFile));
 145  
         try {
 146  0
           for (IArtifact jarFile : analyzer.getArtifacts(new SonicFSArtifact("ESBResources/"))) {
 147  0
             packagedJarFiles.add(jarFile.getArchivePath());
 148  0
           }
 149  0
         } catch (Exception e) {
 150  0
           e.printStackTrace();
 151  0
         }
 152  
       }
 153  
     }
 154  0
     return packagedJarFiles;
 155  
   }
 156  
 
 157  
   /**
 158  
    * Calculate an additional ESB Container classpath. That classpath consists of the jar files that occurr in the
 159  
    * dependency directory, but are not referenced in the servicetypes of the dependent xar archives. We will only add
 160  
    * those jar files to the classpath that match a given regular expression.
 161  
    * 
 162  
    * @param _cpPattern
 163  
    *          The regular expression to select the jar files that need to be on the classpath
 164  
    * 
 165  
    * @param _existingClasspath
 166  
    *          The existing classpath as a List<String>. Any jars on this list will not be added again.
 167  
    * 
 168  
    * @param _ignoreServiceTypeDeps
 169  
    *          Whether or not to ignore
 170  
    * 
 171  
    * @return a list of urls, that can be set as the classpath attribute of an ESB Container
 172  
    */
 173  
   public final List<String> additionalContainerClassPath(final String _cpPattern,
 174  
       final List<String> _existingClasspath, final boolean _ignoreServiceTypeDeps) {
 175  
 
 176  0
     List<String> result = new ArrayList<String>();
 177  0
     if (_existingClasspath != null) {
 178  0
       result.addAll(_existingClasspath);
 179  
     }
 180  
 
 181  0
     for (String jarFile : getDependencyJars()) {
 182  0
       String jarURL = "sonicfs:///ESBResources/" + jarFile.replaceAll("\\\\", "/");
 183  0
       if (!(_ignoreServiceTypeDeps && getServiceJarFiles().contains(jarURL)) && !result.contains(jarURL)) {
 184  0
         if (jarFile.matches(_cpPattern)) {
 185  0
           result.add(jarURL);
 186  
         }
 187  
       }
 188  
     }
 189  
 
 190  
     /**
 191  
      * If not ignoring service type jars, also attempt to match service type jars that are on the service type level
 192  
      * container classpath.
 193  
      */
 194  0
     if (!_ignoreServiceTypeDeps) {
 195  0
       for (String jarURL : getServiceCtJarFiles()) {
 196  0
         String jarFile = jarURL.substring(jarURL.lastIndexOf('/') + 1);
 197  0
         if (jarFile.matches(_cpPattern) && !result.contains(jarURL)) {
 198  0
           result.add(jarURL);
 199  
         }
 200  0
       }
 201  
     }
 202  
 
 203  0
     return result;
 204  
   }
 205  
 
 206  
   /**
 207  
    * Convenience method to calculate the xar file dependency location.
 208  
    * 
 209  
    * @return the xar file dependency location
 210  
    */
 211  
   public final String getXarDependencyDir() {
 212  
 
 213  0
     return baseDir + "/xar";
 214  
   }
 215  
 
 216  
   /**
 217  
    * Convenience method to calculate the jar file dependency location.
 218  
    * 
 219  
    * @return the jar file dependency location
 220  
    */
 221  
   public final String getJarDependencyDir() {
 222  
 
 223  0
     return baseDir + "/jar";
 224  
   }
 225  
 }