Coverage Report - com.aurea.maven.plugins.sonic.esb.AbstractEsbPackageMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractEsbPackageMojo
0%
0/316
0%
0/88
3
 
 1  
 package com.aurea.maven.plugins.sonic.esb;
 2  
 
 3  
 import java.io.File;
 4  
 import java.io.FileInputStream;
 5  
 import java.io.InputStream;
 6  
 import java.util.ArrayList;
 7  
 import java.util.Iterator;
 8  
 import java.util.List;
 9  
 import java.util.Properties;
 10  
 
 11  
 import org.apache.maven.model.Resource;
 12  
 import org.apache.maven.plugin.MojoExecutionException;
 13  
 import org.apache.maven.plugin.MojoFailureException;
 14  
 import org.apache.maven.project.MavenProjectHelper;
 15  
 import org.codehaus.plexus.archiver.Archiver;
 16  
 import org.codehaus.plexus.archiver.manager.ArchiverManager;
 17  
 import org.codehaus.plexus.util.FileUtils;
 18  
 import org.codehaus.plexus.util.xml.XmlStreamReader;
 19  
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 20  
 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
 21  
 
 22  
 import com.aurea.maven.plugins.sonic.utils.FileUtilities;
 23  
 import com.aurea.maven.plugins.sonic.utils.SnippetsProcessor;
 24  
 import com.aurea.maven.plugins.sonic.utils.Xpp3Utils;
 25  
 import com.sonicsw.deploy.action.CreateMapAction;
 26  
 
 27  0
 public abstract class AbstractEsbPackageMojo extends AbstractESBConnectMojo {
 28  
 
 29  
         /**
 30  
          * @component
 31  
          */
 32  
         private MavenProjectHelper projectHelper;
 33  
 
 34  
         /**
 35  
          * the ESB Tailor properties files for Sonic Connect.
 36  
          * 
 37  
          * @parameter default-value="${project.artifactId}"
 38  
          * @required
 39  
          */
 40  
         private String artifactID;
 41  
 
 42  
         /**
 43  
          * the ESB Tailor properties files.
 44  
          * 
 45  
          * @parameter default-value="src/main/sonicesb/ESBTailorProperties.xml"
 46  
          * @required
 47  
          */
 48  
         protected File esbTailorPropFile;
 49  
 
 50  
         /**
 51  
          * the Test ESB Tailor properties files.
 52  
          * 
 53  
          * @parameter default-value="src/main/sonicesb/TestESBTailorProperties.xml"
 54  
          * @required
 55  
          */
 56  
         protected File testEsbTailorPropFile;
 57  
 
 58  
         /**
 59  
          * To look up Archiver/UnArchiver implementations.
 60  
          * 
 61  
          * @component
 62  
          * @required
 63  
          */
 64  
         protected ArchiverManager archiverManager;
 65  
 
 66  
         /**
 67  
          * @parameter property="sonicesb.ParameterMatches"
 68  
          */
 69  
         ArrayList<String> parameterMatches;
 70  
 
 71  
         /**
 72  
          * @parameter property="sonicesb.TestParameterMatches"
 73  
          */
 74  
         ArrayList<String> testParameterMatches;
 75  
 
 76  
         /**
 77  
          * Comma separated patterns to exclude from src/main/sonicesb while building
 78  
          * the xar archive.
 79  
          * 
 80  
          * @parameter default-value=
 81  
          *            "ContainerProperties.xml,ExportProperties.xml,ESBTailorProperties.xml
 82  
          *            , *.template,**\/.copyarea.db"
 83  
          */
 84  
         String xarExcludes;
 85  
 
 86  
         /**
 87  
          * @parameter property="sonicesb.copyXarToDeploy" default-value="false"
 88  
          */
 89  
         protected boolean copyXarToDeploy;
 90  
 
 91  
         /**
 92  
          * @return the path to the generated source directory
 93  
          */
 94  
         protected abstract String getGeneratedSrcDir();
 95  
 
 96  
         /**
 97  
          * get the temporary location to use while assembling the Maven package.
 98  
          * 
 99  
          * @return a directory for the assembly
 100  
          */
 101  
         protected abstract String getAssemblyDir();
 102  
 
 103  
         /**
 104  
          * @return the Maven resources in a list
 105  
          */
 106  
         protected abstract List<Resource> getResources();
 107  
 
 108  
         /**
 109  
          * @return the esb tailor properties file
 110  
          */
 111  
         protected abstract File getEsbTailorPropFile();
 112  
 
 113  
         /**
 114  
          * {@inheritDoc}
 115  
          */
 116  
         protected void doExecute() throws MojoExecutionException, MojoFailureException {
 117  
 
 118  0
                 super.doExecute();
 119  
 
 120  0
                 File archiveFile = null;
 121  
 
 122  0
                 getLog().info("Packaging ESB project ...");
 123  
                 try {
 124  0
                         archiveFile = createEsbAssembly();
 125  
                         // project.getArtifact().setFile(archiveFile);
 126  0
                 } catch (Exception e) {
 127  0
                         throw new MojoExecutionException("Failed to package ESB archive "
 128  
                                         + (archiveFile == null ? "null" : archiveFile.getAbsolutePath()), e);
 129  0
                 }
 130  0
                 String classifier = getClassifier();
 131  0
                 if (classifier != null) {
 132  0
                         projectHelper.attachArtifact(project, getType(), classifier, archiveFile);
 133  
                 } else {
 134  0
                         project.getArtifact().setFile(archiveFile);
 135  
                 }
 136  0
         }
 137  
 
 138  
         /**
 139  
          * @param _replaceRules
 140  
          *            _replaceRules
 141  
          */
 142  
         protected void addArtifactReplaceRules(final List<Xpp3Dom> _replaceRules) {
 143  
 
 144  0
                 Xpp3Dom projectReplace = new Xpp3Dom("stringReplaceMap");
 145  0
                 projectReplace.setAttribute("name", "sonicfs:///workspace/" + getProjectName()
 146  
                                 + getSonicEsbSourceDirectoryFromBase());
 147  0
                 projectReplace.setAttribute("updatedName", "sonicfs:///" + getSonicFSProjectDir());
 148  0
                 _replaceRules.add(projectReplace);
 149  
 
 150  
                 // loop through any dxsi dependencies, adding stringReplaceMaps for any
 151  
                 // possible references to
 152  
                 // /target/sonicesb/dependency/dxsi/...
 153  
 
 154  
                 // TBO
 155  
                 // This part of the code seems to have become obsolete as we are not
 156  
                 // having the target directory
 157  
                 // situation anymore. Double checking with BA and removing this in order
 158  
                 // to test if it breaks more
 159  
                 // stuff than it fixes.
 160  
 
 161  
                 // for (Object depObj : project.getDependencies()) {
 162  
                 // Dependency dependency = (Dependency) depObj;
 163  
                 // if (dependency.getType().equalsIgnoreCase("dxsi")) {
 164  
                 // Xpp3Dom dependencyReplace = new Xpp3Dom("stringReplaceMap");
 165  
                 // dependencyReplace.setAttribute("name", "sonicfs:///DataXtend/"
 166  
                 // + getProjectName());
 167  
                 // dependencyReplace.setAttribute("updatedName", "sonicfs:///"
 168  
                 // + getSonicFSProjectRoot() + "/"
 169  
                 // + dependency.getGroupId().replace(".", "/") + "/"
 170  
                 // + dependency.getArtifactId() + "/"
 171  
                 // + dependency.getVersion());
 172  
                 // _replaceRules.add(dependencyReplace);
 173  
                 //
 174  
                 // }
 175  
                 // }
 176  
 
 177  
                 // stringReplaceMap for Sonic Connect projects
 178  0
                 Xpp3Dom libReplace = new Xpp3Dom("stringReplaceMap");
 179  0
                 libReplace.setAttribute("name", "sonicfs:///workspace/" + getProjectName() + "/lib/" + getArtifactId());
 180  0
                 libReplace.setAttribute("updatedName", "sonicfs:///" + getSonicFSResourcesDir() + "/"
 181  
                                 + getGroupId().replaceAll("\\.", "/") + "/" + getArtifactId() + "/" + getVersion() + "/"
 182  
                                 + getFinalAssemblyBaseName());
 183  0
                 _replaceRules.add(libReplace);
 184  0
         }
 185  
 
 186  
         /**
 187  
          * @param _sp
 188  
          *            Snippets Processor
 189  
          * @throws Exception
 190  
          *             Exception
 191  
          */
 192  
         protected void generateTailoring(final SnippetsProcessor _sp) throws Exception {
 193  
 
 194  0
                 XmlStreamReader reader = null;
 195  0
                 XmlStreamReader esbtailorReader = null;
 196  
 
 197  0
                 String tailorFile = getTailorDir() + "/" + getFinalAssemblyBaseName() + ".xar.xml";
 198  
 
 199  0
                 ClassLoader cl = getClass().getClassLoader();
 200  
 
 201  0
                 Xpp3Dom tailorDom = null;
 202  
                 // TODO add the 7.6.2 ESB/deploy artifacts
 203  0
                 String tailorRulesFile = "ESB/deploy/" + getESBVersion() + "/DefaultTailorRules.xml";
 204  0
                 String rulesFile = getAssemblyDir() + "/" + getRulesFileName();
 205  
 
 206  
                 try {
 207  0
                         InputStream is = cl.getResourceAsStream(tailorRulesFile);
 208  
 
 209  0
                         byte[] content = new byte[is.available()];
 210  0
                         is.read(content);
 211  0
                         FileUtilities.writeFile(content, rulesFile);
 212  
 
 213  
                         // now we will patch the default tailoring a bit
 214  0
                         reader = new XmlStreamReader(new File(rulesFile));
 215  0
                         tailorDom = Xpp3DomBuilder.build(reader);
 216  0
                 } catch (Exception e) {
 217  0
                         getLog().error("Could not access: " + tailorRulesFile, e);
 218  0
                 }
 219  
 
 220  
                 // rework the string replacement map
 221  0
                 Xpp3Dom stringReplace = tailorDom.getChild("DevEnvRules").getChild("stringReplaceMaps");
 222  0
                 Xpp3Utils.removeAllChildren(tailorDom, "DevEnvRules/stringReplaceMaps");
 223  
 
 224  
                 // We need to collect the replacement rules for all dependent projects
 225  0
                 List<Xpp3Dom> replaceRules = Xpp3Utils.collectElements(getDependencyDirectory(), "**/*.xar.xml",
 226  
                                 "StringReplaceMaps/stringReplaceMap");
 227  
 
 228  
                 // Finally we need to add the project specific rule to the tailoring
 229  
                 // file
 230  0
                 addArtifactReplaceRules(replaceRules);
 231  
 
 232  
                 // Reading Extra Replace Rules from the Tailoring XML File
 233  0
                 getLog().debug("Inserting Tailor Rules ------");
 234  0
                 getLog().debug("Check File " + getEsbTailorPropFile());
 235  0
                 getLog().debug("Base-Dir: " + project.getBasedir());
 236  
                 // if (getEsbTailorPropFile() == null) {
 237  
                 // getEsbTailorPropFile() = "src/main/sonicesb/ESBTailorProperties.xml";
 238  
                 // }
 239  0
                 File esbTailorFile = getEsbTailorPropFile();
 240  0
                 if (esbTailorFile.exists()) {
 241  
                         try {
 242  0
                                 getLog().info("Tailoring the String Replace Maps with custom tailoring");
 243  0
                                 esbtailorReader = new XmlStreamReader(esbTailorFile);
 244  0
                                 Xpp3Dom esbtailorDom = Xpp3DomBuilder.build(esbtailorReader);
 245  0
                                 for (Xpp3Dom child : esbtailorDom.getChild("CustomStringReplaceMaps").getChildren()) {
 246  0
                                         Xpp3Dom stringreplacer = new Xpp3Dom("stringReplaceMap");
 247  0
                                         stringreplacer.setAttribute("name", child.getAttribute("name"));
 248  0
                                         stringreplacer.setAttribute("updatedName", child.getAttribute("updatedName"));
 249  0
                                         replaceRules.add(stringreplacer);
 250  
                                 }
 251  0
                         } catch (Exception e) {
 252  0
                                 getLog().error("Problem encountered with the formatting of the file: " + e.getMessage(), e);
 253  0
                         }
 254  
                 } else {
 255  0
                         getLog().debug("No Tailoring File Found");
 256  
                 }
 257  
 
 258  
                 // We will unify the replace rules with the @name attribute as key.
 259  0
                 replaceRules = Xpp3Utils.unifyXpp3List(replaceRules, "/stringReplaceMap/@name");
 260  
 
 261  0
                 for (Xpp3Dom replaceRule : replaceRules) {
 262  0
                         stringReplace.addChild(replaceRule);
 263  0
                 }
 264  
 
 265  0
                 Xpp3Dom propertiesReplaceMap = getSonicConnectPropertiesReplaceMap();
 266  0
                 if (propertiesReplaceMap != null) {
 267  0
                         getLog().debug("Need to insert the Property Replace Maps ");
 268  0
                         tailorDom.addChild(propertiesReplaceMap);
 269  
                 } else {
 270  0
                         getLog().debug("No Properties File Found for Sonic Connect Project");
 271  
                 }
 272  
                 // now we need to inject the service rule elements
 273  
 
 274  0
                 List<Xpp3Dom> serviceRules = Xpp3Utils.collectElements(getDependencyDirectory(), "**/*.rules.snippet");
 275  0
                 serviceRules = Xpp3Utils.unifyXpp3List(serviceRules, "/ServiceRule/@type");
 276  
 
 277  0
                 Xpp3Dom insertPoint = tailorDom.getChild("ServiceRules");
 278  0
                 for (Xpp3Dom serviceRule : serviceRules) {
 279  0
                         if (serviceRule.getChildCount() > 0) {
 280  0
                                 insertPoint.addChild(serviceRule);
 281  
                         }
 282  0
                 }
 283  
 
 284  0
                 FileUtilities.writeFile(tailorDom.toString().getBytes(), rulesFile);
 285  
 
 286  
                 // Now we leverage the Sonic API to create the map
 287  0
                 CreateMapAction cmAction = new CreateMapAction(new File(tailorFile), new File(rulesFile));
 288  
                 // System.out.println("CMACTION CREATED with " + tailorFile + " and "+
 289  
                 // rulesFile);
 290  
 
 291  0
                 cmAction.run(_sp.getXarAnalyser().getStorage());
 292  
 
 293  
                 // Finally we rework the tailoring file that come out of this
 294  0
                 reader = new XmlStreamReader(new File(tailorFile));
 295  0
                 tailorDom = Xpp3DomBuilder.build(reader);
 296  
 
 297  
                 // We don't do EndpointMapping
 298  0
                 Xpp3Utils.removeAllChildren(tailorDom, "EndpointMaps");
 299  
 
 300  
                 // We don't do Process Step Tailoring either
 301  0
                 Xpp3Utils.removeAllChildren(tailorDom, "ProcessMaps/ProcessMap");
 302  
 
 303  
                 // For the Services instances we only want the parameters
 304  0
                 List<Xpp3Dom> serviceMaps = Xpp3Utils.collectElements(tailorDom, "ServiceMaps/ServiceMap");
 305  0
                 for (Xpp3Dom serviceMap : serviceMaps) {
 306  0
                         Xpp3Dom paramMap = null;
 307  0
                         if (serviceMap.getChild("paramMap") != null) {
 308  0
                                 paramMap = serviceMap.getChild("paramMap");
 309  
                         }
 310  0
                         Xpp3Utils.removeAllChildren(serviceMap);
 311  0
                         if (paramMap != null) {
 312  0
                                 serviceMap.addChild(paramMap);
 313  
                         }
 314  0
                 }
 315  
 
 316  0
                 FileUtilities.writeFile(tailorDom.toString().getBytes(), tailorFile);
 317  
 
 318  0
         }
 319  
 
 320  
         /**
 321  
          * @throws Exception
 322  
          *             if something goes wrong
 323  
          */
 324  
         protected void generateVariables() throws Exception {
 325  
 
 326  0
                 String tailorFile = getTailorDir() + "/" + getFinalAssemblyBaseName() + ".xar.xml";
 327  
 
 328  0
                 Xpp3Dom variables = new Xpp3Dom("Variables");
 329  
 
 330  0
                 XmlStreamReader reader = new XmlStreamReader(new File(tailorFile));
 331  0
                 Xpp3Dom tailorDom = Xpp3DomBuilder.build(reader);
 332  0
                 generateVariablesFromTailoring(tailorDom, variables, null);
 333  0
                 generateVariablesFromProperties(variables);
 334  
 
 335  0
                 FileUtilities.writeFile(variables.toString().getBytes(), getSnippetDir() + "/" + getFinalAssemblyBaseName()
 336  
                                 + ".variables.snippet");
 337  0
                 FileUtilities.writeFile(tailorDom.toString().getBytes(), tailorFile);
 338  0
         }
 339  
 
 340  
         protected void generateVariablesFromProperties(final Xpp3Dom _variables) {
 341  
                 // TODO:
 342  0
         }
 343  
 
 344  
         /**
 345  
          * @param _element
 346  
          *            _element
 347  
          * @param _variables
 348  
          *            _variables
 349  
          * @param _context
 350  
          *            _context
 351  
          */
 352  
         protected void generateVariablesFromTailoring(final Xpp3Dom _element, final Xpp3Dom _variables,
 353  
                         final String _context) {
 354  
 
 355  0
                 String nextContext = _context;
 356  
 
 357  0
                 if (_element != null) {
 358  0
                         if ("StringParam".equals(_element.getName())) {
 359  0
                                 String paramName = "@" + _context + "." + _element.getAttribute("name").trim() + "@";
 360  0
                                 String paramContent = null;
 361  0
                                 if (_element.getAttribute("url") != null) {
 362  0
                                         paramContent = _element.getAttribute("url");
 363  0
                                         _element.setAttribute("url", paramName);
 364  
                                 } else {
 365  0
                                         paramContent = _element.getValue();
 366  0
                                         _element.setValue(paramName);
 367  
                                 }
 368  0
                                 _variables.addChild(Xpp3Utils.createXpp3Dom("Variable", paramContent, "name=" + paramName));
 369  
                         }
 370  0
                         if ("XmlParam".equals(_element.getName())) {
 371  0
                                 if ("connections".equals(_element.getAttribute("name"))) {
 372  0
                                         getLog().debug("TBO: ------ Need to treat a connection Object");
 373  0
                                         for (Xpp3Dom child : _element.getChildren()) {
 374  0
                                                 generateDatabaseVariables(child, _variables, "db.connect." + _context);
 375  
                                         }
 376  
                                 }
 377  
                         }
 378  0
                         if ("stringReplaceMap".equals(_element.getName())) {
 379  0
                                 String updatedNameAttribute = _element.getAttribute("updatedName");
 380  0
                                 if (updatedNameAttribute != null && updatedNameAttribute.startsWith("@")
 381  
                                                 && updatedNameAttribute.endsWith("@")) {
 382  0
                                         getLog().debug("Found stringReplaceMap variable: " + updatedNameAttribute);
 383  0
                                         _variables.addChild(Xpp3Utils.createXpp3Dom("Variable", null, "name=" + updatedNameAttribute));
 384  
                                 }
 385  
                         }
 386  0
                         if ("ServiceMap".equals(_element.getName())) {
 387  0
                                 nextContext = "svc." + _element.getAttribute("name");
 388  0
                         } else if ("ConnectionMap".equals(_element.getName())) {
 389  0
                                 nextContext = "con." + _element.getAttribute("name");
 390  
                         }
 391  
 
 392  0
                         if ("propertyReplaceMap".equals(_element.getName())) {
 393  0
                                 String prop = _element.getAttribute("propertyName");
 394  0
                                 String val = _element.getAttribute("updatedValue");
 395  0
                                 String parameter = "@" + "connect." + artifactID + "." + prop + "@";
 396  0
                                 _element.setAttribute("updatedValue", parameter);
 397  0
                                 _variables.addChild(Xpp3Utils.createXpp3Dom("Variable", val, "name=" + parameter));
 398  0
                                 getLog().debug("Found PropertyReplaceMap Variable " + parameter + " with value " + val);
 399  
                         }
 400  
 
 401  0
                         for (Xpp3Dom child : _element.getChildren()) {
 402  0
                                 generateVariablesFromTailoring(child, _variables, nextContext);
 403  
                         }
 404  
                 }
 405  0
         }
 406  
 
 407  
         /**
 408  
          * @param _element
 409  
          *            _element
 410  
          * @param _variables
 411  
          *            _variables
 412  
          * @param _context
 413  
          *            _context
 414  
          */
 415  
         protected void generateDatabaseVariables(final Xpp3Dom _element, final Xpp3Dom _variables, final String _context) {
 416  
 
 417  0
                 getLog().debug("Element: " + _element.getName() + " --- " + _context);
 418  0
                 if ("connection".equals(_element.getName()) || "rdbms:connection".equals(_element.getName())) {
 419  0
                         getLog().debug("Creating the connection stuff");
 420  0
                         String password = _element.getAttribute("password");
 421  0
                         String passwordref = "@" + _context + "." + _element.getName() + ".password" + "@";
 422  0
                         _element.setAttribute("password", passwordref);
 423  0
                         _variables.addChild(Xpp3Utils.createXpp3Dom("Variable", password, "name=" + passwordref));
 424  
 
 425  0
                         String username = _element.getAttribute("username");
 426  0
                         String usernameref = "@" + _context + "." + _element.getName() + ".username" + "@";
 427  0
                         _element.setAttribute("username", usernameref);
 428  0
                         _variables.addChild(Xpp3Utils.createXpp3Dom("Variable", username, "name=" + usernameref));
 429  
 
 430  0
                         String jdbcURL = _element.getAttribute("jdbcURL");
 431  0
                         String jdbcURLref = "@" + _context + "." + _element.getName() + ".jdbcURL" + "@";
 432  0
                         _element.setAttribute("jdbcURL", jdbcURLref);
 433  0
                         _variables.addChild(Xpp3Utils.createXpp3Dom("Variable", jdbcURL, "name=" + jdbcURLref));
 434  
 
 435  0
                         String name = _element.getAttribute("name");
 436  0
                         String nameref = "@" + _context + "." + _element.getName() + ".name" + "@";
 437  0
                         _element.setAttribute("name", nameref);
 438  0
                         _variables.addChild(Xpp3Utils.createXpp3Dom("Variable", name, "name=" + nameref));
 439  
 
 440  
                         // <connection password="liferay" xmlns="" username="liferay"
 441  
                         // jdbcURL="jdbc:mysql://localhost:1527/liferay" name="Default">
 442  
                 }
 443  0
                 if ("scheduledQueries".equals(_element.getName())) {
 444  0
                         int queryNum = 0;
 445  0
                         getLog().debug("Handling Scheduled Queries --- " + queryNum);
 446  0
                         for (Xpp3Dom child : _element.getChildren()) {
 447  0
                                 generateDatabaseQueries(child, _variables, "query." + queryNum + "." + _context);
 448  0
                                 queryNum++;
 449  
                         }
 450  0
                 } else {
 451  0
                         for (Xpp3Dom child : _element.getChildren()) {
 452  0
                                 generateDatabaseVariables(child, _variables, _context);
 453  
                         }
 454  
                 }
 455  0
         }
 456  
 
 457  
         /**
 458  
          * @param _element
 459  
          *            _element
 460  
          * @param _variables
 461  
          *            _variables
 462  
          * @param _context
 463  
          *            _contexts
 464  
          */
 465  
         protected void generateDatabaseQueries(final Xpp3Dom _element, final Xpp3Dom _variables, final String _context) {
 466  
 
 467  0
                 for (Xpp3Dom child : _element.getChildren()) {
 468  0
                         if ("address".equals(child.getName())) {
 469  0
                                 String ref = child.getAttribute("endpoint_ref");
 470  0
                                 String refname = "@" + _context + "." + child.getName() + ".endpoint_ref@";
 471  0
                                 String type = child.getAttribute("type");
 472  0
                                 String typename = "@" + _context + "." + child.getName() + ".type@";
 473  0
                                 child.setAttribute("endpoint_ref", refname);
 474  0
                                 child.setAttribute("type", typename);
 475  0
                                 _variables.addChild(Xpp3Utils.createXpp3Dom("Variable", ref, "name=" + refname));
 476  0
                                 _variables.addChild(Xpp3Utils.createXpp3Dom("Variable", type, "name=" + typename));
 477  0
                         } else {
 478  0
                                 String paramName = "@" + _context + "." + child.getName().trim() + "@";
 479  0
                                 String paramContent = child.getValue();
 480  0
                                 getLog().debug("TBO ----- Setting paramName = " + paramName + " Content = " + paramContent);
 481  0
                                 child.setValue(paramName);
 482  0
                                 _variables.addChild(Xpp3Utils.createXpp3Dom("Variable", paramContent, "name=" + paramName));
 483  
                         }
 484  
                 }
 485  
 
 486  0
         }
 487  
 
 488  
         /**
 489  
          * copies the generated source to the appropriate xar directory.
 490  
          * 
 491  
          * @throws MojoExecutionException
 492  
          *             if something goes wrong
 493  
          */
 494  
         protected void copyGeneratedSrcToXarDir() throws MojoExecutionException {
 495  
 
 496  0
                 String xarDir = getPackageXarDir();
 497  
 
 498  0
                 getLog().info("Copying generated src to xar dir ...");
 499  0
                 getLog().debug("generatedSrcDir: " + getGeneratedSrcDir());
 500  0
                 getLog().debug("xarDir: " + xarDir);
 501  
 
 502  0
                 new File(xarDir, "ESB").mkdirs();
 503  0
                 new File(xarDir, "SonicFS").mkdirs();
 504  
 
 505  
                 try {
 506  
                         // Figure out which Servicetypes have been built by maven and are
 507  
                         // imported via the dependency settings
 508  0
                         StringBuffer excludes = new StringBuffer();
 509  0
                         String separator = "";
 510  0
                         for (Xpp3Dom svc : Xpp3Utils.collectElements(getDependencyDirectory(), "**/*.rules.snippet")) {
 511  0
                                 String svcName = svc.getAttribute("type");
 512  0
                                 excludes.append(separator);
 513  0
                                 excludes.append("ESB/ServiceTypes/" + svcName + ".*");
 514  0
                                 separator = ",";
 515  0
                                 excludes.append(separator);
 516  0
                                 excludes.append("SonicFS/System/**/" + svcName + ".properties");
 517  0
                                 excludes.append(separator);
 518  0
                                 excludes.append("SonicFS/System/**/" + svcName + ".gif");
 519  0
                         }
 520  
 
 521  0
                         FileUtilities.copyFiles(getGeneratedSrcDir(), xarDir, null, excludes.toString() + ","
 522  
                                         + getXarExcludePatterns(), null, false);
 523  
                         // now iterate over all the processes and copy them over to the xar
 524  
                         // archive
 525  
                         // copyProcessFiles(xarDir);
 526  0
                 } catch (Exception e) {
 527  0
                         throw new MojoExecutionException("Error copying xar content: ", e);
 528  0
                 }
 529  0
         }
 530  
 
 531  
         /**
 532  
          * creates the required directory structure for an assembly.
 533  
          */
 534  
         protected void createAssemblyStructure() {
 535  
 
 536  0
                 String[] dirs = new String[] { getAssemblyDir(), getSnippetDir(), getTailorDir() };
 537  
 
 538  0
                 for (String dirName : dirs) {
 539  0
                         File dir = new File(dirName);
 540  0
                         dir.mkdirs();
 541  
                 }
 542  0
         }
 543  
 
 544  
         /**
 545  
          * @param _sp
 546  
          *            SnippetsProcessor
 547  
          * @throws Exception
 548  
          *             Exception
 549  
          */
 550  
         protected void generateQueuesSnippet(final SnippetsProcessor _sp) throws Exception {
 551  
 
 552  0
                 String queuesFile = getFinalAssemblyBaseName() + ".queues.snippet";
 553  0
                 FileUtilities.writeFile(_sp.getQueuesSnippet().getBytes(), getSnippetDir() + "/" + queuesFile);
 554  0
         }
 555  
 
 556  
         /**
 557  
          * @param _sp
 558  
          *            SnippetsProcessor
 559  
          * @throws Exception
 560  
          *             Exception
 561  
          */
 562  
         protected void generateSvcInstancesSnippet(final SnippetsProcessor _sp) throws Exception {
 563  
 
 564  0
                 String instancesFile = getFinalAssemblyBaseName() + ".instances.snippet";
 565  0
                 FileUtilities.writeFile(_sp.getSvcInstancesSnippet(getGroupId() + "." + getArtifactId()).getBytes(),
 566  
                                 getSnippetDir() + "/" + instancesFile);
 567  0
         }
 568  
 
 569  
         /**
 570  
          * @return the location of the temporary xars directory used for building
 571  
          *         the assembly
 572  
          */
 573  
         protected String getAssemblyXarsDir() {
 574  
 
 575  0
                 return getAssemblyDir() + "/" + XARS_DIR_NAME;
 576  
         }
 577  
 
 578  
         /**
 579  
          * @return String
 580  
          */
 581  
         protected String getTailorDir() {
 582  
 
 583  0
                 return getAssemblyXarsDir() + "/" + TAILOR_DIR_NAME;
 584  
         }
 585  
 
 586  
         /**
 587  
          * @return String
 588  
          */
 589  
         protected String getSnippetDir() {
 590  
 
 591  0
                 return getAssemblyXarsDir() + "/" + SDM_SNIPPET_DIR_NAME;
 592  
         }
 593  
 
 594  
         /**
 595  
          * Overload this to produce a test-esb, for example.
 596  
          * 
 597  
          * @return type of the generated artifact
 598  
          */
 599  
         protected abstract String getType();
 600  
 
 601  
         /**
 602  
          * @return created zip file
 603  
          * @throws Exception
 604  
          *             if something goes wrong
 605  
          */
 606  
         File createEsbAssembly() throws Exception {
 607  
 
 608  
                 // System.out.println("TBO ------ IS THIS THE PLACE TO COPY THE DXSI FILES?");
 609  
 
 610  0
                 copyGeneratedSrcToXarDir();
 611  
 
 612  0
                 createAssemblyStructure();
 613  
 
 614  0
                 getLog().info("Creating ESB assembly ...");
 615  0
                 File archiveFile = new File(getAssemblyDir());
 616  
 
 617  0
                 copyJarFiles();
 618  0
                 File xarArchive = createXarFile();
 619  
                 // FileUtils.copyFile(xarArchive, new File(archiveFile, XARS_DIR_NAME +
 620  
                 // "/" + getFinalName() + ".xar"));
 621  0
                 FileUtils.copyFileToDirectory(xarArchive, new File(archiveFile, XARS_DIR_NAME));
 622  0
                 if (copyXarToDeploy) {
 623  0
                         final File deployDir = new File(project.getBasedir(), "deploy");
 624  0
                         getLog().info("Copying xar archive to " + deployDir.getPath());
 625  0
                         FileUtils.copyFileToDirectory(xarArchive, deployDir);
 626  
                 }
 627  
 
 628  0
                 SnippetsProcessor sp = new SnippetsProcessor(xarArchive);
 629  
 
 630  0
                 getLog().debug("Creating Queue snippet ...");
 631  0
                 generateQueuesSnippet(sp);
 632  0
                 getLog().debug("Creating Tailoring rules ...");
 633  0
                 generateTailorRules(sp);
 634  0
                 getLog().debug("Creating Tailoring file ...");
 635  0
                 generateTailoring(sp);
 636  0
                 getLog().debug("Creating Instance snippet ...");
 637  0
                 generateSvcInstancesSnippet(sp);
 638  0
                 getLog().debug("Extracting Variables ...");
 639  0
                 generateVariables();
 640  
 
 641  0
                 Archiver archiver = archiverManager.getArchiver("zip");
 642  
 
 643  0
                 archiveFile = getArchiveFile(project.getBuild().getDirectory(), getFinalAssemblyBaseName(true), "zip");
 644  0
                 archiver.setDestFile(archiveFile);
 645  0
                 archiver.addDirectory(new File(getAssemblyDir()));
 646  0
                 archiver.createArchive();
 647  
 
 648  
                 // Should we be cleaning up what was created ????
 649  
                 // File myfile = new File("target" + File.separatorChar + "unzip");
 650  
 
 651  
                 // System.out.println("TBO ---- Deleting the unzip folder " +
 652  
                 // myfile.exists() + " " + myfile.isDirectory() + " " +
 653  
                 // myfile.getAbsolutePath());
 654  0
                 FileUtils.forceDelete(new File("target" + File.separatorChar + "unzip"));
 655  
 
 656  0
                 return archiveFile;
 657  
         }
 658  
 
 659  
         /**
 660  
          * @return created xar file
 661  
          * @throws Exception
 662  
          *             if something goes wrong
 663  
          */
 664  
         protected File createXarFile() throws Exception {
 665  
 
 666  
                 // loop through the defined resources directory and copy the resources
 667  0
                 List<Resource> resources = getResources();
 668  0
                 for (Resource resource : resources) {
 669  0
                         FileUtilities.copyFiles(
 670  
                                         resource.getDirectory(),
 671  
                                         getPackageXarDir() + "/SonicFS",
 672  
                                         "**/*",
 673  
                                         getXarExcludePatterns(),
 674  
                                         "/workspace/" + getProjectName()
 675  
                                                         + resource.getDirectory().replace(project.getBasedir().getAbsolutePath(), ""), false);
 676  
 
 677  0
                 }
 678  
 
 679  
                 // do not add the esbp or esbsvc files as we already have them via
 680  
                 // ${sonicesb.sourceDirectory}
 681  0
                 FileUtilities.copyFiles(getSonicEsbSourceDirectory().getAbsolutePath(), getPackageXarDir() + "/SonicFS",
 682  
                                 "**/*", "**/*.esbp,**/*.sid,**/*.esbsvc," + getXarExcludePatterns(), "/workspace/" + getProjectName()
 683  
                                                 + getSonicEsbSourceDirectoryFromBase(), false);
 684  
 
 685  
                 // Now package the xar file
 686  0
                 File archiveFile = getArchiveFile(getPackageXarFileDir(), getFinalAssemblyBaseName(), "xar");
 687  0
                 Archiver archiver = archiverManager.getArchiver("zip");
 688  0
                 archiver.setDestFile(archiveFile);
 689  0
                 archiver.addDirectory(new File(getPackageXarDir()));
 690  0
                 archiver.createArchive();
 691  0
                 return archiveFile;
 692  
         }
 693  
 
 694  
         /**
 695  
          * Get the xar exclude patterns.
 696  
          * 
 697  
          * @return xarExcludes
 698  
          */
 699  
         protected String getXarExcludePatterns() {
 700  
 
 701  0
                 return xarExcludes;
 702  
         }
 703  
 
 704  
         private File getArchiveFile(final String _basedir, final String _finalBaseName, final String _extension) {
 705  
 
 706  0
                 return new File(_basedir, _finalBaseName + "." + _extension);
 707  
         }
 708  
 
 709  
         /**
 710  
          * @param _sp
 711  
          *            Snippets Processor
 712  
          * @throws Exception
 713  
          *             if something goes wrong
 714  
          */
 715  
         private void generateTailorRules(final SnippetsProcessor _sp) throws Exception {
 716  
 
 717  0
                 String rulesFile = getFinalAssemblyBaseName() + ".rules.snippet";
 718  0
                 ArrayList<String> pm = getParameterMatches();
 719  0
                 if (pm == null) {
 720  0
                         pm = new ArrayList<String>();
 721  
                 }
 722  
 
 723  0
                 getLog().debug("Checking the Tailor Rule File for parameter rule definitions");
 724  0
                 XmlStreamReader esbtailorReader = null;
 725  
                 // if (getEsbTailorPropFile() == null) {
 726  
                 // getEsbTailorPropFile() = "src/main/sonicesb/ESBTailorProperties.xml";
 727  
                 // }
 728  0
                 File esbTailorFile = getEsbTailorPropFile();
 729  0
                 if (esbTailorFile.exists()) {
 730  
                         try {
 731  0
                                 getLog().info("Tailoring the Init Parameter Visibility");
 732  0
                                 esbtailorReader = new XmlStreamReader(esbTailorFile);
 733  0
                                 Xpp3Dom esbtailorDom = Xpp3DomBuilder.build(esbtailorReader);
 734  0
                                 if (esbtailorDom.getChild("parameterMatches") == null) {
 735  0
                                         getLog().info("No External Parameter Matches found");
 736  
                                 } else {
 737  0
                                         for (Xpp3Dom child : esbtailorDom.getChild("parameterMatches").getChildren()) {
 738  0
                                                 pm.add(child.getValue());
 739  0
                                                 getLog().debug("added: " + child.getValue());
 740  
                                         }
 741  
                                 }
 742  0
                         } catch (Exception e) {
 743  0
                                 getLog().error("Problem encountered with the formatting of the file: " + e.getMessage(), e);
 744  0
                         }
 745  
                 } else {
 746  0
                         getLog().debug("No Tailoring File Found");
 747  
                 }
 748  0
                 if (pm.size() == 0) {
 749  0
                         pm.add(".*");
 750  
                 }
 751  0
                 FileUtilities.writeFile(_sp.getTailorSnippet(pm).getBytes(), getSnippetDir() + "/" + rulesFile);
 752  0
         }
 753  
 
 754  
         /**
 755  
          * @return parameterMatches array list
 756  
          */
 757  
         protected abstract ArrayList<String> getParameterMatches();
 758  
 
 759  
         private Xpp3Dom getSonicConnectPropertiesReplaceMap() {
 760  0
                 File connectProperties = new File(new File(project.getBasedir(), "lib"), project.getArtifactId()
 761  
                                 + ".properties");
 762  0
                 if (connectProperties.exists()) {
 763  0
                         Properties props = new Properties();
 764  
                         FileInputStream fis;
 765  
                         try {
 766  0
                                 fis = new FileInputStream(connectProperties);
 767  0
                                 props.load(fis);
 768  0
                                 fis.close();
 769  0
                         } catch (Exception e) {
 770  0
                                 getLog().error("No Valid Properties file found!", e);
 771  0
                                 return null;
 772  0
                         }
 773  
                         /*
 774  
                          * <PropertyRules> <PropertyReplaceMaps> <propertyReplaceMap
 775  
                          * artifactName=�test.properties�
 776  
                          * propertyName=�Resource.service.baseUrl�
 777  
                          * updatedValue=�http://serviceHost:1234/service" />
 778  
                          * </PropertyReplaceMaps> </PropertyRules>
 779  
                          */
 780  
 
 781  0
                         Xpp3Dom propertyRules = new Xpp3Dom("PropertyRules");
 782  0
                         Xpp3Dom propertyReplaceMaps = new Xpp3Dom("PropertyReplaceMaps");
 783  
 
 784  0
                         propertyReplaceMaps.setAttribute("artifactName", artifactID + ".properties");
 785  
 
 786  0
                         Xpp3Dom replaceMap = null;
 787  
 
 788  0
                         Iterator<Object> it = props.keySet().iterator();
 789  0
                         while (it.hasNext()) {
 790  0
                                 String key = (String) it.next();
 791  0
                                 getLog().debug("Found Property " + key + ":" + props.getProperty(key));
 792  0
                                 if (props.getProperty(key).startsWith("$")) {
 793  0
                                         getLog().debug("Ignoring key " + key + " : " + props.getProperty(key));
 794  0
                                         continue;
 795  
                                 }
 796  0
                                 replaceMap = new Xpp3Dom("propertyReplaceMap");
 797  
                                 // replaceMap.setAttribute("artifactName", artifactID
 798  
                                 // + ".properties");
 799  0
                                 replaceMap.setAttribute("propertyName", (String) key);
 800  0
                                 replaceMap.setAttribute("updatedValue", props.getProperty(key));
 801  
                                 // replaceMap.setAttribute("artifactName", "Tim");
 802  0
                                 propertyReplaceMaps.addChild(replaceMap);
 803  0
                         }
 804  
 
 805  0
                         propertyRules.addChild(propertyReplaceMaps);
 806  
 
 807  0
                         return propertyRules;
 808  
                 } else {
 809  0
                         return null;
 810  
                 }
 811  
         }
 812  
 
 813  
 }