Coverage Report - com.aurea.maven.plugins.sonic.sdm.SdmGenerateModelMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
SdmGenerateModelMojo
0%
0/1164
0%
0/370
0
SdmGenerateModelMojo$1
0%
0/2
N/A
0
SdmGenerateModelMojo$2
0%
0/4
0%
0/4
0
SdmGenerateModelMojo$3
0%
0/4
0%
0/2
0
SdmGenerateModelMojo$4
0%
0/4
0%
0/2
0
SdmGenerateModelMojo$5
0%
0/4
0%
0/2
0
 
 1  
 package com.aurea.maven.plugins.sonic.sdm;
 2  
 
 3  
 import java.io.ByteArrayOutputStream;
 4  
 import java.io.File;
 5  
 import java.io.FileInputStream;
 6  
 import java.io.FileNotFoundException;
 7  
 import java.io.FileOutputStream;
 8  
 import java.io.FilenameFilter;
 9  
 import java.io.IOException;
 10  
 import java.io.ObjectInputStream;
 11  
 import java.util.ArrayList;
 12  
 import java.util.Collection;
 13  
 import java.util.Collections;
 14  
 import java.util.Enumeration;
 15  
 import java.util.HashMap;
 16  
 import java.util.Iterator;
 17  
 import java.util.List;
 18  
 import java.util.Map;
 19  
 import java.util.Properties;
 20  
 import java.util.SortedSet;
 21  
 import java.util.TreeSet;
 22  
 import java.util.regex.Matcher;
 23  
 import java.util.regex.Pattern;
 24  
 
 25  
 import javax.xml.XMLConstants;
 26  
 import javax.xml.transform.stream.StreamSource;
 27  
 import javax.xml.validation.Schema;
 28  
 import javax.xml.validation.SchemaFactory;
 29  
 
 30  
 import no.geosoft.cc.util.SmartTokenizer;
 31  
 
 32  
 import org.apache.maven.execution.MavenSession;
 33  
 import org.apache.maven.plugin.BuildPluginManager;
 34  
 import org.apache.maven.plugin.MojoExecutionException;
 35  
 import org.apache.maven.plugin.MojoFailureException;
 36  
 import org.codehaus.plexus.archiver.Archiver;
 37  
 import org.codehaus.plexus.archiver.manager.ArchiverManager;
 38  
 import org.codehaus.plexus.util.StringUtils;
 39  
 import org.codehaus.plexus.util.xml.XmlStreamReader;
 40  
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 41  
 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
 42  
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 43  
 import org.twdata.maven.mojoexecutor.MojoExecutor;
 44  
 import org.xml.sax.SAXException;
 45  
 
 46  
 import com.aurea.maven.plugins.sonic.sdm.container.IContainerSetting;
 47  
 import com.aurea.maven.plugins.sonic.sdm.container.IESBContainer;
 48  
 import com.aurea.maven.plugins.sonic.sdm.container.IIncludePattern;
 49  
 import com.aurea.maven.plugins.sonic.sdm.container.IMFContainer;
 50  
 import com.aurea.maven.plugins.sonic.sdm.container.impl.DefaultContainerMap;
 51  
 import com.aurea.maven.plugins.sonic.sdm.container.impl.DefaultContainerSetting;
 52  
 import com.aurea.maven.plugins.sonic.sdm.container.impl.DefaultESBContainer;
 53  
 import com.aurea.maven.plugins.sonic.sdm.container.impl.DefaultIncludePattern;
 54  
 import com.aurea.maven.plugins.sonic.sdm.container.impl.DefaultMFContainer;
 55  
 import com.aurea.maven.plugins.sonic.topology.utils.ParameterHandlingException;
 56  
 import com.aurea.maven.plugins.sonic.topology.utils.TopologyHolder;
 57  
 import com.aurea.maven.plugins.sonic.utils.DependencyAnalyzer;
 58  
 import com.aurea.maven.plugins.sonic.utils.FileUtilities;
 59  
 import com.aurea.maven.plugins.sonic.utils.SnippetsProcessor;
 60  
 import com.aurea.maven.plugins.sonic.utils.Xpp3Utils;
 61  
 
 62  
 import commonj.sdo.DataObject;
 63  
 
 64  
 /**
 65  
  * This Mojo is responsible for generating the SDM Model, by collecting the xar
 66  
  * files and analysing them to update the Model.xml file appropriately.
 67  
  * 
 68  
  * @goal sdm-generate-model
 69  
  */
 70  
 
 71  
 // TODO: Implement a SDM SDO ????
 72  0
 public class SdmGenerateModelMojo extends AbstractSdmMojo {
 73  
 
 74  
         /**
 75  
          * The Maven Session Object.
 76  
          * 
 77  
          * @parameter default-value="${session}"
 78  
          * @required
 79  
          * @readonly
 80  
          */
 81  
         private MavenSession session;
 82  
 
 83  
         /**
 84  
          * The Maven PluginManager Object.
 85  
          * 
 86  
          * @component
 87  
          * @required
 88  
          */
 89  
         protected BuildPluginManager pluginManager;
 90  
 
 91  
         /**
 92  
          * The Sonic Target Version Definition. (not used)
 93  
          * 
 94  
          * @parameter default-value="2015"
 95  
          * @required
 96  
          */
 97  
         protected String sonicTarget;
 98  
 
 99  
         /**
 100  
          * The Sonic ESB Container Definition.
 101  
          * 
 102  
          * @parameter default-value="src/main/sonicesb/ContainerProperties.xml"
 103  
          * @required
 104  
          */
 105  
         protected File containerPropFile;
 106  
 
 107  
         /**
 108  
          * The parameter to manipulate in-line ESB Container generation
 109  
          * 
 110  
          * @parameter property="sonicesb.isInlineGeneration"
 111  
          *            default-value="true"
 112  
          * @required
 113  
          */
 114  
         protected boolean isInlineGeneration;
 115  
 
 116  
         /**
 117  
          * The parameter to manipulate in-line ESB Container generation
 118  
          * 
 119  
          * @parameter property="sonicesb.ESBContainerClasspathOverride"
 120  
          *            default-value=""
 121  
          */
 122  
         protected String eSBContainerClasspathOverride;
 123  
 
 124  
         /**
 125  
          * Queue mappings file, to map queues to Queues elements in the model.
 126  
          * 
 127  
          * @parameter default-value="src/main/sonicesb/QueueMappings.xml"
 128  
          * @required
 129  
          */
 130  
         protected File queueMappingsFile;
 131  
 
 132  
         /**
 133  
          * Booelan to determine the extra generation of a properties file .....
 134  
          * 
 135  
          * @parameter property="sonicesb.forcePropertyGeneration"
 136  
          *            default-value="false"
 137  
          * @required
 138  
          */
 139  
         protected boolean forcePropertyGeneration;
 140  
 
 141  
         /**
 142  
          * To look up Archiver/UnArchiver implementations.
 143  
          * 
 144  
          * @component
 145  
          * @required
 146  
          */
 147  
         protected ArchiverManager archiverManager;
 148  
 
 149  
         /**
 150  
          * CSV File location in order to generate topology files based from an
 151  
          * external file.
 152  
          * 
 153  
          * @parameter default-value="topology"
 154  
          * @required
 155  
          */
 156  
         protected File topogenDir;
 157  
 
 158  
         /**
 159  
          * @parameter
 160  
          * @required
 161  
          */
 162  
         protected DefaultContainerMap containerMap;
 163  
 
 164  
         private Collection<IESBContainer> esbContainers, cleanEsbContainer;
 165  
 
 166  0
         private DependencyAnalyzer depAnalyzer = null;
 167  
 
 168  
         private HashMap<String, String> ContainerIncludeFiles;
 169  
         private ArrayList<String> containerSubst;
 170  
 
 171  
         private static final String DEFAULT_SDM_HOST_VALUE = "ESBHost";
 172  
         private static final String DEFAULT_XAR_HOST_VALUE = "XARHost";
 173  
         
 174  0
         HashMap <String, ArrayList<String>>  ESBtoMFMapping = new HashMap<String,ArrayList<String>>(); 
 175  
 
 176  
 
 177  
         /**
 178  
          * This method is called from the framework to actually perform the work.
 179  
          * 
 180  
          * @throws MojoExecutionException
 181  
          *             MojoExecutionException
 182  
          * @throws MojoFailureException
 183  
          *             MojoFailureException
 184  
          */
 185  
         @Override
 186  
         protected void doExecute() throws MojoExecutionException,
 187  
                         MojoFailureException {
 188  
 
 189  
                 try {
 190  0
                         getLog().info("Generating updated SDM model ...");
 191  
 
 192  0
                         getTargetSDMDir().mkdirs();
 193  
 
 194  
                         // Now create the xar archive that will contain the Container
 195  
                         // definitions
 196  
                         // generateContainerXar();
 197  
 
 198  
                         // Check if topology project output is present.
 199  0
                         File depdir = new File(getDependencyDirectory(),
 200  
                                         topogenDir.getName());
 201  
 
 202  0
                         if (!depdir.exists()) {
 203  
                                 // Now we generate a generated tailoring file as a template for
 204  
                                 // environment specific properties
 205  0
                                 performOldResourceHandling();
 206  
                                 // Now we need to rewrite the Model.xml file in order to reflect
 207  
                                 // the
 208  
                                 // additional settings for
 209  
                                 // containers, queues and xar archives.
 210  0
                                 createSDMModel();
 211  
                                 //if (sonicTarget.equals("8")) {
 212  0
                                         createTopology();
 213  0
                                         generateMasterTopology();
 214  0
                                         if (forcePropertyGeneration) {
 215  0
                                                 createProperties();
 216  
                                         }
 217  
 //                                } else {
 218  
 //                                        createProperties();
 219  
 //                                }
 220  
                         } else {
 221  0
                                 performNewResourceHandling();
 222  0
                                 createSDMModel();
 223  0
                                 createTopologyWithDefault();
 224  0
                                 generateMasterTopologyFromDefaultTopology();
 225  0
                                 if (forcePropertyGeneration) {
 226  0
                                         createProperties();
 227  
                                 }
 228  
                                 // createTopology();
 229  
                                 // generateMasterTopology();
 230  
                         }
 231  
 
 232  0
                 } catch (final MojoExecutionException e) {
 233  0
                         e.printStackTrace();
 234  0
                         throw new MojoExecutionException("Error creating SDM Model - "
 235  
                                         + e.getMessage(), e.getCause());
 236  0
                 } catch (final Exception e) {
 237  0
                         e.printStackTrace();
 238  0
                         throw new MojoExecutionException("Error creating SDM Model - "
 239  
                                         + e.getMessage(), e);
 240  0
                 }
 241  
 
 242  0
         }
 243  
         
 244  
         private void updateTailoringProperties() throws MojoExecutionException {
 245  0
                 Properties generatedProperties = null;
 246  0
                 Properties updateProperties = null;
 247  
 
 248  
                 try {
 249  0
                         generatedProperties = new Properties();
 250  0
                         FileInputStream fis = new FileInputStream(getGeneratedPropertiesFile());
 251  0
                         generatedProperties.load(fis);
 252  0
                         fis.close();
 253  0
                 } catch (Exception e) {
 254  0
                         throw new MojoExecutionException("Could not read generated properties files: "
 255  
                                         + getGeneratedPropertiesFile());
 256  0
                 }
 257  
 
 258  0
                 for (String toUpdate : FileUtilities.getFileList(getTargetSDMDir().getAbsolutePath(), "*.tailoring.properties",
 259  
                                 "generated.tailoring.properties")) {
 260  
                         try {
 261  0
                                 getLog().info("------------------------------------------------------------------------");
 262  0
                                 getLog().info("Updating property file: " + toUpdate);
 263  0
                                 updateProperties = new Properties();
 264  0
                                 FileInputStream fis = new FileInputStream(getTargetSDMDir() + "/" + toUpdate);
 265  0
                                 updateProperties.load(fis);
 266  0
                                 fis.close();
 267  
                                 
 268  0
                                 boolean updated = false;
 269  0
                                 for (final Object parameter : generatedProperties.keySet()) {
 270  0
                                         final String parameterName = parameter.toString();
 271  0
                                         if (!updateProperties.containsKey(parameterName)) {
 272  0
                                                 if (getLog().isDebugEnabled()) { 
 273  0
                                                         getLog().debug("Found "+parameterName+" property with value "+generatedProperties.getProperty(parameterName));
 274  
                                                 }
 275  0
                                                 updateProperties.put(parameterName, generatedProperties.getProperty(parameterName));
 276  0
                                                 updated = true;
 277  
                                         }
 278  0
                                 }
 279  
                                 
 280  0
                                 if (updated) {
 281  
                                         @SuppressWarnings("serial")
 282  0
                         final Properties sortedProperties = new Properties() {
 283  
                                 @Override
 284  
                                 public synchronized Enumeration<Object> keys() {
 285  0
                                         return Collections.enumeration(new TreeSet<Object>(super.keySet()));
 286  
                                 }
 287  
                         };
 288  0
                         sortedProperties.putAll(updateProperties);
 289  0
                         try (FileOutputStream fos = new FileOutputStream(getTargetSDMDir() + "/" + toUpdate)) {
 290  0
                                 sortedProperties.store(fos, "Generated by maven-sonicesb-plugin:sdm-generate-model");
 291  0
                         }
 292  
                                 }
 293  0
                         } catch (Exception e) {
 294  0
                                 getLog().warn("Could not read " + getTargetSDMDir() + "/" + toUpdate);
 295  0
                         }
 296  
 
 297  
                 }
 298  0
         }
 299  
 
 300  
         private void generateMasterTopologyFromDefaultTopology() throws Exception {
 301  
 
 302  
                 // Check for existing Topology File
 303  0
                 final File topo = getMasterTopologyFile();
 304  
 
 305  0
                 if (topo.exists()) {
 306  0
                         getLog().info("Topology file is existing ... will NOT Override");
 307  0
                         return;
 308  
                 }
 309  
 
 310  0
                 final Properties props = assemblePropertyList();
 311  0
                 TopologyHolder tHolder = getTopologyHolder(new File(
 312  
                                 getDependencyDirectory(), getTargetTopologyDir().getName()
 313  
                                                 + File.separator + defaultTopologyPrefix + ".topo.ser"));
 314  
 
 315  0
                 final SortedSet<String> propNames = new TreeSet<String>();
 316  0
                 for (final Enumeration<Object> en = props.keys(); en.hasMoreElements();) {
 317  0
                         propNames.add((String) en.nextElement());
 318  
                 }
 319  
 
 320  0
                 getLog().info(
 321  
                                 "Topology File not found ... Generating new Master Topology File");
 322  
                 // Build up the Tree
 323  0
                 final Xpp3Dom topology = new Xpp3Dom("Topology");
 324  0
                 Xpp3Dom includeDom = new Xpp3Dom("Id");
 325  
 
 326  0
                 final Xpp3Dom parameters = new Xpp3Dom("Parameters");
 327  
                 // Xpp3Dom child = null;
 328  0
                 Xpp3Dom paramId = null;
 329  0
                 Xpp3Dom defaultValue = null;
 330  0
                 Xpp3Dom parameter = null;
 331  
 
 332  0
                 topology.setAttribute("xmlns:xsi",
 333  
                                 "http://www.w3.org/2001/XMLSchema-instance");
 334  0
                 topology.setAttribute("xsi:noNamespaceSchemaLocation", "Topology.xsd");
 335  0
                 topology.setAttribute("xmlns:xi", "http://www.w3.org/2001/XInclude");
 336  
 
 337  0
                 for (final String key : propNames) {
 338  
                         // bos.write((key + "=" + props.get(key) + "\n").getBytes());
 339  0
                         parameter = new Xpp3Dom("Parameter");
 340  0
                         paramId = new Xpp3Dom("Id");
 341  0
                         paramId.setValue(key);
 342  
 
 343  0
                         defaultValue = new Xpp3Dom("DefaultValue");
 344  
 
 345  
                         try {
 346  0
                                 defaultValue.setValue(tHolder.getPholder().getParameter(key));
 347  0
                                 getLog().debug("Master Topo Parameter found ...  " + key);
 348  0
                         } catch (ParameterHandlingException e) {
 349  0
                                 getLog().debug(
 350  
                                                 "Master Topo Parameter not found ... setting default from xml : "
 351  
                                                                 + key);
 352  0
                                 final String keyValue = props.getProperty(key);
 353  
 
 354  0
                                 if (keyValue == null) {
 355  
                                         //defaultValue.setValue("54321");
 356  
                                 } else {
 357  0
                                         defaultValue.setValue(props.getProperty(key));
 358  
                                 }
 359  0
                         }
 360  
 
 361  0
                         parameter.addChild(paramId);
 362  0
                         parameter.addChild(defaultValue);
 363  
 
 364  0
                         parameters.addChild(parameter);
 365  0
                 }
 366  
 
 367  0
                 topology.addChild(parameters);
 368  
 
 369  0
                 for (final String toCompare : FileUtilities.getFileList(
 370  
                                 getTargetSDMDir().getAbsolutePath(), "*.Topology.xml",
 371  
                                 "generated.Topology.xml")) {
 372  0
                         includeDom = new Xpp3Dom("xi:include");
 373  0
                         includeDom.setAttribute("href", toCompare);
 374  0
                         topology.addChild(includeDom);
 375  
                 }
 376  
 
 377  0
                 final Xpp3Dom environment = new Xpp3Dom("Environment");
 378  
 
 379  0
                 final Xpp3Dom envId = new Xpp3Dom("Id");
 380  0
                 envId.setValue("default");
 381  0
                 environment.addChild(envId);
 382  
 
 383  0
                 final Xpp3Dom machines = new Xpp3Dom("Machines");
 384  
 
 385  0
                 final Xpp3Dom machine = new Xpp3Dom("Machine");
 386  
 
 387  0
                 final Xpp3Dom machineId = new Xpp3Dom("Id");
 388  0
                 machineId.setValue("localhost");
 389  0
                 machine.addChild(machineId);
 390  
 
 391  0
                 final Xpp3Dom altId = new Xpp3Dom("AlternateId");
 392  0
                 altId.setValue("127.0.0.1");
 393  0
                 machine.addChild(altId);
 394  
 
 395  0
                 final Xpp3Dom altId2 = new Xpp3Dom("AlternateId");
 396  0
                 altId2.setValue("::1");
 397  0
                 machine.addChild(altId2);
 398  
 
 399  0
                 machines.addChild(machine);
 400  0
                 environment.addChild(machines);
 401  
 
 402  0
                 topology.addChild(environment);
 403  
 
 404  0
                 FileUtilities.writeFile(topology.toString().getBytes(),
 405  
                                 getMasterTopologyFile());
 406  
 
 407  0
         }
 408  
 
 409  
         private Properties assemblePropertyList() throws Exception {
 410  0
                 Properties props = new Properties();
 411  
 
 412  
                 // Get the variable names from the model
 413  0
                 final List<String> varNames = new ArrayList<String>();
 414  
 
 415  0
                 final XmlStreamReader reader = new XmlStreamReader(new File(
 416  
                                 getTargetSDMDir(), "Model.xml"));
 417  0
                 final Xpp3Dom modelDom = Xpp3DomBuilder.build(reader);
 418  0
                 getVariableNamesFromModel(modelDom, varNames);
 419  
 
 420  
                 // TBO: Stripping the parameters from the defaultTuning.xml
 421  0
                 File propfile = new File(getTargetSDMDir(), "DefaultTuning.xml");
 422  0
                 if (propfile.exists()) {
 423  0
                         final XmlStreamReader propreader = new XmlStreamReader(new File(
 424  
                                         getTargetSDMDir(), "DefaultTuning.xml"));
 425  0
                         final Xpp3Dom propmodelDom = Xpp3DomBuilder.build(propreader);
 426  0
                         getVariableNamesFromModel(propmodelDom, varNames);
 427  
                 }
 428  
 
 429  
                 // TBO: Stripping the parameters from the Tuning.xml
 430  0
                 File tunefile = new File(getTargetSDMDir(), "Tuning.xml");
 431  0
                 if (tunefile.exists()) {
 432  0
                         final XmlStreamReader tunereader = new XmlStreamReader(new File(
 433  
                                         getTargetSDMDir(), "Tuning.xml"));
 434  0
                         final Xpp3Dom tunemodelDom = Xpp3DomBuilder.build(tunereader);
 435  0
                         getVariableNamesFromModel(tunemodelDom, varNames);
 436  
                 }
 437  
 
 438  
                 // TBO: Stripping the parameters from the ContainerProperties.xml
 439  
                 // File containerfile = containerPropFile;
 440  0
                 if (containerPropFile.exists()) {
 441  0
                         getLog().debug("CHECKING THE CONTAINER PROPERTIES FILE");
 442  0
                         final XmlStreamReader tunereader = new XmlStreamReader(
 443  
                                         containerPropFile);
 444  0
                         final Xpp3Dom tunemodelDom = Xpp3DomBuilder.build(tunereader);
 445  0
                         getVariableNamesFromModel(tunemodelDom, varNames);
 446  
                 }
 447  
 
 448  0
                 for (final String varName : varNames) {
 449  0
                         getLog().info("Found Variable Name: " + varName);
 450  0
                         props.put(varName, "");
 451  0
                 }
 452  
 
 453  
                 // Get the variable resolution from the dependencies
 454  0
                 for (final Xpp3Dom variable : Xpp3Utils.collectElements(
 455  
                                 getDependencyDirectory(), "**/*.variables.snippet")) {
 456  0
                         final String name = variable.getAttribute("name");
 457  0
                         final String value = variable.getValue();
 458  0
                         getLog().debug("Setting variable: [" + name + "] = [" + value + "]");
 459  0
                         props.put(name, value != null ? value : "");
 460  0
                 }
 461  
 
 462  0
                 return props;
 463  
         }
 464  
 
 465  
         private TopologyHolder getTopologyHolder(File fileName) throws Exception {
 466  0
                 TopologyHolder tHolder = null;
 467  
 
 468  0
                 ObjectInputStream ois = null;
 469  0
                 File topoDefault = fileName;
 470  
                 try {
 471  0
                         ois = new ObjectInputStream(new FileInputStream(topoDefault));
 472  0
                         tHolder = (TopologyHolder) ois.readObject();
 473  
                         // ois.close();
 474  0
                 } catch (FileNotFoundException e) {
 475  
                         // TODO Auto-generated catch block
 476  0
                         e.printStackTrace();
 477  0
                         throw new MojoExecutionException(
 478  
                                         "Error creating SDM Model - Default topology file not found");
 479  0
                 } catch (IOException e) {
 480  
                         // TODO Auto-generated catch block
 481  0
                         e.printStackTrace();
 482  0
                         throw new MojoExecutionException(
 483  
                                         "Error creating SDM Model - File Handling Issues");
 484  0
                 } catch (ClassNotFoundException e) {
 485  
                         // TODO Auto-generated catch block
 486  0
                         e.printStackTrace();
 487  0
                         throw new MojoExecutionException(
 488  
                                         "Error creating SDM Model - Topology Classes not found");
 489  
                 } finally {
 490  0
                         ois.close();
 491  0
                 }
 492  0
                 return tHolder;
 493  
         }
 494  
 
 495  
         private void createTopologyWithDefault() throws Exception {
 496  
                 // find the topology serialized default value
 497  0
                 TopologyHolder tHolder = getTopologyHolder(new File(
 498  
                                 getDependencyDirectory(), getTargetTopologyDir().getName()
 499  
                                                 + File.separator + defaultTopologyPrefix + ".topo.ser"));
 500  
 
 501  0
                 getLog().info("Creating generated.Topology.xml");
 502  
 
 503  0
                 final Properties props = assemblePropertyList();
 504  
 
 505  0
                 final SortedSet<String> propNames = new TreeSet<String>();
 506  0
                 for (final Enumeration<Object> en = props.keys(); en.hasMoreElements();) {
 507  0
                         propNames.add((String) en.nextElement());
 508  
                 }
 509  
 
 510  0
                 final Xpp3Dom topology = new Xpp3Dom("Environment");
 511  0
                 final Xpp3Dom id = new Xpp3Dom("Id");
 512  0
                 final Xpp3Dom machines = new Xpp3Dom("Machines");
 513  0
                 final Xpp3Dom machine = new Xpp3Dom("Machine");
 514  0
                 final Xpp3Dom parameters = new Xpp3Dom("Parameters");
 515  
 
 516  0
                 id.setValue("GeneratedTopology");
 517  0
                 topology.addChild(id);
 518  
 
 519  0
                 machines.addChild(machine);
 520  
 
 521  0
                 Xpp3Dom element = null;
 522  0
                 Xpp3Dom child = null;
 523  0
                 Xpp3Dom paramId = null;
 524  0
                 Xpp3Dom defaultValue = null;
 525  0
                 Xpp3Dom parameter = null;
 526  
 
 527  0
                 element = new Xpp3Dom("Id");
 528  0
                 element.setValue("Generated_Machine");
 529  0
                 machine.addChild(element);
 530  
 
 531  0
                 element = new Xpp3Dom("AlternateId");
 532  0
                 element.setValue("Generated_Alternate");
 533  0
                 machine.addChild(element);
 534  
 
 535  0
                 element = new Xpp3Dom("LogicalHosts");
 536  0
                 machine.addChild(element);
 537  
 
 538  0
                 child = new Xpp3Dom("LogicalHost");
 539  0
                 child.setValue("GeneratedHost");
 540  0
                 element.addChild(child);
 541  
 
 542  0
                 element = new Xpp3Dom("Settings");
 543  
 
 544  0
                 topology.addChild(machines);
 545  0
                 topology.addChild(element);
 546  0
                 topology.addChild(parameters);
 547  
 
 548  0
                 for (final String key : propNames) {
 549  
                         // bos.write((key + "=" + props.get(key) + "\n").getBytes());
 550  0
                         parameter = new Xpp3Dom("Parameter");
 551  0
                         paramId = new Xpp3Dom("Id");
 552  0
                         paramId.setValue(key);
 553  
 
 554  0
                         defaultValue = new Xpp3Dom("Value");
 555  
                         // defaultValue.setValue(props.getProperty(key));
 556  
                         try {
 557  0
                                 defaultValue.setValue(tHolder.getPholder().getParameter(key));
 558  0
                                 getLog().debug("Parameter found ...  " + key);
 559  0
                         } catch (ParameterHandlingException e) {
 560  0
                                 getLog().debug(
 561  
                                                 "Parameter not found ... setting default from xml : "
 562  
                                                                 + key);
 563  0
                                 defaultValue.setValue(props.getProperty(key));
 564  0
                         }
 565  0
                         parameter.addChild(paramId);
 566  0
                         parameter.addChild(defaultValue);
 567  
 
 568  0
                         parameters.addChild(parameter);
 569  0
                 }
 570  
 
 571  0
                 FileUtilities.writeFile(topology.toString().getBytes(),
 572  
                                 getGeneratedTopologyFile());
 573  
 
 574  0
                 getLog().info(
 575  
                                 "Building the remaining Topology Files as they are found in the /toplogy Directory");
 576  0
                 File topodir = new File(getDependencyDirectory(),
 577  
                                 getTargetTopologyDir().getName());
 578  0
                 FilenameFilter myFileNameFilter = new FilenameFilter() {
 579  
                         public boolean accept(File dir, String name) {
 580  0
                                 if (name.endsWith(".topo.ser")
 581  
                                                 && !name.equals(defaultTopologyPrefix + ".topo.ser")) {
 582  0
                                         return true;
 583  
                                 }
 584  0
                                 return false;
 585  
                         }
 586  
                 };
 587  
 
 588  0
                 File[] topoFiles = topodir.listFiles(myFileNameFilter);
 589  0
                 for (File file : topoFiles) {
 590  0
                         getLog().info("Topo Ser File Found: " + file.getName());
 591  0
                         TopologyHolder envTopoHolder = getTopologyHolder(new File(
 592  
                                         getDependencyDirectory(), getTargetTopologyDir().getName()
 593  
                                                         + File.separator + file.getName()));
 594  
 
 595  0
                         getLog().info("Creating " + file.getName() + ".Topology.xml files");
 596  
 
 597  0
                         String topoenv = envTopoHolder.createEnvironmentTopology(tHolder,
 598  
                                         propNames);
 599  0
                         FileUtilities.writeFile(topoenv.getBytes(), getTargetSDMDir()
 600  
                                         + File.separator + envTopoHolder.getTopologyId()
 601  
                                         + ".Topology.xml");
 602  
                 }
 603  0
         }
 604  
 
 605  
         private void performNewResourceHandling() throws MojoExecutionException {
 606  0
                 getLog().info(
 607  
                                 "Copying resources from " + getSonicEsbSourceDirectory()
 608  
                                                 + " to " + getTargetSDMDir().getAbsolutePath());
 609  0
                 MojoExecutor
 610  
                                 .executeMojo(
 611  
                                                 MojoExecutor.plugin(MojoExecutor
 612  
                                                                 .groupId("org.apache.maven.plugins"),
 613  
                                                                 MojoExecutor
 614  
                                                                                 .artifactId("maven-resources-plugin"),
 615  
                                                                 MojoExecutor.version("2.4.3")),
 616  
                                                 MojoExecutor.goal("copy-resources"),
 617  
                                                 MojoExecutor.configuration(new MojoExecutor.Element[] {
 618  
                                                                 MojoExecutor.element(
 619  
                                                                                 MojoExecutor.name("outputDirectory"),
 620  
                                                                                 getTargetSDMDir().getAbsolutePath()),
 621  
                                                                 MojoExecutor.element(
 622  
                                                                                 "resources",
 623  
                                                                                 MojoExecutor
 624  
                                                                                                 .element(
 625  
                                                                                                                 "resource",
 626  
                                                                                                                 MojoExecutor
 627  
                                                                                                                                 .element(
 628  
                                                                                                                                                 "directory",
 629  
                                                                                                                                                 getSonicEsbSourceDirectory()
 630  
                                                                                                                                                                 .getAbsolutePath()),
 631  
                                                                                                                 MojoExecutor
 632  
                                                                                                                                 .element(
 633  
                                                                                                                                                 "excludes",
 634  
                                                                                                                                                 new MojoExecutor.Element[] {
 635  
                                                                                                                                                                 MojoExecutor
 636  
                                                                                                                                                                                 .element(
 637  
                                                                                                                                                                                                 "exclude",
 638  
                                                                                                                                                                                                 containerPropFile
 639  
                                                                                                                                                                                                                 .getName()),
 640  
                                                                                                                                                                 MojoExecutor
 641  
                                                                                                                                                                                 .element(
 642  
                                                                                                                                                                                                 "exclude",
 643  
                                                                                                                                                                                                 queueMappingsFile
 644  
                                                                                                                                                                                                                 .getName()),
 645  
                                                                                                                                                                 MojoExecutor
 646  
                                                                                                                                                                                 .element(
 647  
                                                                                                                                                                                                 "exclude",
 648  
                                                                                                                                                                                                 "*.Topology.xml") }),
 649  
                                                                                                                 MojoExecutor.element(
 650  
                                                                                                                                 "filtering",
 651  
                                                                                                                                 "false"))) }),
 652  
                                                 MojoExecutor.executionEnvironment(project, session,
 653  
                                                                 pluginManager));
 654  
 
 655  0
                 getLog().info("Filtering the following resources: " + "Model.xml");
 656  0
                 MojoExecutor.executeMojo(MojoExecutor.plugin(
 657  
                                 MojoExecutor.groupId("org.apache.maven.plugins"),
 658  
                                 MojoExecutor.artifactId("maven-resources-plugin"),
 659  
                                 MojoExecutor.version("2.4.3")), MojoExecutor
 660  
                                 .goal("copy-resources"), MojoExecutor
 661  
                                 .configuration(new MojoExecutor.Element[] {
 662  
                                                 MojoExecutor.element(
 663  
                                                                 MojoExecutor.name("outputDirectory"),
 664  
                                                                 getTargetSDMDir().getAbsolutePath()),
 665  
                                                 MojoExecutor.element(MojoExecutor.name("overwrite"),
 666  
                                                                 "true"),
 667  
                                                 MojoExecutor.element("escapeString", "\\"),
 668  
                                                 MojoExecutor.element("resources", MojoExecutor.element(
 669  
                                                                 "resource", MojoExecutor.element("directory",
 670  
                                                                                 getSonicEsbSourceDirectory()
 671  
                                                                                                 .getAbsolutePath()),
 672  
                                                                 MojoExecutor.element(
 673  
                                                                                 "includes",
 674  
                                                                                 new MojoExecutor.Element[] {
 675  
                                                                                                 MojoExecutor.element("include",
 676  
                                                                                                                 "Model.xml"),
 677  
                                                                                                 MojoExecutor.element("include",
 678  
                                                                                                                 "*.properties"),
 679  
                                                                                                 MojoExecutor.element("include",
 680  
                                                                                                                 "*Deletes.xml"),
 681  
                                                                                                 MojoExecutor.element("include",
 682  
                                                                                                                 "*Rollback.xml") }),
 683  
                                                                 MojoExecutor.element("filtering", "true"))) }),
 684  
                                 MojoExecutor.executionEnvironment(project, session,
 685  
                                                 pluginManager));
 686  
 
 687  0
                 getLog().info("Filtering the following resources: " + "Tuning.xml");
 688  0
                 MojoExecutor
 689  
                                 .executeMojo(
 690  
                                                 MojoExecutor.plugin(MojoExecutor
 691  
                                                                 .groupId("org.apache.maven.plugins"),
 692  
                                                                 MojoExecutor
 693  
                                                                                 .artifactId("maven-resources-plugin"),
 694  
                                                                 MojoExecutor.version("2.4.3")),
 695  
                                                 MojoExecutor.goal("copy-resources"),
 696  
                                                 MojoExecutor.configuration(new MojoExecutor.Element[] {
 697  
                                                                 MojoExecutor.element(
 698  
                                                                                 MojoExecutor.name("outputDirectory"),
 699  
                                                                                 getTargetSDMDir().getAbsolutePath()),
 700  
                                                                 MojoExecutor.element(
 701  
                                                                                 MojoExecutor.name("overwrite"), "true"),
 702  
                                                                 MojoExecutor.element("escapeString", "\\"),
 703  
                                                                 MojoExecutor.element(
 704  
                                                                                 "resources",
 705  
                                                                                 MojoExecutor
 706  
                                                                                                 .element(
 707  
                                                                                                                 "resource",
 708  
                                                                                                                 MojoExecutor
 709  
                                                                                                                                 .element(
 710  
                                                                                                                                                 "directory",
 711  
                                                                                                                                                 getSonicEsbSourceDirectory()
 712  
                                                                                                                                                                 .getAbsolutePath()),
 713  
                                                                                                                 MojoExecutor
 714  
                                                                                                                                 .element(
 715  
                                                                                                                                                 "includes",
 716  
                                                                                                                                                 new MojoExecutor.Element[] { MojoExecutor
 717  
                                                                                                                                                                 .element(
 718  
                                                                                                                                                                                 "include",
 719  
                                                                                                                                                                                 "Tuning.xml") }),
 720  
                                                                                                                 MojoExecutor.element(
 721  
                                                                                                                                 "filtering",
 722  
                                                                                                                                 "false"))) }),
 723  
                                                 MojoExecutor.executionEnvironment(project, session,
 724  
                                                                 pluginManager));
 725  0
         }
 726  
 
 727  
         private void performOldResourceHandling() throws MojoExecutionException {
 728  0
                 getLog().info(
 729  
                                 "Copying resources from " + getSonicEsbSourceDirectory()
 730  
                                                 + " to " + getTargetSDMDir().getAbsolutePath());
 731  0
                 MojoExecutor
 732  
                                 .executeMojo(
 733  
                                                 MojoExecutor.plugin(MojoExecutor
 734  
                                                                 .groupId("org.apache.maven.plugins"),
 735  
                                                                 MojoExecutor
 736  
                                                                                 .artifactId("maven-resources-plugin"),
 737  
                                                                 MojoExecutor.version("2.4.3")),
 738  
                                                 MojoExecutor.goal("copy-resources"),
 739  
                                                 MojoExecutor.configuration(new MojoExecutor.Element[] {
 740  
                                                                 MojoExecutor.element(
 741  
                                                                                 MojoExecutor.name("outputDirectory"),
 742  
                                                                                 getTargetSDMDir().getAbsolutePath()),
 743  
                                                                 MojoExecutor.element(
 744  
                                                                                 MojoExecutor.name("overwrite"), "true"),
 745  
                                                                 MojoExecutor.element(
 746  
                                                                                 "resources",
 747  
                                                                                 MojoExecutor
 748  
                                                                                                 .element(
 749  
                                                                                                                 "resource",
 750  
                                                                                                                 MojoExecutor
 751  
                                                                                                                                 .element(
 752  
                                                                                                                                                 "directory",
 753  
                                                                                                                                                 getSonicEsbSourceDirectory()
 754  
                                                                                                                                                                 .getAbsolutePath()),
 755  
                                                                                                                 MojoExecutor
 756  
                                                                                                                                 .element(
 757  
                                                                                                                                                 "excludes",
 758  
                                                                                                                                                 new MojoExecutor.Element[] {
 759  
                                                                                                                                                                 MojoExecutor
 760  
                                                                                                                                                                                 .element(
 761  
                                                                                                                                                                                                 "exclude",
 762  
                                                                                                                                                                                                 containerPropFile
 763  
                                                                                                                                                                                                                 .getName()),
 764  
                                                                                                                                                                 MojoExecutor
 765  
                                                                                                                                                                                 .element(
 766  
                                                                                                                                                                                                 "exclude",
 767  
                                                                                                                                                                                                 queueMappingsFile
 768  
                                                                                                                                                                                                                 .getName()) }),
 769  
                                                                                                                 MojoExecutor.element(
 770  
                                                                                                                                 "filtering",
 771  
                                                                                                                                 "false"))) }),
 772  
                                                 MojoExecutor.executionEnvironment(project, session,
 773  
                                                                 pluginManager));
 774  
 
 775  0
                 getLog().info("Filtering the following resources: " + "Model.xml");
 776  0
                 MojoExecutor.executeMojo(MojoExecutor.plugin(
 777  
                                 MojoExecutor.groupId("org.apache.maven.plugins"),
 778  
                                 MojoExecutor.artifactId("maven-resources-plugin"),
 779  
                                 MojoExecutor.version("2.4.3")), MojoExecutor
 780  
                                 .goal("copy-resources"), MojoExecutor
 781  
                                 .configuration(new MojoExecutor.Element[] {
 782  
                                                 MojoExecutor.element(
 783  
                                                                 MojoExecutor.name("outputDirectory"),
 784  
                                                                 getTargetSDMDir().getAbsolutePath()),
 785  
                                                 MojoExecutor.element(MojoExecutor.name("overwrite"),
 786  
                                                                 "true"),
 787  
                                                 MojoExecutor.element("escapeString", "\\"),
 788  
                                                 MojoExecutor.element("resources", MojoExecutor.element(
 789  
                                                                 "resource", MojoExecutor.element("directory",
 790  
                                                                                 getSonicEsbSourceDirectory()
 791  
                                                                                                 .getAbsolutePath()),
 792  
                                                                 MojoExecutor.element(
 793  
                                                                                 "includes",
 794  
                                                                                 new MojoExecutor.Element[] {
 795  
                                                                                                 MojoExecutor.element("include",
 796  
                                                                                                                 "Model.xml"),
 797  
                                                                                                 MojoExecutor.element("include",
 798  
                                                                                                                 "*.properties"),
 799  
                                                                                                 MojoExecutor.element("include",
 800  
                                                                                                                 "*Deletes.xml"),
 801  
                                                                                                 MojoExecutor.element("include",
 802  
                                                                                                                 "*Rollback.xml") }),
 803  
                                                                 MojoExecutor.element("filtering", "true"))) }),
 804  
                                 MojoExecutor.executionEnvironment(project, session,
 805  
                                                 pluginManager));
 806  
 
 807  0
         }
 808  
 
 809  
         /**
 810  
    * 
 811  
    */
 812  
         private void createSDMModel() throws Exception {
 813  
 
 814  0
                 getLog().info("Relocating the Xar Generation code ...");
 815  0
                 if (isInlineGeneration) {
 816  
                         // Generate the includes for the model and tuning.xml files
 817  0
                         generateESBContainerIncludes();
 818  0
                         generateCleanESBContainerXar();
 819  
                         //generateContainerXar();
 820  
                 } else {
 821  0
                         generateContainerXar();
 822  
                 }
 823  0
                 getLog().debug("Creating SDM model from the sources...");
 824  
 
 825  0
                 final File sdmDir = new File(getTargetSDMDir().getAbsolutePath());
 826  0
                 sdmDir.mkdirs();
 827  
 
 828  0
                 getLog().debug("Collecting XAR archives from dependencies...");
 829  0
                 copyXarArchives();
 830  
 
 831  0
                 getLog().debug("Rewriting SDM Model to reflect dependencies...");
 832  0
                 injectSDMSettings();
 833  
 
 834  0
         }
 835  
 
 836  
         private void generateCleanESBContainerXar() throws Exception {
 837  0
                 getLog().info("Creating Clean xar archive for the ESBContainers");
 838  
 
 839  0
                 final String xarDirectory = getPackageXarDir();
 840  
 
 841  0
                 new File(xarDirectory, "ESB/Containers").mkdirs();
 842  0
                 new File(xarDirectory, "SonicFS").mkdirs();
 843  
                 
 844  0
                 HashMap <String,String> instanceReplaceMaps = new  HashMap<String,String>();
 845  
 
 846  0
                 for (final IESBContainer container : getCleanESBContainerSettings(false, instanceReplaceMaps)) {
 847  0
                         final String fileName = container.getName() + ".xml";
 848  
 
 849  0
                         FileUtilities.writeFile(container.toString().getBytes(),
 850  
                                         xarDirectory + "/ESB/Containers/" + fileName);
 851  0
                 }
 852  
                 
 853  
 
 854  
                 
 855  0
                 final File xarFile = new File(getTargetSDMDir(), "xars/"
 856  
                                 + getFinalAssemblyBaseName() + ".xar");
 857  
 
 858  0
                 final Archiver archiver = archiverManager.getArchiver("zip");
 859  0
                 archiver.setDestFile(xarFile);
 860  0
                 archiver.addDirectory(new File(xarDirectory));
 861  
                 
 862  0
                 getLog().error("Creating the archive " + xarFile.getAbsolutePath());
 863  0
                 archiver.createArchive();
 864  
                 
 865  
                 //Generating the ReplaceMaps file
 866  
 
 867  
                 //reading the instanceReplaceMaps
 868  0
                 for (Iterator iterator = instanceReplaceMaps.keySet().iterator(); iterator.hasNext();) {
 869  0
                         String key = (String) iterator.next();
 870  0
                         getLog().error("TBO --- Replacing: " + key + " with: " + instanceReplaceMaps.get(key));
 871  
                         
 872  0
                 }
 873  
                 
 874  0
                 if(! instanceReplaceMaps.isEmpty()){                        
 875  0
                         createCleanESBContainerTailoring(instanceReplaceMaps);
 876  
                 }
 877  
                 
 878  0
         }
 879  
         
 880  
         private void createCleanESBContainerTailoring(HashMap<String,String> instanceReplaceMaps) {
 881  
 //                String tailorFile = getOutputDirectory() +  File.separatorChar + "sonicesb" + File.separatorChar + "assembly" + File.separatorChar + XARS_DIR_NAME + File.separatorChar + TAILOR_DIR_NAME + File.separatorChar + getFinalAssemblyBaseName() + ".xar.xml";
 882  
                 
 883  
 //                String tailorFile = getOutputDirectory() + File.separatorChar + getFinalAssemblyBaseName() + ".xar.xml";
 884  
                 
 885  0
                 File outputFile =  new File(getTargetSDMDir(), "xars" + File.separatorChar + "tailor" + File.separatorChar + getFinalAssemblyBaseName() + ".xar.xml");
 886  0
                 File outputDir = new File(getTargetSDMDir(), "xars" + File.separatorChar + "tailor");
 887  
                 
 888  0
                 if(!outputDir.exists()){
 889  0
                         outputDir.mkdirs();
 890  0
                         getLog().error("TBO ---- CREATED DIRECTORY for TAILORING");
 891  
                 }
 892  
                 //create the xml
 893  0
                 final Xpp3Dom tailorMap = new Xpp3Dom("TailoringMaps");
 894  0
                 tailorMap.setAttribute("xmlns", "http://www.sonicsw.com/sonicxq");
 895  0
                 tailorMap.setAttribute("version", "TailorMap8.0");
 896  0
                 tailorMap.addChild(new Xpp3Dom("ProcessMaps"));
 897  0
                 tailorMap.addChild(new Xpp3Dom("ServiceMaps"));
 898  0
                 tailorMap.addChild(new Xpp3Dom("EndpointMaps"));
 899  0
                 tailorMap.addChild(new Xpp3Dom("ConnectionMaps"));
 900  
                 
 901  0
                 Xpp3Dom stringReplaceMaps = new Xpp3Dom("StringReplaceMaps");
 902  0
                 stringReplaceMaps.setAttribute("xmlns:xi", "http://www.w3.org/2001/XMLSchema-instance");
 903  
                 
 904  0
                 for (Iterator iterator = instanceReplaceMaps.keySet().iterator(); iterator.hasNext();) {
 905  0
                         String key = (String) iterator.next();
 906  0
                         Xpp3Dom map = new Xpp3Dom("stringReplaceMap");
 907  0
                         map.setAttribute("updatedName", instanceReplaceMaps.get(key));
 908  0
                         map.setAttribute("name", key);
 909  0
                         stringReplaceMaps.addChild(map);
 910  0
                 }
 911  
                 
 912  0
                 tailorMap.addChild(stringReplaceMaps);
 913  
                 
 914  
                 
 915  
                 //write the xml to the file
 916  
                 
 917  0
                 getLog().error("TBO --- WRITING FILE " + outputFile);
 918  0
                 FileUtilities.writeFile(tailorMap.toString().getBytes(),
 919  
                                 outputFile);
 920  
                 
 921  
                 
 922  
                 //System.exit(0);
 923  0
         }
 924  
 
 925  
         @SuppressWarnings({ "unchecked", "rawtypes" })
 926  
         private Collection<IESBContainer> getCleanESBContainerSettings(boolean inLine, HashMap <String,String> instanceReplaceMaps)
 927  
                         throws Exception {
 928  
 
 929  0
                 if (cleanEsbContainer == null) {
 930  0
                         final ArrayList[] configs = readESBContainerConfiguration();
 931  0
                         getLog().debug("Adding Clean External ESB Configuration - Start");
 932  
                         // Adding external Configuration
 933  0
                         final ArrayList<IContainerSetting> esbConfig = configs[0];
 934  0
                         for (final Iterator<IContainerSetting> iterator = esbConfig
 935  0
                                         .iterator(); iterator.hasNext();) {
 936  0
                                 final IContainerSetting container = iterator.next();
 937  
 
 938  
                                 // Remove and re-add the element to make sure the newer setting
 939  
                                 // (from ContainerProperties.xml) overrides any settings in
 940  
                                 // pom.xml
 941  0
                                 containerMap.getESBContainerSettings().remove(container);
 942  0
                                 containerMap.getESBContainerSettings().add(container);
 943  
                                 
 944  0
                         }
 945  
 
 946  0
                         getLog().info(
 947  
                                         "ESB Clean container mapping: "
 948  
                                                         + containerMap.getESBContainerSettings());
 949  
 
 950  0
                         final ArrayList<IContainerSetting> mfConfig = configs[1];
 951  0
                         for (final Iterator<IContainerSetting> iterator = mfConfig
 952  0
                                         .iterator(); iterator.hasNext();) {
 953  0
                                 final IContainerSetting container = iterator.next();
 954  
 
 955  
                                 // Remove and re-add the element to make sure the newer setting
 956  
                                 // (from ContainerProperties.xml) overrides any settings in
 957  
                                 // pom.xml
 958  0
                                 containerMap.getMFContainerSettings().remove(container);
 959  0
                                 containerMap.getMFContainerSettings().add(container);
 960  0
                         }
 961  0
                         getLog().debug(
 962  
                                         "MF container mapping: "
 963  
                                                         + containerMap.getMFContainerSettings());
 964  
 
 965  0
                         getLog().debug("Adding External ESB Configuration - Done");
 966  
 
 967  0
                         final HashMap<String, IESBContainer> esbContainerMap = new HashMap<String, IESBContainer>();
 968  
 
 969  0
                         getLog().info("---------------------------------------------");
 970  0
                         getLog().debug("Building Container settings for SDM model...");
 971  
 
 972  0
                         final List<Xpp3Dom> serviceInstances = Xpp3Utils.collectElements(
 973  
                                         getDependencyDirectory(), "**/*.instances.snippet");
 974  0
                         for (final Xpp3Dom serviceInstance : serviceInstances) {
 975  
 
 976  0
                                 final String containerHint = serviceInstance
 977  
                                                 .getAttribute(SnippetsProcessor.HINT_ATTRIBUTE);
 978  
 
 979  0
                                 getLog().debug(
 980  
                                                 "Matching service instance "
 981  
                                                                 + serviceInstance.getAttribute("name") + "/"
 982  
                                                                 + serviceInstance.getAttribute("process")
 983  
                                                                 + "\r\n\t(containerHint: " + containerHint
 984  
                                                                 + ")");
 985  
 
 986  0
                                 if (containerMap == null) {
 987  0
                                         throw new Exception(
 988  
                                                         "containerMap not specified in plugin configuration");
 989  
                                 }
 990  
 
 991  0
                                 final Map<String, Properties> matches = containerMap
 992  
                                                 .matchESBContainerSettings(containerHint);
 993  0
                                 if (matches.size() == 0
 994  
                                                 && containerMap.isUseESBDefaultIncludes()) {
 995  0
                                         getLog().warn(
 996  
                                                         "No matching ESBContainer setting found...will use default...");
 997  0
                                         matches.put(containerMap.getDefaultESBContainerSetting()
 998  
                                                         .getName(), new Properties());
 999  
                                 }
 1000  
 
 1001  0
                                 for (final Iterator<String> it = matches.keySet().iterator(); it
 1002  0
                                                 .hasNext();) {
 1003  
                                         
 1004  
 //                                        HashMap <String,String> ctValues = new HashMap<String,String>();
 1005  
 //                                        instanceReplaceMaps.put(container.getName(), ctValues);
 1006  
                                         
 1007  
                                         
 1008  0
                                         final String matchedName = it.next();
 1009  0
                                         addCleanServiceInstance(esbContainerMap, matchedName,
 1010  
                                                         serviceInstance, matches.get(matchedName), inLine, instanceReplaceMaps);
 1011  0
                                 }
 1012  
 
 1013  0
                         }
 1014  
 
 1015  0
                         cleanEsbContainer = esbContainerMap.values();
 1016  
                 }
 1017  
 
 1018  0
                 return cleanEsbContainer;
 1019  
         }
 1020  
 
 1021  
         /**
 1022  
          * Copy the xar files and the generated snippets over to the target
 1023  
          * directory.
 1024  
          */
 1025  
         private void copyXarArchives() throws Exception {
 1026  
 
 1027  
                 // only copy XARs which are not provided
 1028  0
                 final String xarBaseDirectory = getDepAnalyzer().getXarDependencyDir()
 1029  
                                 + "/compile";
 1030  0
                 final String[] xarFiles = FileUtilities.getFileList(xarBaseDirectory,
 1031  
                                 "**/*.xar", null);
 1032  
 
 1033  0
                 for (final String xarFileName : xarFiles) {
 1034  0
                         final File xarDir = new File(xarBaseDirectory, xarFileName)
 1035  
                                         .getParentFile();
 1036  0
                         FileUtilities.copyFiles(xarDir.getAbsolutePath(), getTargetSDMDir()
 1037  
                                         + "/xars", null, "sdm-snippets/*.snippet", null, false);
 1038  
                 }
 1039  0
         }
 1040  
 
 1041  
         /**
 1042  
          * This method is simply the controller for the modification of the SDM
 1043  
          * model.
 1044  
          */
 1045  
         private void injectSDMSettings() throws Exception {
 1046  
 
 1047  
                 // Get the SDM Model as a DOM tree for manipulation
 1048  0
                 final File sdmFile = new File(getTargetSDMDir(), "Model.xml");
 1049  
 
 1050  0
                 final XmlStreamReader sdmReader = new XmlStreamReader(sdmFile);
 1051  0
                 final Xpp3Dom sdmDom = Xpp3DomBuilder.build(sdmReader);
 1052  
 
 1053  
                 // if (isInlineGeneration) {
 1054  
                 // getLog().info("PERFORMING IN-LINE SUBSTITUTION OF SDM ARTIFACTS");
 1055  
                 // // Finding the XML Container files ...
 1056  
                 //
 1057  
                 // // Parsing the containers
 1058  
                 //
 1059  
                 // // Creating Model XML Includes
 1060  
                 //
 1061  
                 // // Creating Tuning XML Includes
 1062  
                 //
 1063  
                 // } else {
 1064  
                 // getLog().info("Generating the container.xml old fashion style");
 1065  
                 // }
 1066  
 
 1067  
                 // Set the version number in the model
 1068  0
                 setModelVersion(sdmDom);
 1069  
 
 1070  
                 // Assign the queues from the dependencies to their queues sections.
 1071  0
                 assignQueues(sdmDom);
 1072  
 
 1073  
                 // Now we will iterate over the xar archives and add them to the SDM
 1074  
                 // package
 1075  0
                 for (final String xarAchive : FileUtilities.getFileList(
 1076  
                                 getTargetSDMDir() + "/xars", "**/*.xar", null)) {
 1077  0
                         final Xpp3Dom elem = new Xpp3Dom("ESBArchive");
 1078  0
                         final String archiveName = new File(getTargetSDMDir(), xarAchive)
 1079  
                                         .getName();
 1080  0
                         getLog().info("Adding XAR archive SDM package: " + archiveName);
 1081  
                         //if (sonicTarget.equals("8")) {
 1082  0
                                 final Xpp3Dom hosts = new Xpp3Dom("LogicalHosts");
 1083  0
                                 hosts.addChild(Xpp3Utils.createXpp3Dom("LogicalHost",
 1084  
                                                 DEFAULT_XAR_HOST_VALUE));
 1085  0
                                 elem.addChild(hosts);
 1086  0
                                 elem.addChild(Xpp3Utils.createXpp3Dom("Id", archiveName));
 1087  
 //                        } else {
 1088  
 //                                final Xpp3Dom hosts = new Xpp3Dom("Hosts");
 1089  
 //                                hosts.addChild(Xpp3Utils.createXpp3Dom("Host",
 1090  
 //                                                DEFAULT_XAR_HOST_VALUE));
 1091  
 //                                elem.addChild(hosts);
 1092  
 //                                elem.addChild(Xpp3Utils.createXpp3Dom("Id", archiveName));
 1093  
 //                        }
 1094  0
                         sdmDom.addChild(elem);
 1095  
                 }
 1096  
 
 1097  0
                 if (isInlineGeneration) {
 1098  0
                         getLog().info("Lookup of the Includes for the Model.xml file");
 1099  0
                         File includeDir = new File(getTargetSDMDir() + File.separator
 1100  
                                         + "modelincludes");
 1101  0
                         if (!includeDir.exists()) {
 1102  
                                 // no includes used ... skip this
 1103  
                         } else {
 1104  0
                                 sdmDom.setAttribute("xmlns:xi",
 1105  
                                                 "http://www.w3.org/2001/XInclude");
 1106  0
                                 getLog().debug("TBO2 ------------ INCLUDES DIR FOUND .... TEST");
 1107  0
                                 File[] content = includeDir.listFiles(new FilenameFilter() {
 1108  
                                         @Override
 1109  
                                         public boolean accept(File dir, String name) {
 1110  0
                                                 if (name.endsWith("_inc_model.xml")) {
 1111  
 
 1112  0
                                                         return true;
 1113  
 
 1114  
                                                 } else {
 1115  0
                                                         return false;
 1116  
                                                 }
 1117  
                                         }
 1118  
                                 });
 1119  
 
 1120  
                                 // Here I want to find a list of include files that are already
 1121  
                                 // included ...
 1122  0
                                 getLog().debug("FINDING THE FILES AND THE KEYS");
 1123  
 
 1124  0
                                 Iterator<String> it = this.ContainerIncludeFiles.keySet()
 1125  
                                                 .iterator();
 1126  0
                                 while (it.hasNext()) {
 1127  0
                                         String key = it.next();
 1128  0
                                         String value = this.ContainerIncludeFiles.get(key);
 1129  0
                                         getLog().debug("Found key " + key + " : " + value);
 1130  0
                                 }
 1131  
 
 1132  0
                                 getLog().debug("END FINDING THE FILES AND THE KEYS");
 1133  
 
 1134  0
                                 getLog().debug("FINDING THE Model AND THE KEYS");
 1135  
 
 1136  0
                                 replaceMFESBContainers(sdmDom);
 1137  
 
 1138  0
                                 for (int i = 0; i < content.length; i++) {
 1139  0
                                         getLog().debug(
 1140  
                                                         "Filename Found: " + content[i].getName() + " --- "
 1141  
                                                                         + content[i].getPath());
 1142  
                                         // check if it is already included
 1143  0
                                         if (containerSubst != null
 1144  
                                                         && containerSubst.contains(content[i].getName())) {
 1145  0
                                                 getLog().info(
 1146  
                                                                 "href already included ... "
 1147  
                                                                                 + content[i].getName()
 1148  
                                                                                 + " Will skip generic include");
 1149  
                                         } else {
 1150  0
                                                 Xpp3Dom includeDom = new Xpp3Dom("xi:include");
 1151  0
                                                 includeDom.setAttribute("href", "modelincludes/"
 1152  
                                                                 + content[i].getName());
 1153  0
                                                 sdmDom.addChild(includeDom);
 1154  
                                         }
 1155  
                                 }
 1156  
                                 // Adding files to the Tuning.xml file
 1157  
 
 1158  0
                                 final File tuningFile = new File(getTargetSDMDir(),
 1159  
                                                 "Tuning.xml");
 1160  
 
 1161  0
                                 final XmlStreamReader tuningReader = new XmlStreamReader(
 1162  
                                                 tuningFile);
 1163  0
                                 final Xpp3Dom tuningDom = Xpp3DomBuilder.build(tuningReader);
 1164  
 
 1165  0
                                 tuningDom.setAttribute("xmlns:xi",
 1166  
                                                 "http://www.w3.org/2001/XInclude");
 1167  
 
 1168  0
                                 File[] tuningctnt = includeDir.listFiles(new FilenameFilter() {
 1169  
                                         @Override
 1170  
                                         public boolean accept(File dir, String name) {
 1171  0
                                                 if (name.endsWith("_inc_tuning.xml")) {
 1172  0
                                                         return true;
 1173  
                                                 } else {
 1174  0
                                                         return false;
 1175  
                                                 }
 1176  
                                         }
 1177  
                                 });
 1178  
 
 1179  0
                                 for (int i = 0; i < tuningctnt.length; i++) {
 1180  0
                                         getLog().debug(
 1181  
                                                         "Filename Found: " + tuningctnt[i].getName()
 1182  
                                                                         + " --- " + tuningctnt[i].getPath());
 1183  0
                                         Xpp3Dom includeDom = new Xpp3Dom("xi:include");
 1184  0
                                         includeDom.setAttribute("href", "modelincludes/"
 1185  
                                                         + tuningctnt[i].getName());
 1186  0
                                         tuningDom.addChild(includeDom);
 1187  
                                 }
 1188  
 
 1189  0
                                 FileUtilities.writeFile(tuningDom.toString().getBytes(),
 1190  
                                                 tuningFile.getAbsolutePath());
 1191  
 
 1192  
                         }
 1193  
 
 1194  0
                 } else {
 1195  0
                         getLog().info("Generating the container.xml old fashion style");
 1196  
                         // Finally we need to add MFContainer Elements for all the ESB
 1197  
                         // containers
 1198  0
                         for (final Xpp3Dom mfContainer : getMFContainerSettings()) {
 1199  0
                                 Map<String, Properties> map = containerMap
 1200  
                                                 .matchMFContainerSettings(mfContainer.getChild("Id")
 1201  
                                                                 .getValue());
 1202  0
                                 Properties props = map.get(mfContainer.getChild(
 1203  
                                                 "TargetContainer").getValue());
 1204  0
                                 String segment = (String) props.get("Segment");
 1205  0
                                 if (segment != null && !segment.equals("")) {
 1206  0
                                         final Xpp3Dom segmentElement = new Xpp3Dom("Segment");
 1207  0
                                         final Xpp3Dom segID = new Xpp3Dom("Id");
 1208  0
                                         segID.setValue(segment);
 1209  0
                                         segmentElement.addChild(segID);
 1210  0
                                         segmentElement.addChild(mfContainer);
 1211  0
                                         sdmDom.addChild(segmentElement);
 1212  0
                                 } else {
 1213  0
                                         sdmDom.addChild(mfContainer);
 1214  
                                 }
 1215  0
                         }
 1216  
                 }
 1217  
 
 1218  
                 // And spit out the modified SDM DOM
 1219  0
                 FileUtilities.writeFile(sdmDom.toString().getBytes(),
 1220  
                                 sdmFile.getAbsolutePath());
 1221  0
         }
 1222  
 
 1223  
         private void replaceMFESBContainers(Xpp3Dom sdmDom) {
 1224  
                 // Finding all the children and look for the include ref ...
 1225  
 
 1226  0
                 final Xpp3Dom[] elements = sdmDom.getChildren();
 1227  0
                 for (int i = 0; i < elements.length; i++) {
 1228  0
                         handleESBIncludeElements(elements[i]);
 1229  
                 }
 1230  0
         }
 1231  
 
 1232  
         private void handleESBIncludeElements(Xpp3Dom element) {
 1233  
                 // TODO Auto-generated method stub
 1234  
                 // First Print the Elements
 1235  0
                 getLog().debug("Element Name: " + element.getName());
 1236  0
                 if (element.getName().equals("xi:include")) {
 1237  0
                         String ref = element.getAttribute("href");
 1238  0
                         if (ref != null) {
 1239  0
                                 if (ref.startsWith("mfesbcontainer/")) {
 1240  0
                                         getLog().debug("Found a substitute");
 1241  0
                                         String mykey = ref.substring("mfesbcontainer/".length());
 1242  0
                                         getLog().debug("Finding the key value " + mykey);
 1243  
                                         
 1244  
                                         //Here we need to implement the new logic for ESB Containers
 1245  0
                                         if(! ESBtoMFMapping.containsKey(mykey)){
 1246  0
                                                 element.setAttribute("href", "ERROR: No Containers found for key " + mykey);
 1247  
                                         }
 1248  
                                         else{
 1249  
                                                 
 1250  0
                                                 boolean primFlag = true;
 1251  
                                                 //final Xpp3Dom merge = new Xpp3Dom("Segment");
 1252  0
                                                 ArrayList <String> esbContainers = ESBtoMFMapping.get(mykey);
 1253  0
                                                 for(String ctn : esbContainers){
 1254  0
                                                         getLog().info("Found Containers for " + mykey + " : " + ctn);
 1255  0
                                                         if(primFlag){
 1256  0
                                                                 element.setAttribute("href", "modelincludes/" + ctn + "_" + mykey + "_inc_model.xml");
 1257  0
                                                                 primFlag = false;
 1258  
                                                         } else{
 1259  
                                                                 //This is the second element ... as such we need to create a new element to be added to the domtree
 1260  
                                                                 //fasdf
 1261  0
                                                                 final Xpp3Dom newref = new Xpp3Dom("xi:include");
 1262  0
                                                                 newref.setAttribute("href",  "modelincludes/" + ctn + "_" + mykey + "_inc_model.xml");
 1263  0
                                                                 element.getParent().addChild(newref);
 1264  
                                                                 //Xpp3Dom.mergeXpp3Dom(element, newref,true);
 1265  
                                                         }
 1266  0
                                                         if (containerSubst == null) {
 1267  0
                                                                 containerSubst = new ArrayList<String>();
 1268  
                                                         }
 1269  0
                                                         containerSubst.add(ctn + "_" + mykey + "_inc_model.xml");
 1270  0
                                                 }
 1271  
                                         }
 1272  
                                         //Here the new logic is stopped ....
 1273  
                                         //Start Commenting section
 1274  
 //                                        String value = ContainerIncludeFiles.get(mykey);
 1275  
 //                                        element.setAttribute("href", "modelincludes/" + value);
 1276  
 //                                        if (containerSubst == null) {
 1277  
 //                                                containerSubst = new ArrayList<String>();
 1278  
 //                                        }
 1279  
 //                                        containerSubst.add(value);
 1280  
                                         //Stop Commenting sections
 1281  
                                 }
 1282  
                         }
 1283  
                 }
 1284  0
                 final Xpp3Dom[] elements = element.getChildren();
 1285  0
                 for (int i = 0; i < elements.length; i++) {
 1286  0
                         handleESBIncludeElements(elements[i]);
 1287  
                 }
 1288  0
         }
 1289  
 
 1290  
         private void setModelVersion(final Xpp3Dom sdmDom) {
 1291  
 
 1292  0
                 final Xpp3Dom modelNameElement = new Xpp3Dom("ModelName");
 1293  0
                 modelNameElement.setValue(project.getName());
 1294  
 
 1295  0
                 final Xpp3Dom versionElement = new Xpp3Dom("Version");
 1296  0
                 versionElement.setValue(project.getVersion());
 1297  
 
 1298  0
                 final Xpp3Dom descriptionElement = new Xpp3Dom("Description");
 1299  0
                 descriptionElement.setValue(project.getDescription());
 1300  
 
 1301  0
                 final Xpp3Dom generalConfig = sdmDom.getChild("GeneralConfig");
 1302  
 
 1303  
                 // Empty out the GeneralConfig element to guarantee the ordering of the
 1304  
                 // internal elements
 1305  0
                 while (generalConfig.getChildCount() != 0) {
 1306  0
                         generalConfig.removeChild(0);
 1307  
                 }
 1308  
 
 1309  0
                 generalConfig.addChild(modelNameElement);
 1310  0
                 generalConfig.addChild(versionElement);
 1311  0
                 generalConfig.addChild(descriptionElement);
 1312  0
         }
 1313  
 
 1314  
         private void assignQueues(final Xpp3Dom _sdmDom) throws Exception {
 1315  
 
 1316  
                 // Find a queues element in the SDM model and use the first one
 1317  
                 // we can find as the default.
 1318  0
                 Xpp3Dom defaultInsertPoint = _sdmDom.getChild("Queues");
 1319  0
                 if (defaultInsertPoint == null) {
 1320  
                         // TODO JT - does this really work as designed? the <Id> tag isn't
 1321  
                         // created, so queues don't get added to the broker. Perhap
 1322  
                         // the build should fail if <Queues> doesn't exist.
 1323  
                         // What about if <Queues><Id> doesn't exist??
 1324  0
                         _sdmDom.addChild(Xpp3Utils.createXpp3Dom("Queues"));
 1325  0
                         defaultInsertPoint = _sdmDom.getChild("Queues");
 1326  0
                         defaultInsertPoint
 1327  
                                         .addChild(Xpp3Utils.createXpp3Dom("Id", "Queues"));
 1328  
                 }
 1329  
 
 1330  
                 // Read and validate the QueueMappings.xml file
 1331  0
                 final QueueMappings queueMappings = readQueueMappings(defaultInsertPoint
 1332  
                                 .getChild("Id").getValue());
 1333  0
                 getLog().info(queueMappings.toString());
 1334  
 
 1335  
                 // Get all the Queue elements from the snippets and add them to the
 1336  
                 // Model.
 1337  
                 // We will also enter the Queue into the Sonic JNDI storage
 1338  0
                 List<Xpp3Dom> queues = Xpp3Utils.collectElements(
 1339  
                                 getDependencyDirectory(), "**/*.queues.snippet");
 1340  0
                 queues = Xpp3Utils.unifyXpp3List(queues, "/Queue/Id/text()");
 1341  0
                 getLog().info("Assigning " + queues.size() + " queues");
 1342  0
                 for (final Xpp3Dom queue : queues) {
 1343  0
                         final String queueName = queue.getChild("Id").getValue();
 1344  
 
 1345  
                         /**
 1346  
                          * Queues that have :: embedded are references to queues on another
 1347  
                          * broker. These should really not be present in queues snippets.
 1348  
                          */
 1349  0
                         if (queueName.contains("::")) {
 1350  0
                                 getLog().warn(
 1351  
                                                 String.format(
 1352  
                                                                 "Skipping queue '%s' because it references a remote queue.",
 1353  
                                                                 queueName));
 1354  0
                                 continue;
 1355  
                         }
 1356  
 
 1357  
                         // Find the queue mapping for this queue.
 1358  0
                         final QueueMapping queueMapping = queueMappings
 1359  
                                         .getMappingForQueue(queueName);
 1360  
 
 1361  
                         // Add the settings for this queue
 1362  0
                         if (queueMapping.getParameterSetId() != null) {
 1363  0
                                 queue.addChild(Xpp3Utils.createXpp3Dom("ParameterSet",
 1364  
                                                 queueMapping.getParameterSetId()));
 1365  
                         }
 1366  
                         // Removed for SDM 8.0 compliance this is no longer a valid element
 1367  
                         // NVR
 1368  
 
 1369  
                         // Adding backward compatibility ... does not break the cycle
 1370  
 //                        if (!sonicTarget.equals("8")) {
 1371  
 //                                queue.addChild(Xpp3Utils.createXpp3Dom("SuppressJNDI",
 1372  
 //                                                Boolean.toString(queueMapping.isSuppressJNDI())));
 1373  
 //                                if (!queueMapping.isSuppressJNDI()) {
 1374  
 //                                        queue.addChild(Xpp3Utils.createXpp3Dom("JNDIName",
 1375  
 //                                                        queueName));
 1376  
 //                                }
 1377  
 //                        }
 1378  
 
 1379  
                         // Find the element in the model and add the queue to it.
 1380  0
                         getLog().debug(
 1381  
                                         String.format("Adding queue %s to %s", queueName,
 1382  
                                                         queueMapping.getId()));
 1383  0
                         final Xpp3Dom queuesElement = findQueuesElementWithId(_sdmDom,
 1384  
                                         queueMapping.getId());
 1385  0
                         if (queuesElement == null) {
 1386  0
                                 throw new MojoExecutionException(
 1387  
                                                 String.format(
 1388  
                                                                 "Cannot add queue %s, queues element %s does not exist.",
 1389  
                                                                 queueName, queueMapping.getId()));
 1390  
                         }
 1391  0
                         queuesElement.addChild(queue);
 1392  0
                 }
 1393  0
         }
 1394  
 
 1395  
         private Xpp3Dom findQueuesElementWithId(final Xpp3Dom _sdmDom,
 1396  
                         final String _id) {
 1397  
 
 1398  0
                 final Xpp3Dom[] queuesElements = _sdmDom.getChildren("Queues");
 1399  0
                 for (final Xpp3Dom queuesElement : queuesElements) {
 1400  0
                         final Xpp3Dom idElement = queuesElement.getChild("Id");
 1401  0
                         if ((idElement != null) && _id.equals(idElement.getValue())) {
 1402  0
                                 return queuesElement;
 1403  
                         }
 1404  
                 }
 1405  
 
 1406  0
                 return null;
 1407  
         }
 1408  
 
 1409  
         private QueueMappings readQueueMappings(final String _defaultQueuesElement)
 1410  
                         throws MojoExecutionException {
 1411  
 
 1412  0
                 if ((queueMappingsFile == null) || !queueMappingsFile.canRead()) {
 1413  0
                         final String fileName = queueMappingsFile != null ? queueMappingsFile
 1414  
                                         .getPath() : "(null)";
 1415  0
                         getLog().warn(
 1416  
                                         "Unable to read file: " + fileName
 1417  
                                                         + ". Using default queue mapping.");
 1418  0
                         final QueueMappings mappings = new QueueMappings();
 1419  0
                         mappings.addMapping(new QueueMapping(_defaultQueuesElement, ".*"));
 1420  0
                         return mappings;
 1421  
                 }
 1422  
 
 1423  
                 // Validate the queue mapping file
 1424  
                 try {
 1425  0
                         final Schema schema = SchemaFactory.newInstance(
 1426  
                                         XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(
 1427  
                                         new StreamSource(getClass().getResourceAsStream(
 1428  
                                                         "/schema/QueueMappings.xsd")));
 1429  0
                         schema.newValidator().validate(new StreamSource(queueMappingsFile));
 1430  0
                 } catch (final SAXException e) {
 1431  0
                         throw new MojoExecutionException("Unable to validate "
 1432  
                                         + queueMappingsFile, e);
 1433  0
                 } catch (final IOException e) {
 1434  0
                         throw new MojoExecutionException("Unable to read "
 1435  
                                         + queueMappingsFile, e);
 1436  0
                 }
 1437  
 
 1438  
                 // Read the mapping file
 1439  
                 try {
 1440  0
                         final XmlStreamReader xmlReader = new XmlStreamReader(
 1441  
                                         queueMappingsFile);
 1442  0
                         final Xpp3Dom mappingsDom = Xpp3DomBuilder.build(xmlReader);
 1443  0
                         return new QueueMappings(mappingsDom, _defaultQueuesElement);
 1444  0
                 } catch (final IOException e) {
 1445  0
                         throw new MojoExecutionException("Unable to read "
 1446  
                                         + queueMappingsFile, e);
 1447  0
                 } catch (final XmlPullParserException e) {
 1448  0
                         throw new MojoExecutionException("Unable to parse "
 1449  
                                         + queueMappingsFile, e);
 1450  
                 }
 1451  
         }
 1452  
 
 1453  
         private Collection<Xpp3Dom> getMFContainerSettings() throws Exception {
 1454  
 
 1455  0
                 final List<Xpp3Dom> result = new ArrayList<Xpp3Dom>();
 1456  0
                 final HashMap<String, IMFContainer> mfContainerMap = new HashMap<String, IMFContainer>();
 1457  
 
 1458  0
                 getLog().info("Matching ESBContainers to MFContainers ...");
 1459  
 
 1460  0
                 for (final IESBContainer esbContainer : getESBContainerSettings(false)) {
 1461  0
                         getLog().info("Matching ESBContainer : " + esbContainer.getName());
 1462  0
                         final Map<String, Properties> matches = containerMap
 1463  
                                         .matchMFContainerSettings(esbContainer.getName());
 1464  
 
 1465  0
                         if (matches.size() == 0 && containerMap.isUseMFDefaultIncludes()) {
 1466  0
                                 getLog().warn(
 1467  
                                                 "No matching MFContainer setting found...will use default...");
 1468  
 
 1469  0
                                 final IContainerSetting defaultContainer = containerMap
 1470  
                                                 .getDefaultMFContainerSetting();
 1471  0
                                 final String mfName = defaultContainer.getName();
 1472  0
                                 String hosts = null;
 1473  0
                                 String autoStart = null;
 1474  
                                 try {
 1475  0
                                         hosts = defaultContainer.getIncludes().get(0)
 1476  
                                                         .getIncludeProperties().getProperty("Hosts");
 1477  0
                                 } catch (final RuntimeException e) {
 1478  0
                                         hosts = DEFAULT_SDM_HOST_VALUE;
 1479  0
                                 }
 1480  
                                 
 1481  
                                 try{
 1482  0
                                         autoStart = defaultContainer.getIncludes().get(0).getIncludeProperties().getProperty("AutoStart");
 1483  0
                                         getLog().warn("TBO ----- GETTING THE AUTOSTART: - Value: " + autoStart);
 1484  0
                                 } catch (final RuntimeException e){
 1485  0
                                         getLog().warn("TBO -------- No value found for Autostart Element for the ESB Container");
 1486  0
                                 }
 1487  
                                 
 1488  0
                                 getLog().debug("FELICE is my Mate");
 1489  
                                 
 1490  0
                                 final Properties props = new Properties();
 1491  0
                                 props.put("Hosts", hosts);
 1492  0
                                 props.put("bootContainer",
 1493  
                                                 Boolean.toString(defaultContainer.isBootContainer()));
 1494  0
                                 props.put("backupContainer",
 1495  
                                                 defaultContainer.getBackupContainer());
 1496  0
                                 props.put("clone", Boolean.toString(defaultContainer.isClone()));
 1497  0
                                 props.put("Segment", defaultContainer.getSegment());
 1498  0
                                 props.put("logDirectory", defaultContainer.getLogDirectory());
 1499  0
                                 props.put("containerParameterSet",
 1500  
                                                 defaultContainer.getContainerParameterSet());
 1501  0
                                 matches.put(mfName, props);
 1502  
                         }
 1503  
 
 1504  0
                         for (final Iterator<String> it = matches.keySet().iterator(); it
 1505  0
                                         .hasNext();) {
 1506  0
                                 final String mfContainerName = it.next();
 1507  0
                                 getLog().info(
 1508  
                                                 "Adding ESBContainer " + esbContainer.getName()
 1509  
                                                                 + " to MFContainer " + mfContainerName);
 1510  0
                                 addESBContainerToMF(mfContainerMap, mfContainerName,
 1511  
                                                 esbContainer.getName(), matches.get(mfContainerName));
 1512  0
                         }
 1513  0
                 }
 1514  
 
 1515  0
                 for (final IMFContainer container : mfContainerMap.values()) {
 1516  
                         //if (sonicTarget.equals("8")) {
 1517  0
                                 result.add(container.removeNamespaces8());
 1518  
 //                        } else {
 1519  
 //                                result.add(container.removeNamespaces());
 1520  
 //                        }
 1521  0
                 }
 1522  
 
 1523  0
                 return result;
 1524  
         }
 1525  
 
 1526  
         @SuppressWarnings({ "unchecked", "rawtypes" })
 1527  
         private ArrayList[] readESBContainerConfiguration() {
 1528  
 
 1529  0
                 XmlStreamReader esbContainerReader = null;
 1530  0
                 final ArrayList<DefaultContainerSetting> esbContainerList = new ArrayList<DefaultContainerSetting>();
 1531  0
                 final ArrayList<DefaultContainerSetting> mfContainerList = new ArrayList<DefaultContainerSetting>();
 1532  
 
 1533  0
                 getLog().debug("TBO ----- Inserting ESBContainer Configuration ------");
 1534  0
                 getLog().debug("TBO ----- Check File " + containerPropFile);
 1535  0
                 getLog().debug("TBO ----- Base-Dir: " + project.getBasedir());
 1536  
 
 1537  0
                 if (containerPropFile.exists()) {
 1538  
                         try {
 1539  0
                                 getLog().debug("Tailoring the ESBContainers");
 1540  0
                                 esbContainerReader = new XmlStreamReader(containerPropFile);
 1541  0
                                 final Xpp3Dom esbContainerDom = Xpp3DomBuilder
 1542  
                                                 .build(esbContainerReader);
 1543  0
                                 DefaultContainerSetting ecsESB = null;
 1544  0
                                 for (final Xpp3Dom child : esbContainerDom
 1545  
                                                 .getChild("containerMap")
 1546  
                                                 .getChild("esbContainerSettings").getChildren()) {
 1547  0
                                         getLog().debug(
 1548  
                                                         "TBO ----- Configuration Element: "
 1549  
                                                                         + child.getName());
 1550  0
                                         final String name = child.getChild("name").getValue();
 1551  0
                                         getLog().debug("TBO ----- Name of the Container: " + name);
 1552  
 
 1553  
                                         String busConnection;
 1554  
                                         String routingHttpConnection;
 1555  
                                         String enablePayloadCapture;
 1556  
                                         String useForLookingGlass;
 1557  
                                         String useIntraContainerMessaging;
 1558  
 
 1559  0
                                         if (child.getChild("busConnection") != null) {
 1560  0
                                                 busConnection = child.getChild("busConnection")
 1561  
                                                                 .getValue();
 1562  
                                         } else {
 1563  0
                                                 busConnection = null;
 1564  
                                         }
 1565  
 
 1566  0
                                         if (child.getChild("routingHttpConnection") != null) {
 1567  0
                                                 routingHttpConnection = child.getChild(
 1568  
                                                                 "routingHttpConnection").getValue();
 1569  
                                         } else {
 1570  0
                                                 routingHttpConnection = null;
 1571  
                                         }
 1572  
 
 1573  0
                                         if (child.getChild("enablePayloadCapture") != null) {
 1574  0
                                                 enablePayloadCapture = child.getChild(
 1575  
                                                                 "enablePayloadCapture").getValue();
 1576  
                                         } else {
 1577  0
                                                 enablePayloadCapture = null;
 1578  
                                         }
 1579  
 
 1580  0
                                         if (child.getChild("useForLookingGlass") != null) {
 1581  0
                                                 useForLookingGlass = child.getChild(
 1582  
                                                                 "useForLookingGlass").getValue();
 1583  
                                         } else {
 1584  0
                                                 useForLookingGlass = null;
 1585  
                                         }
 1586  
 
 1587  0
                                         if (child.getChild("useIntraContainerMessaging") != null) {
 1588  0
                                                 useIntraContainerMessaging = child.getChild(
 1589  
                                                                 "useIntraContainerMessaging").getValue();
 1590  
                                         } else {
 1591  0
                                                 useIntraContainerMessaging = null;
 1592  
                                         }
 1593  
 
 1594  0
                                         ecsESB = new DefaultContainerSetting(name);
 1595  0
                                         ecsESB.setBusConnection(busConnection);
 1596  0
                                         ecsESB.setEnablePayloadCapture(enablePayloadCapture);
 1597  0
                                         ecsESB.setRoutingHttpConnection(routingHttpConnection);
 1598  0
                                         ecsESB.setUseForLookingGlass(useForLookingGlass);
 1599  0
                                         ecsESB.setUseIntraContainerMessaging(useIntraContainerMessaging);
 1600  
 
 1601  0
                                         if (child.getChild("includes") != null) {
 1602  0
                                                 for (final Xpp3Dom includepatterns : child.getChild(
 1603  
                                                                 "includes").getChildren()) {
 1604  0
                                                         final String pattern = includepatterns.getChild(
 1605  
                                                                         "pattern").getValue();
 1606  0
                                                         getLog().debug("TBO ----- Pattern: " + pattern);
 1607  0
                                                         final DefaultIncludePattern expat = new DefaultIncludePattern(
 1608  
                                                                         pattern);
 1609  
 
 1610  0
                                                         for (final Xpp3Dom properties : includepatterns
 1611  
                                                                         .getChild("includeProperties")
 1612  
                                                                         .getChildren()) {
 1613  0
                                                                 final String propname = properties.getChild(
 1614  
                                                                                 "name").getValue();
 1615  0
                                                                 final String propvalue = properties.getChild(
 1616  
                                                                                 "value").getValue();
 1617  0
                                                                 getLog().debug(
 1618  
                                                                                 "TBO ---- Property [" + propname
 1619  
                                                                                                 + "] Value [" + propvalue + "]");
 1620  0
                                                                 expat.getIncludeProperties().put(propname,
 1621  
                                                                                 propvalue);
 1622  
                                                         }
 1623  
 
 1624  0
                                                         ecsESB.getIncludes().add(expat);
 1625  
                                                 }
 1626  
                                         }
 1627  0
                                         esbContainerList.add(ecsESB);
 1628  
                                 }
 1629  
 
 1630  0
                                 DefaultContainerSetting ecsMF = null;
 1631  0
                                 for (final Xpp3Dom child : esbContainerDom
 1632  
                                                 .getChild("containerMap")
 1633  
                                                 .getChild("mfContainerSettings").getChildren()) {
 1634  0
                                         getLog().debug(
 1635  
                                                         "TBO MF----- Configuration Element: "
 1636  
                                                                         + child.getName());
 1637  0
                                         final String name = child.getChild("name").getValue();
 1638  0
                                         getLog().debug("TBO MF----- Name of the Container: " + name);
 1639  0
                                         ecsMF = new DefaultContainerSetting(name);
 1640  
 
 1641  0
                                         if (child.getChild("backupContainer") != null) {
 1642  0
                                                 String backupContainer = child.getChild(
 1643  
                                                                 "backupContainer").getValue();
 1644  0
                                                 if (StringUtils.isNotEmpty(backupContainer)) {
 1645  0
                                                         ecsMF.setBackupContainer(backupContainer);
 1646  
                                                 }
 1647  0
                                                 getLog().debug(
 1648  
                                                                 "GDR MF----- Backup Container: "
 1649  
                                                                                 + backupContainer);
 1650  
                                         }
 1651  
 
 1652  0
                                         if (child.getChild("bootContainer") != null) {
 1653  0
                                                 final boolean bootContainer = Boolean
 1654  
                                                                 .parseBoolean(child.getChild("bootContainer")
 1655  
                                                                                 .getValue());
 1656  0
                                                 ecsMF.setBootContainer(bootContainer);
 1657  0
                                                 getLog().debug(
 1658  
                                                                 "TBO MF----- Boot Container: " + bootContainer);
 1659  
                                         }
 1660  
 
 1661  0
                                         if (child.getChild("clone") != null) {
 1662  0
                                                 final boolean clone = Boolean.parseBoolean(child
 1663  
                                                                 .getChild("clone").getValue());
 1664  0
                                                 ecsMF.setClone(clone);
 1665  0
                                                 getLog().debug("TBO MF----- clone value: " + clone);
 1666  0
                                         } else {
 1667  0
                                                 ecsMF.setClone(false);
 1668  
                                         }
 1669  
 
 1670  0
                                         if (child.getChild("LogDirectory") != null) {
 1671  0
                                                 String ldir = child.getChild("LogDirectory").getValue();
 1672  0
                                                 if (StringUtils.isNotEmpty(ldir)) {
 1673  0
                                                         ecsMF.setLogDirectory(ldir);
 1674  
                                                 }
 1675  0
                                                 getLog().debug("GDR MF----- Segment: " + ldir);
 1676  
                                         }
 1677  
 
 1678  0
                                         if (child.getChild("ContainerParameterSet") != null) {
 1679  0
                                                 String cps = child.getChild("ContainerParameterSet")
 1680  
                                                                 .getValue();
 1681  0
                                                 if (StringUtils.isNotEmpty(cps)) {
 1682  0
                                                         ecsMF.setContainerParameterSet(cps);
 1683  
                                                 }
 1684  0
                                                 getLog().debug("GDR MF----- Segment: " + cps);
 1685  
                                         }
 1686  
 
 1687  0
                                         if (child.getChild("Segment") != null) {
 1688  0
                                                 String Segment = child.getChild("Segment").getValue();
 1689  0
                                                 if (StringUtils.isNotEmpty(Segment)) {
 1690  0
                                                         ecsMF.setSegment(Segment);
 1691  
                                                 }
 1692  0
                                                 getLog().debug("GDR MF----- Segment: " + Segment);
 1693  
                                         }
 1694  
 
 1695  0
                                         if (child.getChild("includes") != null) {
 1696  
 
 1697  0
                                                 for (final Xpp3Dom includepatterns : child.getChild(
 1698  
                                                                 "includes").getChildren()) {
 1699  0
                                                         final String pattern = includepatterns.getChild(
 1700  
                                                                         "pattern").getValue();
 1701  0
                                                         getLog().debug("TBO MF----- Pattern: " + pattern);
 1702  0
                                                         final DefaultIncludePattern expat = new DefaultIncludePattern(
 1703  
                                                                         pattern);
 1704  0
                                                         for (final Xpp3Dom properties : includepatterns
 1705  
                                                                         .getChild("includeProperties")
 1706  
                                                                         .getChildren()) {
 1707  0
                                                                 final String propname = properties.getChild(
 1708  
                                                                                 "name").getValue();
 1709  0
                                                                 final String propvalue = properties.getChild(
 1710  
                                                                                 "value").getValue();
 1711  0
                                                                 getLog().debug(
 1712  
                                                                                 "TBO MF---- Property [" + propname
 1713  
                                                                                                 + "] Value [" + propvalue + "]");
 1714  0
                                                                 final Properties props = expat
 1715  
                                                                                 .getIncludeProperties();
 1716  0
                                                                 props.put(propname, propvalue);
 1717  
                                                                 
 1718  
                                                         }
 1719  0
                                                         ecsMF.getIncludes().add(expat);
 1720  
                                                 }
 1721  
                                         }
 1722  0
                                         mfContainerList.add(ecsMF);
 1723  
                                 }
 1724  0
                         } catch (final Exception e) {
 1725  0
                                 getLog().debug(
 1726  
                                                 "Problem encountered with the formatting of the file: "
 1727  
                                                                 + e.getMessage());
 1728  0
                                 e.printStackTrace();
 1729  0
                         }
 1730  
 
 1731  0
                         printContainerTailoringContent("ESB", esbContainerList);
 1732  0
                         printContainerTailoringContent("MF", mfContainerList);
 1733  
                 } else {
 1734  0
                         getLog().info("No Tailoring File Found");
 1735  
                 }
 1736  
 
 1737  0
                 final ArrayList[] ret = new ArrayList[2];
 1738  0
                 ret[0] = esbContainerList;
 1739  0
                 ret[1] = mfContainerList;
 1740  
 
 1741  0
                 return ret;
 1742  
         }
 1743  
 
 1744  
         @SuppressWarnings("rawtypes")
 1745  
         private void printContainerTailoringContent(final String _mode,
 1746  
                         final ArrayList<DefaultContainerSetting> _containers) {
 1747  
 
 1748  0
                 for (final Object cs : _containers) {
 1749  0
                         getLog().debug("MODE: " + _mode);
 1750  0
                         getLog().debug(
 1751  
                                         "   Container Name:   "
 1752  
                                                         + ((DefaultContainerSetting) cs).getName());
 1753  0
                         getLog().debug(
 1754  
                                         "   Boot Container:   "
 1755  
                                                         + ((DefaultContainerSetting) cs).isBootContainer());
 1756  0
                         getLog().debug(
 1757  
                                         "   Backup Container: "
 1758  
                                                         + ((DefaultContainerSetting) cs)
 1759  
                                                                         .getBackupContainer());
 1760  0
                         getLog().debug(
 1761  
                                         "   Clone:            "
 1762  
                                                         + ((DefaultContainerSetting) cs).isClone());
 1763  0
                         getLog().debug(
 1764  
                                         "   Segment           "
 1765  
                                                         + ((DefaultContainerSetting) cs).getSegment());
 1766  
 
 1767  0
                         final List<IIncludePattern> includes = ((DefaultContainerSetting) cs)
 1768  
                                         .getIncludes();
 1769  
                         // Iterator it = includes.iterator();
 1770  0
                         for (final IIncludePattern includePattern : includes) {
 1771  0
                                 getLog().debug(
 1772  
                                                 "     Include Pattern: " + includePattern.getPattern());
 1773  0
                                 final Properties props = includePattern.getIncludeProperties();
 1774  0
                                 final Iterator it = props.keySet().iterator();
 1775  0
                                 while (it.hasNext()) {
 1776  0
                                         final String key = (String) it.next();
 1777  0
                                         final String value = props.getProperty(key);
 1778  0
                                         getLog().debug(
 1779  
                                                         "          Key: " + key + " - Value: " + value);
 1780  
 
 1781  0
                                 }
 1782  0
                         }
 1783  0
                 }
 1784  0
         }
 1785  
 
 1786  
         private void addESBContainerToMF(
 1787  
                         final HashMap<String, IMFContainer> _containerMap,
 1788  
                         final String _mfContainerName, final String _esbContainerName,
 1789  
                         final Properties _includeProperties) {
 1790  
 
 1791  0
                 IMFContainer mfContainer = _containerMap.get(_mfContainerName);
 1792  0
                 final String hosts = _includeProperties.getProperty("Hosts",
 1793  
                                 DEFAULT_SDM_HOST_VALUE);
 1794  0
                 if (mfContainer == null) {
 1795  0
                         final boolean bootContainer = Boolean.valueOf(_includeProperties
 1796  
                                         .getProperty("bootContainer"));
 1797  0
                         final String backupContainer = _includeProperties
 1798  
                                         .getProperty("backupContainer");
 1799  0
                         final boolean clone = Boolean.valueOf(_includeProperties
 1800  
                                         .getProperty("clone"));
 1801  0
                         final String Segment = _includeProperties.getProperty("Segment");
 1802  0
                         getLog().debug(
 1803  
                                         "Creating configuration - ESBContainer: "
 1804  
                                                         + _esbContainerName + "  MFContainer: "
 1805  
                                                         + _mfContainerName + "  hosts: " + hosts
 1806  
                                                         + "  Clone: " + clone + "  Bootcontainer: "
 1807  
                                                         + bootContainer + "  backup: " + backupContainer);
 1808  0
                         mfContainer = new DefaultMFContainer(_esbContainerName,
 1809  
                                         _mfContainerName, bootContainer, backupContainer, hosts,
 1810  
                                         clone);
 1811  0
                         _containerMap.put(_mfContainerName, mfContainer);
 1812  0
                 } else {
 1813  0
                         ((DefaultMFContainer) mfContainer).addHosts(hosts);
 1814  
                 }
 1815  0
         }
 1816  
 
 1817  
         /**
 1818  
          * Generate a xar file that contains the resouces that are stored in the SDM
 1819  
          * src tree and the ESB container definitions that are required to host all
 1820  
          * the ESB Service and Process instances defined in all the dependent xar
 1821  
          * archives.
 1822  
          */
 1823  
         private void generateESBContainerIncludes() throws Exception {
 1824  
 
 1825  0
                 getLog().info("Creating ESB Includes for Model.xml and Tuning.xml");
 1826  0
                 ContainerIncludeFiles = new HashMap<String, String>();
 1827  
 
 1828  
                 // final String inlineDirectory = getInlineESBDir();
 1829  
                 //
 1830  
                 // new File(inlineDirectory, "ESBSnippets").mkdirs();
 1831  
 
 1832  0
                 for (final IESBContainer container : getESBContainerSettings(true)) {
 1833  0
                         if (containerMap != null) {
 1834  
 //                                getLog().error("TBO _________ In the container loop");
 1835  0
                                 generateModelTuningESBIncludes(container);
 1836  
 
 1837  
                         } else {
 1838  0
                                 getLog().debug(
 1839  
                                                 "TBO --------- ContainerMap: No Such object present: Need to find another way ... aborting here");
 1840  
                         }
 1841  0
                 }
 1842  0
         }
 1843  
 
 1844  
         @SuppressWarnings("rawtypes")
 1845  
         private void generateModelTuningESBIncludes(IESBContainer container) {
 1846  
                 // Finding out in how many MF definitions this Container is present:
 1847  
                 //ESBtoMFMapping = new HashMap<String,ArrayList<String>>(); 
 1848  0
                 final Map<String, Properties> matches = containerMap
 1849  
                                 .matchMFContainerSettings(container.getName());
 1850  
 //                getLog().error("Matched Map contains " + matches.size() + " Entries");
 1851  0
                 Iterator<String> itMF = matches.keySet().iterator();
 1852  
                 // Generating Model Inlucde File for both MF Containers.
 1853  
 
 1854  
                 // Writing the files
 1855  
                 // Creating Directory
 1856  0
                 File includeDir = new File(getTargetSDMDir() + File.separator
 1857  
                                 + "modelincludes");
 1858  0
                 if (!includeDir.exists()) {
 1859  0
                         includeDir.mkdirs();
 1860  
                 }
 1861  
 
 1862  0
                 Xpp3Dom element = null;
 1863  0
                 while (itMF.hasNext()) {
 1864  0
                         String key = itMF.next();
 1865  0
                         if(!ESBtoMFMapping.containsKey(key)){
 1866  0
                                 ArrayList <String> esbList = new ArrayList<String>();
 1867  0
                                 esbList.add(container.getName());
 1868  0
                                 ESBtoMFMapping.put(key, esbList);
 1869  0
                         }
 1870  
                         else{
 1871  0
                                 if(!ESBtoMFMapping.get(key).contains(container.getName())){
 1872  0
                                         ESBtoMFMapping.get(key).add(container.getName());
 1873  
                                 }
 1874  
                         }
 1875  
 //                        getLog().error("TBO _______ " + key + " iteration " + container.getName());
 1876  0
                         Properties props = matches.get(key);
 1877  
 
 1878  0
                         final Xpp3Dom esbContainer = new Xpp3Dom("ESBContainer");
 1879  0
                         final Xpp3Dom esbservices = new Xpp3Dom("Services");
 1880  0
                         final Xpp3Dom esbprocesses = new Xpp3Dom("Processes");
 1881  0
                         final Xpp3Dom logicalhosts = new Xpp3Dom("LogicalHosts");
 1882  0
                         final Xpp3Dom segmentElement = new Xpp3Dom("Segment");
 1883  0
                         final Xpp3Dom segID = new Xpp3Dom("Id");
 1884  
 
 1885  0
                         if (props.getProperty("Hosts") != null
 1886  
                                         && !props.getProperty("Hosts").equals("")) {
 1887  0
                                 SmartTokenizer st = new SmartTokenizer(
 1888  
                                                 props.getProperty("Hosts"), ",");
 1889  
 
 1890  0
                                 while (st.hasMoreTokens()) {
 1891  0
                                         element = new Xpp3Dom("LogicalHost");
 1892  0
                                         element.setValue(st.nextToken());
 1893  0
                                         logicalhosts.addChild(element);
 1894  
                                 }
 1895  
 
 1896  0
                                 esbContainer.addChild(logicalhosts);
 1897  
                         }
 1898  
                         Xpp3Dom svc;
 1899  
 
 1900  0
                         element = new Xpp3Dom("Id");
 1901  0
                         element.setValue(container.getName());
 1902  0
                         esbContainer.addChild(element);
 1903  
 
 1904  0
                         element = new Xpp3Dom("ParameterSet");
 1905  0
                         element.setValue(container.getName() + "_ContainerParameter");
 1906  0
                         esbContainer.addChild(element);
 1907  
 
 1908  0
                         element = new Xpp3Dom("TargetContainer");
 1909  0
                         element.setValue(key);
 1910  0
                         esbContainer.addChild(element);
 1911  
 
 1912  
                         // System.out.println("TIM TIM TIM --- IS THIS MESSAGE PRINTING?");
 1913  
 
 1914  0
                         if (props.getProperty("containerParameterSet") != null
 1915  
                                         && !props.getProperty("containerParameterSet").equals("")) {
 1916  0
                                 element = new Xpp3Dom("ContainerParameterSet");
 1917  0
                                 element.setValue(props.getProperty("containerParameterSet"));
 1918  0
                                 esbContainer.addChild(element);
 1919  
                         }
 1920  
 
 1921  0
                         if (props.getProperty("logDirectory") != null
 1922  
                                         && !props.getProperty("logDirectory").equals("")) {
 1923  0
                                 element = new Xpp3Dom("LogDirectory");
 1924  0
                                 element.setValue(props.getProperty("logDirectory"));
 1925  0
                                 esbContainer.addChild(element);
 1926  
 
 1927  
                         }
 1928  
                         
 1929  0
                         getLog().debug("AUTOSTART: " + props.getProperty("AutoStart"));
 1930  
                         
 1931  0
                         String autoStart = props.getProperty("AutoStart");
 1932  0
                         if(autoStart != null){
 1933  0
                                 element = new Xpp3Dom("AutoStart");
 1934  0
                                 element.setValue(autoStart);
 1935  0
                                 esbContainer.addChild(element);
 1936  
                         }
 1937  
                         
 1938  
 
 1939  0
                         element = new Xpp3Dom("BootContainer");
 1940  0
                         element.setValue(props.getProperty("bootContainer"));
 1941  0
                         esbContainer.addChild(element);
 1942  
 
 1943  0
                         if (props.getProperty("backupContainer") != null
 1944  
                                         && !props.getProperty("backupContainer").equals("")) {
 1945  
 
 1946  0
                                 element = new Xpp3Dom("BackupContainer");
 1947  0
                                 element.setValue(props.getProperty("backupContainer"));
 1948  0
                                 esbContainer.addChild(element);
 1949  
 
 1950  0
                                 element = new Xpp3Dom("Clone");
 1951  0
                                 element.setValue(props.getProperty("clone"));
 1952  0
                                 esbContainer.addChild(element);
 1953  
                         }
 1954  
 
 1955  0
                         DataObject services = container.getDataObject().getDataObject(
 1956  
                                         "services");
 1957  0
                         List svclist = services.getList("serviceApplication");
 1958  0
                         for (Iterator svcs = svclist.iterator(); svcs.hasNext();) {
 1959  0
                                 DataObject instance = (DataObject) svcs.next();
 1960  
 
 1961  0
                                 if (instance.getString("service_ref").equals("PROCESS")) {
 1962  0
                                         svc = new Xpp3Dom("Process");
 1963  0
                                         esbprocesses.addChild(svc);
 1964  
                                 } else {
 1965  0
                                         svc = new Xpp3Dom("Service");
 1966  0
                                         esbservices.addChild(svc);
 1967  
                                 }
 1968  
 
 1969  0
                                 element = new Xpp3Dom("Id");
 1970  0
                                 element.setValue(instance.getString("name"));
 1971  0
                                 svc.addChild(element);
 1972  
 
 1973  0
                                 element = new Xpp3Dom("ListenerCount");
 1974  0
                                 element.setValue(instance.getString("Instances"));
 1975  0
                                 svc.addChild(element);
 1976  
 
 1977  0
                                 element = new Xpp3Dom("StartupPriorityLevel");
 1978  0
                                 element.setValue(instance.getString("startupPriorityLevel"));
 1979  0
                                 svc.addChild(element);
 1980  
 
 1981  0
                                 element = new Xpp3Dom("ConcurrentCalls");
 1982  0
                                 element.setValue(instance.getString("ConcurrentCalls"));
 1983  0
                                 svc.addChild(element);
 1984  0
                         }
 1985  
 
 1986  0
                         esbContainer.addChild(esbservices);
 1987  0
                         esbContainer.addChild(esbprocesses);
 1988  
 
 1989  0
                         if (props.getProperty("Segment") != null
 1990  
                                         && !props.getProperty("Segment").equals("")) {
 1991  0
                                 segID.setValue(props.getProperty("Segment"));
 1992  0
                                 segmentElement.addChild(segID);
 1993  0
                                 segmentElement.addChild(esbContainer);
 1994  0
                                 FileUtilities.writeFile(segmentElement.toString().getBytes(),
 1995  
                                                 new File(includeDir, container.getName() + "_" + key
 1996  
                                                                 + "_inc_model.xml"));
 1997  
                         } else {
 1998  0
                                 FileUtilities.writeFile(esbContainer.toString().getBytes(),
 1999  
                                                 new File(includeDir, container.getName() + "_" + key
 2000  
                                                                 + "_inc_model.xml"));
 2001  
                         }
 2002  
 
 2003  
                         // FileUtilities.writeFile(segmentElement.toString().getBytes(),
 2004  
                         // new File(includeDir, container.getName() + "_" + key
 2005  
                         // + "_inc_model.xml"));
 2006  0
                         this.ContainerIncludeFiles.put(key,
 2007  
                                         new File(includeDir, container.getName() + "_" + key
 2008  
                                                         + "_inc_model.xml").getName());
 2009  0
                 }
 2010  
                 // generating the includes for the Model
 2011  
 
 2012  
                 // generating the includes for the Tuning
 2013  0
                 final Xpp3Dom parameterSet = new Xpp3Dom("ESBContainerParameters");
 2014  0
                 parameterSet.setAttribute("id", container.getName()
 2015  
                                 + "_ContainerParameter");
 2016  0
                 final Xpp3Dom resources = new Xpp3Dom("Resources");
 2017  0
                 final Xpp3Dom actoinalConfig = new Xpp3Dom("ActionalConfig");
 2018  
 
 2019  0
                 element = new Xpp3Dom("JmsDefaultConnection");
 2020  0
                 element.setValue(container.getDataObject().getString("BusConnection"));
 2021  0
                 parameterSet.addChild(element);
 2022  
 
 2023  0
                 element = new Xpp3Dom("HttpDefaultConnection");
 2024  0
                 element.setValue(container.getDataObject().getString(
 2025  
                                 "RoutingHttpConnection"));
 2026  0
                 parameterSet.addChild(element);
 2027  
 
 2028  0
                 element = new Xpp3Dom("UseIntraContainer");
 2029  0
                 element.setValue(container.getDataObject().getString(
 2030  
                                 "useIntraContainerMessaging"));
 2031  0
                 parameterSet.addChild(element);
 2032  
 
 2033  0
                 element = new Xpp3Dom("InterceptorEnabled");
 2034  0
                 element.setValue(container.getDataObject().getString(
 2035  
                                 "useForLookingGlass"));
 2036  0
                 actoinalConfig.addChild(element);
 2037  
 
 2038  0
                 element = new Xpp3Dom("PayloadReportingEnabled");
 2039  0
                 element.setValue(container.getDataObject().getString(
 2040  
                                 "enablePayloadCapture"));
 2041  0
                 actoinalConfig.addChild(element);
 2042  
 
 2043  0
                 parameterSet.addChild(actoinalConfig);
 2044  
 
 2045  0
                 if (container.getDataObject().getString("CLASSPATH") != null
 2046  
                                 && !container.getDataObject().getString("CLASSPATH").equals("")) {
 2047  0
                         element = new Xpp3Dom("Classpath");
 2048  0
                         element.setValue(container.getDataObject().getString("CLASSPATH"));
 2049  0
                         resources.addChild(element);
 2050  
 
 2051  0
                         parameterSet.addChild(resources);
 2052  
 
 2053  0
                 } else if (eSBContainerClasspathOverride != null
 2054  
                                 && !eSBContainerClasspathOverride.equals("")) {
 2055  0
                         element = new Xpp3Dom("Classpath");
 2056  0
                         element.setValue(eSBContainerClasspathOverride);
 2057  0
                         resources.addChild(element);
 2058  
 
 2059  0
                         parameterSet.addChild(resources);
 2060  
                 }
 2061  
 
 2062  0
                 FileUtilities.writeFile(parameterSet.toString().getBytes(), new File(
 2063  
                                 includeDir, container.getName() + "_inc_tuning.xml"));
 2064  0
         }
 2065  
 
 2066  
         // <ESBContainerParameters id="containerset">
 2067  
         // <HttpDefaultConnection></HttpDefaultConnection>
 2068  
         // <ActionalConfig></ActionalConfig>
 2069  
         // <JmsDefaultConnection></JmsDefaultConnection>
 2070  
         // <Log4jConfiguration></Log4jConfiguration>
 2071  
         // <Resources>
 2072  
         // <ArchiveName></ArchiveName>
 2073  
         // <Classpath></Classpath>
 2074  
         // </Resources>
 2075  
         // <SpringConfiguration></SpringConfiguration>
 2076  
         // <UseIntraContainer>true</UseIntraContainer>
 2077  
         // </ESBContainerParameters>
 2078  
 
 2079  
         // containerMap.getDefaultESBContainerSetting();
 2080  
         // containerMap.getDefaultMFContainerSetting();
 2081  
         // containerMap.getESBContainerSettings();
 2082  
         // containerMap.getMFContainerSettings();
 2083  
         // Map propertiesMap =
 2084  
         // containerMap.matchESBContainerSettings(container.getName());
 2085  
         // if(propertiesMap != null){
 2086  
         // for (Iterator it = propertiesMap.keySet().iterator(); it.hasNext();) {
 2087  
         // String name = (String) it.next();
 2088  
         // getLog().info("TBO --------- Found a KEY: " + name);
 2089  
         // Properties props = (Properties) propertiesMap.get(name);
 2090  
         // getLog().info("TBO ---------- Properties: " + props.toString());
 2091  
         // }
 2092  
         // }
 2093  
 
 2094  
         /**
 2095  
          * Generate a xar file that contains the resouces that are stored in the SDM
 2096  
          * src tree and the ESB container definitions that are required to host all
 2097  
          * the ESB Service and Process instances defined in all the dependent xar
 2098  
          * archives.
 2099  
          */
 2100  
         private void generateContainerXar() throws Exception {
 2101  
 
 2102  0
                 getLog().info("Creating xar archive for the ESBContainers");
 2103  
 
 2104  0
                 final String xarDirectory = getPackageXarDir();
 2105  
 
 2106  0
                 new File(xarDirectory, "ESB/Containers").mkdirs();
 2107  0
                 new File(xarDirectory, "SonicFS").mkdirs();
 2108  
                 // Copy JAR dependencies which are defined on the SDM level
 2109  0
                 copyJarFiles();
 2110  
 
 2111  0
                 for (final IESBContainer container : getESBContainerSettings(false)) {
 2112  0
                         final String fileName = container.getName() + ".xml";
 2113  0
                         FileUtilities.writeFile(container.toString().getBytes(),
 2114  
                                         xarDirectory + "/ESB/Containers/" + fileName);
 2115  0
                 }
 2116  
 
 2117  
                 // TODO: Copy into workspace location and create a tailoring ??
 2118  
 
 2119  0
                 FileUtilities.copyFiles(project.getBasedir() + "/src/main/resources",
 2120  
                                 xarDirectory, "**/*", null, "SonicFS/" + getSonicFSProjectDir()
 2121  
                                                 + "/resources", false);
 2122  
 
 2123  0
                 final File xarFile = new File(getTargetSDMDir(), "xars/"
 2124  
                                 + getFinalAssemblyBaseName() + ".xar");
 2125  
 
 2126  0
                 final Archiver archiver = archiverManager.getArchiver("zip");
 2127  0
                 archiver.setDestFile(xarFile);
 2128  0
                 archiver.addDirectory(new File(xarDirectory));
 2129  0
                 archiver.createArchive();
 2130  
 
 2131  0
         }
 2132  
 
 2133  
         @SuppressWarnings({ "unchecked", "rawtypes" })
 2134  
         private Collection<IESBContainer> getESBContainerSettings(boolean inLine)
 2135  
                         throws Exception {
 2136  
 
 2137  0
                 if (esbContainers == null) {
 2138  0
                         final ArrayList[] configs = readESBContainerConfiguration();
 2139  0
                         getLog().debug("Adding External ESB Configuration - Start");
 2140  
                         // Adding external Configuration
 2141  0
                         final ArrayList<IContainerSetting> esbConfig = configs[0];
 2142  0
                         for (final Iterator<IContainerSetting> iterator = esbConfig
 2143  0
                                         .iterator(); iterator.hasNext();) {
 2144  0
                                 final IContainerSetting container = iterator.next();
 2145  
 
 2146  
                                 // Remove and re-add the element to make sure the newer setting
 2147  
                                 // (from ContainerProperties.xml) overrides any settings in
 2148  
                                 // pom.xml
 2149  0
                                 containerMap.getESBContainerSettings().remove(container);
 2150  0
                                 containerMap.getESBContainerSettings().add(container);
 2151  0
                         }
 2152  
 
 2153  0
                         getLog().info(
 2154  
                                         "ESB container mapping: "
 2155  
                                                         + containerMap.getESBContainerSettings());
 2156  
 
 2157  0
                         final ArrayList<IContainerSetting> mfConfig = configs[1];
 2158  0
                         for (final Iterator<IContainerSetting> iterator = mfConfig
 2159  0
                                         .iterator(); iterator.hasNext();) {
 2160  0
                                 final IContainerSetting container = iterator.next();
 2161  
 
 2162  
                                 // Remove and re-add the element to make sure the newer setting
 2163  
                                 // (from ContainerProperties.xml) overrides any settings in
 2164  
                                 // pom.xml
 2165  0
                                 containerMap.getMFContainerSettings().remove(container);
 2166  0
                                 containerMap.getMFContainerSettings().add(container);
 2167  0
                         }
 2168  0
                         getLog().debug(
 2169  
                                         "MF container mapping: "
 2170  
                                                         + containerMap.getMFContainerSettings());
 2171  
 
 2172  0
                         getLog().debug("Adding External ESB Configuration - Done");
 2173  
 
 2174  0
                         final HashMap<String, IESBContainer> esbContainerMap = new HashMap<String, IESBContainer>();
 2175  
 
 2176  0
                         getLog().info("---------------------------------------------");
 2177  0
                         getLog().debug("Building Container settings for SDM model...");
 2178  
 
 2179  0
                         final List<Xpp3Dom> serviceInstances = Xpp3Utils.collectElements(
 2180  
                                         getDependencyDirectory(), "**/*.instances.snippet");
 2181  0
                         for (final Xpp3Dom serviceInstance : serviceInstances) {
 2182  
 
 2183  0
                                 final String containerHint = serviceInstance
 2184  
                                                 .getAttribute(SnippetsProcessor.HINT_ATTRIBUTE);
 2185  
 
 2186  0
                                 getLog().debug(
 2187  
                                                 "Matching service instance "
 2188  
                                                                 + serviceInstance.getAttribute("name") + "/"
 2189  
                                                                 + serviceInstance.getAttribute("process")
 2190  
                                                                 + "\r\n\t(containerHint: " + containerHint
 2191  
                                                                 + ")");
 2192  
 
 2193  0
                                 if (containerMap == null) {
 2194  0
                                         throw new Exception(
 2195  
                                                         "containerMap not specified in plugin configuration");
 2196  
                                 }
 2197  
 
 2198  0
                                 final Map<String, Properties> matches = containerMap
 2199  
                                                 .matchESBContainerSettings(containerHint);
 2200  0
                                 if (matches.size() == 0
 2201  
                                                 && containerMap.isUseESBDefaultIncludes()) {
 2202  0
                                         getLog().warn(
 2203  
                                                         "No matching ESBContainer setting found...will use default...");
 2204  0
                                         matches.put(containerMap.getDefaultESBContainerSetting()
 2205  
                                                         .getName(), new Properties());
 2206  
                                 }
 2207  
 
 2208  0
                                 for (final Iterator<String> it = matches.keySet().iterator(); it
 2209  0
                                                 .hasNext();) {
 2210  0
                                         final String matchedName = it.next();
 2211  0
                                         addServiceInstance(esbContainerMap, matchedName,
 2212  
                                                         serviceInstance, matches.get(matchedName), inLine);
 2213  0
                                 }
 2214  
 
 2215  0
                         }
 2216  
 
 2217  0
                         esbContainers = esbContainerMap.values();
 2218  
                 }
 2219  
 
 2220  0
                 return esbContainers;
 2221  
         }
 2222  
 
 2223  
         private void addServiceInstance(
 2224  
                         final HashMap<String, IESBContainer> _containerMap,
 2225  
                         final String _esbContainerName, final Xpp3Dom _serviceInstance,
 2226  
                         final Properties _includeProperties, boolean inLine) {
 2227  
 
 2228  0
                 IESBContainer esbContainer = _containerMap.get(_esbContainerName);
 2229  0
                 if (esbContainer == null) {
 2230  0
                         esbContainer = createESBContainer(_containerMap, _esbContainerName,
 2231  
                                         _includeProperties);
 2232  
                 }
 2233  0
                 String instances = "1";
 2234  
                 try {
 2235  0
                         final String instancesStr = _includeProperties.getProperty(
 2236  
                                         "instances", "1");
 2237  0
                         instances = instancesStr;
 2238  0
                 } catch (final Exception e) {
 2239  
                         // do nothing
 2240  0
                 }
 2241  0
                 getLog().debug(
 2242  
                                 "Adding " + _serviceInstance.getAttribute("name")
 2243  
                                                 + " to ESBContainer " + _esbContainerName);
 2244  0
                 esbContainer.addServiceInstance(_serviceInstance.getAttribute("name"),
 2245  
                                 instances, _serviceInstance.getAttribute("process"), Integer
 2246  
                                                 .parseInt(_includeProperties.getProperty(
 2247  
                                                                 "startupPriorityLevel", "0")), Integer
 2248  
                                                 .parseInt(_includeProperties.getProperty(
 2249  
                                                                 "ConcurrentCalls", "0")), inLine);
 2250  0
         }
 2251  
 
 2252  
         private void addCleanServiceInstance(
 2253  
                         final HashMap<String, IESBContainer> _containerMap,
 2254  
                         final String _esbContainerName, final Xpp3Dom _serviceInstance,
 2255  
                         final Properties _includeProperties, boolean inLine, HashMap <String,String> instanceReplaceMaps) {
 2256  
 
 2257  0
                 IESBContainer esbContainer = _containerMap.get(_esbContainerName);
 2258  0
                 if (esbContainer == null) {
 2259  0
                         esbContainer = createESBContainer(_containerMap, _esbContainerName,
 2260  
                                         _includeProperties);
 2261  
                 }
 2262  
                 
 2263  
 
 2264  
                 
 2265  0
                 String instances = "1";
 2266  
                 try {
 2267  0
                         final String instancesStr = _includeProperties.getProperty(
 2268  
                                         "instances", "1");
 2269  0
                         if(instancesStr.startsWith("@") && instancesStr.endsWith("@")){
 2270  
                                 //replace the 2 @ signs
 2271  0
                                 instances = "{" + instancesStr.substring(1, instancesStr.length()-1) + "}";
 2272  0
                                 getLog().error("TBO - SUBBING the tailoring : " + instances);
 2273  0
                                 instanceReplaceMaps.put(instances, instancesStr);
 2274  
                         }
 2275  
                         else{                                
 2276  0
                                 instances = instancesStr;
 2277  
                         }
 2278  0
                 } catch (final Exception e) {
 2279  
                         // do nothing
 2280  0
                 }
 2281  0
                 getLog().debug(
 2282  
                                 "Adding " + _serviceInstance.getAttribute("name")
 2283  
                                                 + " to ESBContainer " + _esbContainerName);
 2284  0
                 esbContainer.addServiceInstance(_serviceInstance.getAttribute("name"),
 2285  
                                 instances, _serviceInstance.getAttribute("process"), Integer
 2286  
                                                 .parseInt(_includeProperties.getProperty(
 2287  
                                                                 "startupPriorityLevel", "0")), Integer
 2288  
                                                 .parseInt(_includeProperties.getProperty(
 2289  
                                                                 "ConcurrentCalls", "0")), inLine);
 2290  0
         }
 2291  
 
 2292  
         
 2293  
         private IESBContainer createESBContainer(
 2294  
                         final HashMap<String, IESBContainer> _containerMap,
 2295  
                         final String _esbContainerName, final Properties _includeProperties) {
 2296  
 
 2297  0
                 final IESBContainer esbContainer = new DefaultESBContainer();
 2298  0
                 getLog().info(
 2299  
                                 "Creating ESBContainer configuration: " + _esbContainerName);
 2300  0
                 esbContainer.setName(_esbContainerName);
 2301  
 
 2302  
                 //TBO Introducing the option to override the ESB Container Classpath.
 2303  
                 
 2304  0
                 final String classpathOverride = _includeProperties.getProperty(
 2305  
                                 "classpathOverride", null);
 2306  
                 
 2307  0
                 getLog().info("ClasspathOverride ... Trying to get the Classpath Override " + classpathOverride);
 2308  
                 
 2309  0
                 if (classpathOverride == null) {
 2310  
 
 2311  0
                         final String extendedAppendCpPattern = _includeProperties
 2312  
                                         .getProperty("classpathPatternEx", "^$");
 2313  0
                         final String appendCpPattern = _includeProperties.getProperty(
 2314  
                                         "classpathPattern", ".*");
 2315  0
                         final String classpathSortPattern = _includeProperties.getProperty(
 2316  
                                         "classpathSortPattern", null);
 2317  
 
 2318  
                         List<String> classPath;
 2319  
 
 2320  
                         // First add the prepend classpath, this allwos adding JArs which
 2321  
                         // are
 2322  
                         // already on the Service Type classpath (needed
 2323  
                         // for some use cases!)
 2324  0
                         classPath = getDepAnalyzer().additionalContainerClassPath(
 2325  
                                         extendedAppendCpPattern, null, false);
 2326  
                         // Now add the additional classpath
 2327  0
                         classPath = getDepAnalyzer().additionalContainerClassPath(
 2328  
                                         appendCpPattern, classPath, true);
 2329  
 
 2330  
                         // optionally sort the classpath entries
 2331  0
                         if (classpathSortPattern != null) {
 2332  
                                 /*
 2333  
                                  * sort the classpath entries usign the provided regex pattern
 2334  
                                  * the provided pattern is split using ;
 2335  
                                  */
 2336  0
                                 final String[] sortPatterns = classpathSortPattern.split(";");
 2337  0
                                 final List<String> tempClassPath = new ArrayList<String>();
 2338  
 
 2339  0
                                 for (final String regex : sortPatterns) {
 2340  
                                         // array is needed to avoid concurrentmodification exception
 2341  
                                         // in
 2342  
                                         // for each loop!!
 2343  0
                                         final String[] entries = classPath
 2344  
                                                         .toArray(new String[classPath.size()]);
 2345  0
                                         for (final String entry : entries) {
 2346  0
                                                 if (entry.matches(regex)) {
 2347  0
                                                         classPath.remove(entry);
 2348  0
                                                         tempClassPath.add(entry);
 2349  
                                                 }
 2350  
                                         }
 2351  
                                 }
 2352  
                                 // add the remaining entries at the end
 2353  0
                                 tempClassPath.addAll(classPath);
 2354  0
                                 classPath = tempClassPath;
 2355  
                         }
 2356  
 
 2357  0
                         if ((classPath != null) && (classPath.size() > 0)) {
 2358  0
                                 final StringBuilder b = new StringBuilder();
 2359  0
                                 for (final String s : classPath) {
 2360  0
                                         b.append(s);
 2361  0
                                         b.append(';');
 2362  0
                                 }
 2363  
 
 2364  0
                                 final String cp = b.substring(0, b.length() - 1);
 2365  0
                                 getLog().info("Setting container classpath to : " + cp);
 2366  0
                                 esbContainer.getDataObject().set("CLASSPATH", cp);
 2367  
                         }
 2368  0
                 }
 2369  
                 else{
 2370  0
                         esbContainer.getDataObject().set("CLASSPATH", classpathOverride);
 2371  
                 }
 2372  
 
 2373  0
                 if (!((String) _includeProperties.getProperty("busConnection", ""))
 2374  
                                 .equals("")) {
 2375  0
                         esbContainer.getDataObject().set(
 2376  
                                         "BusConnection",
 2377  
                                         (String) _includeProperties
 2378  
                                                         .getProperty("busConnection", ""));
 2379  
                 }
 2380  0
                 if (!((String) _includeProperties.getProperty("routingHttpConnection",
 2381  
                                 "")).equals("")) {
 2382  0
                         esbContainer.getDataObject().set(
 2383  
                                         "RoutingHttpConnection",
 2384  
                                         (String) _includeProperties.getProperty(
 2385  
                                                         "routingHttpConnection", ""));
 2386  
                 }
 2387  0
                 if (!((String) _includeProperties.getProperty("enablePayloadCapture",
 2388  
                                 "")).equals("")) {
 2389  0
                         esbContainer.getDataObject().set(
 2390  
                                         "enablePayloadCapture",
 2391  
                                         new Boolean((String) _includeProperties.getProperty(
 2392  
                                                         "enablePayloadCapture", "")));
 2393  
                 }
 2394  0
                 if (!((String) _includeProperties.getProperty("useForLookingGlass", ""))
 2395  
                                 .equals("")) {
 2396  0
                         esbContainer.getDataObject().set(
 2397  
                                         "useForLookingGlass",
 2398  
                                         new Boolean((String) _includeProperties.getProperty(
 2399  
                                                         "useForLookingGlass", "")));
 2400  
                 }
 2401  0
                 if (!((String) _includeProperties.getProperty(
 2402  
                                 "useIntraContainerMessaging", "")).equals("")) {
 2403  0
                         esbContainer.getDataObject().set(
 2404  
                                         "useIntraContainerMessaging",
 2405  
                                         new Boolean((String) _includeProperties.getProperty(
 2406  
                                                         "useIntraContainerMessaging", "")));
 2407  
                 }
 2408  
 
 2409  0
                 _containerMap.put(_esbContainerName, esbContainer);
 2410  0
                 return esbContainer;
 2411  
         }
 2412  
 
 2413  
         private void createProperties() throws Exception {
 2414  
 
 2415  0
                 getLog().info("Creating generated.tailoring.properties");
 2416  
 
 2417  0
                 final Properties props = new Properties();
 2418  
 
 2419  
                 // Get the variable names from the model
 2420  0
                 final List<String> varNames = new ArrayList<String>();
 2421  
 
 2422  
                 // final XmlStreamReader reader = new XmlStreamReader(new
 2423  
                 // File(getTargetSDMDir(), "Model.xml"));
 2424  
                 // final Xpp3Dom modelDom = Xpp3DomBuilder.build(reader);
 2425  
                 // getVariableNamesFromModel(modelDom, varNames);
 2426  
 
 2427  0
                 getModelProperties("Model.xml", varNames);
 2428  0
                 getModelProperties("Tuning.xml", varNames);
 2429  0
                 getModelProperties("DefaultTuning.xml", varNames);
 2430  0
                 getModelProperties("ContainerProperties.xml", varNames);
 2431  
 
 2432  0
                 addModelPropertiesFromStaticFiles(new File(getTargetSDMDir()
 2433  
                                 + File.separator + "modelincludes"), "_refOnly.xml", varNames);
 2434  
 
 2435  
                 // TBO------INSERT SEBASTIEN HIS FILES
 2436  
 
 2437  0
                 for (final String varName : varNames) {
 2438  0
                         getLog().info("Found Variable Name: " + varName);
 2439  0
                         props.put(varName, "");
 2440  0
                 }
 2441  
 
 2442  
                 // Get the variable resolution from the dependencies
 2443  0
                 for (final Xpp3Dom variable : Xpp3Utils.collectElements(
 2444  
                                 getDependencyDirectory(), "**/*.variables.snippet")) {
 2445  0
                         final String name = variable.getAttribute("name");
 2446  0
                         final String value = variable.getValue();
 2447  0
                         getLog().debug("Setting variable: [" + name + "] = [" + value + "]");
 2448  0
                         props.put(name, value != null ? value : "");
 2449  0
                 }
 2450  
 
 2451  0
                 final SortedSet<String> propNames = new TreeSet<String>();
 2452  0
                 for (final Enumeration<Object> en = props.keys(); en.hasMoreElements();) {
 2453  0
                         propNames.add((String) en.nextElement());
 2454  
                 }
 2455  
 
 2456  0
                 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
 2457  
 
 2458  0
                 for (final String key : propNames) {
 2459  0
                         bos.write((key + "=" + props.get(key) + "\n").getBytes());
 2460  0
                 }
 2461  
 
 2462  0
                 FileUtilities
 2463  
                                 .writeFile(bos.toByteArray(), getGeneratedPropertiesFile());
 2464  
                 
 2465  
                 
 2466  0
                 updateTailoringProperties();
 2467  0
         }
 2468  
 
 2469  
         private void createTopology() throws Exception {
 2470  
 
 2471  0
                 getLog().info("Creating generated.Topology.xml");
 2472  
 
 2473  0
                 final Properties props = new Properties();
 2474  
 
 2475  
                 // Get the variable names from the model
 2476  0
                 final List<String> varNames = new ArrayList<String>();
 2477  
 
 2478  0
                 getModelProperties("Model.xml", varNames);
 2479  0
                 getModelProperties("Tuning.xml", varNames);
 2480  0
                 getModelProperties("DefaultTuning.xml", varNames);
 2481  0
                 getModelProperties("ContainerProperties.xml", varNames);
 2482  
 
 2483  
                 // TBO------INSERT SEBASTIEN HIS FILES
 2484  0
                 addModelPropertiesFromStaticFiles(new File(getTargetSDMDir()
 2485  
                                 + File.separator + "modelincludes"), "_refOnly.xml", varNames);
 2486  
 
 2487  0
                 for (final String varName : varNames) {
 2488  0
                         getLog().info("Found Variable Name: " + varName);
 2489  0
                         props.put(varName, "");
 2490  0
                 }
 2491  
 
 2492  
                 // Get the variable resolution from the dependencies
 2493  0
                 for (final Xpp3Dom variable : Xpp3Utils.collectElements(
 2494  
                                 getDependencyDirectory(), "**/*.variables.snippet")) {
 2495  0
                         final String name = variable.getAttribute("name");
 2496  0
                         final String value = variable.getValue();
 2497  0
                         getLog().debug("Setting variable: [" + name + "] = [" + value + "]");
 2498  0
                         props.put(name, value != null ? value : "");
 2499  0
                 }
 2500  
 
 2501  0
                 final SortedSet<String> propNames = new TreeSet<String>();
 2502  0
                 for (final Enumeration<Object> en = props.keys(); en.hasMoreElements();) {
 2503  0
                         propNames.add((String) en.nextElement());
 2504  
                 }
 2505  
 
 2506  0
                 final Xpp3Dom topology = new Xpp3Dom("Environment");
 2507  0
                 final Xpp3Dom id = new Xpp3Dom("Id");
 2508  0
                 final Xpp3Dom machines = new Xpp3Dom("Machines");
 2509  0
                 final Xpp3Dom machine = new Xpp3Dom("Machine");
 2510  0
                 final Xpp3Dom parameters = new Xpp3Dom("Parameters");
 2511  
 
 2512  0
                 id.setValue("GeneratedTopology");
 2513  0
                 topology.addChild(id);
 2514  
 
 2515  0
                 machines.addChild(machine);
 2516  
 
 2517  0
                 Xpp3Dom element = null;
 2518  0
                 Xpp3Dom child = null;
 2519  0
                 Xpp3Dom paramId = null;
 2520  0
                 Xpp3Dom defaultValue = null;
 2521  0
                 Xpp3Dom parameter = null;
 2522  
 
 2523  0
                 element = new Xpp3Dom("Id");
 2524  0
                 element.setValue("Generated_Machine");
 2525  0
                 machine.addChild(element);
 2526  
 
 2527  0
                 element = new Xpp3Dom("AlternateId");
 2528  0
                 element.setValue("Generated_Alternate");
 2529  0
                 machine.addChild(element);
 2530  
 
 2531  0
                 element = new Xpp3Dom("LogicalHosts");
 2532  0
                 machine.addChild(element);
 2533  
 
 2534  0
                 child = new Xpp3Dom("LogicalHost");
 2535  0
                 child.setValue("GeneratedHost");
 2536  0
                 element.addChild(child);
 2537  
 
 2538  0
                 element = new Xpp3Dom("Settings");
 2539  
 
 2540  0
                 topology.addChild(machines);
 2541  0
                 topology.addChild(element);
 2542  0
                 topology.addChild(parameters);
 2543  
 
 2544  0
                 for (final String key : propNames) {
 2545  
                         // bos.write((key + "=" + props.get(key) + "\n").getBytes());
 2546  0
                         parameter = new Xpp3Dom("Parameter");
 2547  0
                         paramId = new Xpp3Dom("Id");
 2548  0
                         paramId.setValue(key);
 2549  
 
 2550  0
                         defaultValue = new Xpp3Dom("Value");
 2551  0
                         defaultValue.setValue(props.getProperty(key));
 2552  
 
 2553  0
                         parameter.addChild(paramId);
 2554  0
                         parameter.addChild(defaultValue);
 2555  
 
 2556  0
                         parameters.addChild(parameter);
 2557  0
                 }
 2558  
 
 2559  
                 // topology.setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
 2560  
                 // topology.setAttribute("xsi:noNamespaceSchemaLocation","Topology.xsd");
 2561  
                 // topology.setAttribute("xmlns:xi","http://www.w3.org/2001/XInclude");
 2562  
 
 2563  0
                 FileUtilities.writeFile(topology.toString().getBytes(),
 2564  
                                 getGeneratedTopologyFile());
 2565  
 
 2566  0
         }
 2567  
 
 2568  
         private void generateMasterTopology() throws Exception {
 2569  
 
 2570  
                 // Check for existing Topology File
 2571  
 //                final File topo = getMasterTopologyFile();
 2572  
 //                if (topo.exists()) {
 2573  
 //                        getLog().warn("Topology file is existing ... will NOT Override");
 2574  
 //                        return;
 2575  
 //                }
 2576  
 
 2577  0
                 final Properties props = new Properties();
 2578  
 
 2579  
                 // Get the variable names from the model
 2580  0
                 final List<String> varNames = new ArrayList<String>();
 2581  
 
 2582  0
                 getModelProperties("Model.xml", varNames);
 2583  0
                 getModelProperties("Tuning.xml", varNames);
 2584  0
                 getModelProperties("DefaultTuning.xml", varNames);
 2585  0
                 getModelProperties("ContainerProperties.xml", varNames);
 2586  
 
 2587  
                 // TBO------INSERT SEBASTIEN HIS FILES
 2588  0
                 addModelPropertiesFromStaticFiles(new File(getTargetSDMDir()
 2589  
                                 + File.separator + "modelincludes"), "_refOnly.xml", varNames);
 2590  
 
 2591  0
                 for (final String varName : varNames) {
 2592  0
                         getLog().info("Found Variable Name: " + varName);
 2593  0
                         props.put(varName, "");
 2594  0
                 }
 2595  
                 
 2596  
                 //CHECK environment Topology files
 2597  0
                 for (final String toCompare : FileUtilities.getFileList(
 2598  
                                 getTargetSDMDir().getAbsolutePath(), "*.Topology.xml",
 2599  
                                 "generated.Topology.xml")) {
 2600  0
                         Properties envProperties = buildPropertiesFromTopology(new File(getTargetSDMDir().getAbsolutePath(), toCompare));
 2601  0
                         for (final Object envPropKey : envProperties.keySet()) {
 2602  0
                                 if (!props.containsKey(envPropKey.toString())) {
 2603  0
                                         getLog().info("Found Variable Name: " + envPropKey);
 2604  0
                                         props.put(envPropKey, "");
 2605  
                                 }
 2606  0
                         }
 2607  
                 }
 2608  
 
 2609  
                 // Get the variable resolution from the dependencies
 2610  0
                 for (final Xpp3Dom variable : Xpp3Utils.collectElements(
 2611  
                                 getDependencyDirectory(), "**/*.variables.snippet")) {
 2612  0
                         final String name = variable.getAttribute("name");
 2613  0
                         final String value = variable.getValue();
 2614  0
                         getLog().debug("Setting variable: [" + name + "] = [" + value + "]");
 2615  0
                         props.put(name, value != null ? value : "");
 2616  0
                 }
 2617  
 
 2618  0
                 final SortedSet<String> propNames = new TreeSet<String>();
 2619  0
                 for (final Enumeration<Object> en = props.keys(); en.hasMoreElements();) {
 2620  0
                         propNames.add((String) en.nextElement());
 2621  
                 }
 2622  
 
 2623  0
                 getLog().info(
 2624  
                                 "Generating Master Topology File");
 2625  
                 // Build up the Tree
 2626  0
                 final Xpp3Dom topology = new Xpp3Dom("Topology");
 2627  0
                 Xpp3Dom includeDom = new Xpp3Dom("Id");
 2628  
 
 2629  0
                 final Xpp3Dom parameters = new Xpp3Dom("Parameters");
 2630  
                 // Xpp3Dom child = null;
 2631  0
                 Xpp3Dom paramId = null;
 2632  0
                 Xpp3Dom defaultValue = null;
 2633  0
                 Xpp3Dom parameter = null;
 2634  
 
 2635  0
                 topology.setAttribute("xmlns:xsi",
 2636  
                                 "http://www.w3.org/2001/XMLSchema-instance");
 2637  0
                 topology.setAttribute("xsi:noNamespaceSchemaLocation", "Topology.xsd");
 2638  0
                 topology.setAttribute("xmlns:xi", "http://www.w3.org/2001/XInclude");
 2639  
 
 2640  0
                 for (final String key : propNames) {
 2641  
                         // bos.write((key + "=" + props.get(key) + "\n").getBytes());
 2642  0
                         parameter = new Xpp3Dom("Parameter");
 2643  0
                         paramId = new Xpp3Dom("Id");
 2644  0
                         paramId.setValue(key);
 2645  
 
 2646  0
                         defaultValue = new Xpp3Dom("DefaultValue");
 2647  0
                         final String keyValue = props.getProperty(key);
 2648  0
                         if (keyValue == null) {
 2649  
                                 //defaultValue.setValue("54321");
 2650  
                         } else {
 2651  0
                                 defaultValue.setValue(props.getProperty(key));
 2652  
                         }
 2653  
 
 2654  0
                         parameter.addChild(paramId);
 2655  0
                         parameter.addChild(defaultValue);
 2656  
 
 2657  0
                         parameters.addChild(parameter);
 2658  0
                 }
 2659  
 
 2660  0
                 topology.addChild(parameters);
 2661  
 
 2662  0
                 for (final String toCompare : FileUtilities.getFileList(
 2663  
                                 getTargetSDMDir().getAbsolutePath(), "*.Topology.xml",
 2664  
                                 "generated.Topology.xml")) {
 2665  0
                         includeDom = new Xpp3Dom("xi:include");
 2666  0
                         includeDom.setAttribute("href", toCompare);
 2667  0
                         topology.addChild(includeDom);
 2668  
                 }
 2669  
 
 2670  0
                 final Xpp3Dom environment = new Xpp3Dom("Environment");
 2671  
 
 2672  0
                 final Xpp3Dom envId = new Xpp3Dom("Id");
 2673  0
                 envId.setValue("default");
 2674  0
                 environment.addChild(envId);
 2675  
 
 2676  0
                 final Xpp3Dom machines = new Xpp3Dom("Machines");
 2677  
 
 2678  0
                 final Xpp3Dom machine = new Xpp3Dom("Machine");
 2679  
 
 2680  0
                 final Xpp3Dom machineId = new Xpp3Dom("Id");
 2681  0
                 machineId.setValue("localhost");
 2682  0
                 machine.addChild(machineId);
 2683  
 
 2684  0
                 final Xpp3Dom altId = new Xpp3Dom("AlternateId");
 2685  0
                 altId.setValue("127.0.0.1");
 2686  0
                 machine.addChild(altId);
 2687  
 
 2688  0
                 final Xpp3Dom altId2 = new Xpp3Dom("AlternateId");
 2689  0
                 altId2.setValue("::1");
 2690  0
                 machine.addChild(altId2);
 2691  
 
 2692  0
                 machines.addChild(machine);
 2693  0
                 environment.addChild(machines);
 2694  
 
 2695  0
                 topology.addChild(environment);
 2696  
 
 2697  0
                 FileUtilities.writeFile(topology.toString().getBytes(),
 2698  
                                 getMasterTopologyFile());
 2699  
 
 2700  0
         }
 2701  
 
 2702  
         private void getModelProperties(final String _modelFile,
 2703  
                         final List<String> _names) throws Exception {
 2704  0
                 if (_modelFile.equals("ContainerProperties.xml")) {
 2705  0
                         if (containerPropFile.exists()) {
 2706  0
                                 final XmlStreamReader reader = new XmlStreamReader(
 2707  
                                                 containerPropFile);
 2708  0
                                 final Xpp3Dom modelDom = Xpp3DomBuilder.build(reader);
 2709  0
                                 getVariableNamesFromModel(modelDom, _names);
 2710  0
                         }
 2711  
                 } else {
 2712  0
                         File modelFile = new File(getTargetSDMDir(), _modelFile);
 2713  0
                         if (modelFile.exists()) {
 2714  0
                                 final XmlStreamReader reader = new XmlStreamReader(modelFile);
 2715  0
                                 final Xpp3Dom modelDom = Xpp3DomBuilder.build(reader);
 2716  0
                                 getVariableNamesFromModel(modelDom, _names);
 2717  
                         }
 2718  
                 }
 2719  0
         }
 2720  
 
 2721  
         private void addModelPropertiesFromStaticFiles(File directory,
 2722  
                         final String filter, List<String> varNames) {
 2723  
 
 2724  0
                 if (!directory.exists()) {
 2725  
                         // no includes used ... skip this
 2726  
                 } else {
 2727  0
                         getLog().debug(
 2728  
                                         "TBO2 ------------ INCLUDES DIR FOUND .... for filter "
 2729  
                                                         + filter);
 2730  0
                         File[] content = directory.listFiles(new FilenameFilter() {
 2731  
                                 @Override
 2732  
                                 public boolean accept(File dir, String name) {
 2733  0
                                         if (name.endsWith(filter)) {
 2734  0
                                                 return true;
 2735  
                                         } else {
 2736  0
                                                 return false;
 2737  
                                         }
 2738  
                                 }
 2739  
                         });
 2740  
 
 2741  0
                         for (int i = 0; i < content.length; i++) {
 2742  0
                                 getLog().info(
 2743  
                                                 "TBO --- Found extra include file to check: "
 2744  
                                                                 + content[i].getName());
 2745  
 
 2746  
                                 XmlStreamReader reader;
 2747  
                                 try {
 2748  0
                                         reader = new XmlStreamReader(content[i]);
 2749  0
                                         final Xpp3Dom modelDom = Xpp3DomBuilder.build(reader);
 2750  0
                                         getVariableNamesFromModel(modelDom, varNames);
 2751  
 
 2752  0
                                 } catch (IOException e) {
 2753  
                                         // TODO Auto-generated catch block
 2754  0
                                         e.printStackTrace();
 2755  0
                                 } catch (XmlPullParserException e) {
 2756  
                                         // TODO Auto-generated catch block
 2757  0
                                         e.printStackTrace();
 2758  0
                                 }
 2759  
                         }
 2760  
 
 2761  
                 }
 2762  
                 // TODO Auto-generated method stub
 2763  
 
 2764  0
         }
 2765  
 
 2766  
         // private void getVariableNamesFromModel(final Xpp3Dom _element, final
 2767  
         // List<String> _names) {
 2768  
         //
 2769  
         // String content = _element.getValue();
 2770  
         //
 2771  
         // if ((content != null) && content.endsWith("@#ONLY")) {
 2772  
         // content = content.substring(0, content.length() - "#ONLY".length());
 2773  
         // }
 2774  
         //
 2775  
         // if ((content != null) && content.startsWith("@") &&
 2776  
         // content.endsWith("@")) {
 2777  
         // _names.add(content);
 2778  
         // } else {
 2779  
         // for (final Xpp3Dom child : _element.getChildren()) {
 2780  
         // getVariableNamesFromModel(child, _names);
 2781  
         // }
 2782  
         // }
 2783  
         // }
 2784  
 
 2785  
         private void getVariableNamesFromModel(final Xpp3Dom _element,
 2786  
                         final List<String> _names) {
 2787  
 
 2788  0
                 Pattern pattern = Pattern.compile("(@[^\\s^\\@]*@)");
 2789  
 
 2790  0
                 String content = _element.getValue();
 2791  
 
 2792  
                 // TBO: Replacing this logic with RegExp in order to match the @@
 2793  
                 // multiple variables
 2794  0
                 boolean valueFound = false;
 2795  0
                 if (content != null) {
 2796  0
                         Matcher matcher = pattern.matcher(content);
 2797  
                         // using Matcher find(), group(), start() and end() methods
 2798  0
                         while (matcher.find()) {
 2799  0
                                 valueFound = true;
 2800  0
                                 if (!_names.contains(matcher.group())) {
 2801  0
                                         _names.add(matcher.group());
 2802  
                                 }
 2803  
                         }
 2804  
                 }
 2805  0
                 if (!valueFound) {
 2806  0
                         for (final Xpp3Dom child : _element.getChildren()) {
 2807  0
                                 getVariableNamesFromModel(child, _names);
 2808  
                         }
 2809  
                 }
 2810  0
         }
 2811  
 
 2812  
         private DependencyAnalyzer getDepAnalyzer() {
 2813  
 
 2814  0
                 if (depAnalyzer == null) {
 2815  0
                         depAnalyzer = new DependencyAnalyzer(getDependencyDirectory());
 2816  
                 }
 2817  0
                 return depAnalyzer;
 2818  
         }
 2819  
 }