| 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 |  |   | 
  | 22 |  |   | 
  | 23 |  |   | 
  | 24 |  |  public class SnippetsProcessor { | 
  | 25 |  |   | 
  | 26 |  |   | 
  | 27 |  |    private XarAnalyzer  xarAnalyser; | 
  | 28 |  |   | 
  | 29 |  |     | 
  | 30 |  |   | 
  | 31 |  |   | 
  | 32 |  |    public static final String HINT_ATTRIBUTE = "hint"; | 
  | 33 |  |   | 
  | 34 |  |     | 
  | 35 |  |   | 
  | 36 |  |   | 
  | 37 |  |   | 
  | 38 |  |   | 
  | 39 |  |   | 
  | 40 | 0 |    public SnippetsProcessor(final IArtifactStorage _as) { | 
  | 41 |  |   | 
  | 42 | 0 |      setXarAnalyser(new XarAnalyzer(_as)); | 
  | 43 | 0 |    } | 
  | 44 |  |   | 
  | 45 |  |     | 
  | 46 |  |   | 
  | 47 |  |   | 
  | 48 |  |   | 
  | 49 |  |   | 
  | 50 |  |   | 
  | 51 |  |   | 
  | 52 |  |   | 
  | 53 | 0 |    public SnippetsProcessor(final File _f) { | 
  | 54 |  |             | 
  | 55 | 0 |      setXarAnalyser(new XarAnalyzer(_f)); | 
  | 56 |  |   | 
  | 57 | 0 |    } | 
  | 58 |  |   | 
  | 59 |  |     | 
  | 60 |  |   | 
  | 61 |  |   | 
  | 62 |  |   | 
  | 63 |  |   | 
  | 64 |  |   | 
  | 65 | 0 |    public SnippetsProcessor(final String _fileName) { | 
  | 66 |  |   | 
  | 67 | 0 |      setXarAnalyser(new XarAnalyzer(_fileName)); | 
  | 68 | 0 |    } | 
  | 69 |  |   | 
  | 70 |  |     | 
  | 71 |  |   | 
  | 72 |  |   | 
  | 73 |  |   | 
  | 74 |  |   | 
  | 75 |  |   | 
  | 76 |  |   | 
  | 77 |  |   | 
  | 78 |  |   | 
  | 79 | 0 |    public SnippetsProcessor(final MFUtils _utils) throws Exception { | 
  | 80 |  |   | 
  | 81 | 0 |      setXarAnalyser(new XarAnalyzer(_utils)); | 
  | 82 | 0 |    } | 
  | 83 |  |   | 
  | 84 |  |   | 
  | 85 |  |     | 
  | 86 |  |   | 
  | 87 |  |   | 
  | 88 |  |   | 
  | 89 |  |   | 
  | 90 |  |   | 
  | 91 |  |   | 
  | 92 |  |   | 
  | 93 |  |   | 
  | 94 |  |   | 
  | 95 |  |   | 
  | 96 |  |   | 
  | 97 |  |   | 
  | 98 |  |   | 
  | 99 |  |   | 
  | 100 |  |   | 
  | 101 |  |   | 
  | 102 |  |   | 
  | 103 |  |   | 
  | 104 |  |   | 
  | 105 |  |   | 
  | 106 |  |   | 
  | 107 |  |   | 
  | 108 |  |   | 
  | 109 |  |   | 
  | 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 |  |           | 
  | 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 |  |         | 
  | 145 |  |         | 
  | 146 |  |         | 
  | 147 |  |         | 
  | 148 | 0 |      } | 
  | 149 |  |       | 
  | 150 |  |      | 
  | 151 | 0 |      return result.toString(); | 
  | 152 |  |    } | 
  | 153 |  |   | 
  | 154 |  |     | 
  | 155 |  |   | 
  | 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 |  |   | 
  | 183 |  |   | 
  | 184 |  |   | 
  | 185 |  |   | 
  | 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 |  |         | 
  | 198 |  |         | 
  | 199 |  |         | 
  | 200 |  |         | 
  | 201 | 0 |      } | 
  | 202 | 0 |      return result.toString(); | 
  | 203 |  |    } | 
  | 204 |  |   | 
  | 205 |  |     | 
  | 206 |  |   | 
  | 207 |  |   | 
  | 208 |  |   | 
  | 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 |  |         | 
  | 233 |  |         | 
  | 234 |  |         | 
  | 235 |  |         | 
  | 236 | 0 |      } | 
  | 237 | 0 |      return result.toString(); | 
  | 238 |  |    } | 
  | 239 |  |   | 
  | 240 |  |   | 
  | 241 |  |   | 
  | 242 |  |     | 
  | 243 |  |   | 
  | 244 |  |   | 
  | 245 |  |    public final XarAnalyzer getXarAnalyser() { | 
  | 246 |  |   | 
  | 247 | 0 |      return xarAnalyser; | 
  | 248 |  |    } | 
  | 249 |  |   | 
  | 250 |  |   | 
  | 251 |  |   | 
  | 252 |  |     | 
  | 253 |  |   | 
  | 254 |  |   | 
  | 255 |  |   | 
  | 256 |  |    public final void setXarAnalyser(final XarAnalyzer _xarAnalyser) { | 
  | 257 |  |   | 
  | 258 | 0 |      this.xarAnalyser = _xarAnalyser; | 
  | 259 | 0 |    } | 
  | 260 |  |   | 
  | 261 |  |  } |