Coverage Report - com.aurea.maven.plugins.sonic.utils.ProjectAnalyzer
 
Classes in this File Line Coverage Branch Coverage Complexity
ProjectAnalyzer
0%
0/91
0%
0/24
3.2
 
 1  
 package com.aurea.maven.plugins.sonic.utils;
 2  
 
 3  
 import java.io.ByteArrayInputStream;
 4  
 import java.io.File;
 5  
 import java.io.IOException;
 6  
 import java.util.ArrayList;
 7  
 import java.util.HashSet;
 8  
 
 9  
 import org.codehaus.plexus.util.xml.XmlStreamReader;
 10  
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 11  
 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
 12  
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 13  
 
 14  
 
 15  
 
 16  
 /**
 17  
  * @author Jamie Townsend
 18  
  * 
 19  
  */
 20  0
 public class ProjectAnalyzer {
 21  
   
 22  
 
 23  
   /**
 24  
    * gets a list of .esbp process files under a given base location.
 25  
    * 
 26  
    * @param _baseDir
 27  
    *          base location to search from
 28  
    * @return list of process filenames
 29  
    */
 30  
   public static File[] getProcessSourceFiles(final String _baseDir) {
 31  
 
 32  0
     String[] processFileLocations = FileUtilities.getFileList(_baseDir, "**/*.esbp", null);
 33  0
     File[] processFiles = new File[processFileLocations.length];
 34  
     
 35  0
     for (int index = 0; index < processFileLocations.length; index++) {
 36  0
       processFiles[index] = new File(_baseDir, processFileLocations[index]);
 37  
     }
 38  
     
 39  0
     return processFiles;
 40  
   }
 41  
   
 42  
   /**
 43  
    * gets a list of process names located under a given base location.
 44  
    * 
 45  
    * @param _baseDir
 46  
    *          base location to search for process definitions from
 47  
    * @return a list of process names
 48  
    */
 49  
   public static HashSet<String> getProcessNames(final String _baseDir) {
 50  
 
 51  0
     HashSet<String> processNames = new HashSet<String>();
 52  
     
 53  0
     for (File processFile : getProcessSourceFiles(_baseDir)) {
 54  0
       String currProcName = getProcessNameFromFile(processFile);
 55  0
       processNames.add(currProcName);
 56  
     }
 57  0
     return processNames;
 58  
   }
 59  
   
 60  
   /**
 61  
    * gets a list of .esbsvc Service Config files under a given base location.
 62  
    * 
 63  
    * @param _baseDir
 64  
    *          base location to search from
 65  
    * @return list of Service Config filenames
 66  
    */
 67  
   public static File[] getServiceConfigSourceFiles(final String _baseDir) {
 68  
 
 69  0
     String[] serviceConfigFileLocations = FileUtilities.getFileList(_baseDir, "**/*.esbsvc", null);
 70  0
     File[] serviceConfigFiles = new File[serviceConfigFileLocations.length];
 71  
     
 72  0
     for (int index = 0; index < serviceConfigFileLocations.length; index++) {
 73  0
       serviceConfigFiles[index] = new File(_baseDir, serviceConfigFileLocations[index]);
 74  
     }
 75  
     
 76  0
     return serviceConfigFiles;
 77  
   }
 78  
   
 79  
   /**
 80  
    * gets a list of .esbstyp Service Type files under a given base location.
 81  
    * 
 82  
    * @param _baseDir
 83  
    *          base location to search from
 84  
    * @return list of Service Type filenames
 85  
    */
 86  
   public static File[] getServiceTypeSourceFiles(final String _baseDir) {
 87  
 
 88  0
     String[] serviceTypeFileLocations = FileUtilities.getFileList(_baseDir, "**/*.esbstyp", null);
 89  0
     File[] serviceTypeFiles = new File[serviceTypeFileLocations.length];
 90  
     
 91  0
     for (int index = 0; index < serviceTypeFileLocations.length; index++) {
 92  0
       serviceTypeFiles[index] = new File(_baseDir, serviceTypeFileLocations[index]);
 93  
     }
 94  
     
 95  0
     return serviceTypeFiles;
 96  
   }
 97  
   
 98  
 
 99  
   /**
 100  
    * @param _baseDir
 101  
    *          base location to search from
 102  
    * @return list of sub process names referenced in the defined processes
 103  
    */
 104  
   public static HashSet<String> getSubProcessesOfProcesses(final File _baseDir) {
 105  
 
 106  0
     HashSet<String> subProcesses = new HashSet<String>();
 107  0
     ArrayList<String> subProcessesReferenced = new ArrayList<String>();
 108  
     
 109  0
     for (File processFile : getProcessSourceFiles(_baseDir.getAbsolutePath())) {
 110  0
       subProcesses.addAll(getSubProcessesOfProcess(processFile));
 111  0
       subProcessesReferenced.addAll(getSubProcessesOfProcess(processFile));
 112  
     }
 113  
     
 114  0
     return subProcesses;
 115  
   }
 116  
   
 117  
   /**
 118  
    * @param _processFile
 119  
    *          the process file to be searched for sub-process references.
 120  
    * @return a HashSet of sub-process names directly referenced in the _processFile
 121  
    */
 122  
   public static HashSet<String> getSubProcessesOfProcess(final File _processFile) {
 123  
 
 124  0
     HashSet<String> hs = new HashSet<String>();
 125  
     
 126  
     try {
 127  
       
 128  
 
 129  0
       XmlStreamReader reader = new XmlStreamReader(_processFile);
 130  0
       Xpp3Dom processDom = Xpp3DomBuilder.build(reader);
 131  0
       Xpp3Dom itinerary = processDom.getChild("xq:itinerary");
 132  0
       Xpp3Dom[] steps = itinerary.getChildren();
 133  0
       for (Xpp3Dom currStep : steps) {
 134  0
         if ("xq:step".equals(currStep.getName()) && "PROCESS".equals(currStep.getAttribute("type"))) {
 135  0
           hs.add(currStep.getAttribute("endpoint_ref"));
 136  
         }
 137  
       }
 138  0
     } catch (IOException e) {
 139  0
       System.out.println("[ERROR] Could not read process source file: " + _processFile);
 140  0
       e.printStackTrace();
 141  0
     } catch (XmlPullParserException e) {
 142  0
       System.out.println("[ERROR] Could not parse process source file: " + _processFile);
 143  0
       e.printStackTrace();
 144  0
     }
 145  
     
 146  0
     return hs;
 147  
   }
 148  
   
 149  
   public static HashSet<String> getServiceConfigsOfProcess(final String _processDefinition) {
 150  0
     HashSet<String> hs = new HashSet<String>();
 151  
     
 152  
     try {
 153  
 
 154  0
       XmlStreamReader reader = new XmlStreamReader(new ByteArrayInputStream(_processDefinition.getBytes()));
 155  0
       Xpp3Dom processDom = Xpp3DomBuilder.build(reader);
 156  0
       Xpp3Dom itinerary = processDom.getChild("xq:itinerary");
 157  0
       Xpp3Dom[] steps = itinerary.getChildren();
 158  0
       for (Xpp3Dom currStep : steps) {
 159  0
         if ("xq:step".equals(currStep.getName()) && "SERVICE".equals(currStep.getAttribute("type"))) {
 160  0
           hs.add(currStep.getAttribute("endpoint_ref"));
 161  
         }
 162  
       }
 163  0
     } catch (IOException e) {
 164  0
       System.out.println("[ERROR] Could not read process");
 165  0
       e.printStackTrace();
 166  0
     } catch (XmlPullParserException e) {
 167  0
       System.out.println("[ERROR] Could not parse process");
 168  0
       e.printStackTrace();
 169  0
     }
 170  
     
 171  0
     return hs;
 172  
   }
 173  
 
 174  
   
 175  
   /**
 176  
    * returns the name of a process from a given file name.
 177  
    * 
 178  
    * @param _processFile
 179  
    *          path to the file to be identified
 180  
    * @return the name of the process
 181  
    */
 182  
   public static String getProcessNameFromFile(final File _processFile) {
 183  
 
 184  0
     String processName = null;
 185  
     
 186  
     // this could arguably be done be just return the filename with the file extension,
 187  
     // but actually parsing the file is a more secure way
 188  
     try {
 189  0
       XmlStreamReader reader = new XmlStreamReader(_processFile);
 190  0
       Xpp3Dom processDom = Xpp3DomBuilder.build(reader);
 191  0
       processName = processDom.getAttribute("name");
 192  
       
 193  0
     } catch (IOException e) {
 194  
       // getLog().error("Could not read process file: " + _processFile);
 195  0
       System.out.println("[ERROR] Could not read process file: " + _processFile.getName());
 196  0
     } catch (XmlPullParserException e) {
 197  
       // getLog().error("Could not parse process file: " + _processFile);
 198  0
       System.out.println("[ERROR] Could not parse process file: " + _processFile.getName());
 199  0
     }
 200  0
     return processName;
 201  
   }
 202  
   
 203  
   /**
 204  
    * returns the name of a Service Configuration from a given file name.
 205  
    * 
 206  
    * @param _serviceConfigFile
 207  
    *          path to the file to be identified
 208  
    * @return the name of the Service Configuration
 209  
    */
 210  
   public static String getServiceConfigNameFromFile(final File _serviceConfigFile) {
 211  
 
 212  0
     String serviceName = null;
 213  
     
 214  
     try {
 215  0
       XmlStreamReader reader = new XmlStreamReader(_serviceConfigFile);
 216  0
       Xpp3Dom serviceConfigDom = Xpp3DomBuilder.build(reader);
 217  0
       serviceName = serviceConfigDom.getAttribute("name");
 218  
       
 219  0
     } catch (IOException e) {
 220  0
       System.out.println("[ERROR] Could not read Service Configuration file: " + _serviceConfigFile.getName());
 221  0
     } catch (XmlPullParserException e) {
 222  0
       System.out.println("[ERROR] Could not parse Service Configuration file: " + _serviceConfigFile.getName());
 223  0
     }
 224  0
     return serviceName;
 225  
   }
 226  
   
 227  
   /**
 228  
    * returns the name of a endpoint ref of a Service Configuration from a given file name.
 229  
    * 
 230  
    * @param _serviceConfigFile
 231  
    *          path to the file to be identified
 232  
    * @return the name of the endpoint ref of a Service Configuration
 233  
    */
 234  
   public static String getServiceConfigEntryRefFromFile(final File _serviceConfigFile) {
 235  
 
 236  0
     String entryRefName = null;
 237  
     
 238  
     try {
 239  0
       XmlStreamReader reader = new XmlStreamReader(_serviceConfigFile);
 240  0
       Xpp3Dom serviceConfigDom = Xpp3DomBuilder.build(reader);
 241  0
       Xpp3Dom entryRef = serviceConfigDom.getChild("xq:entry_ref");
 242  0
       if (entryRef != null) {
 243  0
               entryRefName = entryRef.getValue();
 244  
       }
 245  0
     } catch (IOException e) {
 246  0
       System.out.println("[ERROR] Could not read Service Configuration file: " + _serviceConfigFile.getName());
 247  0
     } catch (XmlPullParserException e) {
 248  0
       System.out.println("[ERROR] Could not parse Service Configuration file: " + _serviceConfigFile.getName());
 249  0
     }
 250  0
     return entryRefName;
 251  
   }
 252  
   
 253  
 }