Coverage Report - com.aurea.maven.plugins.sonic.utils.SnippetsProcessor
 
Classes in this File Line Coverage Branch Coverage Complexity
SnippetsProcessor
0%
0/77
0%
0/36
3.333
 
 1  
 package com.aurea.maven.plugins.sonic.utils;
 2  
 
 3  
 import java.io.ByteArrayInputStream;
 4  
 import java.io.File;
 5  
 import java.io.StringReader;
 6  
 import java.util.List;
 7  
 
 8  
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 9  
 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
 10  
 
 11  
 import com.aurea.maven.plugins.sonic.sdm.container.IServiceType;
 12  
 import com.aurea.maven.plugins.sonic.sdm.container.impl.DefaultServiceType;
 13  
 import com.progress.sonic.utilities.esb.admin.XarAnalyzer;
 14  
 import com.progress.sonic.utilities.mfutils.MFUtils;
 15  
 import com.sonicsw.deploy.IArtifact;
 16  
 import com.sonicsw.deploy.IArtifactStorage;
 17  
 import com.sonicsw.deploy.artifact.ESBArtifact;
 18  
 
 19  
 import commonj.sdo.DataObject;
 20  
 
 21  
 //import org.codehaus.plexus.util.xml;
 22  
 
 23  
 
 24  
 public class SnippetsProcessor {
 25  
 
 26  
 
 27  
   private XarAnalyzer  xarAnalyser;
 28  
 
 29  
   /**
 30  
    * name of the xml node attribute containing the hint details.
 31  
    */
 32  
   public static final String HINT_ATTRIBUTE = "hint";
 33  
 
 34  
   /**
 35  
    * Initialize the analyzer wih a given storage.
 36  
    * 
 37  
    * @param _as
 38  
    *          The storage to be used in this instance
 39  
    */
 40  0
   public SnippetsProcessor(final IArtifactStorage _as) {
 41  
 
 42  0
     setXarAnalyser(new XarAnalyzer(_as));
 43  0
   }
 44  
 
 45  
   /**
 46  
    * Initialize the analyzer with a file. If the file is a directory the we assume that this is an unzipped xar archive
 47  
    * and create a FileArtifactStorage. If the file is a single file we assume that this file is a xar archive and we
 48  
    * create a ZipArtifactStorage.
 49  
    * 
 50  
    * @param _f
 51  
    *          The file to be used as Artifact Storage
 52  
    */
 53  0
   public SnippetsProcessor(final File _f) {
 54  
           
 55  0
     setXarAnalyser(new XarAnalyzer(_f));
 56  
 
 57  0
   }
 58  
 
 59  
   /**
 60  
    * Convenience constructor accepting a file name as String.
 61  
    * 
 62  
    * @param _fileName
 63  
    *          The file name
 64  
    */
 65  0
   public SnippetsProcessor(final String _fileName) {
 66  
 
 67  0
     setXarAnalyser(new XarAnalyzer(_fileName));
 68  0
   }
 69  
 
 70  
   /**
 71  
    * Initialize the analyzer with a Sonic Domain storage. The access to the domain is encapsulated in the MFUtils
 72  
    * library.
 73  
    * 
 74  
    * @param _utils
 75  
    *          An initialized MFUtils object
 76  
    * @throws Exception
 77  
    *           if the XarAnalyzer can't be instantiated
 78  
    */
 79  0
   public SnippetsProcessor(final MFUtils _utils) throws Exception {
 80  
 
 81  0
     setXarAnalyser(new XarAnalyzer(_utils));
 82  0
   }
 83  
 
 84  
 
 85  
   /**
 86  
    * This calculates the tailoring rules that are needed for a service type. Suppose we want to deploy a xar file and
 87  
    * that xar file contains custom service types. These service types may contain initialization parameters. Potentially
 88  
    * these parameters change from environment to environment, so that they need to be included n a tailoring file for
 89  
    * the xar file. Therefore this method iterates over the service types of the xar archive and examines the parameter
 90  
    * settings. Each parameter that matches at least one of the regular expressions that are passed in as a method
 91  
    * parameter, will be recorded in an xml snippet.
 92  
    * 
 93  
    * That xml snippet can be picked up by other components for insertion in a tailoring rules file, so that the the
 94  
    * initialization paramters of the custom services will appear in the tailoring map as well.
 95  
    * 
 96  
    * The xml fragment will look like
 97  
    * 
 98  
    * <pre>
 99  
    *  &lt;ServiceRules&gt;
 100  
    *    &lt;ServiceRule type=&quot;ParserDecoratorService&quot;&gt;
 101  
    *      &lt;Param name=&quot;springconf&quot;/&gt;
 102  
    *      &lt;Param name=&quot;mainbean&quot;/&gt;
 103  
    *    &lt;/ServiceRule&gt;
 104  
    *  &lt;/ServiceRules&gt;
 105  
    * </pre>
 106  
    * 
 107  
    * @param _paramPatterns
 108  
    *          _paramPatterns
 109  
    * @return An xml fragment that can be inserted in a tailoring rules files.
 110  
    */
 111  
   @SuppressWarnings("unchecked")
 112  
   public final String getTailorSnippet(final List<String> _paramPatterns) {
 113  
           
 114  0
     Xpp3Dom result = new Xpp3Dom("ServiceRules");
 115  
     try {
 116  0
       List<IArtifact> sTypes = xarAnalyser.getArtifacts(ESBArtifact.SERVICE_TYPE);
 117  
 
 118  0
       for (IArtifact sTypeArtifact : sTypes) {
 119  0
         IServiceType sType = new DefaultServiceType();
 120  0
         byte[] bytes = xarAnalyser.getStorage().getContentsAsBytes(sTypeArtifact);
 121  
         // in Sonic 7.6 the self first flag is not set corrctly: <xq:selfFirst/> --> bug 351
 122  0
         String temp = new String(bytes);
 123  0
         temp = temp.replace("<xq:selfFirst/>", "<xq:selfFirst>false</xq:selfFirst>");
 124  0
         sType.load(new ByteArrayInputStream(temp.getBytes()));
 125  0
         List<DataObject> params = sType.getDataObject().getList("validParams/init/stringParam");
 126  0
         if (params != null) {
 127  0
           Xpp3Dom svcRule = Xpp3Utils.createXpp3Dom("ServiceRule", null, "type="
 128  
               + sType.getDataObject().getString("name"));
 129  0
           for (DataObject param : params) {
 130  0
             String pName = param.getString("name");
 131  0
             for (String paramPattern : _paramPatterns) {
 132  0
               if (pName.matches(paramPattern)) {
 133  0
                 svcRule.addChild(Xpp3Utils.createXpp3Dom("Param", null, "name=" + pName));
 134  0
                 break;
 135  
               }
 136  0
             }
 137  0
           }
 138  0
           if (svcRule.getChildCount() > 0) {
 139  0
             result.addChild(svcRule);
 140  
           }
 141  
         }
 142  0
       }
 143  0
     } catch (Exception e) {
 144  
       // log_.error("Error creating Service rule set, empty set returned", e);
 145  
       // if (log_.isDebugEnabled()) {
 146  
       // e.printStackTrace();
 147  
       // }
 148  0
     }
 149  
     
 150  
    // System.out.println("TBO LEFT THE BUILDING");
 151  0
     return result.toString();
 152  
   }
 153  
 
 154  
   /**
 155  
    * @return String
 156  
    */
 157  
   public final String getQueuesSnippet() {
 158  
 
 159  0
     Xpp3Dom result = new Xpp3Dom("Queues");
 160  
 
 161  
     try {
 162  0
       List<IArtifact> endpoints = xarAnalyser.getArtifacts(ESBArtifact.ENDPOINT);
 163  
 
 164  0
       for (IArtifact a : endpoints) {
 165  
 
 166  0
         Xpp3Dom artifactDom = Xpp3DomBuilder.build(new StringReader(xarAnalyser.getStorage().getContentsAsString(a)));
 167  0
         List<Xpp3Dom> params = Xpp3Utils.collectElements(artifactDom, "(xq:)?params/(xq:)?stringParam");
 168  
 
 169  0
         boolean isQueue = false;
 170  0
         String destName = null;
 171  
 
 172  0
         for (Xpp3Dom param : params) {
 173  0
           if ("destination".equals(param.getAttribute("name"))) {
 174  0
             destName = param.getValue();
 175  
           }
 176  0
           if ("type".equals(param.getAttribute("name"))) {
 177  0
             isQueue = ("QUEUE".equals(param.getValue()));
 178  
           }
 179  0
         }
 180  
 
 181  
         /*
 182  
          * Queue names containing "::" are references to remote queues. They should not be present in a queues snippet
 183  
          * because the real queue will be included by the project containing the process listening on that queue.
 184  
          * 
 185  
          * The same applies to reference to SonicMQ.deadMessage & SonicMQ.routingQueue.
 186  
          */
 187  0
         if (isQueue && destName != null && !destName.contains("::") && !destName.equals("SonicMQ.deadMessage")
 188  
             && !destName.equals("SonicMQ.routingQueue")) {
 189  0
           Xpp3Dom q = new Xpp3Dom("Queue");
 190  0
           Xpp3Dom id = new Xpp3Dom("Id");
 191  0
           id.setValue(destName);
 192  0
           q.addChild(id);
 193  0
           result.addChild(q);
 194  
         }
 195  0
       }
 196  0
     } catch (Exception e) {
 197  
       // log_.error("Error creating Queue set, empty set returned", e);
 198  
       // if (log_.isDebugEnabled()) {
 199  
       // e.printStackTrace();
 200  
       // }
 201  0
     }
 202  0
     return result.toString();
 203  
   }
 204  
 
 205  
   /**
 206  
    * @param _containerHint
 207  
    *          containerHint
 208  
    * @return String
 209  
    */
 210  
   public final String getSvcInstancesSnippet(final String _containerHint) {
 211  
 
 212  0
     Xpp3Dom result = new Xpp3Dom("ServiceInstances");
 213  
 
 214  
     try {
 215  0
       for (IArtifact svcInstance : xarAnalyser.getArtifacts(ESBArtifact.SERVICE)) {
 216  0
         String svcName = svcInstance.getName();
 217  0
         result.addChild(Xpp3Utils.createXpp3Dom("ServiceInstance", null, "name=" + svcName
 218  
             + Xpp3Utils.PROPERTY_SEPARATOR + HINT_ATTRIBUTE + "=" + _containerHint + "." + svcName));
 219  0
       }
 220  
 
 221  0
       for (IArtifact procInstance : xarAnalyser.getArtifacts(ESBArtifact.PROCESS)) {
 222  0
         String procName = procInstance.getName();
 223  0
         Xpp3Dom procDOM = Xpp3DomBuilder.build(new StringReader(xarAnalyser.getStorage().getContentsAsString(
 224  
             procInstance)));
 225  0
         if (procDOM.getAttribute("entry_ref") != null) {
 226  0
           result.addChild(Xpp3Utils.createXpp3Dom("ServiceInstance", null, "name=BPEType"
 227  
               + Xpp3Utils.PROPERTY_SEPARATOR + "hint=" + _containerHint + ".process." + procName
 228  
               + Xpp3Utils.PROPERTY_SEPARATOR + "process=" + procName));
 229  
         }
 230  0
       }
 231  0
     } catch (Exception e) {
 232  
       // log_.error("Error creating Service Instance set, empty set returned", e);
 233  
       // if (log_.isDebugEnabled()) {
 234  
       // e.printStackTrace();
 235  
       // }
 236  0
     }
 237  0
     return result.toString();
 238  
   }
 239  
 
 240  
 
 241  
 
 242  
   /**
 243  
    * @return the xarAnalyser
 244  
    */
 245  
   public final XarAnalyzer getXarAnalyser() {
 246  
 
 247  0
     return xarAnalyser;
 248  
   }
 249  
 
 250  
 
 251  
 
 252  
   /**
 253  
    * @param _xarAnalyser
 254  
    *          the xarAnalyser to set
 255  
    */
 256  
   public final void setXarAnalyser(final XarAnalyzer _xarAnalyser) {
 257  
 
 258  0
     this.xarAnalyser = _xarAnalyser;
 259  0
   }
 260  
 
 261  
 }