Coverage Report - com.aurea.maven.plugins.sonic.utils.ProcessDependencyFinder
 
Classes in this File Line Coverage Branch Coverage Complexity
ProcessDependencyFinder
0%
0/150
0%
0/54
4.091
 
 1  
 package com.aurea.maven.plugins.sonic.utils;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.List;
 5  
 
 6  
 import com.sonicsw.deploy.IArtifact;
 7  
 import com.sonicsw.deploy.IArtifactTraverser;
 8  
 import com.sonicsw.deploy.artifact.ESBArtifact;
 9  
 import com.sonicsw.deploy.tools.common.ExportPropertiesArtifact;
 10  
 import com.sonicsw.deploy.traversal.AbstractTraverser;
 11  
 import com.sonicsw.deploy.traversal.TraverserContext;
 12  
 import com.sonicsw.deploy.traversal.TraverserFactory;
 13  
 import com.sonicsw.esb.mgmtapi.ESBAPI;
 14  
 import com.sonicsw.esb.mgmtapi.ESBAPIFactory;
 15  
 import com.sonicsw.esb.mgmtapi.config.IContainerConfig;
 16  
 import com.sonicsw.esb.mgmtapi.config.IProcessConfig;
 17  
 import com.sonicsw.esb.mgmtapi.config.IProcessConfigAPI;
 18  
 import com.sonicsw.esb.mgmtapi.config.IServiceConfig;
 19  
 import com.sonicsw.esb.mgmtapi.config.IServiceConfigAPI;
 20  
 import com.progress.sonic.utilities.mfutils.MFUtils;
 21  
 
 22  
 /**
 23  
  * 
 24  
  */
 25  0
 public class ProcessDependencyFinder {
 26  
   
 27  
 
 28  
   // TODO Remove references to Toyota
 29  0
   private String[] processName = { "testProcess", "ESBPluginTester", "Proc.Dealer_CarLookup",
 30  
     "Proc.RecreateToyotaEnvironment" };
 31  
   
 32  
   private MFUtils  mfutil;
 33  0
   private boolean  failOnError = true;
 34  
   
 35  
   /**
 36  
    * @param _args
 37  
    *          command line arguments
 38  
    */
 39  
   public static void main(final String[] _args) {
 40  
 
 41  
     // TODO Auto-generated method stub
 42  0
     ProcessDependencyFinder pdf = new ProcessDependencyFinder();
 43  
     // pdf.connect();
 44  0
     pdf.go();
 45  
     // pdf.disconnect();
 46  0
   }
 47  
   
 48  
   private void disconnect() {
 49  
 
 50  0
     mfutil.cleanup();
 51  0
   }
 52  
   
 53  
   private void connect() {
 54  
 
 55  0
     mfutil = new MFUtils("Domain1", "tcp://localhost:2506", "Administrator", "Administrator");
 56  0
   }
 57  
   
 58  
   private void go() {
 59  
 
 60  0
     connect();
 61  0
     ExportPropertiesArtifact exportprop = new ExportPropertiesArtifact();
 62  0
     for (String process : processName) {
 63  0
       System.out.println("Searching for process: " + process);
 64  0
       IArtifact artifact = new ESBArtifact(ESBArtifact.PROCESS, process);
 65  0
       exportprop.addRoot(artifact);
 66  
     }
 67  0
     IArtifact[] deptree = lookup(exportprop);
 68  0
     String[][] depitems = filterContainerItems(deptree);
 69  0
     depitems[0] = processName;
 70  0
     listArtifacts(depitems);
 71  0
     disconnect();
 72  0
     addArtifactsToContainer(depitems);
 73  0
   }
 74  
   
 75  
   private void addArtifactsToContainer(final String[][] _depitems) {
 76  
 
 77  0
     System.out.println("addArtifactsToContainer");
 78  
     // Create connection
 79  0
     ESBAPI eapi = null;
 80  
     try {
 81  0
       System.out.println("Creating ESB API Factory");
 82  0
       ESBAPIFactory efac = ESBAPIFactory.createESBAPIFactory();
 83  0
       System.out.println("Factory API Created");
 84  
       
 85  0
       System.out.println("Creating the API Handle");
 86  0
       eapi = efac.createAPI("Domain1", "tcp://localhost:2506", "Administrator", "Administrator");
 87  0
       System.out.println("API Handle Created");
 88  
       
 89  0
       System.out.println("Getting the Container");
 90  0
       IContainerConfig esbContainer = eapi.getContainerConfigAPI().getESBContainer("TestDomainESB");
 91  
       
 92  0
       System.out.println("Adding the Services");
 93  0
       for (String service : _depitems[2]) {
 94  0
         System.out.println("Adding Service: " + service);
 95  0
         IServiceConfigAPI svcconf = eapi.getServiceConfigAPI();
 96  0
         IServiceConfig svc = svcconf.getServiceConfig(service);
 97  
         try {
 98  0
           svc.addToContainer(esbContainer, 1);
 99  0
           eapi.getContainerConfigAPI().saveESBContainer(esbContainer);
 100  0
         } catch (Exception e) {
 101  0
           System.out.println("MSG: " + e.getMessage());
 102  0
         }
 103  
       }
 104  0
       System.out.println("Adding the Main Processes");
 105  0
       for (String process : _depitems[0]) {
 106  0
         System.out.println("Adding Process: " + process);
 107  0
         IProcessConfigAPI proconf = eapi.getProcessConfigAPI();
 108  0
         IProcessConfig pro = proconf.getProcess(process);
 109  0
         if (pro.getEntryEndpointConfig() == null) {
 110  0
           System.out.println("No Endpoint Config found ... skipping the add to the container");
 111  0
           continue;
 112  
         } else {
 113  0
           if (pro.getEntryEndpointConfig().getName() == null || pro.getEntryEndpointConfig().getName().equals("")) {
 114  0
             System.out.println("No Endpoint Name Found ... skipping the add to the container");
 115  0
             continue;
 116  
           }
 117  
         }
 118  
         try {
 119  0
           pro.addToContainer(esbContainer, 1);
 120  0
           eapi.getContainerConfigAPI().saveESBContainer(esbContainer);
 121  
           
 122  0
         } catch (Exception e) {
 123  0
           System.out.println("MSG: " + e.getMessage());
 124  0
         }
 125  
         
 126  
       }
 127  0
       System.out.println("Adding the sub Processes");
 128  0
       for (String process : _depitems[1]) {
 129  0
         System.out.println("Adding Process: " + process);
 130  0
         IProcessConfigAPI proconf = eapi.getProcessConfigAPI();
 131  0
         IProcessConfig pro = proconf.getProcess(process);
 132  0
         if (pro.getEntryEndpointConfig() == null) {
 133  0
           System.out.println("No Endpoint Config found ... skipping the add to the container");
 134  0
           continue;
 135  
         } else {
 136  0
           if (pro.getEntryEndpointConfig().getName() == null || pro.getEntryEndpointConfig().getName().equals("")) {
 137  0
             System.out.println("No Endpoint Name Found ... skipping the add to the container");
 138  0
             continue;
 139  
           }
 140  
         }
 141  
         try {
 142  0
           pro.addToContainer(esbContainer, 1);
 143  0
           eapi.getContainerConfigAPI().saveESBContainer(esbContainer);
 144  0
         } catch (Exception e) {
 145  0
           System.out.println("MSG: " + e.getMessage());
 146  0
         }
 147  
         
 148  
       }
 149  
       
 150  0
     } catch (Exception e) {
 151  0
       System.out.println("Error happened: " + e.getMessage());
 152  
     } finally {
 153  0
       eapi.dispose();
 154  0
     }
 155  
     
 156  
 
 157  0
     System.out.println("addArtifactsToContainer Done");
 158  
     
 159  
 
 160  
 
 161  
     // add the services
 162  
     
 163  
     // add the processes (if possible as they need an Entry Endpoint
 164  
     
 165  0
   }
 166  
   
 167  
   private String[][] filterContainerItems(final IArtifact[] _deptree) {
 168  
 
 169  0
     String[][] returnList = new String[3][];
 170  0
     int procCounter = countProcesses(_deptree);
 171  0
     returnList[1] = new String[procCounter];
 172  0
     int svcCounter = countSvc(_deptree);
 173  0
     returnList[2] = new String[svcCounter];
 174  
     
 175  0
     int countsvc = 0;
 176  0
     int countpro = 0;
 177  0
     for (IArtifact a : _deptree) {
 178  0
       if (a.getPath().startsWith("/Services/")) {
 179  0
         returnList[2][countsvc] = a.getName();
 180  0
         countsvc++;
 181  
       }
 182  0
       if (a.getPath().startsWith("/Processes/")) {
 183  0
         returnList[1][countpro] = a.getName();
 184  0
         countpro++;
 185  
       }
 186  
     }
 187  
     
 188  0
     return returnList;
 189  
   }
 190  
   
 191  
   private int countSvc(final IArtifact[] _deptree) {
 192  
 
 193  0
     int count = 0;
 194  0
     for (IArtifact a : _deptree) {
 195  0
       if (a.getPath().startsWith("/Services/")) {
 196  0
         count++;
 197  
       }
 198  
     }
 199  0
     return count;
 200  
   }
 201  
   
 202  
   private int countProcesses(final IArtifact[] _deptree) {
 203  
 
 204  0
     int count = 0;
 205  0
     for (IArtifact a : _deptree) {
 206  0
       if (a.getPath().startsWith("/Processes/")) {
 207  0
         count++;
 208  
       }
 209  
     }
 210  0
     return count;
 211  
   }
 212  
   
 213  
   private void listArtifacts(final String[][] _depitems) {
 214  
 
 215  0
     System.out.println("Listing Main Processes");
 216  0
     for (String art : _depitems[0]) {
 217  0
       System.out.println("Process Name: " + art);
 218  
     }
 219  0
     System.out.println("Listing Processes");
 220  0
     for (String art : _depitems[1]) {
 221  0
       System.out.println("Process Name: " + art);
 222  
     }
 223  0
     System.out.println("Listing Services");
 224  0
     for (String art : _depitems[2]) {
 225  0
       System.out.println("Service Name: " + art);
 226  
     }
 227  0
   }
 228  
   
 229  
   private IArtifact[] lookup(final ExportPropertiesArtifact _processes) {
 230  
 
 231  0
     ExportPropertiesArtifact processes = _processes;
 232  
     
 233  0
     if (processes == null) {
 234  0
       processes = new ExportPropertiesArtifact();
 235  
     }
 236  0
     List<IArtifact> ignores = filterIgnoreList(processes.getIgnoreList());
 237  0
     processes.clearIgnoreList();
 238  0
     processes.addIgnore(ignores.toArray(new IArtifact[] {}));
 239  
     TraverserContext ctxt;
 240  
     try {
 241  0
       ctxt = new TraverserContext(mfutil.getDSArtifactStorage(), processes.getIgnoreList());
 242  0
       ctxt.setTraverseCompressed(false);
 243  
       
 244  0
       for (int i = 0; i < processes.getRootList().length; i++) {
 245  0
         IArtifactTraverser traverser = TraverserFactory.createTraverser(processes.getRootList()[i]);
 246  0
         ((AbstractTraverser) traverser)._traverse(ctxt);
 247  
       }
 248  
       
 249  0
       IArtifact[] errored = ctxt.getErrored();
 250  0
       if (errored.length > 0) {
 251  0
         StringBuffer buf = new StringBuffer();
 252  0
         buf.append("The following Artifacts could not be traversed : [\n");
 253  0
         for (IArtifact error : errored) {
 254  0
           buf.append("  " + error.getDisplayType() + ":" + error.getPath() + "\n");
 255  
         }
 256  0
         buf.append("]\n");
 257  0
         if (failOnError) {
 258  0
           throw new Exception(buf.toString());
 259  
         }
 260  
       }
 261  
       
 262  0
       IArtifact[] result = ctxt.completeTraversal();
 263  0
       return result;
 264  
       
 265  0
     } catch (Exception e) {
 266  
       // TODO Auto-generated catch block
 267  0
       e.printStackTrace();
 268  
     }
 269  0
     return null;
 270  
     
 271  
   }
 272  
   
 273  
   /**
 274  
    * This method calculates the default ignore list for exporting a xar file from a directory service. We will use the
 275  
    * Sonic default ignore list EXCEPT for the connections and sonicfs:///System roots.
 276  
    * 
 277  
    * @return The ignore list for generating the XAR archive.
 278  
    */
 279  
   private List<IArtifact> filterIgnoreList(final IArtifact[] _ignores) {
 280  
 
 281  0
     List<IArtifact> result = new ArrayList<IArtifact>();
 282  
     
 283  0
     List<String> invalidIgnores = new ArrayList<String>();
 284  0
     invalidIgnores.add("/Connections/http_defaultConnection");
 285  0
     invalidIgnores.add("/Connections/jms_defaultConnection");
 286  0
     invalidIgnores.add("/System/");
 287  
     
 288  0
     for (IArtifact a : _ignores) {
 289  0
       if (!invalidIgnores.contains(a.getPath())) {
 290  0
         result.add(a);
 291  
       }
 292  
     }
 293  
     
 294  0
     return result;
 295  
   }
 296  
 }